mirror of
https://github.com/umami-software/umami.git
synced 2026-02-10 07:37:11 +01:00
Refactored queries.
This commit is contained in:
parent
f36a689817
commit
8904b7b4ed
27 changed files with 137 additions and 144 deletions
|
|
@ -9,7 +9,7 @@ import {
|
|||
methodNotAllowed,
|
||||
} from 'next-basics';
|
||||
import redis from '@umami/redis-client';
|
||||
import { getUser } from 'queries';
|
||||
import { getUserByUsername } from 'queries';
|
||||
import { secret } from 'lib/crypto';
|
||||
import { NextApiRequestQueryBody, User } from 'lib/types';
|
||||
import { setAuthKey } from 'lib/auth';
|
||||
|
|
@ -37,7 +37,7 @@ export default async (
|
|||
return badRequest(res);
|
||||
}
|
||||
|
||||
const user = await getUser({ username }, { includePassword: true });
|
||||
const user = await getUserByUsername(username, { includePassword: true });
|
||||
|
||||
if (user && checkPassword(password, user.password)) {
|
||||
if (redis.enabled) {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import {
|
|||
forbidden,
|
||||
ok,
|
||||
} from 'next-basics';
|
||||
import { getUser, updateUser } from 'queries';
|
||||
import { getUserById, updateUser } from 'queries';
|
||||
|
||||
export interface UserPasswordRequestQuery {
|
||||
id: string;
|
||||
|
|
@ -34,7 +34,7 @@ export default async (
|
|||
const { id } = req.auth.user;
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const user = await getUser({ id }, { includePassword: true });
|
||||
const user = await getUserById(id, { includePassword: true });
|
||||
|
||||
if (!checkPassword(currentPassword, user.password)) {
|
||||
return badRequest(res, 'Current password is incorrect');
|
||||
|
|
|
|||
|
|
@ -52,19 +52,14 @@ export default async (
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const result = await updateReport(
|
||||
{
|
||||
websiteId,
|
||||
userId,
|
||||
type,
|
||||
name,
|
||||
description,
|
||||
parameters: JSON.stringify(parameters),
|
||||
} as any,
|
||||
{
|
||||
id: reportId,
|
||||
},
|
||||
);
|
||||
const result = await updateReport(reportId, {
|
||||
websiteId,
|
||||
userId,
|
||||
type,
|
||||
name,
|
||||
description,
|
||||
parameters: JSON.stringify(parameters),
|
||||
} as any);
|
||||
|
||||
return ok(res, result);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { useAuth, useCors } from 'lib/middleware';
|
|||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { createReport, getReports } from 'queries';
|
||||
import { createReport, getWebsiteReports } from 'queries';
|
||||
import { canViewWebsite } from 'lib/auth';
|
||||
import { uuid } from 'lib/crypto';
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ export default async (
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const data = await getReports({ websiteId });
|
||||
const data = await getWebsiteReports(websiteId);
|
||||
|
||||
return ok(res, data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { NextApiRequestQueryBody } from 'lib/types';
|
|||
import { secret } from 'lib/crypto';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { createToken, methodNotAllowed, notFound, ok } from 'next-basics';
|
||||
import { getWebsite } from 'queries';
|
||||
import { getWebsiteByShareId } from 'queries';
|
||||
|
||||
export interface ShareRequestQuery {
|
||||
id: string;
|
||||
|
|
@ -20,7 +20,7 @@ export default async (
|
|||
const { id: shareId } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const website = await getWebsite({ shareId });
|
||||
const website = await getWebsiteByShareId(shareId);
|
||||
|
||||
if (website) {
|
||||
const data = { websiteId: website.id };
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { canDeleteTeam, canUpdateTeam, canViewTeam } from 'lib/auth';
|
|||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { deleteTeam, getTeam, updateTeam } from 'queries';
|
||||
import { deleteTeam, getTeamById, updateTeam } from 'queries';
|
||||
|
||||
export interface TeamRequestQuery {
|
||||
id: string;
|
||||
|
|
@ -28,7 +28,7 @@ export default async (
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const user = await getTeam({ id: teamId });
|
||||
const user = await getTeamById(teamId, { includeTeamUser: true });
|
||||
|
||||
return ok(res, user);
|
||||
}
|
||||
|
|
@ -41,7 +41,7 @@ export default async (
|
|||
const { name, accessCode } = req.body;
|
||||
const data = { name, accessCode };
|
||||
|
||||
const updated = await updateTeam(data, { id: teamId });
|
||||
const updated = await updateTeam(teamId, data);
|
||||
|
||||
return ok(res, updated);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { useAuth } from 'lib/middleware';
|
|||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { createTeamUser, getTeamUsers, getUser } from 'queries';
|
||||
import { createTeamUser, getTeamUsers, getUserByUsername } from 'queries';
|
||||
|
||||
export interface TeamUserRequestQuery {
|
||||
id: string;
|
||||
|
|
@ -40,7 +40,7 @@ export default async (
|
|||
const { email, roleId: roleId } = req.body;
|
||||
|
||||
// Check for User
|
||||
const user = await getUser({ username: email });
|
||||
const user = await getUserByUsername(email);
|
||||
|
||||
if (!user) {
|
||||
return badRequest(res, 'The User does not exists.');
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { NextApiRequestQueryBody } from 'lib/types';
|
|||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, notFound } from 'next-basics';
|
||||
import { createTeamUser, getTeam, getTeamUser } from 'queries';
|
||||
import { createTeamUser, getTeamByAccessCode, getTeamUser } from 'queries';
|
||||
import { ROLES } from 'lib/constants';
|
||||
|
||||
export interface TeamsJoinRequestBody {
|
||||
|
|
@ -19,7 +19,7 @@ export default async (
|
|||
if (req.method === 'POST') {
|
||||
const { accessCode } = req.body;
|
||||
|
||||
const team = await getTeam({ accessCode });
|
||||
const team = await getTeamByAccessCode(accessCode);
|
||||
|
||||
if (!team) {
|
||||
return notFound(res, 'message.team-not-found');
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { canDeleteUser, canUpdateUser, canViewUser } from 'lib/auth';
|
|||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { deleteUser, getUser, updateUser } from 'queries';
|
||||
import { deleteUser, getUserById, getUserByUsername, updateUser } from 'queries';
|
||||
|
||||
export interface UserRequestQuery {
|
||||
id: string;
|
||||
|
|
@ -31,7 +31,7 @@ export default async (
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const user = await getUser({ id });
|
||||
const user = await getUserById(id);
|
||||
|
||||
return ok(res, user);
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ export default async (
|
|||
|
||||
const { username, password, role } = req.body;
|
||||
|
||||
const user = await getUser({ id });
|
||||
const user = await getUserById(id);
|
||||
|
||||
const data: any = {};
|
||||
|
||||
|
|
@ -62,9 +62,9 @@ export default async (
|
|||
|
||||
// Check when username changes
|
||||
if (data.username && user.username !== data.username) {
|
||||
const userByUsername = await getUser({ username });
|
||||
const user = await getUserByUsername(username);
|
||||
|
||||
if (userByUsername) {
|
||||
if (user) {
|
||||
return badRequest(res, 'User already exists');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { useAuth } from 'lib/middleware';
|
|||
import { NextApiRequestQueryBody, Role, User } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { createUser, getUser, getUsers } from 'queries';
|
||||
import { createUser, getUserByUsername, getUsers } from 'queries';
|
||||
|
||||
export interface UsersRequestBody {
|
||||
username: string;
|
||||
|
|
@ -37,7 +37,7 @@ export default async (
|
|||
|
||||
const { username, password, role, id } = req.body;
|
||||
|
||||
const existingUser = await getUser({ username }, { showDeleted: true });
|
||||
const existingUser = await getUserByUsername(username, { showDeleted: true });
|
||||
|
||||
if (existingUser) {
|
||||
return badRequest(res, 'User already exists');
|
||||
|
|
|
|||
|
|
@ -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 } from 'lib/middleware';
|
||||
import { deleteWebsite, getWebsite, updateWebsite } from 'queries';
|
||||
import { deleteWebsite, getWebsiteById, updateWebsite } from 'queries';
|
||||
import { SHARE_ID_REGEX } from 'lib/constants';
|
||||
|
||||
export interface WebsiteRequestQuery {
|
||||
|
|
@ -30,7 +30,7 @@ export default async (
|
|||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const website = await getWebsite({ id: websiteId });
|
||||
const website = await getWebsiteById(websiteId);
|
||||
|
||||
return ok(res, website);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue