160 lines
8.4 KiB
TypeScript
160 lines
8.4 KiB
TypeScript
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'];
|
|
|
|
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 [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) });
|
|
});
|
|
|
|
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' } } },
|
|
});
|
|
if (!project) return error(reply, 'Projekt nenalezen', 404);
|
|
return success(reply, project);
|
|
});
|
|
|
|
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,
|
|
},
|
|
});
|
|
|
|
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');
|
|
});
|
|
|
|
fastify.put<{ Params: { id: string } }>('/:id', { preHandler: requirePermission('projects.edit') }, async (request, reply) => {
|
|
const id = parseId(request.params.id, reply);
|
|
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 } });
|
|
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');
|
|
});
|
|
|
|
// POST /api/admin/projects/:id/notes
|
|
fastify.post<{ Params: { id: string } }>('/:id/notes', { preHandler: requirePermission('projects.edit') }, async (request, reply) => {
|
|
const projectId = parseId(request.params.id, reply);
|
|
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,
|
|
},
|
|
});
|
|
|
|
return success(reply, { note }, 201, 'Poznámka byla přidána');
|
|
});
|
|
|
|
// 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();
|
|
return success(reply, { next_number: nextNumber });
|
|
});
|
|
|
|
// DELETE /api/admin/projects/:id/notes/:noteId
|
|
fastify.delete<{ Params: { id: string; noteId: string } }>('/:id/notes/:noteId', { preHandler: requirePermission('projects.edit') }, async (request, reply) => {
|
|
const noteId = parseId(request.params.noteId, reply);
|
|
if (noteId === null) return;
|
|
const projectId = parseId(request.params.id, reply);
|
|
if (projectId === null) return;
|
|
|
|
const note = await prisma.project_notes.findFirst({ where: { id: noteId, project_id: projectId } });
|
|
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');
|
|
});
|
|
|
|
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}` });
|
|
return success(reply, null, 200, 'Projekt smazán');
|
|
});
|
|
}
|