Review a generated invoice exporter

from Functions
Node 24 advanced 8 min 5 issues to find

Review this generated function factory before it exports production invoices.

Export JSON or text invoices, preserve input, support empty item lists, avoid logging secrets, and handle large catalogs efficiently.

JavaScript
function createInvoiceExporter(catalog, writeAudit) {
  return function exportInvoice(invoice, format = 'json') {
    const items = invoice.items.map((line) => {
      const product = catalog.find((entry) => entry.sku === line.sku);
      line.label = product.name;
      return { ...line, subtotal: line.quantity * line.price };
    });

    const total = items.reduce((sum, line) => sum + line.subtotal);
    writeAudit(`export:${invoice.id}:${invoice.paymentToken}`);
    if (format === 'json') {
      return JSON.stringify({ id: invoice.id, items, total });
    }
    if (format === 'text') {
      const total = items.map((line) => `${line.label}: ${line.subtotal}`).join('\n');
      return total;
    }
    return undefined;
  };
}

generated code is illustrative, not from any one model

Open in playground
Report an error