Skip to content

Scheduling infrastructure for absolutely everyone.

feat: add date and time columns to insights CSV exports (#23771)

Eunjae Leeb407be933dad
diff --git a/packages/features/insights/server/routing-events.ts b/packages/features/insights/server/routing-events.ts
index 238eca8482c..aa56f0ef000 100644
--- a/packages/features/insights/server/routing-events.ts
+++ b/packages/features/insights/server/routing-events.ts
@@ -8,6 +8,7 @@ import {
isValidRoutingFormFieldType,
} from "@calcom/app-store/routing-forms/lib/FieldTypes";
import { zodFields as routingFormFieldsSchema } from "@calcom/app-store/routing-forms/zod";
+import dayjs from "@calcom/dayjs";
import { WEBAPP_URL } from "@calcom/lib/constants";
import type { InsightsRoutingBaseService } from "@calcom/lib/server/service/InsightsRoutingBaseService";
import { readonlyPrisma as prisma } from "@calcom/prisma";
@@ -71,6 +72,7 @@ class RoutingEventsInsights {
teamId: {
in: teamIds,
},
+ ...(routingFormId && { id: routingFormId }),
}
: {
userId: userId ?? -1,
@@ -119,9 +121,11 @@ class RoutingEventsInsights {
static async getRoutingFormPaginatedResponsesForDownload({
headersPromise,
dataPromise,
+ timeZone,
}: {
headersPromise: ReturnType<typeof RoutingEventsInsights.getRoutingFormHeaders>;
dataPromise: ReturnType<typeof InsightsRoutingBaseService.prototype.getTableData>;
+ timeZone: string;
}) {
const [headers, data] = await Promise.all([headersPromise, dataPromise]);
@@ -157,11 +161,31 @@ class RoutingEventsInsights {
"Response ID": item.id,
"Form Name": item.formName,
"Submitted At": item.createdAt.toISOString(),
+ "Submitted At_date": dayjs(item.createdAt).tz(timeZone).format("YYYY-MM-DD"),
+ "Submitted At_time": dayjs(item.createdAt).tz(timeZone).format("HH:mm:ss"),
"Has Booking": item.bookingUid !== null,
"Booking Status": item.bookingStatus || "NO_BOOKING",
"Booking Created At": item.bookingCreatedAt?.toISOString() || "",
+ "Booking Created At_date": item.bookingCreatedAt
+ ? dayjs(item.bookingCreatedAt).tz(timeZone).format("YYYY-MM-DD")
+ : "",
+ "Booking Created At_time": item.bookingCreatedAt
+ ? dayjs(item.bookingCreatedAt).tz(timeZone).format("HH:mm:ss")
+ : "",
"Booking Start Time": item.bookingStartTime?.toISOString() || "",
+ "Booking Start Time_date": item.bookingStartTime
+ ? dayjs(item.bookingStartTime).tz(timeZone).format("YYYY-MM-DD")
+ : "",
+ "Booking Start Time_time": item.bookingStartTime
+ ? dayjs(item.bookingStartTime).tz(timeZone).format("HH:mm:ss")
+ : "",
"Booking End Time": item.bookingEndTime?.toISOString() || "",
+ "Booking End Time_date": item.bookingEndTime
+ ? dayjs(item.bookingEndTime).tz(timeZone).format("YYYY-MM-DD")
+ : "",
+ "Booking End Time_time": item.bookingEndTime
+ ? dayjs(item.bookingEndTime).tz(timeZone).format("HH:mm:ss")
+ : "",
"Assignment Reason": item.bookingAssignmentReason || "",
"Routed To Name": item.bookingUserName || "",
"Routed To Email": item.bookingUserEmail || "",
diff --git a/packages/features/insights/server/trpc-router.ts b/packages/features/insights/server/trpc-router.ts
index e97d7f15edf..a287db00aef 100644
--- a/packages/features/insights/server/trpc-router.ts
+++ b/packages/features/insights/server/trpc-router.ts
@@ -822,7 +822,7 @@ export const insightsRouter = router({
})
)
.query(async ({ ctx, input }) => {
- const { limit, offset } = input;
+ const { limit, offset, timeZone } = input;
const insightsBookingService = createInsightsBookingService(ctx, input);
@@ -830,6 +830,7 @@ export const insightsRouter = router({
return await insightsBookingService.getCsvData({
limit: limit ?? 100,
offset: offset ?? 0,
+ timeZone,
});
} catch (e) {
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
@@ -886,6 +887,7 @@ export const insightsRouter = router({
return await RoutingEventsInsights.getRoutingFormPaginatedResponsesForDownload({
headersPromise,
dataPromise,
+ timeZone: ctx.user.timeZone,
});
}),
getRoutingFormFieldOptions: userBelongsToTeamProcedure
diff --git a/packages/lib/server/service/InsightsBookingBaseService.ts b/packages/lib/server/service/InsightsBookingBaseService.ts
index 3046bd59137..183f9af07ad 100644
--- a/packages/lib/server/service/InsightsBookingBaseService.ts
+++ b/packages/lib/server/service/InsightsBookingBaseService.ts
@@ -479,7 +479,17 @@ export class InsightsBookingBaseService {
});
}
- async getCsvData({ limit = 100, offset = 0 }: { limit?: number; offset?: number }) {
+ async getCsvData({
+ limit = 100,
+ offset = 0,
+ timeZone,
+ }: {
+ limit?: number;
+ offset?: number;
+ timeZone: string;
+ }) {
+ const DATE_FORMAT = "YYYY-MM-DD";
+ const TIME_FORMAT = "HH:mm:ss";
const baseConditions = await this.getBaseConditions();
// Get total count first
@@ -628,8 +638,20 @@ export class InsightsBookingBaseService {
})
);
- // 6. Combine booking data with attendee data
+ // 6. Combine booking data with attendee data and add ISO timestamp columns
const data = csvData.map((bookingTimeStatus) => {
+ const dateAndTime = {
+ createdAt: bookingTimeStatus.createdAt.toISOString(),
+ createdAt_date: dayjs(bookingTimeStatus.createdAt).tz(timeZone).format(DATE_FORMAT),
+ createdAt_time: dayjs(bookingTimeStatus.createdAt).tz(timeZone).format(TIME_FORMAT),
+ startTime: bookingTimeStatus.startTime.toISOString(),
+ startTime_date: dayjs(bookingTimeStatus.startTime).tz(timeZone).format(DATE_FORMAT),
+ startTime_time: dayjs(bookingTimeStatus.startTime).tz(timeZone).format(TIME_FORMAT),
+ endTime: bookingTimeStatus.endTime.toISOString(),
+ endTime_date: dayjs(bookingTimeStatus.endTime).tz(timeZone).format(DATE_FORMAT),
+ 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> = {};
@@ -639,6 +661,7 @@ export class InsightsBookingBaseService {
return {
...bookingTimeStatus,
+ ...dateAndTime,
noShowGuests: null,
noShowGuestsCount: 0,
...nullAttendeeFields,
@@ -655,6 +678,7 @@ export class InsightsBookingBaseService {
return {
...bookingTimeStatus,
+ ...dateAndTime,
noShowGuests: null,
noShowGuestsCount: 0,
...nullAttendeeFields,
@@ -663,6 +687,7 @@ export class InsightsBookingBaseService {
return {
...bookingTimeStatus,
+ ...dateAndTime,
noShowGuests: attendeeData.noShowGuests,
noShowGuestsCount: attendeeData.noShowGuestsCount,
...Object.fromEntries(Object.entries(attendeeData).filter(([key]) => key.startsWith("attendee"))),