Skip to content

Commit cf4e7a5

Browse files
refactor: add useInsightsRoutingParameters() for InsightsRoutingService (calcom#22792)
Co-authored-by: Devanshu Sharma <devanshusharma658@gmail.com>
1 parent 0e2ee3a commit cf4e7a5

3 files changed

Lines changed: 67 additions & 26 deletions

File tree

packages/features/insights/components/routing/RoutingFunnel.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use client";
22

3-
import { useColumnFilters } from "@calcom/features/data-table";
4-
import { useInsightsParameters } from "@calcom/features/insights/hooks/useInsightsParameters";
3+
import { useInsightsRoutingParameters } from "@calcom/features/insights/hooks/useInsightsRoutingParameters";
54
import { useLocale } from "@calcom/lib/hooks/useLocale";
65
import { trpc } from "@calcom/trpc";
76

@@ -11,18 +10,9 @@ import { RoutingFunnelSkeleton } from "./RoutingFunnelSkeleton";
1110

1211
export function RoutingFunnel() {
1312
const { t } = useLocale();
14-
const { scope, selectedTeamId, startDate, endDate } = useInsightsParameters();
15-
const columnFilters = useColumnFilters({
16-
exclude: ["createdAt"],
17-
});
13+
const insightsRoutingParams = useInsightsRoutingParameters();
1814
const { data, isSuccess, isLoading } = trpc.viewer.insights.getRoutingFunnelData.useQuery(
19-
{
20-
scope,
21-
selectedTeamId,
22-
startDate,
23-
endDate,
24-
columnFilters,
25-
},
15+
insightsRoutingParams,
2616
{
2717
staleTime: 30000,
2818
trpc: {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { useMemo } from "react";
2+
3+
import dayjs from "@calcom/dayjs";
4+
import { useFilterValue, useColumnFilters, ZDateRangeFilterValue } from "@calcom/features/data-table";
5+
import { useChangeTimeZoneWithPreservedLocalTime } from "@calcom/features/data-table/hooks/useChangeTimeZoneWithPreservedLocalTime";
6+
import { getDefaultStartDate, getDefaultEndDate } from "@calcom/features/data-table/lib/dateRange";
7+
8+
import { useInsightsOrgTeams } from "./useInsightsOrgTeams";
9+
10+
export function useInsightsRoutingParameters() {
11+
const { scope, selectedTeamId } = useInsightsOrgTeams();
12+
13+
const createdAtRange = useFilterValue("createdAt", ZDateRangeFilterValue)?.data;
14+
// TODO for future: this preserving local time & startOf & endOf should be handled
15+
// from DateRangeFilter out of the box.
16+
// When we do it, we also need to remove those timezone handling logic from the backend side at the same time.
17+
const startDate = useChangeTimeZoneWithPreservedLocalTime(
18+
useMemo(() => {
19+
return dayjs(createdAtRange?.startDate ?? getDefaultStartDate().toISOString())
20+
.startOf("day")
21+
.toISOString();
22+
}, [createdAtRange?.startDate])
23+
);
24+
const endDate = useChangeTimeZoneWithPreservedLocalTime(
25+
useMemo(() => {
26+
return dayjs(createdAtRange?.endDate ?? getDefaultEndDate().toISOString())
27+
.endOf("day")
28+
.toISOString();
29+
}, [createdAtRange?.endDate])
30+
);
31+
32+
const columnFilters = useColumnFilters({
33+
exclude: ["createdAt"],
34+
});
35+
36+
return {
37+
scope,
38+
selectedTeamId,
39+
startDate,
40+
endDate,
41+
columnFilters,
42+
};
43+
}

packages/features/insights/server/trpc-router.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,26 @@ function createInsightsBookingService(
341341
},
342342
});
343343
}
344+
345+
function createInsightsRoutingService(
346+
ctx: { insightsDb: typeof readonlyPrisma; user: { id: number; organizationId: number | null } },
347+
input: z.infer<typeof routingRepositoryBaseInputSchema>
348+
) {
349+
return getInsightsRoutingService({
350+
options: {
351+
scope: input.scope,
352+
teamId: input.selectedTeamId,
353+
userId: ctx.user.id,
354+
orgId: ctx.user.organizationId,
355+
},
356+
filters: {
357+
startDate: input.startDate,
358+
endDate: input.endDate,
359+
columnFilters: input.columnFilters,
360+
},
361+
});
362+
}
363+
344364
export const insightsRouter = router({
345365
bookingKPIStats: userBelongsToTeamProcedure
346366
.input(bookingRepositoryBaseInputSchema)
@@ -1002,19 +1022,7 @@ export const insightsRouter = router({
10021022
timeView,
10031023
weekStart: ctx.user.weekStart,
10041024
});
1005-
const insightsRoutingService = getInsightsRoutingService({
1006-
options: {
1007-
scope: input.scope,
1008-
teamId: input.selectedTeamId,
1009-
userId: ctx.user.id,
1010-
orgId: ctx.user.organizationId,
1011-
},
1012-
filters: {
1013-
startDate: input.startDate,
1014-
endDate: input.endDate,
1015-
columnFilters: input.columnFilters,
1016-
},
1017-
});
1025+
const insightsRoutingService = createInsightsRoutingService(ctx, input);
10181026
try {
10191027
return await insightsRoutingService.getRoutingFunnelData(dateRanges);
10201028
} catch (e) {

0 commit comments

Comments
 (0)