* init migration and fix name for team event availability toggle * show and save restriction schedule * better naming around * fix loading stored restricted schedule instead of default * add the booker timezone checkbox in the form * restriction schedule application babbbyyyy * remove logs * test * typefix * fix * fix?? * fix??? * --- * typefix * functional fix * restrictionschedule logic --1 * timezone adjustment for regular constraint * noice * test suite for restrictionSchedule * auth restrictionschedule for eventtype * remove unnecessary comments * index * type fix * add concurrent * resolve change request * fix unauth erro * type fix * schedule select extracted from select * revert * fix auth vulnerabililty * fix e22 * chore: hide restriction schedule on platform * init review feedback resolutions * fixes * fix type * multiple windows of recurring rule-same day * team feature flag * fix type err * fix type * improvements * fix test * improve * fix error propagation * fix unused var lint * cleanup and using buildDateRanges * travel schedule inclusion * address comment --------- Co-authored-by: supalarry <laurisskraucis@gmail.com>
144 lines
3.7 KiB
TypeScript
144 lines
3.7 KiB
TypeScript
import type { PrismaClient } from "@calcom/prisma";
|
|
import prisma from "@calcom/prisma";
|
|
import { getDefaultScheduleId } from "@calcom/trpc/server/routers/viewer/availability/util";
|
|
|
|
import { hasReadPermissionsForUserId } from "../../hasEditPermissionForUser";
|
|
import {
|
|
transformAvailabilityForAtom,
|
|
transformDateOverridesForAtom,
|
|
transformWorkingHoursForAtom,
|
|
} from "../../schedules/transformers";
|
|
|
|
export type FindDetailedScheduleByIdReturnType = Awaited<
|
|
ReturnType<typeof ScheduleRepository.findDetailedScheduleById>
|
|
>;
|
|
|
|
export class ScheduleRepository {
|
|
// when instantiating, prismaClient injection is required
|
|
constructor(private prismaClient: PrismaClient) {}
|
|
|
|
async findScheduleByIdForBuildDateRanges({ scheduleId }: { scheduleId: number }) {
|
|
const schedule = await this.prismaClient.schedule.findUnique({
|
|
where: { id: scheduleId },
|
|
select: {
|
|
id: true,
|
|
timeZone: true,
|
|
userId: true,
|
|
availability: {
|
|
select: {
|
|
days: true,
|
|
startTime: true,
|
|
endTime: true,
|
|
date: true,
|
|
},
|
|
},
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
defaultScheduleId: true,
|
|
travelSchedules: {
|
|
select: {
|
|
id: true,
|
|
timeZone: true,
|
|
startDate: true,
|
|
endDate: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
return schedule;
|
|
}
|
|
|
|
async findScheduleByIdForOwnershipCheck({ scheduleId }: { scheduleId: number }) {
|
|
const schedule = await this.prismaClient.schedule.findUnique({
|
|
where: {
|
|
id: scheduleId,
|
|
},
|
|
select: {
|
|
userId: true,
|
|
},
|
|
});
|
|
return schedule;
|
|
}
|
|
|
|
static async findScheduleById({ id }: { id: number }) {
|
|
const schedule = await prisma.schedule.findUnique({
|
|
where: {
|
|
id,
|
|
},
|
|
select: {
|
|
id: true,
|
|
userId: true,
|
|
name: true,
|
|
availability: true,
|
|
timeZone: true,
|
|
},
|
|
});
|
|
|
|
return schedule;
|
|
}
|
|
|
|
static async findDetailedScheduleById({
|
|
isManagedEventType,
|
|
scheduleId,
|
|
userId,
|
|
defaultScheduleId,
|
|
timeZone: userTimeZone,
|
|
}: {
|
|
timeZone: string;
|
|
userId: number;
|
|
defaultScheduleId: number | null;
|
|
scheduleId?: number;
|
|
isManagedEventType?: boolean;
|
|
}) {
|
|
const schedule = await prisma.schedule.findUnique({
|
|
where: {
|
|
id: scheduleId || (await getDefaultScheduleId(userId, prisma)),
|
|
},
|
|
select: {
|
|
id: true,
|
|
userId: true,
|
|
name: true,
|
|
availability: true,
|
|
timeZone: true,
|
|
},
|
|
});
|
|
|
|
if (!schedule) {
|
|
throw new Error("Schedule not found");
|
|
}
|
|
const isCurrentUserPartOfTeam = hasReadPermissionsForUserId({ memberId: schedule?.userId, userId });
|
|
|
|
const isCurrentUserOwner = schedule?.userId === userId;
|
|
|
|
if (!isCurrentUserPartOfTeam && !isCurrentUserOwner) {
|
|
throw new Error("UNAUTHORIZED");
|
|
}
|
|
|
|
const timeZone = schedule.timeZone || userTimeZone;
|
|
|
|
const schedulesCount = await prisma.schedule.count({
|
|
where: {
|
|
userId: userId,
|
|
},
|
|
});
|
|
// disabling utc casting while fetching WorkingHours
|
|
return {
|
|
id: schedule.id,
|
|
name: schedule.name,
|
|
isManaged: schedule.userId !== userId,
|
|
workingHours: transformWorkingHoursForAtom(schedule),
|
|
schedule: schedule.availability,
|
|
availability: transformAvailabilityForAtom(schedule),
|
|
timeZone,
|
|
dateOverrides: transformDateOverridesForAtom(schedule, timeZone),
|
|
isDefault: !scheduleId || defaultScheduleId === schedule.id,
|
|
isLastSchedule: schedulesCount <= 1,
|
|
readOnly: schedule.userId !== userId && !isManagedEventType,
|
|
};
|
|
}
|
|
}
|