sqlite
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { CREATE_LINE } from '../providers/mssql/queries/manifestDoc.js';
|
||||
import request from './request.js';
|
||||
import { CREATE_LINE as MSSQL_CREATE_LINE } from '../providers/mssql/queries/manifestDoc.js';
|
||||
import { CREATE_LINE as SQLITE_CREATE_LINE } from '../providers/sqlite3/queries/manifestDoc.js';
|
||||
import makeRunner from './runner.js';
|
||||
|
||||
/**
|
||||
* Normalizes and validates an array of manifest line inputs.
|
||||
@@ -92,35 +93,47 @@ export function shapeLine(row) {
|
||||
* Inserts manifest lines for a document, storing each SKU and taxrate directly,
|
||||
* and ensuring the tax rate exists in the tax catalog.
|
||||
*
|
||||
* @param {object} pool - The mssql connection pool.
|
||||
* @param {object} tx - The transaction.
|
||||
* @param {object} params - The insert parameters.
|
||||
* @param {number} params.manifestId - The manifest id.
|
||||
* @param {Array<object>} params.lines - The normalized lines.
|
||||
* @param {Function} [params.ensureTax] - The tax ensure function.
|
||||
* @param {object} runnerOrPool - The runner or mssql connection pool.
|
||||
* @param {object} txOrParams - The transaction or params object.
|
||||
* @param {object} [maybeParams] - The insert parameters.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function insertLines(
|
||||
pool,
|
||||
tx,
|
||||
{ manifestId, lines, ensureTax },
|
||||
runnerOrPool,
|
||||
txOrParams,
|
||||
maybeParams,
|
||||
) {
|
||||
let runner;
|
||||
let params;
|
||||
if (maybeParams !== undefined) {
|
||||
runner = makeRunner(runnerOrPool, txOrParams);
|
||||
params = maybeParams;
|
||||
} else {
|
||||
runner = makeRunner(runnerOrPool);
|
||||
params = txOrParams;
|
||||
}
|
||||
|
||||
const { manifestId, lines, ensureTax } = params;
|
||||
const isSqlite = runner?.dialect === 'sqlite3';
|
||||
const createLineSql = isSqlite ? SQLITE_CREATE_LINE : MSSQL_CREATE_LINE;
|
||||
|
||||
for (const line of lines) {
|
||||
if (ensureTax) {
|
||||
await ensureTax(line.taxrate, tx);
|
||||
await ensureTax(line.taxrate, runner);
|
||||
}
|
||||
|
||||
await request(pool, tx)
|
||||
.input('manifestId', manifestId)
|
||||
.input('sku', line.sku ?? null)
|
||||
.input('name', line.name ?? null)
|
||||
.input('pu', line.pu ?? 1)
|
||||
.input('amount', line.amount)
|
||||
.input('net', line.net)
|
||||
.input('comment', line.comment)
|
||||
.input('rebate', line.rebate)
|
||||
.input('taxrate', line.taxrate)
|
||||
.query(CREATE_LINE);
|
||||
await runner.query(createLineSql, {
|
||||
manifestId,
|
||||
sku: line.sku ?? null,
|
||||
name: line.name ?? null,
|
||||
pu: line.pu ?? 1,
|
||||
amount: line.amount,
|
||||
net: line.net,
|
||||
comment: line.comment,
|
||||
rebate: line.rebate,
|
||||
taxrate: line.taxrate,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user