2023-05-18 22:13:18 +02:00
|
|
|
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';
|
2023-05-29 06:37:34 +02:00
|
|
|
import { createReport, getReports } from 'queries';
|
2023-05-18 22:13:18 +02:00
|
|
|
|
2023-05-29 06:37:34 +02:00
|
|
|
export interface ReportRequestBody {
|
2023-05-18 22:13:18 +02:00
|
|
|
websiteId: string;
|
2023-05-29 06:37:34 +02:00
|
|
|
name: string;
|
|
|
|
type: string;
|
|
|
|
description: string;
|
|
|
|
parameters: {
|
|
|
|
window: string;
|
|
|
|
urls: string[];
|
|
|
|
};
|
2023-05-18 22:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async (
|
2023-05-29 06:37:34 +02:00
|
|
|
req: NextApiRequestQueryBody<any, ReportRequestBody>,
|
2023-05-18 22:13:18 +02:00
|
|
|
res: NextApiResponse,
|
|
|
|
) => {
|
|
|
|
await useCors(req, res);
|
|
|
|
await useAuth(req, res);
|
|
|
|
|
|
|
|
const {
|
|
|
|
user: { id: userId },
|
|
|
|
} = req.auth;
|
|
|
|
|
|
|
|
if (req.method === 'GET') {
|
2023-05-29 06:37:34 +02:00
|
|
|
const data = await getReports(userId);
|
2023-05-18 22:13:18 +02:00
|
|
|
|
|
|
|
return ok(res, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'POST') {
|
2023-05-29 06:37:34 +02:00
|
|
|
const { websiteId, type, name, description, parameters } = req.body;
|
|
|
|
|
|
|
|
const result = await createReport({
|
2023-05-18 22:13:18 +02:00
|
|
|
id: uuid(),
|
|
|
|
userId,
|
2023-05-29 06:37:34 +02:00
|
|
|
websiteId,
|
|
|
|
type,
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
parameters: JSON.stringify(parameters),
|
|
|
|
} as any);
|
|
|
|
|
|
|
|
return ok(res, result);
|
2023-05-18 22:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|