feat: optimize Prisma queries by replacing findFirst with findUnique where applicable (#21826)

* 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: [email protected] <[email protected]>

* 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: [email protected] <[email protected]>

* feat: optimize membership query in attributeUtils to use findUnique with userId_teamId constraint

Co-Authored-By: [email protected] <[email protected]>

* revert: exclude test files from Prisma query optimizations per user request

Co-Authored-By: [email protected] <[email protected]>

* revert: revert attributeUtils.ts to use findFirst for test compatibility

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: [email protected] <[email protected]>
Co-authored-by: Anik Dhabal Babu <[email protected]>
This commit is contained in:
authored and GitHub committed 2025-06-17 09:52:02 +03:00
1 parent a6b98bd35d
commit 4920abdaf7
93 files changed
+205 -172

No files matched your search

@@ -31,7 +31,7 @@ export class PaymentService implements IAbstractPaymentService {
bookingId: Booking["id"]
) {
try {
const booking = await prisma.booking.findFirst({
const booking = await prisma.booking.findUnique({
select: {
uid: true,
title: true,
@@ -41,7 +41,7 @@ export class PaymentService implements IAbstractPaymentService {
bookerEmail: string
) {
try {
const booking: PaidBooking | null = await prisma.booking.findFirst({
const booking: PaidBooking | null = await prisma.booking.findUnique({
where: {
id: bookingId,
},
@@ -10,7 +10,7 @@ export class PaymentService implements IAbstractPaymentService {
bookingId: Booking["id"]
) {
try {
const booking = await prisma.booking.findFirst({
const booking = await prisma.booking.findUnique({
select: {
uid: true,
title: true,
@@ -71,7 +71,7 @@ export class PaymentService implements IAbstractPaymentService {
paymentOption: PaymentOption
): Promise<Payment> {
try {
const booking = await prisma.booking.findFirst({
const booking = await prisma.booking.findUnique({
select: {
uid: true,
title: true,
@@ -37,7 +37,7 @@ export class PaymentService implements IAbstractPaymentService {
bookingId: Booking["id"]
) {
try {
const booking = await prisma.booking.findFirst({
const booking = await prisma.booking.findUnique({
select: {
uid: true,
title: true,
@@ -113,7 +113,7 @@ export class PaymentService implements IAbstractPaymentService {
throw new Error("Payment option is not compatible with create method");
}
try {
const booking = await prisma.booking.findFirst({
const booking = await prisma.booking.findUnique({
select: {
uid: true,
title: true,
@@ -26,7 +26,7 @@ export const getServerSideProps = async function getServerSideProps(
const isEmbed = appPages[1] === "embed";
const form = await prisma.app_RoutingForms_Form.findFirst({
const form = await prisma.app_RoutingForms_Form.findUnique({
where: {
id: formId,
},
@@ -235,7 +235,7 @@ export const formMutationHandler = async ({ ctx, input }: FormMutationHandlerOpt
inputFields: InputFields
) {
for (const [, connectedForm] of Object.entries(serializedForm.connectedForms)) {
const connectedFormDb = await prisma.app_RoutingForms_Form.findFirst({
const connectedFormDb = await prisma.app_RoutingForms_Form.findUnique({
where: {
id: connectedForm.id,
},
@@ -28,7 +28,7 @@ const getInCompleteBookingSettingsHandler = async (options: GetIncompleteBooking
formId: input.formId,
},
}),
prisma.app_RoutingForms_Form.findFirst({
prisma.app_RoutingForms_Form.findUnique({
where: {
id: input.formId,
},
@@ -51,7 +51,7 @@ const getInCompleteBookingSettingsHandler = async (options: GetIncompleteBooking
if (teamId) {
// Need to get the credentials for the team and org
const orgQuery = await prisma.team.findFirst({
const orgQuery = await prisma.team.findUnique({
where: {
id: teamId,
},
@@ -1356,7 +1356,7 @@ export default class SalesforceCRMService implements CRM {
if (!organizerEmail) {
this.log.warn(`No organizer email found for bookingUid ${bookingUid}`);
}
const booking = await prisma.booking.findFirst({
const booking = await prisma.booking.findUnique({
where: { uid: bookingUid },
select: { createdAt: true },
});
@@ -29,7 +29,7 @@ export async function assignmentReasonHandler({
}
async function handleAccountLookupFieldReason(routingFormResponseId: number, teamMemberEmail: string) {
const routingFormResponse = await prisma.app_RoutingForms_FormResponse.findFirst({
const routingFormResponse = await prisma.app_RoutingForms_FormResponse.findUnique({
where: {
id: routingFormResponseId,
},
+1 -1
View File
@@ -67,7 +67,7 @@ const Reschedule = async (bookingUid: string, cancellationReason: string) => {
if (bookingToReschedule && bookingToReschedule.eventTypeId && bookingToReschedule.user) {
const userOwner = bookingToReschedule.user;
const event = await prisma.eventType.findFirstOrThrow({
const event = await prisma.eventType.findUniqueOrThrow({
select: {
title: true,
},
@@ -69,7 +69,7 @@ const Reschedule = async (bookingUid: string, cancellationReason: string) => {
if (bookingToReschedule && bookingToReschedule.eventTypeId && bookingToReschedule.user) {
const userOwner = bookingToReschedule.user;
const event = await prisma.eventType.findFirstOrThrow({
const event = await prisma.eventType.findUniqueOrThrow({
select: {
title: true,
schedulingType: true,