umami/src/lib/query.ts

32 lines
884 B
TypeScript
Raw Normal View History

2023-07-26 08:59:08 +02:00
import { NextApiRequest } from 'next';
import { getAllowedUnits, getMinimumUnit } from './date';
import { getWebsiteDateRange } from '../queries';
export async function parseDateRangeQuery(req: NextApiRequest) {
2024-02-01 07:08:48 +01:00
const { websiteId, startAt, endAt, unit } = req.query;
2023-07-26 08:59:08 +02:00
// All-time
if (+startAt === 0 && +endAt === 1) {
const result = await getWebsiteDateRange(websiteId as string);
const { min, max } = result[0];
const startDate = new Date(min);
const endDate = new Date(max);
2023-07-26 08:59:08 +02:00
return {
startDate,
endDate,
unit: getMinimumUnit(startDate, endDate),
2023-07-26 08:59:08 +02:00
};
2023-04-02 02:38:35 +02:00
}
2023-07-26 08:59:08 +02:00
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
const minUnit = getMinimumUnit(startDate, endDate);
2023-04-02 02:38:35 +02:00
2023-07-26 08:59:08 +02:00
return {
startDate,
endDate,
2023-07-26 18:55:54 +02:00
unit: (getAllowedUnits(startDate, endDate).includes(unit as string) ? unit : minUnit) as string,
2023-07-26 08:59:08 +02:00
};
2023-04-02 02:38:35 +02:00
}