Files
cal.diy/packages/trpc/server/routers/viewer/availability/schedule/update.handler.ts
T
3bc9fa11d0 chore: remove @calcom/lib barrel file (#21267)
* refactor: replace imports from @calcom/lib barrel file with direct imports

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: add deprecated barrel file with warning to support tests

Co-Authored-By: benny@cal.com <benny@cal.com>

* remove barrel file

* fix: restore barrel file with deprecation notice and add CreditType enum export

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: add CreditType enum definition to barrel file

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: add mock for CreditType enum in credit-service test

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: use importOriginal in CreditType enum mock

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: remove barrel file completely

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: restore barrel file with deprecation notice to support tests

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: update test-setup to use direct import from @calcom/ui/classNames

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: completely remove barrel file

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: update package.json to remove references to index.ts

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: remove main and types fields from package.json

Co-Authored-By: benny@cal.com <benny@cal.com>

* fix: restore barrel file with deprecation notice to support tests

Co-Authored-By: benny@cal.com <benny@cal.com>

* remove barrel file

* fix

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: benny@cal.com <benny@cal.com>
Co-authored-by: hbjORbj <sldisek783@gmail.com>
2025-05-13 21:55:58 -04:00

134 lines
3.7 KiB
TypeScript

import { getAvailabilityFromSchedule } from "@calcom/lib/availability";
import { hasEditPermissionForUserID } from "@calcom/lib/hasEditPermissionForUser";
import { transformScheduleToAvailabilityForAtom } from "@calcom/lib/schedules/transformers/for-atom";
import { prisma } from "@calcom/prisma";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../../types";
import { setupDefaultSchedule } from "../util";
import type { TUpdateInputSchema } from "./update.schema";
type User = NonNullable<TrpcSessionUser>;
type UpdateOptions = {
ctx: {
user: { id: User["id"]; defaultScheduleId: User["defaultScheduleId"]; timeZone: User["timeZone"] };
};
input: TUpdateInputSchema;
};
export const updateHandler = async ({ input, ctx }: UpdateOptions) => {
const { user } = ctx;
const availability = input.schedule
? getAvailabilityFromSchedule(input.schedule)
: (input.dateOverrides || []).map((dateOverride) => ({
startTime: dateOverride.start,
endTime: dateOverride.end,
date: dateOverride.start,
days: [],
}));
// Not able to update the schedule with userId where clause, so fetch schedule separately and then validate
// Bug: https://github.com/prisma/prisma/issues/7290
const userSchedule = await prisma.schedule.findUnique({
where: {
id: input.scheduleId,
},
select: {
userId: true,
name: true,
id: true,
},
});
if (!userSchedule) {
throw new TRPCError({
code: "UNAUTHORIZED",
});
}
if (userSchedule?.userId !== user.id) {
const hasEditPermission = await hasEditPermissionForUserID({
ctx,
input: { memberId: userSchedule.userId },
});
if (!hasEditPermission) {
throw new TRPCError({
code: "UNAUTHORIZED",
});
}
}
let updatedUser;
if (input.isDefault) {
const setupDefault = await setupDefaultSchedule(user.id, input.scheduleId, prisma);
updatedUser = setupDefault;
}
if (!input.name) {
// TODO: Improve
// We don't want to pass the full schedule for just a set as default update
// but in the current logic, this wipes the existing availability.
// Return early to prevent this from happening.
return {
schedule: userSchedule,
isDefault: updatedUser
? updatedUser.defaultScheduleId === input.scheduleId
: user.defaultScheduleId === input.scheduleId,
};
}
const schedule = await prisma.schedule.update({
where: {
id: input.scheduleId,
},
data: {
timeZone: input.timeZone,
name: input.name,
availability: {
deleteMany: {
scheduleId: {
equals: input.scheduleId,
},
},
createMany: {
data: [
...availability,
...(input.dateOverrides || []).map((override) => ({
date: override.start,
startTime: override.start,
endTime: override.end,
})),
],
},
},
},
select: {
id: true,
userId: true,
name: true,
availability: true,
timeZone: true,
eventType: {
select: {
id: true,
eventName: true,
},
},
},
});
const userAvailability = transformScheduleToAvailabilityForAtom(schedule);
return {
schedule,
availability: userAvailability,
timeZone: schedule.timeZone || user.timeZone,
isDefault: updatedUser
? updatedUser.defaultScheduleId === schedule.id
: user.defaultScheduleId === schedule.id,
prevDefaultId: user.defaultScheduleId,
currentDefaultId: updatedUser ? updatedUser.defaultScheduleId : user.defaultScheduleId,
};
};