refactor: extract projects business logic into projects.service.ts
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,55 +1,33 @@
|
|||||||
import { FastifyInstance } from 'fastify';
|
import { FastifyInstance } from 'fastify';
|
||||||
import prisma from '../../config/database';
|
|
||||||
import { requirePermission } from '../../middleware/auth';
|
import { requirePermission } from '../../middleware/auth';
|
||||||
import { logAudit } from '../../services/audit';
|
import { logAudit } from '../../services/audit';
|
||||||
import { success, error, parseId } from '../../utils/response';
|
import { success, error, parseId } from '../../utils/response';
|
||||||
import { parsePagination, buildPaginationMeta } from '../../utils/pagination';
|
import { parsePagination, buildPaginationMeta } from '../../utils/pagination';
|
||||||
import { parseBody } from '../../schemas/common';
|
import { parseBody } from '../../schemas/common';
|
||||||
import { CreateProjectSchema, UpdateProjectSchema, CreateProjectNoteSchema } from '../../schemas/projects.schema';
|
import { CreateProjectSchema, UpdateProjectSchema, CreateProjectNoteSchema } from '../../schemas/projects.schema';
|
||||||
import { generateSharedNumber } from '../../services/numbering.service';
|
import {
|
||||||
|
listProjects, getProject, createProject, updateProject, deleteProject,
|
||||||
const PROJECT_ALLOWED_SORT_FIELDS = ['id', 'project_number', 'name', 'status', 'created_at'];
|
createProjectNote, deleteProjectNote, getNextProjectNumber,
|
||||||
|
} from '../../services/projects.service';
|
||||||
|
|
||||||
export default async function projectsRoutes(fastify: FastifyInstance): Promise<void> {
|
export default async function projectsRoutes(fastify: FastifyInstance): Promise<void> {
|
||||||
fastify.get('/', { preHandler: requirePermission('projects.view') }, async (request, reply) => {
|
fastify.get('/', { preHandler: requirePermission('projects.view') }, async (request, reply) => {
|
||||||
const query = request.query as Record<string, unknown>;
|
const query = request.query as Record<string, unknown>;
|
||||||
const { page, limit, skip, sort, order, search } = parsePagination(query);
|
const { page, limit, skip, sort, order, search } = parsePagination(query);
|
||||||
const sortField = PROJECT_ALLOWED_SORT_FIELDS.includes(sort) ? sort : 'id';
|
|
||||||
|
|
||||||
const where: Record<string, unknown> = {};
|
const result = await listProjects({
|
||||||
if (query.status) where.status = String(query.status);
|
page, limit, skip, sort, order, search,
|
||||||
if (query.customer_id) where.customer_id = Number(query.customer_id);
|
status: query.status ? String(query.status) : undefined,
|
||||||
if (search) where.OR = [{ name: { contains: search } }, { project_number: { contains: search } }];
|
customer_id: query.customer_id ? Number(query.customer_id) : undefined,
|
||||||
|
});
|
||||||
|
|
||||||
const [projects, total] = await Promise.all([
|
return reply.send({ success: true, data: result.data, pagination: buildPaginationMeta(result.total, page, limit) });
|
||||||
prisma.projects.findMany({
|
|
||||||
where, skip, take: limit, orderBy: { [sortField]: order },
|
|
||||||
include: {
|
|
||||||
customers: { select: { id: true, name: true } },
|
|
||||||
users: { select: { id: true, first_name: true, last_name: true } },
|
|
||||||
orders: { select: { order_number: true } },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
prisma.projects.count({ where }),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const enriched = projects.map(p => ({
|
|
||||||
...p,
|
|
||||||
customer_name: p.customers?.name || null,
|
|
||||||
responsible_user_name: p.users ? `${p.users.first_name} ${p.users.last_name}`.trim() : null,
|
|
||||||
order_number: p.orders?.[0]?.order_number || null,
|
|
||||||
}));
|
|
||||||
|
|
||||||
return reply.send({ success: true, data: enriched, pagination: buildPaginationMeta(total, page, limit) });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.get<{ Params: { id: string } }>('/:id', { preHandler: requirePermission('projects.view') }, async (request, reply) => {
|
fastify.get<{ Params: { id: string } }>('/:id', { preHandler: requirePermission('projects.view') }, async (request, reply) => {
|
||||||
const id = parseId(request.params.id, reply);
|
const id = parseId(request.params.id, reply);
|
||||||
if (id === null) return;
|
if (id === null) return;
|
||||||
const project = await prisma.projects.findUnique({
|
const project = await getProject(id);
|
||||||
where: { id },
|
|
||||||
include: { customers: true, users: true, quotations: true, orders: true, project_notes: { orderBy: { created_at: 'desc' } } },
|
|
||||||
});
|
|
||||||
if (!project) return error(reply, 'Projekt nenalezen', 404);
|
if (!project) return error(reply, 'Projekt nenalezen', 404);
|
||||||
return success(reply, project);
|
return success(reply, project);
|
||||||
});
|
});
|
||||||
@@ -57,22 +35,8 @@ export default async function projectsRoutes(fastify: FastifyInstance): Promise<
|
|||||||
fastify.post('/', { preHandler: requirePermission('projects.create') }, async (request, reply) => {
|
fastify.post('/', { preHandler: requirePermission('projects.create') }, async (request, reply) => {
|
||||||
const parsed = parseBody(CreateProjectSchema, request.body);
|
const parsed = parseBody(CreateProjectSchema, request.body);
|
||||||
if ('error' in parsed) return error(reply, parsed.error, 400);
|
if ('error' in parsed) return error(reply, parsed.error, 400);
|
||||||
const body = parsed.data;
|
|
||||||
|
|
||||||
const project = await prisma.projects.create({
|
const project = await createProject(parsed.data);
|
||||||
data: {
|
|
||||||
project_number: body.project_number ? String(body.project_number) : null,
|
|
||||||
name: body.name ? String(body.name) : null,
|
|
||||||
customer_id: body.customer_id ? Number(body.customer_id) : null,
|
|
||||||
responsible_user_id: body.responsible_user_id ? Number(body.responsible_user_id) : null,
|
|
||||||
quotation_id: body.quotation_id ? Number(body.quotation_id) : null,
|
|
||||||
order_id: body.order_id ? Number(body.order_id) : null,
|
|
||||||
status: body.status ? String(body.status) : 'aktivni',
|
|
||||||
start_date: body.start_date ? new Date(String(body.start_date)) : null,
|
|
||||||
end_date: body.end_date ? new Date(String(body.end_date)) : null,
|
|
||||||
notes: body.notes ? String(body.notes) : null,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
await logAudit({ request, authData: request.authData, action: 'create', entityType: 'project', entityId: project.id, description: `Vytvořen projekt ${project.name}` });
|
await logAudit({ request, authData: request.authData, action: 'create', entityType: 'project', entityId: project.id, description: `Vytvořen projekt ${project.name}` });
|
||||||
return success(reply, { id: project.id }, 201, 'Projekt byl vytvořen');
|
return success(reply, { id: project.id }, 201, 'Projekt byl vytvořen');
|
||||||
@@ -83,22 +47,10 @@ export default async function projectsRoutes(fastify: FastifyInstance): Promise<
|
|||||||
if (id === null) return;
|
if (id === null) return;
|
||||||
const parsed = parseBody(UpdateProjectSchema, request.body);
|
const parsed = parseBody(UpdateProjectSchema, request.body);
|
||||||
if ('error' in parsed) return error(reply, parsed.error, 400);
|
if ('error' in parsed) return error(reply, parsed.error, 400);
|
||||||
const body = parsed.data;
|
|
||||||
|
|
||||||
const existing = await prisma.projects.findUnique({ where: { id } });
|
const existing = await updateProject(id, parsed.data);
|
||||||
if (!existing) return error(reply, 'Projekt nenalezen', 404);
|
if (!existing) return error(reply, 'Projekt nenalezen', 404);
|
||||||
|
|
||||||
const data: Record<string, unknown> = { modified_at: new Date() };
|
|
||||||
const strFields = ['project_number', 'name', 'status', 'notes'];
|
|
||||||
for (const f of strFields) if (body[f] !== undefined) data[f] = body[f] ? String(body[f]) : null;
|
|
||||||
if (body.customer_id !== undefined) data.customer_id = body.customer_id ? Number(body.customer_id) : null;
|
|
||||||
if (body.responsible_user_id !== undefined) data.responsible_user_id = body.responsible_user_id ? Number(body.responsible_user_id) : null;
|
|
||||||
if (body.quotation_id !== undefined) data.quotation_id = body.quotation_id ? Number(body.quotation_id) : null;
|
|
||||||
if (body.order_id !== undefined) data.order_id = body.order_id ? Number(body.order_id) : null;
|
|
||||||
if (body.start_date !== undefined) data.start_date = body.start_date ? new Date(String(body.start_date)) : null;
|
|
||||||
if (body.end_date !== undefined) data.end_date = body.end_date ? new Date(String(body.end_date)) : null;
|
|
||||||
|
|
||||||
await prisma.projects.update({ where: { id }, data });
|
|
||||||
await logAudit({ request, authData: request.authData, action: 'update', entityType: 'project', entityId: id, description: `Upraven projekt ${existing.name}` });
|
await logAudit({ request, authData: request.authData, action: 'update', entityType: 'project', entityId: id, description: `Upraven projekt ${existing.name}` });
|
||||||
return success(reply, { id }, 200, 'Projekt byl uložen');
|
return success(reply, { id }, 200, 'Projekt byl uložen');
|
||||||
});
|
});
|
||||||
@@ -109,16 +61,13 @@ export default async function projectsRoutes(fastify: FastifyInstance): Promise<
|
|||||||
if (projectId === null) return;
|
if (projectId === null) return;
|
||||||
const parsed = parseBody(CreateProjectNoteSchema, request.body);
|
const parsed = parseBody(CreateProjectNoteSchema, request.body);
|
||||||
if ('error' in parsed) return error(reply, parsed.error, 400);
|
if ('error' in parsed) return error(reply, parsed.error, 400);
|
||||||
const body = parsed.data;
|
|
||||||
const authData = request.authData!;
|
const authData = request.authData!;
|
||||||
|
|
||||||
const note = await prisma.project_notes.create({
|
const note = await createProjectNote(projectId, {
|
||||||
data: {
|
userId: authData.userId,
|
||||||
project_id: projectId,
|
firstName: authData.firstName,
|
||||||
user_id: authData.userId,
|
lastName: authData.lastName,
|
||||||
user_name: `${authData.firstName} ${authData.lastName}`,
|
content: parsed.data.content,
|
||||||
content: body.content ? String(body.content) : null,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return success(reply, { note }, 201, 'Poznámka byla přidána');
|
return success(reply, { note }, 201, 'Poznámka byla přidána');
|
||||||
@@ -126,7 +75,7 @@ export default async function projectsRoutes(fastify: FastifyInstance): Promise<
|
|||||||
|
|
||||||
// GET /api/admin/projects/next-number — shared sequence with orders (matches PHP)
|
// GET /api/admin/projects/next-number — shared sequence with orders (matches PHP)
|
||||||
fastify.get('/next-number', { preHandler: requirePermission('projects.create') }, async (_request, reply) => {
|
fastify.get('/next-number', { preHandler: requirePermission('projects.create') }, async (_request, reply) => {
|
||||||
const nextNumber = await generateSharedNumber();
|
const nextNumber = await getNextProjectNumber();
|
||||||
return success(reply, { next_number: nextNumber });
|
return success(reply, { next_number: nextNumber });
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -137,10 +86,9 @@ export default async function projectsRoutes(fastify: FastifyInstance): Promise<
|
|||||||
const projectId = parseId(request.params.id, reply);
|
const projectId = parseId(request.params.id, reply);
|
||||||
if (projectId === null) return;
|
if (projectId === null) return;
|
||||||
|
|
||||||
const note = await prisma.project_notes.findFirst({ where: { id: noteId, project_id: projectId } });
|
const note = await deleteProjectNote(projectId, noteId);
|
||||||
if (!note) return error(reply, 'Poznámka nenalezena', 404);
|
if (!note) return error(reply, 'Poznámka nenalezena', 404);
|
||||||
|
|
||||||
await prisma.project_notes.delete({ where: { id: noteId } });
|
|
||||||
await logAudit({ request, authData: request.authData, action: 'delete', entityType: 'project', entityId: projectId, description: `Smazána poznámka projektu` });
|
await logAudit({ request, authData: request.authData, action: 'delete', entityType: 'project', entityId: projectId, description: `Smazána poznámka projektu` });
|
||||||
return success(reply, null, 200, 'Poznámka smazána');
|
return success(reply, null, 200, 'Poznámka smazána');
|
||||||
});
|
});
|
||||||
@@ -148,12 +96,14 @@ export default async function projectsRoutes(fastify: FastifyInstance): Promise<
|
|||||||
fastify.delete<{ Params: { id: string } }>('/:id', { preHandler: requirePermission('projects.delete') }, async (request, reply) => {
|
fastify.delete<{ Params: { id: string } }>('/:id', { preHandler: requirePermission('projects.delete') }, async (request, reply) => {
|
||||||
const id = parseId(request.params.id, reply);
|
const id = parseId(request.params.id, reply);
|
||||||
if (id === null) return;
|
if (id === null) return;
|
||||||
const existing = await prisma.projects.findUnique({ where: { id } });
|
|
||||||
if (!existing) return error(reply, 'Projekt nenalezen', 404);
|
|
||||||
if (existing.order_id) return error(reply, 'Nelze smazat projekt propojený s objednávkou. Nejdříve smažte objednávku.', 400);
|
|
||||||
|
|
||||||
await prisma.projects.delete({ where: { id } });
|
const result = await deleteProject(id);
|
||||||
await logAudit({ request, authData: request.authData, action: 'delete', entityType: 'project', entityId: id, description: `Smazán projekt ${existing.name}` });
|
if (result && 'error' in result) {
|
||||||
|
if (result.error === 'not_found') return error(reply, 'Projekt nenalezen', 404);
|
||||||
|
if (result.error === 'has_order') return error(reply, 'Nelze smazat projekt propojený s objednávkou. Nejdříve smažte objednávku.', 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
await logAudit({ request, authData: request.authData, action: 'delete', entityType: 'project', entityId: id, description: `Smazán projekt ${(result as any).name}` });
|
||||||
return success(reply, null, 200, 'Projekt smazán');
|
return success(reply, null, 200, 'Projekt smazán');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
123
src/services/projects.service.ts
Normal file
123
src/services/projects.service.ts
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
import prisma from '../config/database';
|
||||||
|
import { generateSharedNumber } from './numbering.service';
|
||||||
|
|
||||||
|
const ALLOWED_SORT_FIELDS = ['id', 'project_number', 'name', 'status', 'created_at'];
|
||||||
|
|
||||||
|
interface ListProjectsParams {
|
||||||
|
page: number;
|
||||||
|
limit: number;
|
||||||
|
skip: number;
|
||||||
|
sort: string;
|
||||||
|
order: 'asc' | 'desc';
|
||||||
|
search: string;
|
||||||
|
status?: string;
|
||||||
|
customer_id?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function listProjects(params: ListProjectsParams) {
|
||||||
|
const { page, limit, skip, sort, order, search, status, customer_id } = params;
|
||||||
|
const sortField = ALLOWED_SORT_FIELDS.includes(sort) ? sort : 'id';
|
||||||
|
|
||||||
|
const where: Record<string, unknown> = {};
|
||||||
|
if (status) where.status = status;
|
||||||
|
if (customer_id) where.customer_id = customer_id;
|
||||||
|
if (search) where.OR = [{ name: { contains: search } }, { project_number: { contains: search } }];
|
||||||
|
|
||||||
|
const [projects, total] = await Promise.all([
|
||||||
|
prisma.projects.findMany({
|
||||||
|
where, skip, take: limit, orderBy: { [sortField]: order },
|
||||||
|
include: {
|
||||||
|
customers: { select: { id: true, name: true } },
|
||||||
|
users: { select: { id: true, first_name: true, last_name: true } },
|
||||||
|
orders: { select: { order_number: true } },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
prisma.projects.count({ where }),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const enriched = projects.map(p => ({
|
||||||
|
...p,
|
||||||
|
customer_name: p.customers?.name || null,
|
||||||
|
responsible_user_name: p.users ? `${p.users.first_name} ${p.users.last_name}`.trim() : null,
|
||||||
|
order_number: p.orders?.[0]?.order_number || null,
|
||||||
|
}));
|
||||||
|
|
||||||
|
return { data: enriched, total, page, limit };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getProject(id: number) {
|
||||||
|
const project = await prisma.projects.findUnique({
|
||||||
|
where: { id },
|
||||||
|
include: { customers: true, users: true, quotations: true, orders: true, project_notes: { orderBy: { created_at: 'desc' } } },
|
||||||
|
});
|
||||||
|
return project || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createProject(body: Record<string, any>) {
|
||||||
|
const project = await prisma.projects.create({
|
||||||
|
data: {
|
||||||
|
project_number: body.project_number ? String(body.project_number) : null,
|
||||||
|
name: body.name ? String(body.name) : null,
|
||||||
|
customer_id: body.customer_id ? Number(body.customer_id) : null,
|
||||||
|
responsible_user_id: body.responsible_user_id ? Number(body.responsible_user_id) : null,
|
||||||
|
quotation_id: body.quotation_id ? Number(body.quotation_id) : null,
|
||||||
|
order_id: body.order_id ? Number(body.order_id) : null,
|
||||||
|
status: body.status ? String(body.status) : 'aktivni',
|
||||||
|
start_date: body.start_date ? new Date(String(body.start_date)) : null,
|
||||||
|
end_date: body.end_date ? new Date(String(body.end_date)) : null,
|
||||||
|
notes: body.notes ? String(body.notes) : null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return project;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateProject(id: number, body: Record<string, any>) {
|
||||||
|
const existing = await prisma.projects.findUnique({ where: { id } });
|
||||||
|
if (!existing) return null;
|
||||||
|
|
||||||
|
const data: Record<string, unknown> = { modified_at: new Date() };
|
||||||
|
const strFields = ['project_number', 'name', 'status', 'notes'];
|
||||||
|
for (const f of strFields) if (body[f] !== undefined) data[f] = body[f] ? String(body[f]) : null;
|
||||||
|
if (body.customer_id !== undefined) data.customer_id = body.customer_id ? Number(body.customer_id) : null;
|
||||||
|
if (body.responsible_user_id !== undefined) data.responsible_user_id = body.responsible_user_id ? Number(body.responsible_user_id) : null;
|
||||||
|
if (body.quotation_id !== undefined) data.quotation_id = body.quotation_id ? Number(body.quotation_id) : null;
|
||||||
|
if (body.order_id !== undefined) data.order_id = body.order_id ? Number(body.order_id) : null;
|
||||||
|
if (body.start_date !== undefined) data.start_date = body.start_date ? new Date(String(body.start_date)) : null;
|
||||||
|
if (body.end_date !== undefined) data.end_date = body.end_date ? new Date(String(body.end_date)) : null;
|
||||||
|
|
||||||
|
await prisma.projects.update({ where: { id }, data });
|
||||||
|
return existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteProject(id: number) {
|
||||||
|
const existing = await prisma.projects.findUnique({ where: { id } });
|
||||||
|
if (!existing) return { error: 'not_found' as const };
|
||||||
|
if (existing.order_id) return { error: 'has_order' as const };
|
||||||
|
|
||||||
|
await prisma.projects.delete({ where: { id } });
|
||||||
|
return existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createProjectNote(projectId: number, data: { userId: number; firstName: string; lastName: string; content?: string }) {
|
||||||
|
const note = await prisma.project_notes.create({
|
||||||
|
data: {
|
||||||
|
project_id: projectId,
|
||||||
|
user_id: data.userId,
|
||||||
|
user_name: `${data.firstName} ${data.lastName}`,
|
||||||
|
content: data.content ? String(data.content) : null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteProjectNote(projectId: number, noteId: number) {
|
||||||
|
const note = await prisma.project_notes.findFirst({ where: { id: noteId, project_id: projectId } });
|
||||||
|
if (!note) return null;
|
||||||
|
|
||||||
|
await prisma.project_notes.delete({ where: { id: noteId } });
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getNextProjectNumber() {
|
||||||
|
return generateSharedNumber();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user