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, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, Role, User } from 'lib/types';
import { NextApiResponse } from 'next';
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { deleteUser, getUserById, getUserByUsername, updateUser } from 'queries';
import { deleteUser, getUser, getUserByUsername, updateUser } from 'queries';
import * as yup from 'yup';
export interface UserRequestQuery {
@ -45,7 +45,7 @@ export default async (
return unauthorized(res);
}
const user = await getUserById(id);
const user = await getUser(id);
return ok(res, user);
}
@ -57,7 +57,7 @@ export default async (
const { username, password, role } = req.body;
const user = await getUserById(id);
const user = await getUser(id);
const data: any = {};

View file

@ -4,7 +4,7 @@ import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getTeamsByUserId } from 'queries';
import { getUserTeams } from 'queries';
export interface UserTeamsRequestQuery extends SearchFilter {
id: string;
@ -41,7 +41,7 @@ export default async (
const { page, query, pageSize } = req.query;
const teams = await getTeamsByUserId(userId, {
const teams = await getUserTeams(userId, {
query,
page,
pageSize: +pageSize || undefined,

View file

@ -2,7 +2,7 @@ import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getEventDataUsage, getEventUsage, getUserWebsites } from 'queries';
import { getAllWebsites, getEventDataUsage, getEventUsage } from 'queries';
import * as yup from 'yup';
export interface UserUsageRequestQuery {
@ -26,7 +26,7 @@ const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
startAt: yup.number().integer().required(),
endAt: yup.number().integer().moreThan(yup.ref('startAt')).required(),
endAt: yup.number().integer().moreThan(yup.ref<number>('startAt')).required(),
}),
};
@ -50,7 +50,7 @@ export default async (
const startDate = new Date(+startAt);
const endDate = new Date(+endAt);
const websites = await getUserWebsites(userId);
const websites = await getAllWebsites(userId);
const websiteIds = websites.map(a => a.id);

View file

@ -3,7 +3,7 @@ import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
import { pageInfo } from 'lib/schema';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getWebsitesByUserId } from 'queries';
import { getUserWebsites } from 'queries';
import * as yup from 'yup';
export interface UserWebsitesRequestQuery extends SearchFilter {
@ -37,7 +37,7 @@ export default async (
return unauthorized(res);
}
const websites = await getWebsitesByUserId(userId, {
const websites = await getUserWebsites(userId, {
page,
pageSize: +pageSize || undefined,
query,