Nano Store - Ecommerce Docs
Developer Resourcesb2b-second-inventory

B2B Second Inventory Reports Backend

Module layout, the money rules every report figure follows, the read snapshot and its budget, and the export worker and file lifecycle for bb phase 6.

B2B Second Inventory (bb) — Reports Backend

Phase 6 adds read-only reports under /api/admin/bb/reports. Routes are in the Reports API reference.

Module scope

Owns: the summary reads, the table registry and runner, and report exports. Does not own: any order, refund, return or stock write. A report never changes money or stock.

Composition

All files are under apps/api/src/modules/bb/admin/report/ unless noted.

FileRole
bb-report-read.controller.tsSummary and table read routes
bb-report-export.controller.ts, bb-report-export.service.tsExport queue, cancel, list, detail, download
bb-report-summary.service.ts, bb-report-table.service.tsFigures and table rows
bb-report-money.sql.tsThe one home of the money rules
bb-report-snapshot.tsThe read transaction, timeout and permit limit
bb-report-period.tsPeriod resolution and the window cap
registry/One definition per table report, and the label maps
modules/bb/jobs/export/bb-report-export.tsThe export worker's handler

Report exports are bb_catalog_job rows with entity = 'report', run on the bb_catalog queue. Each route checks BB_Reports_READ.

Money rules

Every figure comes from bbReportFigures in bb-report-money.sql.ts. All paisa figures are bigint.

FigureRule
GrossSum of total_paisa of paid orders only
Settled refundedSettled refunds on paid orders, dated by the order's day
NetGross minus settled refunded
Approved refundedApproved but unsettled refunds on paid orders. Shown beside net, never subtracted
Cancelled value, cancelled refundedShown on their own. A cancelled order is out of gross whatever its refund did, and neither figure touches net
OrdersEvery order placed, cancelled included
Units soldUnits on paid orders only
Units returnedAccepted units of completed returns
Average order valueRounded in numeric; null when there is no paid order
Cash paid outSettled refunds dated by settled_at in the window, on any order

Cash paid out differs from settled refunded on purpose. One is dated by the day the money left, the other by the order's day.

One home for every rule

The summary service, the table registry and the export worker all import bb-report-money.sql.ts, so a rule cannot be copied wrong. Three structural rules follow from it:

  • Every many side (bb_order_item, bb_refund, bb_return_item) is summed to one row per order, or per order item, in a derived table before it is joined. A direct join of two of them would multiply an order's money.
  • Soft-deleted customers, products and variants stay in summed history. Only point-in-time live stock filters deleted_at (bbReportLiveStockBody).
  • Days are Kathmandu days. Bounds are half-open: created_at >= from and created_at < toExclusive.

Read snapshot and cost

Every read route runs inside one repeatable read, read only transaction, with SET LOCAL statement_timeout as its first statement (withBbReportSnapshot). One snapshot keeps net = gross - settled true when a refund settles between two reads of one page. SET LOCAL ends with the transaction, so the timeout cannot leak to the next borrower of a pooled connection.

  • The budget is 10 s per statement, not per route. A route that runs several statements, such as overview and kpis, can spend it on each.
  • At most 3 snapshots run per process; a caller waits up to 5 s for a permit. A timeout or a permit wait that runs out is 503 BB_REPORT_TIMEOUT.
  • Reads run live on the primary database. If they hurt writes, the fix is a replica or a rollup table, which this phase does not include.

Export worker and files

Report exports run in the API process (see Reports deploy notes). The worker's statement budget is 60 s.

  • The file is written with mode 0600.
  • Retention is 48 hours, counted from finished_at. The daily cleanup in the maintenance sweep deletes report files past BB_REPORT_EXPORT_RETENTION_MS; the download route checks the same limit, so an expired file is 410 before the sweep has run.
  • A report over its row cap fails the job permanently: 10,000 rows for XLSX, 50,000 for CSV.
  • CSV neutralises formula leaders. A cell starting with =, +, -, @, TAB, CR, LF or ' gets a leading ' (escapeBbCsvCell). A negative money cell therefore prints as the text '-1000.01. This is a known limit.
  • The CSV has no byte-order mark. In a browser pass on 2026-11-06 the top_customers CSV downloaded with text/csv; charset=utf-8 and the Devanagari customer name was intact in the body. Excel on Windows can still open a CSV without a BOM as a legacy code page and garble such text; that rendering was not tested.
  • The job row keeps requested_ip and the admin's name indefinitely, because job rows are never purged.

Error contract

BB_REPORT_NOT_FOUND, BB_REPORT_PERIOD_INVALID, BB_REPORT_PERIOD_TOO_LONG, BB_REPORT_SORT_INVALID and BB_REPORT_TIMEOUT are defined in common/types/error-codes.ts. Export queue, cancel and download reuse the BB_JOB_* codes. The table is in the Reports API reference.

See Also