u
This commit is contained in:
85
src/store/ledger/listByManifestarticleId.js
Normal file
85
src/store/ledger/listByManifestarticleId.js
Normal file
@@ -0,0 +1,85 @@
|
||||
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,
|
||||
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.
|
||||
*
|
||||
* @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 }.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<Array<{id: number, manifestarticle_id: number, 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, 'PAYMENT');
|
||||
* // => [{ id, manifestarticle_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 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 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,
|
||||
});
|
||||
|
||||
return res.rows.map(shape);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user