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 prisma from '../../config/database';
|
||||
import { requirePermission } from '../../middleware/auth';
|
||||
import { logAudit } from '../../services/audit';
|
||||
import { success, error, parseId } from '../../utils/response';
|
||||
import { parsePagination, buildPaginationMeta } from '../../utils/pagination';
|
||||
import { parseBody } from '../../schemas/common';
|
||||
import { CreateProjectSchema, UpdateProjectSchema, CreateProjectNoteSchema } from '../../schemas/projects.schema';
|
||||
import { generateSharedNumber } from '../../services/numbering.service';
|
||||
|
||||
const PROJECT_ALLOWED_SORT_FIELDS = ['id', 'project_number', 'name', 'status', 'created_at'];
|
||||
import {
|
||||
listProjects, getProject, createProject, updateProject, deleteProject,
|
||||
createProjectNote, deleteProjectNote, getNextProjectNumber,
|
||||
} from '../../services/projects.service';
|
||||
|
||||
export default async function projectsRoutes(fastify: FastifyInstance): Promise<void> {
|
||||
fastify.get('/', { preHandler: requirePermission('projects.view') }, async (request, reply) => {
|
||||
const query = request.query as Record<string, unknown>;
|
||||
const { page, limit, skip, sort, order, search } = parsePagination(query);
|
||||
const sortField = PROJECT_ALLOWED_SORT_FIELDS.includes(sort) ? sort : 'id';
|
||||
|
||||
const where: Record<string, unknown> = {};
|
||||
if (query.status) where.status = String(query.status);
|
||||
if (query.customer_id) where.customer_id = Number(query.customer_id);
|
||||
if (search) where.OR = [{ name: { contains: search } }, { project_number: { contains: search } }];
|
||||
const result = await listProjects({
|
||||
page, limit, skip, sort, order, search,
|
||||
status: query.status ? String(query.status) : undefined,
|
||||
customer_id: query.customer_id ? Number(query.customer_id) : undefined,
|
||||
});
|
||||
|
||||
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 reply.send({ success: true, data: enriched, pagination: buildPaginationMeta(total, page, limit) });
|
||||
return reply.send({ success: true, data: result.data, pagination: buildPaginationMeta(result.total, page, limit) });
|
||||
});
|
||||
|
||||
fastify.get<{ Params: { id: string } }>('/:id', { preHandler: requirePermission('projects.view') }, async (request, reply) => {
|
||||
const id = parseId(request.params.id, reply);
|
||||
if (id === null) return;
|
||||
const project = await prisma.projects.findUnique({
|
||||
where: { id },
|
||||
include: { customers: true, users: true, quotations: true, orders: true, project_notes: { orderBy: { created_at: 'desc' } } },
|
||||
});
|
||||
const project = await getProject(id);
|
||||
if (!project) return error(reply, 'Projekt nenalezen', 404);
|
||||
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) => {
|
||||
const parsed = parseBody(CreateProjectSchema, request.body);
|
||||
if ('error' in parsed) return error(reply, parsed.error, 400);
|
||||
const body = parsed.data;
|
||||
|
||||
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,
|
||||
},
|
||||
});
|
||||
const project = await createProject(parsed.data);
|
||||
|
||||
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');
|
||||
@@ -83,22 +47,10 @@ export default async function projectsRoutes(fastify: FastifyInstance): Promise<
|
||||
if (id === null) return;
|
||||
const parsed = parseBody(UpdateProjectSchema, request.body);
|
||||
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);
|
||||
|
||||
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}` });
|
||||
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;
|
||||
const parsed = parseBody(CreateProjectNoteSchema, request.body);
|
||||
if ('error' in parsed) return error(reply, parsed.error, 400);
|
||||
const body = parsed.data;
|
||||
const authData = request.authData!;
|
||||
|
||||
const note = await prisma.project_notes.create({
|
||||
data: {
|
||||
project_id: projectId,
|
||||
user_id: authData.userId,
|
||||
user_name: `${authData.firstName} ${authData.lastName}`,
|
||||
content: body.content ? String(body.content) : null,
|
||||
},
|
||||
const note = await createProjectNote(projectId, {
|
||||
userId: authData.userId,
|
||||
firstName: authData.firstName,
|
||||
lastName: authData.lastName,
|
||||
content: parsed.data.content,
|
||||
});
|
||||
|
||||
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)
|
||||
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 });
|
||||
});
|
||||
|
||||
@@ -137,10 +86,9 @@ export default async function projectsRoutes(fastify: FastifyInstance): Promise<
|
||||
const projectId = parseId(request.params.id, reply);
|
||||
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);
|
||||
|
||||
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` });
|
||||
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) => {
|
||||
const id = parseId(request.params.id, reply);
|
||||
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 } });
|
||||
await logAudit({ request, authData: request.authData, action: 'delete', entityType: 'project', entityId: id, description: `Smazán projekt ${existing.name}` });
|
||||
const result = await deleteProject(id);
|
||||
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');
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user