UTM report.

This commit is contained in:
Mike Cao 2024-03-14 02:45:00 -07:00
parent e602aedd21
commit bca9c87021
25 changed files with 379 additions and 39 deletions

View file

@ -27,7 +27,7 @@ const schema: YupRequest = {
websiteId: yup.string().uuid().required(),
type: yup
.string()
.matches(/funnel|insights|retention/i)
.matches(/funnel|insights|retention|utm/i)
.required(),
name: yup.string().max(200).required(),
description: yup.string().max(500),

View file

@ -27,7 +27,7 @@ const schema = {
name: yup.string().max(200).required(),
type: yup
.string()
.matches(/funnel|insights|retention/i)
.matches(/funnel|insights|retention|utm/i)
.required(),
description: yup.string().max(500),
parameters: yup

View file

@ -0,0 +1,54 @@
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { TimezoneTest } from 'lib/yup';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getUTM } from 'queries';
import * as yup from 'yup';
export interface UTMRequestBody {
websiteId: string;
dateRange: { startDate: string; endDate: string; timezone: string };
}
const schema = {
POST: yup.object().shape({
websiteId: yup.string().uuid().required(),
dateRange: yup
.object()
.shape({
startDate: yup.date().required(),
endDate: yup.date().required(),
timezone: TimezoneTest,
})
.required(),
}),
};
export default async (req: NextApiRequestQueryBody<any, UTMRequestBody>, res: NextApiResponse) => {
await useCors(req, res);
await useAuth(req, res);
await useValidate(schema, req, res);
if (req.method === 'POST') {
const {
websiteId,
dateRange: { startDate, endDate, timezone },
} = req.body;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const data = await getUTM(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
timezone,
});
return ok(res, data);
}
return methodNotAllowed(res);
};