u
This commit is contained in:
76
src/store/ledger/listBySku.js
Normal file
76
src/store/ledger/listBySku.js
Normal file
@@ -0,0 +1,76 @@
|
||||
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 given article SKU, optionally filtered by transaction type.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {string} sku - The article SKU.
|
||||
* @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 SKU invalid.
|
||||
*
|
||||
* @example
|
||||
* const entries = await store.ledger.listBySku(googleAuthId, 'SKU-001', 'DELIVERY');
|
||||
* // => [{ id, manifestarticle_id, sku, transaction_type, amount, transaction_text, hash_version, prev_hash, created_at }]
|
||||
*/
|
||||
export default function listBySku(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const queries = isSqlite ? sqliteQueries : mssqlQueries;
|
||||
|
||||
return async (googleAuthId, sku, 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 trimmedSku = typeof sku === 'string' ? sku.trim() : '';
|
||||
if (!trimmedSku) {
|
||||
throw new Error('Invalid SKU');
|
||||
}
|
||||
|
||||
const runner = makeRunner(provider, explicitTx);
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const res = await runner.query(queries.LIST_BY_SKU, {
|
||||
sku: trimmedSku,
|
||||
companyId,
|
||||
transactionType: transactionType || null,
|
||||
});
|
||||
|
||||
return res.rows.map(shape);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user