2022-10-22 06:33:23 +02:00
|
|
|
import moment from 'moment-timezone';
|
|
|
|
import { getEventData } from 'queries';
|
|
|
|
import { ok, badRequest, methodNotAllowed, unauthorized } from 'next-basics';
|
|
|
|
import { allowQuery } from 'lib/auth';
|
|
|
|
import { useAuth, useCors } from 'lib/middleware';
|
2022-10-25 19:45:56 +02:00
|
|
|
import { TYPE_WEBSITE } from 'lib/constants';
|
2022-10-22 06:33:23 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
await useCors(req, res);
|
|
|
|
await useAuth(req, res);
|
|
|
|
|
|
|
|
if (req.method === 'POST') {
|
2022-10-25 19:45:56 +02:00
|
|
|
if (!(await allowQuery(req, TYPE_WEBSITE))) {
|
2022-10-22 06:33:23 +02:00
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { id: websiteId } = req.query;
|
|
|
|
|
|
|
|
const { start_at, end_at, timezone, event_name: eventName, columns, filters } = req.body;
|
|
|
|
|
|
|
|
if (!moment.tz.zone(timezone)) {
|
|
|
|
return badRequest(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
const startDate = new Date(+start_at);
|
|
|
|
const endDate = new Date(+end_at);
|
|
|
|
|
|
|
|
const events = await getEventData(websiteId, {
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
timezone,
|
|
|
|
eventName,
|
|
|
|
columns,
|
|
|
|
filters,
|
|
|
|
});
|
|
|
|
|
|
|
|
return ok(res, events);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|