Files
calendar/packages/features/tasker/tasks/crm/lib/buildCalendarEvent.ts
T
devin-ai-integration[bot]GitHubbenny@cal.com <benny@cal.com>benny@cal.com <benny@cal.com>benny@cal.com <benny@cal.com>benny@cal.com <benny@cal.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>benny@cal.com <benny@cal.com>Anik Dhabal Babu
4920abdaf7 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: 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>
2025-06-17 09:52:02 +03:00

98 lines
2.7 KiB
TypeScript

import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses";
import { addVideoCallDataToEvent } from "@calcom/features/bookings/lib/handleNewBooking/addVideoCallDataToEvent";
import { getTranslation } from "@calcom/lib/server/i18n";
import prisma from "@calcom/prisma";
import type { CalendarEvent } from "@calcom/types/Calendar";
const buildCalendarEvent: (bookingUid: string) => Promise<CalendarEvent> = async (bookingUid: string) => {
const booking = await prisma.booking.findUnique({
where: {
uid: bookingUid,
},
include: {
user: {
select: {
name: true,
email: true,
locale: true,
username: true,
timeZone: true,
timeFormat: true,
},
},
eventType: {
select: {
slug: true,
bookingFields: true,
},
},
attendees: {
select: {
email: true,
locale: true,
name: true,
timeZone: true,
phoneNumber: true,
},
},
references: true,
},
});
if (!booking) {
throw new Error(`booking not found for bookings: ${bookingUid}`);
}
if (!booking.user) {
throw new Error(`organizer not found for booking: ${bookingUid}`);
}
if (!booking.eventType) {
throw new Error(`event type not found for booking ${bookingUid}`);
}
const organizerT = await getTranslation(booking.user?.locale ?? "en", "common");
const attendeePromises = [];
for (const attendee of booking.attendees) {
attendeePromises.push(
getTranslation(attendee.locale ?? "en", "common").then((tAttendee) => ({
email: attendee.email,
name: attendee.name,
timeZone: attendee.timeZone,
language: { translate: tAttendee, locale: attendee.locale ?? "en" },
phoneNumber: attendee.phoneNumber || undefined,
}))
);
}
const attendeeList = await Promise.all(attendeePromises);
let calendarEvent: CalendarEvent = {
uid: bookingUid,
type: booking.eventType.slug,
title: booking.title,
startTime: booking.startTime.toISOString(),
endTime: booking.endTime.toISOString(),
organizer: {
email: booking.user.email,
name: booking.user.name || "Nameless",
username: booking.user?.username || "No username",
language: { translate: organizerT, locale: booking.user?.locale ?? "en" },
timeZone: booking.user.timeZone,
},
attendees: attendeeList,
location: booking.location,
...getCalEventResponses({
bookingFields: booking?.eventType?.bookingFields ?? null,
booking,
}),
};
calendarEvent = addVideoCallDataToEvent(booking.references, calendarEvent);
return calendarEvent;
};
export default buildCalendarEvent;