Merge pull request #2192 from umami-software/feat/um-376-retention-report

Feat/um 376 retention report
This commit is contained in:
Mike Cao 2023-08-10 19:43:06 -10:00 committed by GitHub
commit 24669a3f70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 359 additions and 7 deletions

View file

@ -0,0 +1,44 @@
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 { getRetention } from 'queries';
export interface RetentionRequestBody {
websiteId: string;
dateRange: { window; startDate: string; endDate: string };
}
export interface RetentionResponse {
startAt: number;
endAt: number;
}
export default async (
req: NextApiRequestQueryBody<any, RetentionRequestBody>,
res: NextApiResponse<RetentionResponse>,
) => {
await useCors(req, res);
await useAuth(req, res);
if (req.method === 'POST') {
const {
websiteId,
dateRange: { startDate, endDate },
} = req.body;
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const data = await getRetention(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
});
return ok(res, data);
}
return methodNotAllowed(res);
};