Skip to content

Scheduling infrastructure for absolutely everyone.

fix: do not delete last schedule (#10444)

Rama Krishna Reddyd21135e07694
diff --git a/packages/trpc/server/routers/viewer/availability/schedule/delete.handler.ts b/packages/trpc/server/routers/viewer/availability/schedule/delete.handler.ts
index d77aeecb1f4..1d1c13784a8 100644
--- a/packages/trpc/server/routers/viewer/availability/schedule/delete.handler.ts
+++ b/packages/trpc/server/routers/viewer/availability/schedule/delete.handler.ts
@@ -26,6 +26,8 @@ export const deleteHandler = async ({ input, ctx }: DeleteOptions) => {
if (scheduleToDelete?.userId !== user.id) throw new TRPCError({ code: "UNAUTHORIZED" });
+ // cannot remove this schedule if this is the last schedule remaining
+ // if this is the last remaining schedule of the user then this would be the default schedule and so cannot remove it
if (user.defaultScheduleId === input.scheduleId) {
// set a new default or unset default if no other schedule
const scheduleToSetAsDefault = await prisma.schedule.findFirst({
@@ -40,6 +42,9 @@ export const deleteHandler = async ({ input, ctx }: DeleteOptions) => {
},
});
+ // to throw the error if there arent any other schedules
+ if (!scheduleToSetAsDefault) throw new TRPCError({ code: "BAD_REQUEST" });
+
await prisma.user.update({
where: {
id: user.id,