mirror of
https://github.com/umami-software/umami.git
synced 2026-02-08 06:37:18 +01:00
Refactored queries.
This commit is contained in:
parent
18e36aa7b3
commit
b16f5cc067
67 changed files with 523 additions and 576 deletions
|
|
@ -42,10 +42,7 @@ export default async (
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const { page, query, pageSize } = req.query;
|
||||
|
||||
const users = await getUsers(
|
||||
{ page, query, pageSize: +pageSize || undefined },
|
||||
{
|
||||
include: {
|
||||
_count: {
|
||||
|
|
@ -57,6 +54,7 @@ export default async (
|
|||
},
|
||||
},
|
||||
},
|
||||
req.query,
|
||||
);
|
||||
|
||||
return ok(res, users);
|
||||
|
|
|
|||
|
|
@ -39,25 +39,19 @@ export default async (
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const websites = await getWebsites(req.query, {
|
||||
include: {
|
||||
teamWebsite: {
|
||||
include: {
|
||||
team: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
const websites = await getWebsites(
|
||||
{
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
username: true,
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
user: {
|
||||
select: {
|
||||
username: true,
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
req.query,
|
||||
);
|
||||
|
||||
return ok(res, websites);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,16 @@ import { NextApiRequestAuth } from 'lib/types';
|
|||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { ok } from 'next-basics';
|
||||
import { getUserTeams } from 'queries/admin/team';
|
||||
|
||||
export default async (req: NextApiRequestAuth, res: NextApiResponse) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
return ok(res, req.auth.user);
|
||||
const { user } = req.auth;
|
||||
|
||||
const teams = await getUserTeams(user.id);
|
||||
|
||||
user['teams'] = teams.data.map(n => n);
|
||||
|
||||
return ok(res, user);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import {
|
|||
methodNotAllowed,
|
||||
ok,
|
||||
} from 'next-basics';
|
||||
import { getUserById, updateUser } from 'queries';
|
||||
import { getUser, updateUser } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface UserPasswordRequestQuery {
|
||||
|
|
@ -43,7 +43,7 @@ export default async (
|
|||
const { id } = req.auth.user;
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const user = await getUserById(id, { includePassword: true });
|
||||
const user = await getUser(id, { includePassword: true });
|
||||
|
||||
if (!checkPassword(currentPassword, user.password)) {
|
||||
return badRequest(res, 'Current password is incorrect');
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { useValidate } from 'lib/middleware';
|
|||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { createToken, methodNotAllowed, notFound, ok } from 'next-basics';
|
||||
import { getWebsiteByShareId } from 'queries';
|
||||
import { getSharedWebsite } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface ShareRequestQuery {
|
||||
|
|
@ -30,7 +30,7 @@ export default async (
|
|||
const { id: shareId } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const website = await getWebsiteByShareId(shareId);
|
||||
const website = await getSharedWebsite(shareId);
|
||||
|
||||
if (website) {
|
||||
const data = { websiteId: website.id };
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { useAuth, useValidate } from 'lib/middleware';
|
|||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, notFound, ok, unauthorized } from 'next-basics';
|
||||
import { deleteTeam, getTeamById, updateTeam } from 'queries';
|
||||
import { deleteTeam, getTeam, updateTeam } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface TeamRequestQuery {
|
||||
|
|
@ -44,7 +44,7 @@ export default async (
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const team = await getTeamById(teamId, { includeTeamUser: true });
|
||||
const team = await getTeam(teamId, { includeMembers: true });
|
||||
|
||||
if (!team) {
|
||||
return notFound(res);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { pageInfo } from 'lib/schema';
|
|||
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { createTeamUser, getTeamUser, getUsersByTeamId } from 'queries';
|
||||
import { createTeamUser, getTeamUser, getTeamUsers } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface TeamUserRequestQuery extends SearchFilter {
|
||||
|
|
@ -46,7 +46,7 @@ export default async (
|
|||
|
||||
const { query, page, pageSize } = req.query;
|
||||
|
||||
const users = await getUsersByTeamId(teamId, {
|
||||
const users = await getTeamUsers(teamId, {
|
||||
query,
|
||||
page,
|
||||
pageSize: +pageSize || undefined,
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
|
|||
import { pageInfo } from 'lib/schema';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { createWebsite, getWebsitesByTeamId } from 'queries';
|
||||
import { createWebsite, getTeamWebsites } from 'queries';
|
||||
import { uuid } from 'lib/crypto';
|
||||
|
||||
export interface TeamWebsiteRequestQuery extends SearchFilter {
|
||||
|
|
@ -46,7 +46,7 @@ export default async (
|
|||
|
||||
const { page, query, pageSize } = req.query;
|
||||
|
||||
const websites = await getWebsitesByTeamId(teamId, {
|
||||
const websites = await getTeamWebsites(teamId, {
|
||||
page,
|
||||
query,
|
||||
pageSize: +pageSize || undefined,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
|
|||
import { pageInfo } from 'lib/schema';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { createTeam, getTeamsByUserId } from 'queries';
|
||||
import { createTeam, getUserTeams } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
|
||||
export interface TeamsRequestQuery extends SearchFilter {}
|
||||
|
|
@ -37,7 +37,7 @@ export default async (
|
|||
if (req.method === 'GET') {
|
||||
const { page, query, pageSize } = req.query;
|
||||
|
||||
const results = await getTeamsByUserId(userId, {
|
||||
const results = await getUserTeams(userId, {
|
||||
page,
|
||||
query,
|
||||
pageSize: +pageSize || undefined,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { useAuth, useValidate } from 'lib/middleware';
|
|||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, notFound, ok } from 'next-basics';
|
||||
import { createTeamUser, getTeamByAccessCode, getTeamUser } from 'queries';
|
||||
import { createTeamUser, findTeam, getTeamUser } from 'queries';
|
||||
import * as yup from 'yup';
|
||||
export interface TeamsJoinRequestBody {
|
||||
accessCode: string;
|
||||
|
|
@ -26,7 +26,11 @@ export default async (
|
|||
if (req.method === 'POST') {
|
||||
const { accessCode } = req.body;
|
||||
|
||||
const team = await getTeamByAccessCode(accessCode);
|
||||
const team = await findTeam({
|
||||
where: {
|
||||
accessCode,
|
||||
},
|
||||
});
|
||||
|
||||
if (!team) {
|
||||
return notFound(res, 'message.team-not-found');
|
||||
|
|
|
|||
|
|
@ -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 = {};
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { methodNotAllowed, ok, serverError, unauthorized } from 'next-basics';
|
|||
import { Website, NextApiRequestQueryBody } from 'lib/types';
|
||||
import { canViewWebsite, canUpdateWebsite, canDeleteWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { deleteWebsite, getWebsiteById, updateWebsite } from 'queries';
|
||||
import { deleteWebsite, getWebsite, updateWebsite } from 'queries';
|
||||
import { SHARE_ID_REGEX } from 'lib/constants';
|
||||
|
||||
export interface WebsiteRequestQuery {
|
||||
|
|
@ -45,7 +45,7 @@ export default async (
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const website = await getWebsiteById(websiteId);
|
||||
const website = await getWebsite(websiteId);
|
||||
|
||||
return ok(res, website);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { useAuth, useCors, useValidate } from 'lib/middleware';
|
|||
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { getReportsByWebsiteId } from 'queries';
|
||||
import { getWebsiteReports } from 'queries';
|
||||
import { pageInfo } from 'lib/schema';
|
||||
|
||||
export interface ReportsRequestQuery extends SearchFilter {
|
||||
|
|
@ -35,7 +35,7 @@ export default async (
|
|||
|
||||
const { page, query, pageSize } = req.query;
|
||||
|
||||
const data = await getReportsByWebsiteId(websiteId, {
|
||||
const data = await getWebsiteReports(websiteId, {
|
||||
page,
|
||||
pageSize: +pageSize || undefined,
|
||||
query,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue