Skip to content

Scheduling infrastructure for absolutely everyone.

fix: timezone of default schedule should change if profile timezone is updated (#11533)

Carina Wollendorfer79b60adf9b58
diff --git a/packages/trpc/server/routers/loggedInViewer/updateProfile.handler.ts b/packages/trpc/server/routers/loggedInViewer/updateProfile.handler.ts
index c741c3f1bf9..af16a06aa5c 100644
--- a/packages/trpc/server/routers/loggedInViewer/updateProfile.handler.ts
+++ b/packages/trpc/server/routers/loggedInViewer/updateProfile.handler.ts
@@ -19,6 +19,7 @@ import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
import { TRPCError } from "@trpc/server";
+import { getDefaultScheduleId } from "../viewer/availability/util";
import { updateUserMetadataAllowedKeys, type TUpdateProfileInputSchema } from "./updateProfile.schema";
type UpdateProfileOptions = {
@@ -130,9 +131,40 @@ export const updateProfileHandler = async ({ ctx, input }: UpdateProfileOptions)
name: true,
createdDate: true,
locale: true,
+ schedules: {
+ select: {
+ id: true,
+ },
+ },
},
});
+ if (user.timeZone !== data.timeZone && updatedUser.schedules.length > 0) {
+ // on timezone change update timezone of default schedule
+ const defaultScheduleId = await getDefaultScheduleId(user.id, prisma);
+
+ if (!user.defaultScheduleId) {
+ // set default schedule if not already set
+ await prisma.user.update({
+ where: {
+ id: user.id,
+ },
+ data: {
+ defaultScheduleId,
+ },
+ });
+ }
+
+ await prisma.schedule.updateMany({
+ where: {
+ id: defaultScheduleId,
+ },
+ data: {
+ timeZone: data.timeZone,
+ },
+ });
+ }
+
if (hasEmailChangedOnNonCalProvider) {
// Because the email has changed, we are now attempting to use the CAL provider-
// which has no password yet. We have to send the reset password email.
diff --git a/packages/trpc/server/routers/viewer/availability/schedule/create.handler.ts b/packages/trpc/server/routers/viewer/availability/schedule/create.handler.ts
index 4117b996d6e..2c1f78c74b3 100644
--- a/packages/trpc/server/routers/viewer/availability/schedule/create.handler.ts
+++ b/packages/trpc/server/routers/viewer/availability/schedule/create.handler.ts
@@ -54,6 +54,8 @@ export const createHandler = async ({ input, ctx }: CreateOptions) => {
},
};
+ data.timeZone = user.timeZone;
+
const schedule = await prisma.schedule.create({
data,
});