From d35ce9c95ef4afc70d7ce1e3990df9e74d333980 Mon Sep 17 00:00:00 2001 From: GitStart <1501599+gitstart@users.noreply.github.com> Date: Thu, 15 Sep 2022 08:20:04 +0100 Subject: [PATCH] Availability default bug (#4345) Co-authored-by: Nitesh Singh Co-authored-by: gitstart Co-authored-by: Grace Nshokano Co-authored-by: Thiago Nascimbeni Co-authored-by: gitstart Co-authored-by: Olusanya Timothy <48022904+seunexplicit@users.noreply.github.com> Co-authored-by: seunexplicit Co-authored-by: Matheus Muniz <87545749+matheusmuniz03@users.noreply.github.com> Co-authored-by: gitstart Co-authored-by: Nitesh Singh Co-authored-by: Grace Nshokano Co-authored-by: Thiago Nascimbeni Co-authored-by: gitstart Co-authored-by: Olusanya Timothy <48022904+seunexplicit@users.noreply.github.com> Co-authored-by: seunexplicit Co-authored-by: Matheus Muniz <87545749+matheusmuniz03@users.noreply.github.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../trpc/server/routers/viewer/availability.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/trpc/server/routers/viewer/availability.tsx b/packages/trpc/server/routers/viewer/availability.tsx index f1db5d1eb8..8a3404ceee 100644 --- a/packages/trpc/server/routers/viewer/availability.tsx +++ b/packages/trpc/server/routers/viewer/availability.tsx @@ -155,13 +155,25 @@ export const availabilityRouter = createProtectedRouter() if (scheduleToDelete?.userId !== user.id) throw new TRPCError({ code: "UNAUTHORIZED" }); if (user.defaultScheduleId === input.scheduleId) { - // unset default + // set a new default or unset default if no other schedule + const scheduleToSetAsDefault = await prisma.schedule.findFirst({ + where: { + userId: user.id, + NOT: { + id: input.scheduleId, + }, + }, + select: { + id: true, + }, + }); + await prisma.user.update({ where: { id: user.id, }, data: { - defaultScheduleId: undefined, + defaultScheduleId: scheduleToSetAsDefault?.id, }, }); }