add date param to values

This commit is contained in:
Francis Cao 2023-10-13 09:31:53 -07:00
parent c18daf4845
commit 88aa341821
3 changed files with 29 additions and 7 deletions

View file

@ -5,15 +5,20 @@ import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { EVENT_COLUMNS, FILTER_COLUMNS, SESSION_COLUMNS } from 'lib/constants';
import { getValues } from 'queries';
import { parseDateRangeQuery } from 'lib/query';
export interface ValuesRequestQuery {
id: string;
startAt: number;
endAt: number;
}
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
startAt: yup.number().required(),
endAt: yup.number().required(),
}),
};
@ -23,6 +28,7 @@ export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: Nex
await useValidate(schema, req, res);
const { id: websiteId, type } = req.query;
const { startDate, endDate } = await parseDateRangeQuery(req);
if (req.method === 'GET') {
if (!SESSION_COLUMNS.includes(type as string) && !EVENT_COLUMNS.includes(type as string)) {
@ -33,7 +39,7 @@ export default async (req: NextApiRequestQueryBody<ValuesRequestQuery>, res: Nex
return unauthorized(res);
}
const values = await getValues(websiteId, FILTER_COLUMNS[type as string]);
const values = await getValues(websiteId, FILTER_COLUMNS[type as string], startDate, endDate);
return ok(
res,