({
websiteId,
startAt,
endAt,
unit,
}: {
websiteId: string;
startAt: number;
endAt: number;
unit?: string;
})
| 178 | } |
| 179 | |
| 180 | export async function parseDateRange({ |
| 181 | websiteId, |
| 182 | startAt, |
| 183 | endAt, |
| 184 | unit, |
| 185 | }: { |
| 186 | websiteId: string; |
| 187 | startAt: number; |
| 188 | endAt: number; |
| 189 | unit?: string; |
| 190 | }) { |
| 191 | // All-time |
| 192 | if (+startAt === 0 && +endAt === 1) { |
| 193 | const { min, max } = await getWorkspaceWebsiteDateRange( |
| 194 | websiteId as string |
| 195 | ); |
| 196 | const startDate = new Date(min!); |
| 197 | const endDate = new Date(max!); |
| 198 | |
| 199 | return { |
| 200 | startDate, |
| 201 | endDate, |
| 202 | unit: getMinimumUnit(startDate, endDate), |
| 203 | }; |
| 204 | } |
| 205 | |
| 206 | const startDate = new Date(+startAt); |
| 207 | const endDate = new Date(+endAt); |
| 208 | const minUnit = getMinimumUnit(startDate, endDate); |
| 209 | |
| 210 | return { |
| 211 | startDate, |
| 212 | endDate, |
| 213 | unit: (getAllowedUnits(startDate, endDate).includes(unit as string) |
| 214 | ? unit |
| 215 | : minUnit) as string, |
| 216 | }; |
| 217 | } |
| 218 | |
| 219 | export function getAllowedUnits(startDate: Date, endDate: Date) { |
| 220 | const units = ['minute', 'hour', 'day', 'month', 'year']; |
no test coverage detected