import { FastifyInstance } from "fastify"; import prisma from "../../config/database"; import { requirePermission } from "../../middleware/auth"; import { success, error, parseId } from "../../utils/response"; import { parseBody } from "../../schemas/common"; import { CreateScopeTemplateSchema, CreateItemTemplateSchema, UpdateScopeTemplateSchema, } from "../../schemas/scope-templates.schema"; interface ScopeSectionInput { title?: string; title_cz?: string; content?: string; position?: number; } export default async function scopeTemplatesRoutes( fastify: FastifyInstance, ): Promise { // Legacy ?action= dispatcher for item templates fastify.get( "/", { preHandler: requirePermission("settings.manage") }, async (request, reply) => { const query = request.query as Record; const action = query.action ? String(query.action) : null; if (action === "items") { const items = await prisma.item_templates.findMany({ where: { is_deleted: false }, orderBy: { name: "asc" }, }); return success(reply, items); } // Default: scope templates const templates = await prisma.scope_templates.findMany({ where: { is_deleted: false }, include: { scope_template_sections: { where: { is_deleted: false }, orderBy: { position: "asc" }, }, }, orderBy: { name: "asc" }, }); return success(reply, templates); }, ); // Item template CRUD via ?action=item fastify.post( "/", { preHandler: requirePermission("settings.manage") }, async (request, reply) => { const query = request.query as Record; if (String(query.action) === "item") { const itemParsed = parseBody(CreateItemTemplateSchema, request.body); if ("error" in itemParsed) return error(reply, itemParsed.error, 400); const body = itemParsed.data; const itemData = { name: body.name ? String(body.name) : null, description: body.description ? String(body.description) : null, default_price: body.default_price != null ? Number(body.default_price) : 0, category: body.category ? String(body.category) : null, }; if (body.id) { const existingItem = await prisma.item_templates.findUnique({ where: { id: Number(body.id) }, }); if (!existingItem) return error(reply, "Šablona nenalezena", 404); await prisma.item_templates.update({ where: { id: Number(body.id) }, data: { ...itemData, modified_at: new Date() }, }); return success( reply, { id: Number(body.id) }, 200, "Položka byla uložena", ); } const item = await prisma.item_templates.create({ data: itemData }); return success(reply, { id: item.id }, 201, "Položka byla vytvořena"); } const scopeParsed = parseBody(CreateScopeTemplateSchema, request.body); if ("error" in scopeParsed) return error(reply, scopeParsed.error, 400); const body = scopeParsed.data; const template = await prisma.scope_templates.create({ data: { name: body.name ? String(body.name) : null, title: body.title ? String(body.title) : null, description: body.description ? String(body.description) : null, }, }); if (Array.isArray(body.sections)) { await prisma.scope_template_sections.createMany({ data: (body.sections as ScopeSectionInput[]).map((s, i) => ({ scope_template_id: template.id, title: s.title ?? null, title_cz: s.title_cz ?? null, content: s.content ?? null, position: s.position ?? i, })), }); } return success(reply, { id: template.id }, 201, "Šablona byla vytvořena"); }, ); // Item template delete via DELETE ?action=item&id=X fastify.delete( "/", { preHandler: requirePermission("settings.manage") }, async (request, reply) => { const query = request.query as Record; if (String(query.action) === "item" && query.id) { const id = Number(query.id); await prisma.item_templates.update({ where: { id }, data: { is_deleted: true, modified_at: new Date() }, }); return success(reply, null, 200, "Šablona smazána"); } return error(reply, "Neplatná akce", 400); }, ); fastify.get<{ Params: { id: string } }>( "/:id", { preHandler: requirePermission("settings.manage") }, async (request, reply) => { const id = parseId(request.params.id, reply); if (id === null) return; const template = await prisma.scope_templates.findUnique({ where: { id }, include: { scope_template_sections: { where: { is_deleted: false }, orderBy: { position: "asc" }, }, }, }); if (!template || template.is_deleted) return error(reply, "Šablona nenalezena", 404); return success(reply, template); }, ); fastify.put<{ Params: { id: string } }>( "/:id", { preHandler: requirePermission("settings.manage") }, async (request, reply) => { const id = parseId(request.params.id, reply); if (id === null) return; const parsed = parseBody(UpdateScopeTemplateSchema, request.body); if ("error" in parsed) return error(reply, parsed.error, 400); const body = parsed.data; const existing = await prisma.scope_templates.findUnique({ where: { id }, }); if (!existing) return error(reply, "Šablona nenalezena", 404); await prisma.scope_templates.update({ where: { id }, data: { name: body.name !== undefined ? String(body.name) : undefined, title: body.title !== undefined ? String(body.title) : undefined, description: body.description !== undefined ? String(body.description) : undefined, modified_at: new Date(), }, }); if (Array.isArray(body.sections)) { await prisma.scope_template_sections.deleteMany({ where: { scope_template_id: id }, }); await prisma.scope_template_sections.createMany({ data: (body.sections as ScopeSectionInput[]).map((s, i) => ({ scope_template_id: id, title: s.title ?? null, title_cz: s.title_cz ?? null, content: s.content ?? null, position: s.position ?? i, })), }); } return success(reply, { id }, 200, "Šablona byla uložena"); }, ); fastify.delete<{ Params: { id: string } }>( "/:id", { preHandler: requirePermission("settings.manage") }, async (request, reply) => { const id = parseId(request.params.id, reply); if (id === null) return; await prisma.scope_templates.update({ where: { id }, data: { is_deleted: true, modified_at: new Date() }, }); return success(reply, null, 200, "Šablona smazána"); }, ); }