(input: T, fetchPrevious: boolean, timezone: string)
| 86 | ); |
| 87 | |
| 88 | function 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 | |
| 138 | export const overviewRouter = createTRPCRouter({ |
| 139 | liveVisitors: overviewProcedure |
no test coverage detected