mirror of
https://github.com/umami-software/umami.git
synced 2026-02-07 14:17:13 +01:00
Add userReport api
This commit is contained in:
parent
de509e7ccc
commit
4df7d6a2a1
7 changed files with 189 additions and 2 deletions
60
pages/api/reports/[id].ts
Normal file
60
pages/api/reports/[id].ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { canUpdateUserReport, canViewUserReport } from 'lib/auth';
|
||||
import { useAuth, useCors } from 'lib/middleware';
|
||||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { getUserReportById, updateUserReport } from 'queries';
|
||||
|
||||
export interface UserReportRequestQuery {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface UserReportRequestBody {
|
||||
websiteId: string;
|
||||
reportName: string;
|
||||
templateName: string;
|
||||
parameters: string;
|
||||
}
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<UserReportRequestQuery, UserReportRequestBody>,
|
||||
res: NextApiResponse,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const { id: userReportId } = req.query;
|
||||
|
||||
const data = await getUserReportById(userReportId);
|
||||
|
||||
if (!(await canViewUserReport(req.auth, data))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
return ok(res, data);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { id: userReportId } = req.query;
|
||||
|
||||
const data = await getUserReportById(userReportId);
|
||||
|
||||
if (!(await canUpdateUserReport(req.auth, data))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const updated = await updateUserReport(
|
||||
{
|
||||
...req.body,
|
||||
},
|
||||
{
|
||||
id: userReportId,
|
||||
},
|
||||
);
|
||||
|
||||
return ok(res, updated);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
43
pages/api/reports/index.ts
Normal file
43
pages/api/reports/index.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { uuid } from 'lib/crypto';
|
||||
import { useAuth, useCors } from 'lib/middleware';
|
||||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok } from 'next-basics';
|
||||
import { createUserReport, getUserReports } from 'queries';
|
||||
|
||||
export interface UserReportRequestBody {
|
||||
websiteId: string;
|
||||
reportName: string;
|
||||
templateName: string;
|
||||
parameters: string;
|
||||
}
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<any, UserReportRequestBody>,
|
||||
res: NextApiResponse,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
|
||||
const {
|
||||
user: { id: userId },
|
||||
} = req.auth;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const data = await getUserReports(userId);
|
||||
|
||||
return ok(res, data);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const data = await createUserReport({
|
||||
id: uuid(),
|
||||
userId,
|
||||
...req.body,
|
||||
});
|
||||
|
||||
return ok(res, data);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue