feat: allow duplicate availability (#9615)
Co-authored-by: Carina Wollendorfer <[email protected]>
This commit is contained in:
1 parent
78393a2b8b
commit
eedf6913b8
5 files changed
+129
-1
No files matched your search
@@ -2,6 +2,7 @@ import authedProcedure from "../../../../procedures/authedProcedure";
|
||||
import { router } from "../../../../trpc";
|
||||
import { ZCreateInputSchema } from "./create.schema";
|
||||
import { ZDeleteInputSchema } from "./delete.schema";
|
||||
import { ZScheduleDuplicateSchema } from "./duplicate.schema";
|
||||
import { ZGetInputSchema } from "./get.schema";
|
||||
import { ZUpdateInputSchema } from "./update.schema";
|
||||
|
||||
@@ -10,6 +11,7 @@ type ScheduleRouterHandlerCache = {
|
||||
create?: typeof import("./create.handler").createHandler;
|
||||
delete?: typeof import("./delete.handler").deleteHandler;
|
||||
update?: typeof import("./update.handler").updateHandler;
|
||||
duplicate?: typeof import("./duplicate.handler").duplicateHandler;
|
||||
};
|
||||
|
||||
const UNSTABLE_HANDLER_CACHE: ScheduleRouterHandlerCache = {};
|
||||
@@ -78,4 +80,22 @@ export const scheduleRouter = router({
|
||||
input,
|
||||
});
|
||||
}),
|
||||
|
||||
duplicate: authedProcedure.input(ZScheduleDuplicateSchema).mutation(async ({ input, ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.duplicate) {
|
||||
UNSTABLE_HANDLER_CACHE.duplicate = await import("./duplicate.handler").then(
|
||||
(mod) => mod.duplicateHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.duplicate) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.duplicate({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../../trpc";
|
||||
import type { TScheduleDuplicateSchema } from "./duplicate.schema";
|
||||
import type { Prisma } from ".prisma/client";
|
||||
|
||||
type DuplicateScheduleOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TScheduleDuplicateSchema;
|
||||
};
|
||||
|
||||
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" });
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZScheduleDuplicateSchema = z.object({
|
||||
scheduleId: z.number(),
|
||||
});
|
||||
|
||||
export type TScheduleDuplicateSchema = z.infer<typeof ZScheduleDuplicateSchema>;
|
||||
Reference in new issue
Block a user