Add revenue.

This commit is contained in:
Brian Cao 2024-06-19 21:41:45 -07:00
parent 3f477c5d50
commit f3efe0c9c3
17 changed files with 488 additions and 4 deletions

View file

@ -27,7 +27,7 @@ const schema: YupRequest = {
websiteId: yup.string().uuid().required(),
type: yup
.string()
.matches(/funnel|insights|retention|utm|goals|journey/i)
.matches(/funnel|insights|retention|utm|goals|journey|revenue/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|utm|goals|journey/i)
.matches(/funnel|insights|retention|utm|goals|journey|revenue/i)
.required(),
description: yup.string().max(500),
parameters: yup

View file

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