Refactored realtime.

This commit is contained in:
Mike Cao 2024-06-19 21:47:27 -07:00
parent cda3ba345b
commit 5108b91f80
16 changed files with 205 additions and 227 deletions

View file

@ -10,13 +10,13 @@ import { REALTIME_RANGE } from 'lib/constants';
export interface RealtimeRequestQuery {
websiteId: string;
startAt: number;
timezone: string;
}
const schema = {
GET: yup.object().shape({
websiteId: yup.string().uuid().required(),
startAt: yup.number().integer().required(),
timezone: yup.string().required(),
}),
};
@ -28,19 +28,15 @@ export default async (
await useValidate(schema, req, res);
if (req.method === 'GET') {
const { websiteId, startAt } = req.query;
const { websiteId, timezone } = req.query;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
let startTime = subMinutes(startOfMinute(new Date()), REALTIME_RANGE);
const startDate = subMinutes(startOfMinute(new Date()), REALTIME_RANGE);
if (+startAt > startTime.getTime()) {
startTime = new Date(+startAt);
}
const data = await getRealtimeData(websiteId, startTime);
const data = await getRealtimeData(websiteId, { startDate, timezone });
return ok(res, data);
}