attempt fetching custom event data for data table

This commit is contained in:
emma 2023-05-07 14:56:39 -04:00
parent f12f4f5697
commit 334297f194
5 changed files with 67 additions and 5 deletions

View file

@ -1,10 +1,12 @@
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { methodNotAllowed, ok, unauthorized, badRequest } from 'next-basics';
import { WebsiteMetric, NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { SESSION_COLUMNS, EVENT_COLUMNS, FILTER_COLUMNS } from 'lib/constants';
import { getPageviewMetrics, getSessionMetrics } from 'queries';
import { getEventMetrics, getPageviewMetrics, getSessionMetrics } from 'queries';
import moment from 'moment-timezone';
const unitTypes = ['year', 'month', 'hour', 'day'];
export interface WebsiteMetricsRequestQuery {
id: string;
@ -47,6 +49,9 @@ export default async (
country,
region,
city,
includeEventData,
timezone,
unit,
} = req.query;
if (req.method === 'GET') {
@ -54,6 +59,15 @@ export default async (
return unauthorized(res);
}
if (
typeof timezone !== 'string' ||
typeof unit !== 'string' ||
!moment.tz.zone(String(timezone)) ||
!unitTypes.includes(String(unit))
) {
return badRequest(res);
}
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
@ -114,6 +128,17 @@ export default async (
filters[type] = undefined;
if (type === 'event' && includeEventData) {
const data = await getEventMetrics(websiteId, {
startDate,
endDate,
timezone,
unit,
filters: { url: filters.url, eventName: undefined },
});
return ok(res, data);
}
const data = await getPageviewMetrics(websiteId, {
startDate,
endDate,