Merge pull request #53 from calcom/fix/51-delete-schedule-unset-default

pull/9078/head
Agusti Fernandez 2022-04-23 02:46:57 +02:00 committed by GitHub
commit 70fd578a0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 2 deletions

View File

@ -100,7 +100,7 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse<Att
const attendees = userBookings.map((booking) => booking.attendees).flat();
const attendeeIds = attendees.map((attendee) => attendee.id);
// Here we make sure to only return attendee's of the user's own bookings.
if (!attendeeIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });
if (!attendeeIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });
else {
switch (method) {
case "GET":

View File

@ -126,6 +126,20 @@ export async function scheduleById(req: NextApiRequest, res: NextApiResponse<Sch
break;
case "DELETE":
// Look for user to check if schedule is user's default
const user = await prisma.user.findUnique({ where: { id: userId } });
if (!user) throw new Error("User not found");
if (user.defaultScheduleId === safeQuery.data.id) {
// unset default
await prisma.user.update({
where: {
id: userId,
},
data: {
defaultScheduleId: undefined,
},
});
}
await prisma.schedule
.delete({ where: { id: safeQuery.data.id } })
.then(() =>

View File

@ -91,7 +91,7 @@ export async function userById(req: NextApiRequest, res: NextApiResponse<UserRes
const safeBody = schemaUserBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = req.userId;
if (safeQuery.data.id !== userId) res.status(401).json({ message: "Unauthorized" });
if (safeQuery.data.id !== userId) res.status(401).json({ message: "Unauthorized" });
else {
switch (method) {
case "GET":