Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Brian Cao 2023-07-26 10:49:21 -07:00
commit 81813d1d11
65 changed files with 971 additions and 838 deletions

View file

@ -0,0 +1,39 @@
import { canViewWebsite } from 'lib/auth';
import { useCors, useAuth } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
import { getEventDataEvents } from 'queries';
export interface EventDataFieldsRequestBody {
websiteId: string;
dateRange: {
startDate: string;
endDate: string;
};
}
export default async (
req: NextApiRequestQueryBody<any, EventDataFieldsRequestBody>,
res: NextApiResponse<any>,
) => {
await useCors(req, res);
await useAuth(req, res);
if (req.method === 'GET') {
const { websiteId, startAt, endAt, field, event } = req.query;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const data = await getEventDataEvents(websiteId, new Date(+startAt), new Date(+endAt), {
field,
event,
});
return ok(res, data);
}
return methodNotAllowed(res);
};

View file

@ -21,20 +21,13 @@ export default async (
await useAuth(req, res);
if (req.method === 'GET') {
const { websiteId, startAt, endAt, field, event, withEventNames } = req.query;
const { websiteId, startAt, endAt, field } = req.query;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const data = await getEventDataFields(
websiteId,
new Date(+startAt),
new Date(+endAt),
field,
event,
withEventNames,
);
const data = await getEventDataFields(websiteId, new Date(+startAt), new Date(+endAt), field);
return ok(res, data);
}

View file

@ -3,7 +3,7 @@ import { useCors, useAuth } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
import { getPageviewFunnel } from 'queries';
import { getFunnel } from 'queries';
export interface FunnelRequestBody {
websiteId: string;
@ -41,7 +41,7 @@ export default async (
return unauthorized(res);
}
const data = await getPageviewFunnel(websiteId, {
const data = await getFunnel(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
urls,

View file

@ -1,8 +1,7 @@
import { uuid } from 'lib/crypto';
import { useAuth, useCors } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { methodNotAllowed, ok, unauthorized, uuid } from 'next-basics';
import { createReport, getReports } from 'queries';
import { canViewWebsite } from 'lib/auth';

View file

@ -0,0 +1,51 @@
import { canViewWebsite } from 'lib/auth';
import { useCors, useAuth } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
import { getInsights } from 'queries';
export interface InsightsRequestBody {
websiteId: string;
dateRange: {
startDate: string;
endDate: string;
};
fields: string[];
filters: string[];
groups: string[];
}
export default async (
req: NextApiRequestQueryBody<any, InsightsRequestBody>,
res: NextApiResponse,
) => {
await useCors(req, res);
await useAuth(req, res);
if (req.method === 'POST') {
const {
websiteId,
dateRange: { startDate, endDate },
fields,
filters,
groups,
} = req.body;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const data = await getInsights(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
fields,
filters,
groups,
});
return ok(res, data);
}
return methodNotAllowed(res);
};

View file

@ -1,7 +1,7 @@
import isbot from 'isbot';
import ipaddr from 'ipaddr.js';
import { createToken, ok, send, badRequest, forbidden } from 'next-basics';
import { saveEvent } from 'queries';
import { saveEvent, saveSessionData } from 'queries';
import { useCors, useSession } from 'lib/middleware';
import { getJsonBody, getIpAddress } from 'lib/detect';
import { secret } from 'lib/crypto';
@ -9,7 +9,6 @@ import { NextApiRequest, NextApiResponse } from 'next';
import { Resolver } from 'dns/promises';
import { CollectionType } from 'lib/types';
import { COLLECTION_TYPE } from 'lib/constants';
import { saveSessionData } from 'queries/analytics/session/saveSessionData';
export interface CollectRequestBody {
payload: {

View file

@ -1,7 +1,7 @@
import { Team } from '@prisma/client';
import { NextApiRequestQueryBody } from 'lib/types';
import { canCreateTeam } from 'lib/auth';
import { uuid } from 'lib/crypto';
import { uuid } from 'next-basics';
import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';

View file

@ -1,6 +1,6 @@
import { canCreateUser, canViewUsers } from 'lib/auth';
import { ROLES } from 'lib/constants';
import { uuid } from 'lib/crypto';
import { uuid } from 'next-basics';
import { useAuth } from 'lib/middleware';
import { NextApiRequestQueryBody, Role, User } from 'lib/types';
import { NextApiResponse } from 'next';

View file

@ -5,6 +5,7 @@ import moment from 'moment-timezone';
import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getEventMetrics } from 'queries';
import { parseDateRangeQuery } from 'lib/query';
const unitTypes = ['year', 'month', 'hour', 'day'];
@ -25,7 +26,8 @@ export default async (
await useCors(req, res);
await useAuth(req, res);
const { id: websiteId, startAt, endAt, unit, timezone, url, eventName } = req.query;
const { id: websiteId, timezone, url, eventName } = req.query;
const { startDate, endDate, unit } = await parseDateRangeQuery(req);
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
@ -35,8 +37,6 @@ export default async (
if (!moment.tz.zone(timezone) || !unitTypes.includes(unit)) {
return badRequest(res);
}
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
const events = await getEventMetrics(websiteId, {
startDate,

View file

@ -5,6 +5,7 @@ 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 { parseDateRangeQuery } from 'lib/query';
export interface WebsiteMetricsRequestQuery {
id: string;
@ -34,8 +35,6 @@ export default async (
const {
id: websiteId,
type,
startAt,
endAt,
url,
referrer,
title,
@ -54,8 +53,7 @@ export default async (
return unauthorized(res);
}
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
const { startDate, endDate } = await parseDateRangeQuery(req);
if (SESSION_COLUMNS.includes(type)) {
const column = FILTER_COLUMNS[type] || type;

View file

@ -5,8 +5,7 @@ import { NextApiRequestQueryBody, WebsitePageviews } from 'lib/types';
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { getPageviewStats } from 'queries';
const unitTypes = ['year', 'month', 'hour', 'day'];
import { parseDateRangeQuery } from 'lib/query';
export interface WebsitePageviewRequestQuery {
id: string;
@ -34,9 +33,6 @@ export default async (
const {
id: websiteId,
startAt,
endAt,
unit,
timezone,
url,
referrer,
@ -54,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) || !unitTypes.includes(unit)) {
if (!moment.tz.zone(timezone)) {
return badRequest(res);
}

View file

@ -1,8 +1,10 @@
import { addMinutes, differenceInMinutes } from 'date-fns';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware';
import { NextApiRequestQueryBody, WebsiteStats } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { parseDateRangeQuery } from 'lib/query';
import { getWebsiteStats } from 'queries';
export interface WebsiteStatsRequestQuery {
@ -31,8 +33,6 @@ export default async (
const {
id: websiteId,
startAt,
endAt,
url,
referrer,
title,
@ -51,12 +51,10 @@ export default async (
return unauthorized(res);
}
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
const distance = endAt - startAt;
const prevStartDate = new Date(+startAt - distance);
const prevEndDate = new Date(+endAt - distance);
const { startDate, endDate } = await parseDateRangeQuery(req);
const diff = differenceInMinutes(endDate, startDate);
const prevStartDate = addMinutes(startDate, -diff);
const prevEndDate = addMinutes(endDate, -diff);
const metrics = await getWebsiteStats(websiteId, {
startDate,

View file

@ -1,5 +1,5 @@
import { canCreateWebsite } from 'lib/auth';
import { uuid } from 'lib/crypto';
import { uuid } from 'next-basics';
import { useAuth, useCors } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';