Skip to content

Commit ab96707

Browse files
authored
fix: clean up fields columns on /insights/routing (#19062)
* fix: clean up fields columns on /insights/routing * remove duplicates * move implementation to the backend
1 parent 7f86f69 commit ab96707

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

packages/features/insights/components/RoutingFormResponsesTable.tsx

+2-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import classNames from "@calcom/lib/classNames";
2929
import { useCopy } from "@calcom/lib/hooks/useCopy";
3030
import { useLocale } from "@calcom/lib/hooks/useLocale";
3131
import { BookingStatus } from "@calcom/prisma/enums";
32-
import { RoutingFormFieldType, isValidRoutingFormFieldType } from "@calcom/routing-forms/lib/FieldTypes";
32+
import { RoutingFormFieldType } from "@calcom/routing-forms/lib/FieldTypes";
3333
import { trpc, type RouterOutputs } from "@calcom/trpc";
3434
import {
3535
Badge,
@@ -258,7 +258,7 @@ export function RoutingFormResponsesTable() {
258258
useInsightsParameters();
259259

260260
const {
261-
data: headersRaw,
261+
data: headers,
262262
isLoading: isHeadersLoading,
263263
isSuccess: isHeadersSuccess,
264264
} = trpc.viewer.insights.routingFormResponsesHeaders.useQuery({
@@ -268,11 +268,6 @@ export function RoutingFormResponsesTable() {
268268
routingFormId,
269269
});
270270

271-
const headers = useMemo(() => {
272-
if (!headersRaw) return;
273-
return headersRaw.filter((header) => header.label && isValidRoutingFormFieldType(header.type));
274-
}, [headersRaw]);
275-
276271
const { data: forms } = trpc.viewer.insights.getRoutingFormsForFilters.useQuery({
277272
userId,
278273
teamId,

packages/features/insights/server/routing-events.ts

+36-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import mapKeys from "lodash/mapKeys";
44
// eslint-disable-next-line no-restricted-imports
55
import startCase from "lodash/startCase";
66

7+
import {
8+
RoutingFormFieldType,
9+
isValidRoutingFormFieldType,
10+
} from "@calcom/app-store/routing-forms/lib/FieldTypes";
711
import { zodFields as routingFormFieldsSchema } from "@calcom/app-store/routing-forms/zod";
812
import dayjs from "@calcom/dayjs";
913
import type { ColumnFilter, TypedColumnFilter } from "@calcom/features/data-table";
@@ -617,14 +621,38 @@ class RoutingEventsInsights {
617621
});
618622

619623
const fields = routingFormFieldsSchema.parse(routingForms.map((f) => f.fields).flat());
620-
const headers = fields?.map((f) => {
621-
return {
622-
id: f.id,
623-
label: f.label,
624-
type: f.type,
625-
options: f.options,
626-
};
627-
});
624+
const ids = new Set<string>();
625+
const headers = (fields || [])
626+
.map((f) => {
627+
return {
628+
id: f.id,
629+
label: f.label,
630+
type: f.type,
631+
options: f.options,
632+
};
633+
})
634+
.filter((field) => {
635+
if (!field.label || !isValidRoutingFormFieldType(field.type)) {
636+
return false;
637+
}
638+
if (
639+
field.type === RoutingFormFieldType.SINGLE_SELECT ||
640+
field.type === RoutingFormFieldType.MULTI_SELECT
641+
) {
642+
return field.options && field.options.length > 0;
643+
}
644+
return true;
645+
})
646+
.filter((field) => {
647+
// Remove duplicate fields
648+
// because we aggregate fields from multiple routing forms.
649+
if (ids.has(field.id)) {
650+
return false;
651+
} else {
652+
ids.add(field.id);
653+
return true;
654+
}
655+
});
628656

629657
return headers;
630658
}

0 commit comments

Comments
 (0)