Files
calendar/packages/trpc/server/routers/viewer/availability/schedule/duplicate.handler.ts
T
Rajiv SahalandGitHub 0757b00db7 feat: list schedules atom (#24205)
* feat: add api v2 endpoint to fetch all user schedules

* chore: update typing

* feat: custom hook to fetch all user schedules

* refactor: shift logic to transform schedules into function of its own

* init: list schedules atom

* feat: api endpoints for list schedules atom

* refactor: accept redirect url as prop

* fix: pass redirect url prop

* integrate list schedules atom with availability settings

* refactor: extract types to be reused in api v2 endpoints

* skip availability settings page for the time being until we have api v2 endpoints in prod

* feat: add docs for list schedules atom

* fixup

* export Schedule type

* make sure we always have a default schedule if user deletes his default schedule

* chore: implement code rabbit feedback

* chore: implement PR feedback

* fix: resolve merge conflicts

* fix: import path

* update platform libraries

* fix: type check

* resolve  merge conflicts

* update atoms export

* update platform libraries schedule

* chore: arrange atoms in alphabetical order

* update atoms controller to include endpoints for list schedules atom

* add create atom scheule atom

* fix: invalidate schedules on new schedule creation

* chore: add changesets
2025-10-15 17:57:22 +02:00

72 lines
1.7 KiB
TypeScript

import { prisma } from "@calcom/prisma";
import type { Prisma } from "@calcom/prisma/client";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../../types";
import type { TScheduleDuplicateSchema } from "./duplicate.schema";
type DuplicateScheduleOptions = {
ctx: {
user: Pick<NonNullable<TrpcSessionUser>, "id" | "timeZone">;
};
input: TScheduleDuplicateSchema;
};
export type DuplicateScheduleHandlerReturn = Awaited<ReturnType<typeof duplicateHandler>>;
export const duplicateHandler = async ({ ctx, input }: DuplicateScheduleOptions) => {
try {
const { scheduleId } = input;
const { user } = ctx;
const schedule = await prisma.schedule.findUnique({
where: {
id: scheduleId,
},
select: {
id: true,
userId: true,
name: true,
availability: true,
timeZone: true,
},
});
if (!schedule || schedule.userId !== user.id) {
throw new TRPCError({
code: "UNAUTHORIZED",
});
}
const { availability } = schedule;
const data: Prisma.ScheduleCreateInput = {
name: `${schedule.name} (Copy)`,
user: {
connect: {
id: user.id,
},
},
timeZone: schedule.timeZone ?? user.timeZone,
availability: {
createMany: {
data: availability.map((schedule) => ({
days: schedule.days,
startTime: schedule.startTime,
endTime: schedule.endTime,
date: schedule.date,
})),
},
},
};
const newSchedule = await prisma.schedule.create({
data,
});
return { schedule: newSchedule };
} catch (error) {
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
}
};