Added parseDateRangeQuery function.

This commit is contained in:
Mike Cao 2023-07-26 09:55:54 -07:00
parent 09af33c77e
commit 1648707fc7
9 changed files with 48 additions and 80 deletions

View file

@ -3,9 +3,9 @@ import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { NextApiRequestQueryBody, WebsitePageviews } from 'lib/types';
import { canViewWebsite } from 'lib/auth';
import { getAllowedUnits } from 'lib/date';
import { useAuth, useCors } from 'lib/middleware';
import { getPageviewStats } from 'queries';
import { parseDateRangeQuery } from 'lib/query';
export interface WebsitePageviewRequestQuery {
id: string;
@ -33,9 +33,6 @@ export default async (
const {
id: websiteId,
startAt,
endAt,
unit,
timezone,
url,
referrer,
@ -53,10 +50,9 @@ export default async (
return unauthorized(res);
}
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
const { startDate, endDate, unit } = await parseDateRangeQuery(req);
if (!moment.tz.zone(timezone) || !getAllowedUnits(unit).includes(unit)) {
if (!moment.tz.zone(timezone)) {
return badRequest(res);
}