This commit is contained in:
seb
2026-10-13 22:23:45 +02:00
parent 8a626b17fc
commit 3256ab233d
4 changed files with 39 additions and 6 deletions

View File

@@ -1,8 +1,10 @@
import {
COUNT_MANIFESTS as MSSQL_COUNT_MANIFESTS,
GET_MANIFEST_LINES_BY_MANIFEST_IDS as MSSQL_LINES_BY_MANIFEST_IDS,
ORDER_TYPE as MSSQL_ORDER_TYPE,
} from '../../providers/mssql/queries/manifestDoc.js';
import {
COUNT_MANIFESTS as SQLITE_COUNT_MANIFESTS,
GET_MANIFEST_LINES_BY_MANIFEST_IDS as SQLITE_LINES_BY_MANIFEST_IDS,
ORDER_TYPE as SQLITE_ORDER_TYPE,
} from '../../providers/sqlite3/queries/manifestDoc.js';
@@ -23,6 +25,9 @@ export default function list(provider) {
? SQLITE_GET_SHIPMENT_PREFIX
: MSSQL_GET_SHIPMENT_PREFIX;
const orderType = isSqlite ? SQLITE_ORDER_TYPE : MSSQL_ORDER_TYPE;
const sqlCountManifests = isSqlite
? SQLITE_COUNT_MANIFESTS
: MSSQL_COUNT_MANIFESTS;
/**
* Lists the orders of the user's selected company.
@@ -36,11 +41,11 @@ export default function list(provider) {
* @param {boolean} [opts.onlyUndelivered=false] - When true, returns only undelivered orders.
* @param {number} [opts.seekMonths] - Negative integer for cutoff months ago (e.g. -1).
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{orders: Array<{id: number, number: string, type: string, entityId: number|null, externalNumber: string|null, date: string, createdAt: string|null, lines: Array<{id: number, sku: string|null, name: string|null, pu: number, amount: number, net: number, rebate: number, taxrate: number, comment: string|null}>}>, total: number, offset: number, limit: number}|Array<object>>} The orders.
* @returns {Promise<{orders: Array<{id: number, number: string, type: string, entityId: number|null, externalNumber: string|null, date: string, createdAt: string|null, lines: Array<{id: number, sku: string|null, name: string|null, pu: number, amount: number, net: number, rebate: number, taxrate: number, comment: string|null}>}>, total: number, unfilteredTotal: number, offset: number, limit: number}|Array<object>>} The orders.
*
* @example
* const result = await store.order.list(42, { offset: 0, limit: 50, search: '1002', onlyUnpaid: true, onlyUndelivered: true, seekMonths: -1 });
* // => { orders: [...], total: 10, offset: 0, limit: 50 }
* // => { orders: [...], total: 10, unfilteredTotal: 42, offset: 0, limit: 50 }
*
* const orders = await store.order.list(42);
* // => [{ id, number, type, entityId, externalNumber, date, createdAt, lines: [...] }]
@@ -194,22 +199,40 @@ export default function list(provider) {
`;
}
const hasUserFilters = (
opts?.seekMonths !== undefined ||
(typeof opts?.search === 'string' && opts.search.trim() !== '') ||
opts?.onlyUnpaid === true ||
opts?.onlyUndelivered === true
);
let manifestRows;
let total = 0;
let unfilteredTotal = 0;
if (isPaged) {
params.offset = offset;
params.limit = limit;
const [countResult, listResult] = await Promise.all([
const [countResult, listResult, unfilteredCountResult] = await Promise.all([
runner.query(sqlCount, params),
runner.query(sqlList, params),
hasUserFilters ? runner.query(sqlCountManifests, { companyId, type: orderType }) : null,
]);
total = Number(countResult.rows[0]?.total ?? 0);
unfilteredTotal = hasUserFilters
? Number(unfilteredCountResult?.rows[0]?.total ?? 0)
: total;
manifestRows = listResult.rows;
} else {
const result = await runner.query(sqlList, params);
manifestRows = result.rows;
total = manifestRows.length;
if (hasUserFilters) {
const countRes = await runner.query(sqlCountManifests, { companyId, type: orderType });
unfilteredTotal = Number(countRes.rows[0]?.total ?? 0);
} else {
unfilteredTotal = total;
}
}
const manifestIds = manifestRows.map((r) => r.id);
@@ -246,6 +269,7 @@ export default function list(provider) {
return {
orders,
total,
unfilteredTotal,
offset,
limit,
};