2023-05-09 08:46:58 +02:00
|
|
|
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 { getPageviewFunnel } from 'queries';
|
|
|
|
|
|
|
|
export interface FunnelRequestBody {
|
|
|
|
websiteId: string;
|
|
|
|
urls: string[];
|
|
|
|
window: number;
|
2023-05-31 01:49:22 +02:00
|
|
|
dateRange: {
|
|
|
|
startDate: string;
|
|
|
|
endDate: string;
|
|
|
|
};
|
2023-05-09 08:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface FunnelResponse {
|
|
|
|
urls: string[];
|
|
|
|
window: number;
|
|
|
|
startAt: number;
|
|
|
|
endAt: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async (
|
|
|
|
req: NextApiRequestQueryBody<any, FunnelRequestBody>,
|
|
|
|
res: NextApiResponse<FunnelResponse>,
|
|
|
|
) => {
|
|
|
|
await useCors(req, res);
|
|
|
|
await useAuth(req, res);
|
|
|
|
|
|
|
|
if (req.method === 'POST') {
|
2023-05-31 01:49:22 +02:00
|
|
|
const {
|
|
|
|
websiteId,
|
|
|
|
urls,
|
|
|
|
window,
|
|
|
|
dateRange: { startDate, endDate },
|
|
|
|
} = req.body;
|
2023-05-25 06:40:02 +02:00
|
|
|
|
2023-05-09 08:46:58 +02:00
|
|
|
if (!(await canViewWebsite(req.auth, websiteId))) {
|
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2023-05-12 01:42:58 +02:00
|
|
|
const data = await getPageviewFunnel(websiteId, {
|
2023-05-31 01:49:22 +02:00
|
|
|
startDate: new Date(startDate),
|
|
|
|
endDate: new Date(endDate),
|
2023-05-09 08:46:58 +02:00
|
|
|
urls,
|
2023-05-15 06:38:03 +02:00
|
|
|
windowMinutes: +window,
|
2023-05-09 08:46:58 +02:00
|
|
|
});
|
|
|
|
|
2023-05-12 01:42:58 +02:00
|
|
|
return ok(res, data);
|
2023-05-09 08:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|