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) {
|
|
|
|
const { id: websiteId, startAt, endAt, unit } = req.query;
|
|
|
|
|
|
|
|
// All-time
|
|
|
|
if (+startAt === 0 && +endAt === 1) {
|
2023-07-26 22:42:55 +02:00
|
|
|
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 {
|
2023-07-26 22:42:55 +02:00
|
|
|
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
|
|
|
}
|