57 lines
2.8 KiB
TypeScript
57 lines
2.8 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import test from "node:test"
|
|
import { tenants } from "../db/schema"
|
|
import { PgDialect } from "drizzle-orm/pg-core"
|
|
import { assertOutgoingEInvoiceRequirements, attachZugferdToPdf, checkOutgoingEInvoice, EInvoiceRequirementsError } from "../src/modules/einvoice/outgoingZugferd.service"
|
|
|
|
const invoice = { type: "invoices", documentNumber: "RE-1", payment_type: "sepa", rows: [] }
|
|
function serverFor(businessInfo: any) {
|
|
const tenantIds: number[] = []
|
|
let otherReads = 0
|
|
const server = { db: { select: () => ({ from: (table: any) => {
|
|
const rows = table === tenants ? [{ businessInfo }] : []
|
|
if (table !== tenants) otherReads++
|
|
const query: any = {
|
|
where: (condition: any) => {
|
|
if (table === tenants) tenantIds.push(new PgDialect().sqlToQuery(condition).params[0] as number)
|
|
return query
|
|
},
|
|
limit: async () => rows,
|
|
then: (resolve: any, reject: any) => Promise.resolve(rows).then(resolve, reject),
|
|
}
|
|
return query
|
|
} }) } } as any
|
|
return { server, tenantIds, otherReads: () => otherReads }
|
|
}
|
|
|
|
for (const businessInfo of [undefined, {}, { outgoingEInvoiceEnabled: false }, { outgoingEInvoiceEnabled: "true" }]) {
|
|
test(`disabled/legacy tenant skips e-invoice requirements: ${JSON.stringify(businessInfo)}`, async () => {
|
|
const { server, tenantIds, otherReads } = serverFor(businessInfo)
|
|
assert.deepEqual(await checkOutgoingEInvoice(server, 8, invoice), [])
|
|
await assertOutgoingEInvoiceRequirements(server, 8, invoice)
|
|
const pdf = Buffer.from("original PDF bytes")
|
|
const result = await attachZugferdToPdf(server, 8, invoice, pdf)
|
|
assert.equal(result.pdf, pdf)
|
|
assert.equal(result.einvoice, false)
|
|
assert.deepEqual(result.issues, [])
|
|
assert.deepEqual(tenantIds, [8, 8, 8])
|
|
assert.equal(otherReads(), 0)
|
|
})
|
|
}
|
|
|
|
test("enabled tenant still blocks incomplete invoices before finalization and embedding", async () => {
|
|
const { server, tenantIds } = serverFor({ outgoingEInvoiceEnabled: true })
|
|
const issues = await checkOutgoingEInvoice(server, 9, invoice)
|
|
assert.ok(issues.some(issue => issue.type === "breaking"))
|
|
await assert.rejects(assertOutgoingEInvoiceRequirements(server, 9, invoice), EInvoiceRequirementsError)
|
|
await assert.rejects(attachZugferdToPdf(server, 9, invoice, Buffer.from("pdf")), EInvoiceRequirementsError)
|
|
assert.ok(tenantIds.every(id => id === 9))
|
|
})
|
|
|
|
test("document payload cannot disable the tenant setting", async () => {
|
|
const { server } = serverFor({ outgoingEInvoiceEnabled: true })
|
|
await assert.rejects(assertOutgoingEInvoiceRequirements(server, 8, {
|
|
...invoice, businessInfo: { outgoingEInvoiceEnabled: false }, outgoingEInvoiceEnabled: false,
|
|
}), EInvoiceRequirementsError)
|
|
})
|