Skip to content

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
Copy link

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

Copy link

Choose a reason for hiding this comment

The 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,
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { prisma } from "@calcom/prisma";
import { TRPCError } from "@trpc/server";

import type { TrpcSessionUser } from "../../../../types";
import { setupDefaultSchedule } from "../util";
import { setupDefaultSchedule, updateHostsWithNewDefaultSchedule } from "../util";
import type { TUpdateInputSchema } from "./update.schema";

type User = NonNullable<TrpcSessionUser>;
Expand Down Expand Up @@ -61,6 +61,10 @@ export const updateHandler = async ({ input, ctx }: UpdateOptions) => {

let updatedUser;
if (input.isDefault) {
if (user?.defaultScheduleId) {
await updateHostsWithNewDefaultSchedule(user.id, user.defaultScheduleId, input.scheduleId, prisma);
}

const setupDefault = await setupDefaultSchedule(user.id, input.scheduleId, prisma);
updatedUser = setupDefault;
}
Expand Down
17 changes: 17 additions & 0 deletions packages/trpc/server/routers/viewer/availability/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to use prisma directly in trpc routers

We should create a repository function and use that here

where: {
userId,
scheduleId: defaultScheduleId,
},
data: {
scheduleId,
},
});
};
Loading