style: run prettier on entire codebase
This commit is contained in:
@@ -1,158 +1,225 @@
|
||||
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';
|
||||
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 }
|
||||
interface ScopeSectionInput {
|
||||
title?: string;
|
||||
title_cz?: string;
|
||||
content?: string;
|
||||
position?: number;
|
||||
}
|
||||
|
||||
export default async function scopeTemplatesRoutes(fastify: FastifyInstance): Promise<void> {
|
||||
export default async function scopeTemplatesRoutes(
|
||||
fastify: FastifyInstance,
|
||||
): Promise<void> {
|
||||
// Legacy ?action= dispatcher for item templates
|
||||
fastify.get('/', { preHandler: requirePermission('offers.settings') }, async (request, reply) => {
|
||||
const query = request.query as Record<string, unknown>;
|
||||
const action = query.action ? String(query.action) : null;
|
||||
fastify.get(
|
||||
"/",
|
||||
{ preHandler: requirePermission("offers.settings") },
|
||||
async (request, reply) => {
|
||||
const query = request.query as Record<string, unknown>;
|
||||
const action = query.action ? String(query.action) : null;
|
||||
|
||||
// Item templates
|
||||
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('offers.settings') }, async (request, reply) => {
|
||||
const query = request.query as Record<string, unknown>;
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
// Update existing item if id is provided
|
||||
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() },
|
||||
// Item templates
|
||||
if (action === "items") {
|
||||
const items = await prisma.item_templates.findMany({
|
||||
where: { is_deleted: false },
|
||||
orderBy: { name: "asc" },
|
||||
});
|
||||
return success(reply, { id: Number(body.id) }, 200, 'Položka byla uložena');
|
||||
return success(reply, items);
|
||||
}
|
||||
|
||||
const item = await prisma.item_templates.create({ data: itemData });
|
||||
return success(reply, { id: item.id }, 201, 'Položka byla vytvořena');
|
||||
}
|
||||
|
||||
// Scope template create (original logic below)
|
||||
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,
|
||||
})),
|
||||
// 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);
|
||||
},
|
||||
);
|
||||
|
||||
return success(reply, { id: template.id }, 201, 'Šablona byla vytvořena');
|
||||
});
|
||||
// Item template CRUD via ?action=item
|
||||
fastify.post(
|
||||
"/",
|
||||
{ preHandler: requirePermission("offers.settings") },
|
||||
async (request, reply) => {
|
||||
const query = request.query as Record<string, unknown>;
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
// Update existing item if id is provided
|
||||
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");
|
||||
}
|
||||
|
||||
// Scope template create (original logic below)
|
||||
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('offers.settings') }, async (request, reply) => {
|
||||
const query = request.query as Record<string, unknown>;
|
||||
fastify.delete(
|
||||
"/",
|
||||
{ preHandler: requirePermission("offers.settings") },
|
||||
async (request, reply) => {
|
||||
const query = request.query as Record<string, unknown>;
|
||||
|
||||
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');
|
||||
}
|
||||
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);
|
||||
});
|
||||
return error(reply, "Neplatná akce", 400);
|
||||
},
|
||||
);
|
||||
|
||||
fastify.get<{ Params: { id: string } }>('/:id', { preHandler: requirePermission('offers.settings') }, 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('offers.settings') }, 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,
|
||||
})),
|
||||
fastify.get<{ Params: { id: string } }>(
|
||||
"/:id",
|
||||
{ preHandler: requirePermission("offers.settings") },
|
||||
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);
|
||||
},
|
||||
);
|
||||
|
||||
return success(reply, { id }, 200, 'Šablona byla uložena');
|
||||
});
|
||||
fastify.put<{ Params: { id: string } }>(
|
||||
"/:id",
|
||||
{ preHandler: requirePermission("offers.settings") },
|
||||
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;
|
||||
|
||||
fastify.delete<{ Params: { id: string } }>('/:id', { preHandler: requirePermission('offers.settings') }, 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');
|
||||
});
|
||||
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("offers.settings") },
|
||||
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");
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user