fix: do not delete last schedule (#10444)
Co-authored-by: rkreddy99 <[email protected]>
This commit is contained in:
1 parent
65384d7b1f
commit
d21135e076
1 file changed
+5
@@ -26,6 +26,8 @@ export const deleteHandler = async ({ input, ctx }: DeleteOptions) => {
|
|||||||
|
|
||||||
if (scheduleToDelete?.userId !== user.id) throw new TRPCError({ code: "UNAUTHORIZED" });
|
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) {
|
if (user.defaultScheduleId === input.scheduleId) {
|
||||||
// set a new default or unset default if no other schedule
|
// set a new default or unset default if no other schedule
|
||||||
const scheduleToSetAsDefault = await prisma.schedule.findFirst({
|
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({
|
await prisma.user.update({
|
||||||
where: {
|
where: {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
|
|||||||
Reference in new issue
Block a user