MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / getCurrentAndPrevious

Function getCurrentAndPrevious

packages/trpc/src/routers/overview.ts:88–136  ·  view source on GitHub ↗
(input: T, fetchPrevious: boolean, timezone: string)

Source from the content-addressed store, hash-verified

86);
87
88function getCurrentAndPrevious<
89 T extends {
90 startDate?: string | null;
91 endDate?: string | null;
92 range: IChartRange;
93 projectId: string;
94 },
95>(input: T, fetchPrevious: boolean, timezone: string) {
96 const current = getChartStartEndDate(input, timezone);
97 const previous = getChartPrevStartEndDate(current);
98
99 return async <R>(
100 fn: (input: T & { startDate: string; endDate: string }) => Promise<R>
101 ): Promise<{
102 current: R;
103 previous: R | null;
104 }> => {
105 const endDate = await getOrganizationSubscriptionChartEndDate(
106 input.projectId,
107 current.endDate
108 );
109 if (endDate) {
110 current.endDate = endDate;
111 // Only expired trial scenarios
112 if (new Date(current.startDate) > new Date(current.endDate)) {
113 current.startDate = current.endDate;
114 }
115 }
116 const res = await Promise.all([
117 fn({
118 ...input,
119 startDate: current.startDate,
120 endDate: current.endDate,
121 }),
122 fetchPrevious
123 ? fn({
124 ...input,
125 startDate: previous.startDate,
126 endDate: previous.endDate,
127 })
128 : Promise.resolve(null),
129 ]);
130
131 return {
132 current: res[0],
133 previous: res[1],
134 };
135 };
136}
137
138export const overviewRouter = createTRPCRouter({
139 liveVisitors: overviewProcedure

Callers 1

overview.tsFile · 0.85

Calls 4

getChartStartEndDateFunction · 0.90
getChartPrevStartEndDateFunction · 0.90
fnFunction · 0.85

Tested by

no test coverage detected