sqlite
This commit is contained in:
@@ -1,18 +1,30 @@
|
||||
import {
|
||||
DELETE_LINES_BY_MANIFEST,
|
||||
GET_MANIFEST_HEADER,
|
||||
ORDER_TYPE,
|
||||
UPDATE_MANIFEST,
|
||||
DELETE_LINES_BY_MANIFEST as MSSQL_DELETE_LINES,
|
||||
GET_MANIFEST_HEADER as MSSQL_GET_HEADER,
|
||||
ORDER_TYPE as MSSQL_ORDER_TYPE,
|
||||
UPDATE_MANIFEST as MSSQL_UPDATE_MANIFEST,
|
||||
} from '../../providers/mssql/queries/manifestDoc.js';
|
||||
import request from '../request.js';
|
||||
import {
|
||||
DELETE_LINES_BY_MANIFEST as SQLITE_DELETE_LINES,
|
||||
GET_MANIFEST_HEADER as SQLITE_GET_HEADER,
|
||||
ORDER_TYPE as SQLITE_ORDER_TYPE,
|
||||
UPDATE_MANIFEST as SQLITE_UPDATE_MANIFEST,
|
||||
} from '../../providers/sqlite3/queries/manifestDoc.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import taxEnsure from '../tax/ensure.js';
|
||||
import { insertLines, normalizeLines, parseDate } from '../manifestLines.js';
|
||||
import { assertCustomerEntity, loadDoc, shapeSummary } from '../manifestDoc/helpers.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function update(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const ensureTax = taxEnsure(pool);
|
||||
export default function update(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const ensureTax = taxEnsure(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
|
||||
const deleteLinesSql = isSqlite ? SQLITE_DELETE_LINES : MSSQL_DELETE_LINES;
|
||||
const getHeaderSql = isSqlite ? SQLITE_GET_HEADER : MSSQL_GET_HEADER;
|
||||
const orderType = isSqlite ? SQLITE_ORDER_TYPE : MSSQL_ORDER_TYPE;
|
||||
const updateManifestSql = isSqlite ? SQLITE_UPDATE_MANIFEST : MSSQL_UPDATE_MANIFEST;
|
||||
|
||||
/**
|
||||
* Updates an order's header and/or lines.
|
||||
@@ -25,6 +37,7 @@ export default function update(pool) {
|
||||
* @param {number} [patch.entityId] - The new customer entity id.
|
||||
* @param {string} [patch.externalNumber] - The new external (customer) reference number.
|
||||
* @param {Array<{sku?: string, name?: string, pu?: number, amount: number, net: number, rebate?: number, taxrate: number, comment?: string|null}>} [patch.lines] - The new order lines.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, number: string, type: string, entityId: number|null, externalNumber: string|null, date: string, lines: Array<{id: number, sku: string|null, name: string|null, pu: number, amount: number, net: number, rebate: number, taxrate: number, comment: string|null}>}>} The updated order.
|
||||
* @throws {Error} If the order is not found.
|
||||
*
|
||||
@@ -32,7 +45,7 @@ export default function update(pool) {
|
||||
* const order = await store.order.update(42, 5, { number: 'B-2026-002', lines: [...] });
|
||||
* // => { id, number, type, entityId, date, lines: [...] }
|
||||
*/
|
||||
return async (googleAuthId, id, patch = {}) => {
|
||||
return async (googleAuthId, id, patch = {}, tx) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Order not found');
|
||||
}
|
||||
@@ -52,24 +65,21 @@ export default function update(pool) {
|
||||
normalizedLines = normalizeLines(patch.lines);
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
const runUpdate = async (runner) => {
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
const existing = await runner.query(getHeaderSql, {
|
||||
id,
|
||||
companyId,
|
||||
type: orderType,
|
||||
});
|
||||
|
||||
try {
|
||||
const existing = await request(pool, tx)
|
||||
.input('id', id)
|
||||
.input('companyId', companyId)
|
||||
.input('type', ORDER_TYPE)
|
||||
.query(GET_MANIFEST_HEADER);
|
||||
|
||||
const current = existing.recordset[0];
|
||||
const current = existing.rows[0];
|
||||
if (!current) {
|
||||
throw new Error('Order not found');
|
||||
}
|
||||
|
||||
const summary = shapeSummary(ORDER_TYPE, current);
|
||||
const summary = shapeSummary(orderType, current);
|
||||
|
||||
let number = summary.number;
|
||||
if (hasNumber) {
|
||||
@@ -90,8 +100,7 @@ export default function update(pool) {
|
||||
let entityId = summary.entityId;
|
||||
if (hasEntityId) {
|
||||
entityId = await assertCustomerEntity(
|
||||
pool,
|
||||
tx,
|
||||
runner,
|
||||
companyId,
|
||||
patch.entityId ?? null,
|
||||
);
|
||||
@@ -104,38 +113,39 @@ export default function update(pool) {
|
||||
: null;
|
||||
}
|
||||
|
||||
await request(pool, tx)
|
||||
.input('id', id)
|
||||
.input('companyId', companyId)
|
||||
.input('type', ORDER_TYPE)
|
||||
.input('number', number)
|
||||
.input('entityId', entityId)
|
||||
.input('externalNumber', externalNumber)
|
||||
.input('date', date)
|
||||
.query(UPDATE_MANIFEST);
|
||||
await runner.query(updateManifestSql, {
|
||||
id,
|
||||
companyId,
|
||||
type: orderType,
|
||||
number,
|
||||
entityId,
|
||||
externalNumber,
|
||||
date,
|
||||
});
|
||||
|
||||
if (hasLines) {
|
||||
await request(pool, tx)
|
||||
.input('manifestId', id)
|
||||
.query(DELETE_LINES_BY_MANIFEST);
|
||||
await runner.query(deleteLinesSql, {
|
||||
manifestId: id,
|
||||
});
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
await insertLines(runner, {
|
||||
manifestId: id,
|
||||
lines: normalizedLines,
|
||||
ensureTax,
|
||||
});
|
||||
}
|
||||
|
||||
const doc = await loadDoc(pool, tx, { type: ORDER_TYPE, companyId, id });
|
||||
await tx.commit();
|
||||
const doc = await loadDoc(runner, { type: orderType, companyId, id });
|
||||
return doc;
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return runUpdate(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runUpdate);
|
||||
}
|
||||
return runUpdate(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user