* refactor: platform-types schedule types * refactor: lib add bidirectional schedule transformers api <-> atom * refactor: trpc handlers use renamed lib transformers * refactor: platform-libraries export new bi-directional transformers * chore: v2 update platform-libraries * chore: enable also old platform-libraries * fix: platform-types support previous types * refactor: versioning date * chore: version old schedules as schedules_2024_04_15 module * chore: version old schedules as schedules_2024_04_15 module * feat: new 2024_06_11 schedules module * feat: atoms use refactored schedule api * refactor: remove ?for=atom in schedules hooks * fix: unit test
78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import {
|
|
transformAvailabilityForAtom,
|
|
transformDateOverridesForAtom,
|
|
transformWorkingHoursForAtom,
|
|
} from "@calcom/lib";
|
|
import { hasReadPermissionsForUserId } from "@calcom/lib/hasEditPermissionForUser";
|
|
import { prisma } from "@calcom/prisma";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../../trpc";
|
|
import { getDefaultScheduleId } from "../util";
|
|
import type { TGetInputSchema } from "./get.schema";
|
|
|
|
type GetOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TGetInputSchema;
|
|
};
|
|
|
|
export const getHandler = async ({ ctx, input }: GetOptions) => {
|
|
const { user } = ctx;
|
|
|
|
const schedule = await prisma.schedule.findUnique({
|
|
where: {
|
|
id: input.scheduleId || (await getDefaultScheduleId(user.id, prisma)),
|
|
},
|
|
select: {
|
|
id: true,
|
|
userId: true,
|
|
name: true,
|
|
availability: true,
|
|
timeZone: true,
|
|
},
|
|
});
|
|
|
|
if (!schedule) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
});
|
|
}
|
|
const isCurrentUserPartOfTeam = hasReadPermissionsForUserId({
|
|
ctx,
|
|
input: { memberId: schedule?.userId },
|
|
});
|
|
|
|
const isCurrentUserOwner = schedule?.userId === user.id;
|
|
|
|
if (!isCurrentUserPartOfTeam && !isCurrentUserOwner) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
});
|
|
}
|
|
|
|
const timeZone = schedule.timeZone || user.timeZone;
|
|
|
|
const schedulesCount = await prisma.schedule.count({
|
|
where: {
|
|
userId: user.id,
|
|
},
|
|
});
|
|
// disabling utc casting while fetching WorkingHours
|
|
return {
|
|
id: schedule.id,
|
|
name: schedule.name,
|
|
isManaged: schedule.userId !== user.id,
|
|
workingHours: transformWorkingHoursForAtom(schedule),
|
|
schedule: schedule.availability,
|
|
availability: transformAvailabilityForAtom(schedule),
|
|
timeZone,
|
|
dateOverrides: transformDateOverridesForAtom(schedule, timeZone),
|
|
isDefault: !input.scheduleId || user.defaultScheduleId === schedule.id,
|
|
isLastSchedule: schedulesCount <= 1,
|
|
readOnly: schedule.userId !== user.id && !input.isManagedEventType,
|
|
};
|
|
};
|