feat/add-edit-event-type-children-via-api
alishaz-polymath 2023-10-10 15:20:46 +04:00
parent 2faf24fb98
commit efe2a8baf5
4 changed files with 55 additions and 4 deletions

View File

@ -209,15 +209,23 @@ export async function patchHandler(req: NextApiRequest) {
hosts = [], hosts = [],
bookingLimits, bookingLimits,
durationLimits, durationLimits,
/** FIXME: Updating event-type children from API not supported for now */ children: parsedChildren,
children: _,
...parsedBody ...parsedBody
} = schemaEventTypeEditBodyParams.parse(body); } = schemaEventTypeEditBodyParams.parse(body);
// validate children ownership
const data: Prisma.EventTypeUpdateArgs["data"] = { const data: Prisma.EventTypeUpdateArgs["data"] = {
...parsedBody, ...parsedBody,
bookingLimits: bookingLimits === null ? Prisma.DbNull : bookingLimits, bookingLimits: bookingLimits === null ? Prisma.DbNull : bookingLimits,
durationLimits: durationLimits === null ? Prisma.DbNull : durationLimits, durationLimits: durationLimits === null ? Prisma.DbNull : durationLimits,
...(parsedChildren
? {
children: {
connect: parsedChildren.map((child) => ({ id: child.id })),
},
}
: {}),
}; };
if (hosts) { if (hosts) {

View File

@ -10,6 +10,7 @@ import checkParentEventOwnership from "./_utils/checkParentEventOwnership";
import checkTeamEventEditPermission from "./_utils/checkTeamEventEditPermission"; import checkTeamEventEditPermission from "./_utils/checkTeamEventEditPermission";
import checkUserMembership from "./_utils/checkUserMembership"; import checkUserMembership from "./_utils/checkUserMembership";
import ensureOnlyMembersAsHosts from "./_utils/ensureOnlyMembersAsHosts"; import ensureOnlyMembersAsHosts from "./_utils/ensureOnlyMembersAsHosts";
import isUserMemberOfTeam from "./_utils/isUserMemberOfTeam";
/** /**
* @swagger * @swagger
@ -268,17 +269,29 @@ async function postHandler(req: NextApiRequest) {
hosts = [], hosts = [],
bookingLimits, bookingLimits,
durationLimits, durationLimits,
/** FIXME: Adding event-type children from API not supported for now */ children: parsedChildren,
children: _,
...parsedBody ...parsedBody
} = schemaEventTypeCreateBodyParams.parse(body || {}); } = schemaEventTypeCreateBodyParams.parse(body || {});
// validate that these child event type owners are members of parent
const validatedChildren =
parsedBody.teamId && parsedChildren
? parsedChildren.filter((child) => isUserMemberOfTeam(parsedBody.teamId, child.userId))
: [];
let data: Prisma.EventTypeCreateArgs["data"] = { let data: Prisma.EventTypeCreateArgs["data"] = {
...parsedBody, ...parsedBody,
userId, userId,
users: { connect: { id: userId } }, users: { connect: { id: userId } },
bookingLimits: bookingLimits === null ? Prisma.DbNull : bookingLimits, bookingLimits: bookingLimits === null ? Prisma.DbNull : bookingLimits,
durationLimits: durationLimits === null ? Prisma.DbNull : durationLimits, durationLimits: durationLimits === null ? Prisma.DbNull : durationLimits,
...(validatedChildren
? {
children: {
connect: validatedChildren.map((child) => ({ id: child.id })),
},
}
: {}),
}; };
await checkPermissions(req); await checkPermissions(req);

View File

@ -12,6 +12,12 @@ import { HttpError } from "@calcom/lib/http-error";
* or if the user isn't a member of the associated team. * or if the user isn't a member of the associated team.
*/ */
export default async function checkUserMembership(parentId: number, userId?: number) { export default async function checkUserMembership(parentId: number, userId?: number) {
if (!userId) {
throw new HttpError({
statusCode: 400,
message: "Invalid request.",
});
}
const parentEventType = await prisma.eventType.findUnique({ const parentEventType = await prisma.eventType.findUnique({
where: { where: {
id: parentId, id: parentId,

View File

@ -0,0 +1,24 @@
import { HttpError } from "@calcom/lib/http-error";
export default async function isUserMemberOfTeam(teamId?: number | null, userId?: number) {
if (!teamId || !userId) {
throw new HttpError({
statusCode: 400,
message: "Bad request.",
});
}
const teamMember = await prisma.membership.findFirst({
where: {
teamId: teamId,
userId: userId,
accepted: true,
},
});
if (!teamMember) {
throw new HttpError({
statusCode: 400,
message: "User is not a team member.",
});
}
}