Use getRequestDateRange in all routes.

This commit is contained in:
Mike Cao 2025-06-25 22:53:07 -07:00
parent b0023feee9
commit 5171bdaf47
21 changed files with 72 additions and 173 deletions

View file

@ -1,9 +1,8 @@
import { z } from 'zod/v4';
import { FILTER_COLUMNS } from '@/lib/constants';
import { badRequest, unauthorized } from '@/lib/response';
import { getAllowedUnits, getMinimumUnit } from '@/lib/date';
import { getAllowedUnits, getCompareDate, getMinimumUnit } from '@/lib/date';
import { checkAuth } from '@/lib/auth';
import { getWebsiteDateRange } from '@/queries';
export async function parseRequest(
request: Request,
@ -48,31 +47,26 @@ export async function getJsonBody(request: Request) {
}
}
export async function getRequestDateRange(query: Record<string, any>) {
const { websiteId, startAt, endAt, unit } = query;
// 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);
return {
startDate,
endDate,
unit: getMinimumUnit(startDate, endDate),
};
}
export async function getRequestDateRange(query: Record<string, string>) {
const { startAt, endAt, unit, compare } = query;
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
const minUnit = getMinimumUnit(startDate, endDate);
const { startDate: compareStartDate, endDate: compareEndDate } = getCompareDate(
compare,
startDate,
endDate,
);
return {
startDate,
endDate,
unit: (getAllowedUnits(startDate, endDate).includes(unit as string) ? unit : minUnit) as string,
compareStartDate,
compareEndDate,
unit: getAllowedUnits(startDate, endDate).includes(unit)
? unit
: getMinimumUnit(startDate, endDate),
};
}