Update api for new teams.

This commit is contained in:
Brian Cao 2024-01-26 11:28:16 -08:00
parent f319ce7915
commit 80883b5ff1
6 changed files with 122 additions and 109 deletions

View file

@ -1,18 +1,21 @@
import * as yup from 'yup';
import { canViewTeam } from 'lib/auth';
import { canCreateTeamWebsite, canViewTeam } from 'lib/auth';
import { useAuth, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getWebsitesByTeamId } from 'queries';
import { createWebsite, getWebsitesByTeamId } from 'queries';
import { uuid } from 'lib/crypto';
export interface TeamWebsiteRequestQuery extends SearchFilter {
id: string;
}
export interface TeamWebsiteRequestBody {
websiteIds?: string[];
name: string;
domain: string;
shareId: string;
}
const schema = {
@ -21,8 +24,9 @@ const schema = {
...pageInfo,
}),
POST: yup.object().shape({
id: yup.string().uuid().required(),
websiteIds: yup.array().of(yup.string()).min(1).required(),
name: yup.string().max(100).required(),
domain: yup.string().max(500).required(),
shareId: yup.string().max(50).nullable(),
}),
};
@ -51,5 +55,17 @@ export default async (
return ok(res, websites);
}
if (req.method === 'POST') {
if (!(await canCreateTeamWebsite(req.auth, teamId))) {
return unauthorized(res);
}
const { name, domain, shareId } = req.body;
const website = await createWebsite({ id: uuid(), name, domain, shareId });
return ok(res, website);
}
return methodNotAllowed(res);
};