umami/lib/query.ts

31 lines
806 B
TypeScript
Raw Normal View History

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