Scheduling infrastructure for absolutely everyone.
feat: Include booking question response in csv file from insights page (#25454)
Dhairyashil Shindea306a7767836
diff --git a/packages/features/insights/services/InsightsBookingBaseService.ts b/packages/features/insights/services/InsightsBookingBaseService.tsindex 560686c2b8e..27e1825dc53 100644--- a/packages/features/insights/services/InsightsBookingBaseService.ts+++ b/packages/features/insights/services/InsightsBookingBaseService.ts@@ -17,9 +17,11 @@ import { extractDateRangeFromColumnFilters } from "@calcom/features/insights/libimport type { DateRange } from "@calcom/features/insights/server/insightsDateUtils";import { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";+import { SYSTEM_PHONE_FIELDS } from "@calcom/lib/bookings/SystemField";import type { PrismaClient } from "@calcom/prisma";import { Prisma } from "@calcom/prisma/client";import { MembershipRole } from "@calcom/prisma/enums";+import { eventTypeBookingFields } from "@calcom/prisma/zod-utils";// Utility function to build user hash map with avatar URL fallbackexport const buildHashMapForUsers = <@@ -552,7 +554,7 @@ export class InsightsBookingBaseService {return { data: csvData, total: totalCount };}- // 2. Get all bookings with their attendees and seat references+ // 2. Get all bookings with their attendees, seat references, and phone dataconst bookings = await this.prisma.booking.findMany({where: {uid: {@@ -561,10 +563,12 @@ export class InsightsBookingBaseService {},select: {uid: true,+ eventTypeId: true,attendees: {select: {name: true,email: true,+ phoneNumber: true,noShow: true,},},@@ -574,66 +578,124 @@ export class InsightsBookingBaseService {select: {name: true,email: true,+ phoneNumber: true,noShow: true,},},},},+ responses: true,+ eventType: {+ select: {+ bookingFields: true,+ },+ },},});- // 3. Create booking map with attendee data (matching original logic)- const bookingMap = new Map(- bookings.map((booking) => {- const attendeeList =- booking.seatsReferences.length > 0- ? booking.seatsReferences.map((ref) => ref.attendee)- : booking.attendees;-- // List all no-show guests (name and email)- const noShowGuests =- attendeeList- .filter((attendee) => attendee?.noShow)- .map((attendee) => (attendee ? `${attendee.name} (${attendee.email})` : null))- .filter(Boolean) // remove null values- .join("; ") || null;- const noShowGuestsCount = attendeeList.filter((attendee) => attendee?.noShow).length;-- const formattedAttendees = attendeeList- .map((attendee) => (attendee ? `${attendee.name} (${attendee.email})` : null))- .filter(Boolean);-- return [booking.uid, { attendeeList: formattedAttendees, noShowGuests, noShowGuestsCount }];- })- );+ // 3. Process bookings: extract phone data and build attendee map+ const phoneFieldsCache = new Map<number, { name: string; label: string }[]>();+ const allPhoneFieldLabels = new Set<string>();+ let maxAttendees = 0;+ const finalBookingMap = new Map<+ string,+ {+ noShowGuests: string | null;+ noShowGuestsCount: number;+ attendeeList: string[];+ attendeePhoneNumbers: (string | null)[];+ phoneQuestionResponses: Record<string, string | null>;+ }+ >();- // 4. Calculate max attendees for dynamic columns- const maxAttendees = Math.max(- ...Array.from(bookingMap.values()).map((data) => data.attendeeList.length),- 0- );+ const extractPhoneValue = (value: unknown): string | null => {+ if (typeof value === "string" && value.trim()) return value;+ if (value && typeof value === "object" && "value" in value) {+ const val = (value as { value: unknown }).value;+ if (typeof val === "string" && val.trim()) return val;+ }+ return null;+ };++ for (const booking of bookings) {+ const eventTypeId = booking.eventTypeId;+ let phoneFields: { name: string; label: string }[] | null = null;++ if (eventTypeId) {+ if (phoneFieldsCache.has(eventTypeId)) {+ phoneFields = phoneFieldsCache.get(eventTypeId) || null;+ } else if (booking.eventType?.bookingFields) {+ const parsed = eventTypeBookingFields.safeParse(booking.eventType.bookingFields);+ if (parsed.success) {+ phoneFields = parsed.data+ .filter((field) => field.type === "phone" && !SYSTEM_PHONE_FIELDS.has(field.name))+ .map((field) => ({ name: field.name, label: field.label || field.name }));+ phoneFieldsCache.set(eventTypeId, phoneFields);+ phoneFields.forEach((field) => allPhoneFieldLabels.add(field.label));+ }+ }+ }- // 5. Create final booking map with attendee fields- const finalBookingMap = new Map(- Array.from(bookingMap.entries()).map(([uid, data]) => {- const attendeeFields: Record<string, string | null> = {};+ const attendeeList =+ booking.seatsReferences.length > 0+ ? booking.seatsReferences.map((ref) => ref.attendee)+ : booking.attendees;- for (let i = 1; i <= maxAttendees; i++) {- attendeeFields[`attendee${i}`] = data.attendeeList[i - 1] || null;+ const formattedAttendees: string[] = [];+ const noShowAttendees: string[] = [];+ const attendeePhoneNumbers: (string | null)[] = [];+ let noShowGuestsCount = 0;++ const phoneQuestionResponses: Record<string, string | null> = {};+ let systemPhoneValue: string | null = null;++ if (booking.responses && typeof booking.responses === "object") {+ const responses = booking.responses as Record<string, unknown>;++ systemPhoneValue =+ extractPhoneValue(responses.attendeePhoneNumber) ||+ extractPhoneValue(responses.smsReminderNumber) ||+ null;++ if (phoneFields) {+ for (const field of phoneFields) {+ phoneQuestionResponses[field.label] = extractPhoneValue(responses[field.name]);+ }}+ }- return [- uid,- {- noShowGuests: data.noShowGuests,- noShowGuestsCount: data.noShowGuestsCount,- ...attendeeFields,- },- ];- })- );+ const firstPhoneQuestionValue = Object.values(phoneQuestionResponses).find((v) => v !== null) || null;+ const phoneFallback = systemPhoneValue || firstPhoneQuestionValue;++ for (const attendee of attendeeList) {+ if (attendee) {+ const formatted = `${attendee.name} (${attendee.email})`;+ formattedAttendees.push(formatted);+ attendeePhoneNumbers.push(attendee.phoneNumber || phoneFallback);+ if (attendee.noShow) {+ noShowAttendees.push(formatted);+ noShowGuestsCount++;+ }+ }+ }++ if (formattedAttendees.length > maxAttendees) {+ maxAttendees = formattedAttendees.length;+ }- // 6. Combine booking data with attendee data and add ISO timestamp columns+ // List all no-show guests (name and email)+ const noShowGuests = noShowAttendees.length > 0 ? noShowAttendees.join("; ") : null;++ finalBookingMap.set(booking.uid, {+ noShowGuests,+ noShowGuestsCount,+ attendeeList: formattedAttendees,+ attendeePhoneNumbers,+ phoneQuestionResponses,+ });+ }++ // 4. Combine booking data with attendee data and format for CSVconst data = csvData.map((bookingTimeStatus) => {const dateAndTime = {createdAt: bookingTimeStatus.createdAt.toISOString(),@@ -647,46 +709,25 @@ export class InsightsBookingBaseService {endTime_time: dayjs(bookingTimeStatus.endTime).tz(timeZone).format(TIME_FORMAT),};- if (!bookingTimeStatus.uid) {- // should not be reached because we filtered above- const nullAttendeeFields: Record<string, null> = {};- for (let i = 1; i <= maxAttendees; i++) {- nullAttendeeFields[`attendee${i}`] = null;- }-- return {- ...bookingTimeStatus,- ...dateAndTime,- noShowGuests: null,- noShowGuestsCount: 0,- ...nullAttendeeFields,- };- }-- const attendeeData = finalBookingMap.get(bookingTimeStatus.uid);-- if (!attendeeData) {- const nullAttendeeFields: Record<string, null> = {};- for (let i = 1; i <= maxAttendees; i++) {- nullAttendeeFields[`attendee${i}`] = null;- }-- return {- ...bookingTimeStatus,- ...dateAndTime,- noShowGuests: null,- noShowGuestsCount: 0,- ...nullAttendeeFields,- };- }+ const attendeeData = bookingTimeStatus.uid ? finalBookingMap.get(bookingTimeStatus.uid) : null;- return {+ const result: Record<string, unknown> = {...bookingTimeStatus,...dateAndTime,- noShowGuests: attendeeData.noShowGuests,- noShowGuestsCount: attendeeData.noShowGuestsCount,- ...Object.fromEntries(Object.entries(attendeeData).filter(([key]) => key.startsWith("attendee"))),+ noShowGuests: attendeeData?.noShowGuests || null,+ noShowGuestsCount: attendeeData?.noShowGuestsCount || 0,};++ for (let i = 1; i <= maxAttendees; i++) {+ result[`attendee${i}`] = attendeeData?.attendeeList[i - 1] || null;+ result[`attendeePhone${i}`] = attendeeData?.attendeePhoneNumbers[i - 1] || null;+ }++ allPhoneFieldLabels.forEach((label) => {+ result[label] = attendeeData?.phoneQuestionResponses[label] || null;+ });++ return result;});return { data, total: totalCount };diff --git a/packages/lib/bookings/SystemField.ts b/packages/lib/bookings/SystemField.tsindex 9b8b4fe7f59..3dbe9a1b241 100644--- a/packages/lib/bookings/SystemField.ts+++ b/packages/lib/bookings/SystemField.ts@@ -16,6 +16,13 @@ export const SystemField = z.enum([export const SMS_REMINDER_NUMBER_FIELD = "smsReminderNumber";export const CAL_AI_AGENT_PHONE_NUMBER_FIELD = "aiAgentCallPhoneNumber";export const TITLE_FIELD = "title";+export const ATTENDEE_PHONE_NUMBER_FIELD = "attendeePhoneNumber";++export const SYSTEM_PHONE_FIELDS = new Set([+ ATTENDEE_PHONE_NUMBER_FIELD,+ SMS_REMINDER_NUMBER_FIELD,+ CAL_AI_AGENT_PHONE_NUMBER_FIELD,+]);/*** Check if a field should be displayed in custom responses section.