Refactored queries.

This commit is contained in:
Mike Cao 2024-01-30 00:10:25 -08:00
parent 18e36aa7b3
commit b16f5cc067
67 changed files with 523 additions and 576 deletions

View file

@ -3,7 +3,7 @@ import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, ReportType, YupRequest } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { deleteReport, getReportById, updateReport } from 'queries';
import { deleteReport, getReport, updateReport } from 'queries';
import * as yup from 'yup';
export interface ReportRequestQuery {
@ -54,7 +54,7 @@ export default async (
} = req.auth;
if (req.method === 'GET') {
const report = await getReportById(reportId);
const report = await getReport(reportId);
if (!(await canViewReport(req.auth, report))) {
return unauthorized(res);
@ -68,7 +68,7 @@ export default async (
if (req.method === 'POST') {
const { websiteId, type, name, description, parameters } = req.body;
const report = await getReportById(reportId);
const report = await getReport(reportId);
if (!(await canUpdateReport(req.auth, report))) {
return unauthorized(res);
@ -87,7 +87,7 @@ export default async (
}
if (req.method === 'DELETE') {
const report = await getReportById(reportId);
const report = await getReport(reportId);
if (!(await canDeleteReport(req.auth, report))) {
return unauthorized(res);

View file

@ -4,7 +4,7 @@ import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok } from 'next-basics';
import { createReport, getReportsByUserId } from 'queries';
import { createReport, getUserReports } from 'queries';
import * as yup from 'yup';
export interface ReportsRequestQuery extends SearchFilter {}
@ -52,11 +52,10 @@ export default async (
if (req.method === 'GET') {
const { page, query, pageSize } = req.query;
const data = await getReportsByUserId(userId, {
const data = await getUserReports(userId, {
page,
pageSize: +pageSize || undefined,
query,
includeTeams: true,
});
return ok(res, data);