acc_invoices: внутренние проекты (acc_internal_projects), описание, ссылка для оплаты, «Файла Invoice пока нет», строки необязательны, ждёт автор по умолчанию
All checks were successful
ci / test (push) Successful in 52s
ci / deploy (push) Successful in 3m5s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-04 15:30:38 +03:00
parent ade259515f
commit d0a22b8cec
9 changed files with 93 additions and 29 deletions

View File

@@ -35,6 +35,10 @@ const FIELD_SCHEMA = z
invoice_date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).nullable(),
amount: money,
project_customer_id: nullableStr,
internal_project_id: uuid.nullable(),
description: z.string().trim().max(4000).nullable(),
pay_link: z.string().trim().url().max(2000).nullable(),
no_invoice_file: z.boolean(),
jt_job_id: nullableStr,
contract_id: uuid.nullable(),
target_employee: uuid.nullable(),
@@ -136,7 +140,9 @@ export function buildApi(deps: ApiDeps): FastifyInstance {
const a = await need(req);
const row = await load(a, req.params.id, reply);
if (!row) return;
const lines = await getDetails(pool, row.id);
const real = await getDetails(pool, row.id);
// строки не заданы: считаем одной строкой на всю сумму инвойса (в БД не пишется)
const lines = real.length ? real : [{ id: "virtual", virtual: true, line_no: 1, description: row.description ?? null, quantity: null, unit_price: null, amount: row.amount, category_qbo_id: null, category_name: null, contract_detail_id: null }];
return {
invoice: decorate(row, a),
details: lines,
@@ -148,17 +154,18 @@ export function buildApi(deps: ApiDeps): FastifyInstance {
/* ------------------------------------------------------------- создание */
app.post("/api/invoices", async (req, reply) => {
const a = await need(req);
const body = FIELD_SCHEMA.pick({ vendor_name: true, vendor_qbo_id: true, invoice_number: true, invoice_date: true, amount: true, project_customer_id: true, jt_job_id: true, contract_id: true, target_employee: true, need_employee_refund: true, invoice_url: true, payment_url: true, pay_method: true })
const body = FIELD_SCHEMA.pick({ vendor_name: true, vendor_qbo_id: true, invoice_number: true, invoice_date: true, amount: true, project_customer_id: true, internal_project_id: true, description: true, pay_link: true, no_invoice_file: true, jt_job_id: true, contract_id: true, target_employee: true, need_employee_refund: true, invoice_url: true, payment_url: true, pay_method: true })
.extend({ vendor_name: z.string().trim().min(1).max(300), amount: money.default(0) })
.parse(req.body ?? {});
const row = (
await pool.query(
`insert into acc_invoices (id, vendor_name, vendor_qbo_id, invoice_number, invoice_date, amount, project_customer_id, jt_job_id, contract_id,
target_employee, need_employee_refund, invoice_url, payment_url, pay_method, is_draft, author, stage)
values (gen_random_uuid(), $1, $2, $3, coalesce($4::date, current_date), $5, $6, $7, $8, $9, coalesce($10, false), $11, $12, $13, true, $14, 'checking')
target_employee, need_employee_refund, invoice_url, payment_url, pay_method, is_draft, author, stage, internal_project_id, description, pay_link, no_invoice_file)
values (gen_random_uuid(), $1, $2, $3, coalesce($4::date, current_date), $5, $6, $7, $8, $9, coalesce($10, false), $11, $12, $13, true, $14, 'checking', $15, $16, $17, coalesce($18, false))
returning id`,
[body.vendor_name, body.vendor_qbo_id ?? null, body.invoice_number ?? null, body.invoice_date ?? null, body.amount, body.project_customer_id ?? null, body.jt_job_id ?? null, body.contract_id ?? null,
body.target_employee ?? a.personId, body.need_employee_refund ?? null, body.invoice_url ?? null, body.payment_url ?? null, body.pay_method ?? null, a.personId],
body.target_employee ?? a.personId, body.need_employee_refund ?? null, body.invoice_url ?? null, body.payment_url ?? null, body.pay_method ?? null, a.personId,
body.project_customer_id ? null : (body.internal_project_id ?? null), body.description ?? null, body.pay_link ?? null, body.no_invoice_file ?? null],
)
).rows[0];
return reply.code(201).send({ id: row.id });