Revert "refactor"

This reverts commit afbac4fa44.
This commit is contained in:
supalarry 2024-01-12 16:25:16 +01:00
parent afbac4fa44
commit 175eb3d985
2 changed files with 27 additions and 20 deletions

View File

@ -7,13 +7,20 @@ export const schemaTeamBaseBodyParams = Team.omit({ id: true, createdAt: true })
metadata: true,
});
export const schemaTeamUpdateBodyParams = schemaTeamBaseBodyParams.partial();
const schemaTeamRequiredParams = z.object({
slug: z.string().min(3).max(255),
name: z.string().max(255),
});
export const schemaTeamCreateBodyParams = schemaTeamBaseBodyParams
.extend({
ownerId: z.number().optional(),
})
.strict();
export const schemaTeamBodyParams = schemaTeamBaseBodyParams.merge(schemaTeamRequiredParams).strict();
export const schemaTeamUpdateBodyParams = schemaTeamBodyParams.partial();
const schemaOwnerId = z.object({
ownerId: z.number().optional(),
});
export const schemaTeamCreateBodyParams = schemaTeamBodyParams.merge(schemaOwnerId).strict();
export const schemaTeamReadPublic = Team.omit({});

View File

@ -77,32 +77,32 @@ import { schemaTeamCreateBodyParams, schemaTeamReadPublic } from "~/lib/validati
*/
async function postHandler(req: NextApiRequest) {
const { prisma, body, userId, isAdmin } = req;
const { ownerId, ...teamData } = schemaTeamCreateBodyParams.parse(body);
const { ownerId, ...data } = schemaTeamCreateBodyParams.parse(body);
await checkPermissions(req);
const effectiveUserId = isAdmin && ownerId ? ownerId : userId;
if (teamData.slug) {
if (data.slug) {
const alreadyExist = await prisma.team.findFirst({
where: {
slug: teamData.slug,
slug: data.slug,
},
});
if (alreadyExist) throw new HttpError({ statusCode: 409, message: "Team slug already exists" });
if (IS_TEAM_BILLING_ENABLED) {
// Setting slug in metadata, so it can be published later
teamData.metadata = {
requestedSlug: teamData.slug,
data.metadata = {
requestedSlug: data.slug,
};
delete teamData.slug;
delete data.slug;
}
}
// Check if parentId is related to this user
if (teamData.parentId) {
if (data.parentId) {
const parentTeam = await prisma.team.findFirst({
where: { id: teamData.parentId, members: { some: { userId, role: { in: ["OWNER", "ADMIN"] } } } },
where: { id: data.parentId, members: { some: { userId, role: { in: ["OWNER", "ADMIN"] } } } },
});
if (!parentTeam)
throw new HttpError({
@ -112,11 +112,11 @@ async function postHandler(req: NextApiRequest) {
}
// TODO: Perhaps there is a better fix for this?
const cloneData: typeof teamData & {
metadata: NonNullable<typeof teamData.metadata> | undefined;
const cloneData: typeof data & {
metadata: NonNullable<typeof data.metadata> | undefined;
} = {
...teamData,
metadata: teamData.metadata === null ? {} : teamData.metadata || undefined,
...data,
metadata: data.metadata === null ? {} : data.metadata || undefined,
};
const team = await prisma.team.create({
data: {
@ -132,8 +132,8 @@ async function postHandler(req: NextApiRequest) {
req.statusCode = 201;
// We are also returning the new ownership relation as owner besides team.
return {
team: schemaTeamReadPublic.parse(teamData),
owner: schemaMembershipPublic.parse(teamData.members[0]),
team: schemaTeamReadPublic.parse(team),
owner: schemaMembershipPublic.parse(team.members[0]),
message: isAdmin
? "Team created successfully, we also made user with submitted userId the owner of this team"
: "Team created successfully, we also made you the owner of this team",