feat: schedules hardend
This commit is contained in:
1 parent
80c2c4bdae
commit
cabe4ae9c6
3 files changed
+68
-50
No files matched your search
+53
-38
@@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import type { ScheduleResponse } from "@lib/types";
|
||||
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
|
||||
import { schemaScheduleBodyParams, schemaSchedulePublic } from "@lib/validations/schedule";
|
||||
import {
|
||||
schemaQueryIdParseInt,
|
||||
@@ -90,47 +91,61 @@ export async function scheduleById(req: NextApiRequest, res: NextApiResponse<Sch
|
||||
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = schemaScheduleBodyParams.safeParse(body);
|
||||
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
|
||||
const userId = getCalcomUserId(res);
|
||||
const userSchedules = await prisma.schedule.findMany({ where: { userId } });
|
||||
const userScheduleIds = userSchedules.map((schedule) => schedule.id);
|
||||
if (userScheduleIds.includes(safeQuery.data.id)) {
|
||||
switch (method) {
|
||||
case "GET":
|
||||
await prisma.schedule
|
||||
.findUnique({ where: { id: safeQuery.data.id } })
|
||||
.then((data) => schemaSchedulePublic.parse(data))
|
||||
.then((schedule) => res.status(200).json({ schedule }))
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({
|
||||
message: `Schedule with id: ${safeQuery.data.id} not found`,
|
||||
error,
|
||||
})
|
||||
);
|
||||
break;
|
||||
|
||||
switch (method) {
|
||||
case "GET":
|
||||
await prisma.schedule
|
||||
.findUnique({ where: { id: safeQuery.data.id } })
|
||||
.then((data) => schemaSchedulePublic.parse(data))
|
||||
.then((schedule) => res.status(200).json({ schedule }))
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({ message: `Schedule with id: ${safeQuery.data.id} not found`, error })
|
||||
);
|
||||
break;
|
||||
case "PATCH":
|
||||
if (!safeBody.success) {
|
||||
throw new Error("Invalid request body");
|
||||
}
|
||||
await prisma.schedule
|
||||
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
|
||||
.then((data) => schemaSchedulePublic.parse(data))
|
||||
.then((schedule) => res.status(200).json({ schedule }))
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({
|
||||
message: `Schedule with id: ${safeQuery.data.id} not found`,
|
||||
error,
|
||||
})
|
||||
);
|
||||
break;
|
||||
|
||||
case "PATCH":
|
||||
if (!safeBody.success) throw new Error("Invalid request body");
|
||||
await prisma.schedule
|
||||
.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: safeBody.data,
|
||||
})
|
||||
.then((data) => schemaSchedulePublic.parse(data))
|
||||
.then((schedule) => res.status(200).json({ schedule }))
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({ message: `Schedule with id: ${safeQuery.data.id} not found`, error })
|
||||
);
|
||||
break;
|
||||
case "DELETE":
|
||||
await prisma.schedule
|
||||
.delete({ where: { id: safeQuery.data.id } })
|
||||
.then(() =>
|
||||
res.status(200).json({
|
||||
message: `Schedule with id: ${safeQuery.data.id} deleted successfully`,
|
||||
})
|
||||
)
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({
|
||||
message: `Schedule with id: ${safeQuery.data.id} not found`,
|
||||
error,
|
||||
})
|
||||
);
|
||||
break;
|
||||
|
||||
case "DELETE":
|
||||
await prisma.schedule
|
||||
.delete({ where: { id: safeQuery.data.id } })
|
||||
.then(() =>
|
||||
res.status(200).json({ message: `Schedule with id: ${safeQuery.data.id} deleted successfully` })
|
||||
)
|
||||
.catch((error: Error) =>
|
||||
res.status(404).json({ message: `Schedule with id: ${safeQuery.data.id} not found`, error })
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
res.status(405).json({ message: "Method not allowed" });
|
||||
break;
|
||||
}
|
||||
default:
|
||||
res.status(405).json({ message: "Method not allowed" });
|
||||
break;
|
||||
}
|
||||
} else res.status(401).json({ message: "Unauthorized" });
|
||||
}
|
||||
|
||||
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(scheduleById));
|
||||
@@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";
|
||||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import { ScheduleResponse, SchedulesResponse } from "@lib/types";
|
||||
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
|
||||
import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } from "@lib/validations/schedule";
|
||||
|
||||
/**
|
||||
@@ -42,8 +43,10 @@ async function createOrlistAllSchedules(
|
||||
res: NextApiResponse<SchedulesResponse | ScheduleResponse>
|
||||
) {
|
||||
const { method } = req;
|
||||
const userId = getCalcomUserId(res);
|
||||
|
||||
if (method === "GET") {
|
||||
const data = await prisma.schedule.findMany();
|
||||
const data = await prisma.schedule.findMany({ where: { userId } });
|
||||
const schedules = data.map((schedule) => schemaSchedulePublic.parse(schedule));
|
||||
if (schedules) res.status(200).json({ schedules });
|
||||
else
|
||||
@@ -55,8 +58,7 @@ async function createOrlistAllSchedules(
|
||||
} else if (method === "POST") {
|
||||
const safe = schemaScheduleBodyParams.safeParse(req.body);
|
||||
if (!safe.success) throw new Error("Invalid request body");
|
||||
|
||||
const data = await prisma.schedule.create({ data: safe.data });
|
||||
const data = await prisma.schedule.create({ data: { ...safe.data, userId } });
|
||||
const schedule = schemaSchedulePublic.parse(data);
|
||||
|
||||
if (schedule) res.status(201).json({ schedule, message: "Schedule created successfully" });
|
||||
|
||||
Reference in new issue
Block a user