-
Notifications
You must be signed in to change notification settings - Fork 9.3k
fix: Host schedule not updating upon updating/deleting default schedule #20750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { prisma } from "@calcom/prisma"; | |
import { TRPCError } from "@trpc/server"; | ||
|
||
import type { TrpcSessionUser } from "../../../../types"; | ||
import { updateHostsWithNewDefaultSchedule } from "../util"; | ||
import type { TDeleteInputSchema } from "./delete.schema"; | ||
|
||
type DeleteOptions = { | ||
|
@@ -45,6 +46,8 @@ 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 updateHostsWithNewDefaultSchedule(user.id, input.scheduleId, scheduleToSetAsDefault.id, prisma); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a database transaction for related database operations to ensure atomicity |
||
|
||
await prisma.user.update({ | ||
where: { | ||
id: user.id, | ||
|
@@ -53,7 +56,12 @@ export const deleteHandler = async ({ input, ctx }: DeleteOptions) => { | |
defaultScheduleId: scheduleToSetAsDefault?.id || null, | ||
}, | ||
}); | ||
} else { | ||
if (user.defaultScheduleId) { | ||
Comment on lines
+59
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. else if |
||
await updateHostsWithNewDefaultSchedule(user.id, input.scheduleId, user.defaultScheduleId, prisma); | ||
} | ||
} | ||
|
||
await prisma.schedule.delete({ | ||
where: { | ||
id: input.scheduleId, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,3 +53,20 @@ export const setupDefaultSchedule = async (userId: number, scheduleId: number, p | |
}, | ||
}); | ||
}; | ||
|
||
export const updateHostsWithNewDefaultSchedule = async ( | ||
userId: number, | ||
defaultScheduleId: number, | ||
scheduleId: number, | ||
prisma: PrismaClient | ||
) => { | ||
return prisma.host.updateMany({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want to use We should create a repository function and use that here |
||
where: { | ||
userId, | ||
scheduleId: defaultScheduleId, | ||
}, | ||
data: { | ||
scheduleId, | ||
}, | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling around this async operation could lead to inconsistent state if it fails