fix: do not delete last schedule (#10444)

Co-authored-by: rkreddy99 <rreddy@e2clouds.com>
This commit is contained in:
Rama Krishna Reddy
2023-07-31 14:48:50 +01:00
committed by GitHub
co-authored by rkreddy99
parent 65384d7b1f
commit d21135e076
@@ -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,