* experiment: cold start perf * fix: update failing test * chore: add database indexes * chore: use json protocol and add query batching back * Update [status].tsx * Update [trpc].ts * Delete getSlimSession.ts * Update createContext.ts * remove trpc caller * correctly import Prisma * lazy ethRouter * replace crypto with md5 * import fixes * public event endpoint refactor * Update yarn.lock * Update yarn.lock * Using yarn.lock from main --------- Co-authored-by: Omar López <zomars@me.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Efraín Rochín <roae.85@gmail.com> Co-authored-by: Keith Williams <keithwillcode@gmail.com>
58 lines
1.2 KiB
TypeScript
58 lines
1.2 KiB
TypeScript
import { prisma } from "@calcom/prisma";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../../trpc";
|
|
import type { TDeleteInputSchema } from "./delete.schema";
|
|
|
|
type DeleteOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TDeleteInputSchema;
|
|
};
|
|
|
|
export const deleteHandler = async ({ input, ctx }: DeleteOptions) => {
|
|
const { user } = ctx;
|
|
|
|
const scheduleToDelete = await prisma.schedule.findFirst({
|
|
where: {
|
|
id: input.scheduleId,
|
|
},
|
|
select: {
|
|
userId: true,
|
|
},
|
|
});
|
|
|
|
if (scheduleToDelete?.userId !== user.id) throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
if (user.defaultScheduleId === input.scheduleId) {
|
|
// set a new default or unset default if no other schedule
|
|
const scheduleToSetAsDefault = await prisma.schedule.findFirst({
|
|
where: {
|
|
userId: user.id,
|
|
NOT: {
|
|
id: input.scheduleId,
|
|
},
|
|
},
|
|
select: {
|
|
id: true,
|
|
},
|
|
});
|
|
|
|
await prisma.user.update({
|
|
where: {
|
|
id: user.id,
|
|
},
|
|
data: {
|
|
defaultScheduleId: scheduleToSetAsDefault?.id || null,
|
|
},
|
|
});
|
|
}
|
|
await prisma.schedule.delete({
|
|
where: {
|
|
id: input.scheduleId,
|
|
},
|
|
});
|
|
};
|