style: run prettier on entire codebase
This commit is contained in:
@@ -1,24 +1,49 @@
|
||||
import prisma from '../config/database';
|
||||
import { generateSharedNumber } from './numbering.service';
|
||||
import prisma from "../config/database";
|
||||
import { generateSharedNumber } from "./numbering.service";
|
||||
|
||||
interface OrderItemInput { description?: string | null; item_description?: string | null; quantity?: number; unit?: string | null; unit_price?: number; is_included_in_total?: boolean; position?: number }
|
||||
interface OrderSectionInput { title?: string; title_cz?: string; content?: string; position?: number }
|
||||
interface OrderItemInput {
|
||||
description?: string | null;
|
||||
item_description?: string | null;
|
||||
quantity?: number;
|
||||
unit?: string | null;
|
||||
unit_price?: number;
|
||||
is_included_in_total?: boolean;
|
||||
position?: number;
|
||||
}
|
||||
interface OrderSectionInput {
|
||||
title?: string;
|
||||
title_cz?: string;
|
||||
content?: string;
|
||||
position?: number;
|
||||
}
|
||||
|
||||
// Status transition rules matching PHP
|
||||
export const VALID_TRANSITIONS: Record<string, string[]> = {
|
||||
prijata: ['v_realizaci', 'stornovana'],
|
||||
v_realizaci: ['dokoncena', 'stornovana'],
|
||||
prijata: ["v_realizaci", "stornovana"],
|
||||
v_realizaci: ["dokoncena", "stornovana"],
|
||||
dokoncena: [],
|
||||
stornovana: [],
|
||||
};
|
||||
|
||||
const ORDER_ALLOWED_SORT_FIELDS = ['id', 'order_number', 'status', 'currency', 'created_at'];
|
||||
const ORDER_ALLOWED_SORT_FIELDS = [
|
||||
"id",
|
||||
"order_number",
|
||||
"status",
|
||||
"currency",
|
||||
"created_at",
|
||||
];
|
||||
|
||||
function enrichOrder(o: any) {
|
||||
const subtotal = o.order_items
|
||||
.filter((i: any) => i.is_included_in_total !== false)
|
||||
.reduce((s: number, i: any) => s + (Number(i.quantity) || 0) * (Number(i.unit_price) || 0), 0);
|
||||
const vatAmount = o.apply_vat ? subtotal * ((Number(o.vat_rate) || 21) / 100) : 0;
|
||||
.reduce(
|
||||
(s: number, i: any) =>
|
||||
s + (Number(i.quantity) || 0) * (Number(i.unit_price) || 0),
|
||||
0,
|
||||
);
|
||||
const vatAmount = o.apply_vat
|
||||
? subtotal * ((Number(o.vat_rate) || 21) / 100)
|
||||
: 0;
|
||||
const { order_items, order_sections, ...rest } = o;
|
||||
const invoice = o.invoices?.[0] || null;
|
||||
return {
|
||||
@@ -41,14 +66,16 @@ interface ListOrdersParams {
|
||||
limit: number;
|
||||
skip: number;
|
||||
sort: string;
|
||||
order: 'asc' | 'desc';
|
||||
order: "asc" | "desc";
|
||||
status?: string;
|
||||
customer_id?: number;
|
||||
}
|
||||
|
||||
export async function listOrders(params: ListOrdersParams) {
|
||||
const { page, limit, skip, order } = params;
|
||||
const sortField = ORDER_ALLOWED_SORT_FIELDS.includes(params.sort) ? params.sort : 'id';
|
||||
const sortField = ORDER_ALLOWED_SORT_FIELDS.includes(params.sort)
|
||||
? params.sort
|
||||
: "id";
|
||||
|
||||
const where: Record<string, unknown> = {};
|
||||
if (params.status) where.status = params.status;
|
||||
@@ -56,11 +83,14 @@ export async function listOrders(params: ListOrdersParams) {
|
||||
|
||||
const [orders, total] = await Promise.all([
|
||||
prisma.orders.findMany({
|
||||
where, skip, take: limit, orderBy: { [sortField]: order },
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { [sortField]: order },
|
||||
include: {
|
||||
customers: { select: { id: true, name: true } },
|
||||
order_items: { orderBy: { position: 'asc' } },
|
||||
order_sections: { orderBy: { position: 'asc' } },
|
||||
order_items: { orderBy: { position: "asc" } },
|
||||
order_sections: { orderBy: { position: "asc" } },
|
||||
quotations: { select: { quotation_number: true, project_code: true } },
|
||||
invoices: { select: { id: true, invoice_number: true }, take: 1 },
|
||||
},
|
||||
@@ -77,11 +107,18 @@ export async function getOrder(id: number) {
|
||||
where: { id },
|
||||
include: {
|
||||
customers: true,
|
||||
order_items: { orderBy: { position: 'asc' } },
|
||||
order_sections: { orderBy: { position: 'asc' } },
|
||||
quotations: { select: { id: true, quotation_number: true, project_code: true } },
|
||||
projects: { select: { id: true, project_number: true, name: true, status: true } },
|
||||
invoices: { select: { id: true, invoice_number: true, status: true }, take: 1 },
|
||||
order_items: { orderBy: { position: "asc" } },
|
||||
order_sections: { orderBy: { position: "asc" } },
|
||||
quotations: {
|
||||
select: { id: true, quotation_number: true, project_code: true },
|
||||
},
|
||||
projects: {
|
||||
select: { id: true, project_number: true, name: true, status: true },
|
||||
},
|
||||
invoices: {
|
||||
select: { id: true, invoice_number: true, status: true },
|
||||
take: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!order) return null;
|
||||
@@ -99,7 +136,7 @@ export async function getOrder(id: number) {
|
||||
invoice: invoice,
|
||||
invoice_id: invoice?.id || null,
|
||||
invoice_number: invoice?.invoice_number || null,
|
||||
valid_transitions: VALID_TRANSITIONS[(order.status as string) || ''] || [],
|
||||
valid_transitions: VALID_TRANSITIONS[(order.status as string) || ""] || [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -122,19 +159,26 @@ interface CreateOrderFromQuotationData {
|
||||
attachmentName?: string | null;
|
||||
}
|
||||
|
||||
export async function createOrderFromQuotation(data: CreateOrderFromQuotationData) {
|
||||
const { quotationId, customerOrderNumber, attachmentBuffer, attachmentName } = data;
|
||||
export async function createOrderFromQuotation(
|
||||
data: CreateOrderFromQuotationData,
|
||||
) {
|
||||
const { quotationId, customerOrderNumber, attachmentBuffer, attachmentName } =
|
||||
data;
|
||||
|
||||
const quotation = await prisma.quotations.findUnique({
|
||||
where: { id: quotationId },
|
||||
include: {
|
||||
quotation_items: { orderBy: { position: 'asc' } },
|
||||
scope_sections: { orderBy: { position: 'asc' } },
|
||||
quotation_items: { orderBy: { position: "asc" } },
|
||||
scope_sections: { orderBy: { position: "asc" } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!quotation) return { error: 'Nabídka nenalezena', status: 404 } as const;
|
||||
if (quotation.order_id) return { error: 'Z této nabídky již byla vytvořena objednávka', status: 400 } as const;
|
||||
if (!quotation) return { error: "Nabídka nenalezena", status: 404 } as const;
|
||||
if (quotation.order_id)
|
||||
return {
|
||||
error: "Z této nabídky již byla vytvořena objednávka",
|
||||
status: 400,
|
||||
} as const;
|
||||
|
||||
const orderNumber = await generateSharedNumber();
|
||||
const projectNumber = await generateSharedNumber();
|
||||
@@ -146,15 +190,17 @@ export async function createOrderFromQuotation(data: CreateOrderFromQuotationDat
|
||||
customer_order_number: customerOrderNumber || null,
|
||||
quotation_id: quotationId,
|
||||
customer_id: quotation.customer_id,
|
||||
status: 'prijata',
|
||||
currency: quotation.currency || 'CZK',
|
||||
language: quotation.language || 'cs',
|
||||
status: "prijata",
|
||||
currency: quotation.currency || "CZK",
|
||||
language: quotation.language || "cs",
|
||||
vat_rate: quotation.vat_rate ?? 21.0,
|
||||
apply_vat: quotation.apply_vat ?? true,
|
||||
exchange_rate: quotation.exchange_rate ?? 1.0,
|
||||
scope_title: quotation.scope_title,
|
||||
scope_description: quotation.scope_description,
|
||||
attachment_data: attachmentBuffer ? new Uint8Array(attachmentBuffer) : null,
|
||||
attachment_data: attachmentBuffer
|
||||
? new Uint8Array(attachmentBuffer)
|
||||
: null,
|
||||
attachment_name: attachmentName || null,
|
||||
},
|
||||
});
|
||||
@@ -188,24 +234,32 @@ export async function createOrderFromQuotation(data: CreateOrderFromQuotationDat
|
||||
|
||||
await tx.quotations.update({
|
||||
where: { id: quotationId },
|
||||
data: { order_id: order.id, status: 'ordered', modified_at: new Date() },
|
||||
data: { order_id: order.id, status: "ordered", modified_at: new Date() },
|
||||
});
|
||||
|
||||
const project = await tx.projects.create({
|
||||
data: {
|
||||
project_number: projectNumber,
|
||||
name: quotation.project_code || quotation.quotation_number || orderNumber,
|
||||
name:
|
||||
quotation.project_code || quotation.quotation_number || orderNumber,
|
||||
customer_id: quotation.customer_id,
|
||||
quotation_id: quotationId,
|
||||
order_id: order.id,
|
||||
status: 'aktivni',
|
||||
status: "aktivni",
|
||||
},
|
||||
});
|
||||
|
||||
return { order, project };
|
||||
});
|
||||
|
||||
return { data: { order_id: result.order.id, id: result.order.id, order_number: orderNumber, quotationId } };
|
||||
return {
|
||||
data: {
|
||||
order_id: result.order.id,
|
||||
id: result.order.id,
|
||||
order_number: orderNumber,
|
||||
quotationId,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
interface CreateOrderData {
|
||||
@@ -283,7 +337,8 @@ interface UpdateOrderData {
|
||||
|
||||
export async function updateOrder(id: number, body: UpdateOrderData) {
|
||||
const existing = await prisma.orders.findUnique({ where: { id } });
|
||||
if (!existing) return { error: 'Objednávka nenalezena', status: 404 } as const;
|
||||
if (!existing)
|
||||
return { error: "Objednávka nenalezena", status: 404 } as const;
|
||||
|
||||
const currentStatus = existing.status as string;
|
||||
|
||||
@@ -292,23 +347,41 @@ export async function updateOrder(id: number, body: UpdateOrderData) {
|
||||
const newStatus = String(body.status);
|
||||
const allowed = VALID_TRANSITIONS[currentStatus] || [];
|
||||
if (!allowed.includes(newStatus)) {
|
||||
return { error: `Neplatný přechod stavu z "${currentStatus}" na "${newStatus}"`, status: 400 } as const;
|
||||
return {
|
||||
error: `Neplatný přechod stavu z "${currentStatus}" na "${newStatus}"`,
|
||||
status: 400,
|
||||
} as const;
|
||||
}
|
||||
}
|
||||
|
||||
const data: Record<string, unknown> = { modified_at: new Date() };
|
||||
const strFields = ['order_number', 'customer_order_number', 'status', 'currency', 'language', 'scope_title', 'scope_description', 'notes'];
|
||||
const strFields = [
|
||||
"order_number",
|
||||
"customer_order_number",
|
||||
"status",
|
||||
"currency",
|
||||
"language",
|
||||
"scope_title",
|
||||
"scope_description",
|
||||
"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.customer_id !== undefined)
|
||||
data.customer_id = body.customer_id ? Number(body.customer_id) : null;
|
||||
if (body.vat_rate !== undefined) data.vat_rate = Number(body.vat_rate);
|
||||
if (body.apply_vat !== undefined) data.apply_vat = body.apply_vat === true || body.apply_vat === 1 || body.apply_vat === '1';
|
||||
if (body.apply_vat !== undefined)
|
||||
data.apply_vat =
|
||||
body.apply_vat === true || body.apply_vat === 1 || body.apply_vat === "1";
|
||||
|
||||
await prisma.orders.update({ where: { id }, data });
|
||||
|
||||
// Sync project_number when order_number changes (matching PHP)
|
||||
if (body.order_number !== undefined && String(body.order_number) !== existing.order_number) {
|
||||
if (
|
||||
body.order_number !== undefined &&
|
||||
String(body.order_number) !== existing.order_number
|
||||
) {
|
||||
await prisma.projects.updateMany({
|
||||
where: { order_id: id },
|
||||
data: { project_number: String(body.order_number) },
|
||||
@@ -318,9 +391,9 @@ export async function updateOrder(id: number, body: UpdateOrderData) {
|
||||
// Sync project status when order status changes (matching PHP)
|
||||
if (body.status !== undefined && String(body.status) !== currentStatus) {
|
||||
const statusMap: Record<string, string> = {
|
||||
v_realizaci: 'aktivni',
|
||||
dokoncena: 'dokonceny',
|
||||
stornovana: 'zruseny',
|
||||
v_realizaci: "aktivni",
|
||||
dokoncena: "dokonceny",
|
||||
stornovana: "zruseny",
|
||||
};
|
||||
const projectStatus = statusMap[String(body.status)];
|
||||
if (projectStatus) {
|
||||
@@ -337,9 +410,14 @@ export async function updateOrder(id: number, body: UpdateOrderData) {
|
||||
await tx.order_items.deleteMany({ where: { order_id: id } });
|
||||
await tx.order_items.createMany({
|
||||
data: (body.items as OrderItemInput[]).map((item, i) => ({
|
||||
order_id: id, description: item.description ?? null, item_description: item.item_description ?? null,
|
||||
quantity: item.quantity ?? 1, unit: item.unit ?? null, unit_price: item.unit_price ?? 0,
|
||||
is_included_in_total: item.is_included_in_total !== false, position: item.position ?? i,
|
||||
order_id: id,
|
||||
description: item.description ?? null,
|
||||
item_description: item.item_description ?? null,
|
||||
quantity: item.quantity ?? 1,
|
||||
unit: item.unit ?? null,
|
||||
unit_price: item.unit_price ?? 0,
|
||||
is_included_in_total: item.is_included_in_total !== false,
|
||||
position: item.position ?? i,
|
||||
})),
|
||||
});
|
||||
}
|
||||
@@ -347,7 +425,11 @@ export async function updateOrder(id: number, body: UpdateOrderData) {
|
||||
await tx.order_sections.deleteMany({ where: { order_id: id } });
|
||||
await tx.order_sections.createMany({
|
||||
data: (body.sections as OrderSectionInput[]).map((s, i) => ({
|
||||
order_id: id, title: s.title ?? null, title_cz: s.title_cz ?? null, content: s.content ?? null, position: s.position ?? i,
|
||||
order_id: id,
|
||||
title: s.title ?? null,
|
||||
title_cz: s.title_cz ?? null,
|
||||
content: s.content ?? null,
|
||||
position: s.position ?? i,
|
||||
})),
|
||||
});
|
||||
}
|
||||
@@ -359,7 +441,8 @@ export async function updateOrder(id: number, body: UpdateOrderData) {
|
||||
|
||||
export async function deleteOrder(id: number) {
|
||||
const existing = await prisma.orders.findUnique({ where: { id } });
|
||||
if (!existing) return { error: 'Objednávka nenalezena', status: 404 } as const;
|
||||
if (!existing)
|
||||
return { error: "Objednávka nenalezena", status: 404 } as const;
|
||||
|
||||
// Clear quotation back-reference (matching PHP)
|
||||
await prisma.quotations.updateMany({
|
||||
@@ -368,10 +451,15 @@ export async function deleteOrder(id: number) {
|
||||
});
|
||||
|
||||
// Delete linked project and its notes (matching PHP)
|
||||
const linkedProjects = await prisma.projects.findMany({ where: { order_id: id }, select: { id: true } });
|
||||
const linkedProjects = await prisma.projects.findMany({
|
||||
where: { order_id: id },
|
||||
select: { id: true },
|
||||
});
|
||||
if (linkedProjects.length > 0) {
|
||||
const projectIds = linkedProjects.map(p => p.id);
|
||||
await prisma.project_notes.deleteMany({ where: { project_id: { in: projectIds } } });
|
||||
const projectIds = linkedProjects.map((p) => p.id);
|
||||
await prisma.project_notes.deleteMany({
|
||||
where: { project_id: { in: projectIds } },
|
||||
});
|
||||
await prisma.projects.deleteMany({ where: { order_id: id } });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user