Refactored functionality for all time date range.

This commit is contained in:
Mike Cao 2023-07-26 13:42:55 -07:00
parent c3973f5fb5
commit d06808985b
11 changed files with 91 additions and 33 deletions

View file

@ -0,0 +1,32 @@
import { WebsiteActive, NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getWebsiteDateRange } from 'queries';
export interface WebsiteDateRangeRequestQuery {
id: string;
}
export default async (
req: NextApiRequestQueryBody<WebsiteDateRangeRequestQuery>,
res: NextApiResponse<WebsiteActive>,
) => {
await useCors(req, res);
await useAuth(req, res);
const { id: websiteId } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const result = await getWebsiteDateRange(websiteId);
return ok(res, result);
}
return methodNotAllowed(res);
};

View file

@ -73,6 +73,7 @@ export default async (
city,
},
});
const prevPeriod = await getWebsiteStats(websiteId, {
startDate: prevStartDate,
endDate: prevEndDate,