chore: remove dead code regarding insights (#19022)

This commit is contained in:
Eunjae Lee authored and GitHub committed 2025-01-31 19:23:40 +01:00
1 parent b7df700771
commit 7bbacca32a
6 files changed
+14 -407

No files matched your search

@@ -4,10 +4,7 @@ import mapKeys from "lodash/mapKeys";
// eslint-disable-next-line no-restricted-imports
import startCase from "lodash/startCase";
import {
zodFields as routingFormFieldsSchema,
routingFormResponseInDbSchema,
} from "@calcom/app-store/routing-forms/zod";
import { zodFields as routingFormFieldsSchema } from "@calcom/app-store/routing-forms/zod";
import dayjs from "@calcom/dayjs";
import type { ColumnFilter, TypedColumnFilter } from "@calcom/features/data-table";
import { ColumnFilterType } from "@calcom/features/data-table";
@@ -632,185 +629,6 @@ class RoutingEventsInsights {
return headers;
}
static async getRawData({
teamId,
startDate,
endDate,
isAll,
organizationId,
routingFormId,
userId,
memberUserId,
bookingStatus,
fieldFilter,
take,
skip,
}: Omit<RoutingFormInsightsFilter, "columnFilters"> & { take?: number; skip?: number }) {
const formsWhereCondition = await this.getWhereForTeamOrAllTeams({
userId,
teamId,
isAll,
organizationId,
routingFormId,
});
// First get all forms and their fields to build a mapping
const forms = await prisma.app_RoutingForms_Form.findMany({
where: formsWhereCondition,
select: {
id: true,
name: true,
fields: true,
},
});
// Create a mapping of form ID to fields
type FormFieldOption = {
label: string;
type: string;
options: Record<string, string>;
};
type FormFieldsMap = Record<string, Record<string, FormFieldOption>>;
const formFieldsMap = forms.reduce((acc, form) => {
const fields = routingFormFieldsSchema.parse(form.fields);
acc[form.id] =
fields?.reduce((fieldMap: Record<string, FormFieldOption>, field) => {
fieldMap[field.id] = {
label: field.label,
type: field.type,
options:
field.options?.reduce((optMap, opt) => {
if (opt.id !== null) {
optMap[opt.id] = opt.label;
}
return optMap;
}, {} as Record<string, string>) ?? {},
};
return fieldMap;
}, {}) || {};
return acc;
}, {} as FormFieldsMap);
const responsesWhereCondition: Prisma.App_RoutingForms_FormResponseWhereInput = {
...(startDate &&
endDate && {
createdAt: {
gte: dayjs(startDate).startOf("day").toDate(),
lte: dayjs(endDate).endOf("day").toDate(),
},
}),
...(memberUserId || bookingStatus
? {
...(bookingStatus === "NO_BOOKING"
? { routedToBooking: null }
: {
routedToBooking: {
...(memberUserId && { userId: memberUserId }),
...(bookingStatus && { status: bookingStatus }),
},
}),
}
: {}),
...(fieldFilter && {
response: {
path: [fieldFilter.fieldId, "value"],
array_contains: [fieldFilter.optionId],
},
}),
form: formsWhereCondition,
};
const responses = await prisma.app_RoutingForms_FormResponse.findMany({
select: {
id: true,
response: true,
createdAt: true,
form: {
select: {
id: true,
name: true,
},
},
routedToBooking: {
select: {
uid: true,
status: true,
createdAt: true,
startTime: true,
endTime: true,
attendees: {
select: {
email: true,
name: true,
timeZone: true,
},
},
user: {
select: {
name: true,
email: true,
},
},
assignmentReason: {
select: {
reasonString: true,
},
},
},
},
},
where: responsesWhereCondition,
orderBy: {
createdAt: "desc",
},
take: take,
skip: skip,
});
// Transform the data into a flat structure suitable for CSV
return responses.map((response) => {
const parsedResponse = routingFormResponseInDbSchema.parse(response.response);
const formFields = formFieldsMap[response.form.id];
const flatResponse: Record<string, unknown> = {
"Response ID": response.id,
"Form Name": response.form.name,
"Submitted At": response.createdAt.toISOString(),
"Has Booking": !!response.routedToBooking,
"Booking Status": response.routedToBooking?.status || "NO_BOOKING",
"Booking Created At": response.routedToBooking?.createdAt?.toISOString() || "",
"Booking Start Time": response.routedToBooking?.startTime?.toISOString() || "",
"Booking End Time": response.routedToBooking?.endTime?.toISOString() || "",
"Attendee Name": response.routedToBooking?.attendees[0]?.name || "",
"Attendee Email": response.routedToBooking?.attendees[0]?.email || "",
"Attendee Timezone": response.routedToBooking?.attendees[0]?.timeZone || "",
"Assignment Reason": response.routedToBooking?.assignmentReason[0].reasonString || "",
"Routed To Name": response.routedToBooking?.user?.name || "",
"Routed To Email": response.routedToBooking?.user?.email || "",
};
// Add form fields as columns with their labels
Object.entries(parsedResponse).forEach(([fieldId, field]) => {
const fieldInfo = formFields[fieldId];
if (fieldInfo) {
const fieldLabel = fieldInfo.label;
if (Array.isArray(field.value)) {
// For multi-select fields, map the IDs to labels
const values = field.value.map((val) => fieldInfo.options[val] || val);
flatResponse[fieldLabel] = values.join(", ");
} else {
// For single-select fields, map the ID to label
flatResponse[fieldLabel] = fieldInfo.options[field.value] || field.value;
}
}
});
return flatResponse;
});
}
static async routedToPerPeriod({
userId,
teamId,
@@ -1663,66 +1663,6 @@ export const insightsRouter = router({
return headers || [];
}),
rawRoutingData: userBelongsToTeamProcedure
.input(
rawDataInputSchema.extend({
routingFormId: z.string().optional(),
bookingStatus: bookingStatusSchema,
fieldFilter: z
.object({
fieldId: z.string(),
optionId: z.string(),
})
.optional(),
cursor: z.number().optional(),
})
)
.query(async ({ ctx, input }) => {
const {
teamId,
startDate,
endDate,
userId,
memberUserId,
isAll,
routingFormId,
bookingStatus,
fieldFilter,
cursor,
} = input;
if (!teamId && !userId) {
return { data: [], hasMore: false, nextCursor: null };
}
try {
const csvData = await RoutingEventsInsights.getRawData({
teamId,
startDate,
endDate,
userId,
memberUserId,
isAll: isAll ?? false,
organizationId: ctx.user.organizationId,
routingFormId,
bookingStatus,
fieldFilter,
take: BATCH_SIZE,
skip: cursor || 0,
});
const hasMore = csvData.length === BATCH_SIZE;
const nextCursor = hasMore ? (cursor || 0) + BATCH_SIZE : null;
return {
data: csvData,
hasMore,
nextCursor,
};
} catch (e) {
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
}
}),
routedToPerPeriod: userBelongsToTeamProcedure
.input(
rawDataInputSchema.extend({