94 lines
3.6 KiB
JavaScript
94 lines
3.6 KiB
JavaScript
import * as mssqlQueries from '../../providers/mssql/queries/ledger.js';
|
|
import * as sqliteQueries from '../../providers/sqlite3/queries/ledger.js';
|
|
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
|
import makeRunner from '../runner.js';
|
|
|
|
function shape(row) {
|
|
return {
|
|
id: row.id,
|
|
manifestarticle_id: row.manifestarticle_id,
|
|
warehouse_id: row.warehouse_id ?? null,
|
|
sku: row.sku ?? null,
|
|
transaction_type: row.transaction_type,
|
|
amount: row.amount,
|
|
transaction_text: row.transaction_text,
|
|
hash_version: row.hash_version,
|
|
prev_hash: row.prev_hash,
|
|
created_at:
|
|
row.created_at instanceof Date
|
|
? row.created_at.toISOString()
|
|
: new Date(row.created_at).toISOString(),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Lists ledger entries for a specific manifest article line, optionally filtered by transaction type and/or warehouse.
|
|
*
|
|
* @param {number} googleAuthId - The id of the Google auth user.
|
|
* @param {number} manifestarticleId - The id of the manifest article line.
|
|
* @param {string|object} [optionsOrType] - Transaction type string or options object { transaction_type?: string, transactionType?: string, warehouse_id?: number, warehouseId?: number }.
|
|
* @param {object} [tx] - Optional transaction to run within.
|
|
* @returns {Promise<Array<{id: number, manifestarticle_id: number, warehouse_id: number|null, sku: string|null, transaction_type: string, amount: number, transaction_text: string, hash_version: number, prev_hash: string, created_at: string}>>}
|
|
* @throws {Error} If user not authorized, company not selected, or manifest article not found.
|
|
*
|
|
* @example
|
|
* const entries = await store.ledger.listByManifestarticleId(googleAuthId, 42, 'DELIVERY');
|
|
* // => [{ id, manifestarticle_id, warehouse_id, sku, transaction_type, amount, transaction_text, hash_version, prev_hash, created_at }]
|
|
*/
|
|
export default function listByManifestarticleId(provider) {
|
|
const access = resolveSelectedCompanyAccess(provider);
|
|
const isSqlite = provider?.dialect === 'sqlite3';
|
|
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
|
|
|
return async (googleAuthId, manifestarticleId, optionsOrType, tx) => {
|
|
let transactionType = null;
|
|
let warehouseId = null;
|
|
let explicitTx = tx;
|
|
|
|
if (typeof optionsOrType === 'string') {
|
|
transactionType = optionsOrType.trim() || null;
|
|
} else if (typeof optionsOrType === 'object' && optionsOrType !== null) {
|
|
if (typeof optionsOrType.query === 'function') {
|
|
explicitTx = optionsOrType;
|
|
} else {
|
|
const rawType =
|
|
optionsOrType.transaction_type ?? optionsOrType.transactionType;
|
|
if (typeof rawType === 'string') {
|
|
transactionType = rawType.trim() || null;
|
|
}
|
|
const rawWarehouse =
|
|
optionsOrType.warehouse_id ?? optionsOrType.warehouseId;
|
|
if (rawWarehouse != null) {
|
|
warehouseId = Number(rawWarehouse) || null;
|
|
}
|
|
}
|
|
}
|
|
|
|
const id = Number(manifestarticleId);
|
|
if (!Number.isInteger(id) || id <= 0) {
|
|
throw new Error('Invalid manifest article id');
|
|
}
|
|
|
|
const runner = makeRunner(provider, explicitTx);
|
|
const { companyId } = await access(googleAuthId, runner);
|
|
|
|
const check = await runner.query(queries.CHECK_MANIFESTARTICLE_ACCESS, {
|
|
manifestarticleId: id,
|
|
companyId,
|
|
});
|
|
|
|
if (!check.rows[0]) {
|
|
throw new Error('Manifest article not found');
|
|
}
|
|
|
|
const res = await runner.query(queries.LIST_BY_MANIFESTARTICLE, {
|
|
manifestarticleId: id,
|
|
companyId,
|
|
transactionType: transactionType || null,
|
|
warehouseId: warehouseId || null,
|
|
});
|
|
|
|
return res.rows.map(shape);
|
|
};
|
|
}
|