Compare commits

...

2 Commits

Author SHA1 Message Date
alishaz-polymath fbd39f1eca minor change 2023-10-10 15:42:38 +04:00
alishaz-polymath efe2a8baf5 init 2023-10-10 15:20:46 +04:00
4 changed files with 57 additions and 4 deletions

View File

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

View File

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