* feat: optimize Prisma queries by replacing findFirst with findUnique where applicable - Replace findFirst/findFirstOrThrow with findUnique/findUniqueOrThrow for queries using unique constraints - Maintain existing functionality and error handling behavior - Focus on queries using primary keys and unique index fields from schema - Revert problematic changes that caused test failures to maintain stability Co-Authored-By: benny@cal.com <benny@cal.com> * revert: exclude API files from Prisma query optimizations per user request - Reverted all 55 API-related files to their original state - Kept all non-API Prisma query optimizations intact - API files include apps/api/v1, apps/api/v2, apps/web/app/api, and packages/app-store/*/api - Non-API optimizations remain for packages/lib, packages/features, apps/web (non-api), etc. Co-Authored-By: benny@cal.com <benny@cal.com> * feat: optimize membership query in attributeUtils to use findUnique with userId_teamId constraint Co-Authored-By: benny@cal.com <benny@cal.com> * revert: exclude test files from Prisma query optimizations per user request Co-Authored-By: benny@cal.com <benny@cal.com> * revert: revert attributeUtils.ts to use findFirst for test compatibility Co-Authored-By: benny@cal.com <benny@cal.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: benny@cal.com <benny@cal.com> Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import { prisma } from "@calcom/prisma";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../../types";
|
|
import { updateHostsWithNewDefaultSchedule } from "../util";
|
|
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.findUnique({
|
|
where: {
|
|
id: input.scheduleId,
|
|
},
|
|
select: {
|
|
userId: true,
|
|
},
|
|
});
|
|
|
|
if (scheduleToDelete?.userId !== user.id) throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
// cannot remove this schedule if this is the last schedule remaining
|
|
// if this is the last remaining schedule of the user then this would be the default schedule and so cannot remove it
|
|
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,
|
|
},
|
|
});
|
|
|
|
// to throw the error if there arent any other schedules
|
|
if (!scheduleToSetAsDefault) throw new TRPCError({ code: "BAD_REQUEST" });
|
|
|
|
await updateHostsWithNewDefaultSchedule(user.id, input.scheduleId, scheduleToSetAsDefault.id);
|
|
|
|
await prisma.user.update({
|
|
where: {
|
|
id: user.id,
|
|
},
|
|
data: {
|
|
defaultScheduleId: scheduleToSetAsDefault?.id || null,
|
|
},
|
|
});
|
|
} else if (user.defaultScheduleId) {
|
|
await updateHostsWithNewDefaultSchedule(user.id, input.scheduleId, user.defaultScheduleId);
|
|
}
|
|
|
|
await prisma.schedule.delete({
|
|
where: {
|
|
id: input.scheduleId,
|
|
},
|
|
});
|
|
};
|