style: run prettier on entire codebase
This commit is contained in:
@@ -1,13 +1,28 @@
|
||||
import prisma from '../config/database';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import { config } from '../config/env';
|
||||
import prisma from "../config/database";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { config } from "../config/env";
|
||||
|
||||
const ALLOWED_SORT_FIELDS = ['id', 'username', 'email', 'first_name', 'last_name', 'created_at'];
|
||||
const ALLOWED_SORT_FIELDS = [
|
||||
"id",
|
||||
"username",
|
||||
"email",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"created_at",
|
||||
];
|
||||
|
||||
const USER_SELECT = {
|
||||
id: true, username: true, email: true, first_name: true, last_name: true,
|
||||
role_id: true, is_active: true, last_login: true, totp_enabled: true,
|
||||
created_at: true, updated_at: true,
|
||||
id: true,
|
||||
username: true,
|
||||
email: true,
|
||||
first_name: true,
|
||||
last_name: true,
|
||||
role_id: true,
|
||||
is_active: true,
|
||||
last_login: true,
|
||||
totp_enabled: true,
|
||||
created_at: true,
|
||||
updated_at: true,
|
||||
roles: { select: { id: true, name: true, display_name: true } },
|
||||
} as const;
|
||||
|
||||
@@ -16,7 +31,7 @@ export interface ListUsersParams {
|
||||
limit: number;
|
||||
skip: number;
|
||||
sort: string;
|
||||
order: 'asc' | 'desc';
|
||||
order: "asc" | "desc";
|
||||
search: string;
|
||||
}
|
||||
|
||||
@@ -42,7 +57,7 @@ export interface UpdateUserData {
|
||||
|
||||
export async function listUsers(params: ListUsersParams) {
|
||||
const { page, limit, skip, sort, order, search } = params;
|
||||
const sortField = ALLOWED_SORT_FIELDS.includes(sort) ? sort : 'id';
|
||||
const sortField = ALLOWED_SORT_FIELDS.includes(sort) ? sort : "id";
|
||||
|
||||
const where = search
|
||||
? {
|
||||
@@ -84,22 +99,27 @@ export async function createUser(data: CreateUserData) {
|
||||
|
||||
// Email format
|
||||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
return { error: 'Neplatný formát e-mailu', status: 400 } as const;
|
||||
return { error: "Neplatný formát e-mailu", status: 400 } as const;
|
||||
}
|
||||
|
||||
// Username uniqueness
|
||||
const existingUsername = await prisma.users.findFirst({ where: { username } });
|
||||
const existingUsername = await prisma.users.findFirst({
|
||||
where: { username },
|
||||
});
|
||||
if (existingUsername) {
|
||||
return { error: 'Uživatelské jméno již existuje', status: 409 } as const;
|
||||
return { error: "Uživatelské jméno již existuje", status: 409 } as const;
|
||||
}
|
||||
|
||||
// Email uniqueness
|
||||
const existingEmail = await prisma.users.findFirst({ where: { email } });
|
||||
if (existingEmail) {
|
||||
return { error: 'E-mail již existuje', status: 409 } as const;
|
||||
return { error: "E-mail již existuje", status: 409 } as const;
|
||||
}
|
||||
|
||||
const passwordHash = await bcrypt.hash(data.password, config.security.bcryptCost);
|
||||
const passwordHash = await bcrypt.hash(
|
||||
data.password,
|
||||
config.security.bcryptCost,
|
||||
);
|
||||
|
||||
const user = await prisma.users.create({
|
||||
data: {
|
||||
@@ -119,7 +139,7 @@ export async function createUser(data: CreateUserData) {
|
||||
export async function updateUser(id: number, body: UpdateUserData) {
|
||||
const existing = await prisma.users.findUnique({ where: { id } });
|
||||
if (!existing) {
|
||||
return { error: 'Uživatel nenalezen', status: 404 } as const;
|
||||
return { error: "Uživatel nenalezen", status: 404 } as const;
|
||||
}
|
||||
|
||||
const data: Record<string, unknown> = {};
|
||||
@@ -128,9 +148,14 @@ export async function updateUser(id: number, body: UpdateUserData) {
|
||||
if (body.username !== undefined) {
|
||||
const newUsername = String(body.username).trim();
|
||||
if (newUsername !== existing.username) {
|
||||
const existingUsername = await prisma.users.findFirst({ where: { username: newUsername } });
|
||||
const existingUsername = await prisma.users.findFirst({
|
||||
where: { username: newUsername },
|
||||
});
|
||||
if (existingUsername) {
|
||||
return { error: 'Uživatelské jméno již existuje', status: 409 } as const;
|
||||
return {
|
||||
error: "Uživatelské jméno již existuje",
|
||||
status: 409,
|
||||
} as const;
|
||||
}
|
||||
}
|
||||
data.username = newUsername;
|
||||
@@ -140,27 +165,33 @@ export async function updateUser(id: number, body: UpdateUserData) {
|
||||
if (body.email !== undefined) {
|
||||
const newEmail = String(body.email).trim();
|
||||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(newEmail)) {
|
||||
return { error: 'Neplatný formát e-mailu', status: 400 } as const;
|
||||
return { error: "Neplatný formát e-mailu", status: 400 } as const;
|
||||
}
|
||||
const existingEmail = await prisma.users.findFirst({
|
||||
where: { email: newEmail, id: { not: id } },
|
||||
});
|
||||
if (existingEmail) {
|
||||
return { error: 'E-mail již existuje', status: 409 } as const;
|
||||
return { error: "E-mail již existuje", status: 409 } as const;
|
||||
}
|
||||
data.email = newEmail;
|
||||
}
|
||||
|
||||
if (body.first_name !== undefined) data.first_name = String(body.first_name);
|
||||
if (body.last_name !== undefined) data.last_name = String(body.last_name);
|
||||
if (body.role_id !== undefined) data.role_id = body.role_id ? Number(body.role_id) : null;
|
||||
if (body.is_active !== undefined) data.is_active = body.is_active === true || body.is_active === 1 || body.is_active === '1';
|
||||
if (body.role_id !== undefined)
|
||||
data.role_id = body.role_id ? Number(body.role_id) : null;
|
||||
if (body.is_active !== undefined)
|
||||
data.is_active =
|
||||
body.is_active === true || body.is_active === 1 || body.is_active === "1";
|
||||
if (body.password) {
|
||||
const newPassword = String(body.password);
|
||||
if (newPassword.length < 8) {
|
||||
return { error: 'Heslo musí mít alespoň 8 znaků', status: 400 } as const;
|
||||
return { error: "Heslo musí mít alespoň 8 znaků", status: 400 } as const;
|
||||
}
|
||||
data.password_hash = await bcrypt.hash(newPassword, config.security.bcryptCost);
|
||||
data.password_hash = await bcrypt.hash(
|
||||
newPassword,
|
||||
config.security.bcryptCost,
|
||||
);
|
||||
data.password_changed_at = new Date();
|
||||
}
|
||||
|
||||
@@ -171,12 +202,12 @@ export async function updateUser(id: number, body: UpdateUserData) {
|
||||
|
||||
export async function deleteUser(id: number, currentUserId?: number) {
|
||||
if (id === currentUserId) {
|
||||
return { error: 'Nelze smazat vlastní účet', status: 400 } as const;
|
||||
return { error: "Nelze smazat vlastní účet", status: 400 } as const;
|
||||
}
|
||||
|
||||
const existing = await prisma.users.findUnique({ where: { id } });
|
||||
if (!existing) {
|
||||
return { error: 'Uživatel nenalezen', status: 404 } as const;
|
||||
return { error: "Uživatel nenalezen", status: 404 } as const;
|
||||
}
|
||||
|
||||
await prisma.refresh_tokens.deleteMany({ where: { user_id: id } });
|
||||
|
||||
Reference in New Issue
Block a user