142 lines
4.4 KiB
JavaScript
142 lines
4.4 KiB
JavaScript
import {
|
|
DELETE_LINES_BY_MANIFEST,
|
|
GET_MANIFEST_HEADER,
|
|
ORDER_TYPE,
|
|
UPDATE_MANIFEST,
|
|
} from '../../sql/manifestDoc.js';
|
|
import request from '../request.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';
|
|
|
|
export default function update(pool) {
|
|
const access = resolveSelectedCompanyAccess(pool);
|
|
const ensureTax = taxEnsure(pool);
|
|
|
|
/**
|
|
* Updates an order's header and/or lines.
|
|
*
|
|
* @param {number} googleAuthId - The id of the Google auth user.
|
|
* @param {number} id - The id of the order to update.
|
|
* @param {object} patch - The update patch.
|
|
* @param {string} [patch.number] - The new order number.
|
|
* @param {string} [patch.date] - The new order date.
|
|
* @param {number} [patch.entityId] - The new customer entity id.
|
|
* @param {string} [patch.externalNumber] - The new external (customer) reference number.
|
|
* @param {Array<object>} [patch.lines] - The new order lines.
|
|
* @returns {Promise<{id: number, number: string, type: string, entityId: number|null, externalNumber: string|null, date: string, lines: Array<object>}>} The updated order.
|
|
* @throws {Error} If the order is not found.
|
|
*
|
|
* @example
|
|
* const order = await store.order.update(42, 5, { number: 'B-2026-002', lines: [...] });
|
|
* // => { id, number, type, entityId, date, lines: [...] }
|
|
*/
|
|
return async (googleAuthId, id, patch = {}) => {
|
|
if (!Number.isFinite(id)) {
|
|
throw new Error('Order not found');
|
|
}
|
|
|
|
const hasNumber = Object.prototype.hasOwnProperty.call(patch, 'number');
|
|
const hasDate = Object.prototype.hasOwnProperty.call(patch, 'date');
|
|
const hasEntityId = Object.prototype.hasOwnProperty.call(patch, 'entityId');
|
|
const hasExternalNumber = Object.prototype.hasOwnProperty.call(patch, 'externalNumber');
|
|
const hasLines = Object.prototype.hasOwnProperty.call(patch, 'lines');
|
|
|
|
if (!hasNumber && !hasDate && !hasEntityId && !hasExternalNumber && !hasLines) {
|
|
throw new Error('Invalid order');
|
|
}
|
|
|
|
let normalizedLines;
|
|
if (hasLines) {
|
|
normalizedLines = normalizeLines(patch.lines);
|
|
}
|
|
|
|
const { companyId } = await access(googleAuthId);
|
|
|
|
const tx = pool.transaction();
|
|
await tx.begin();
|
|
|
|
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];
|
|
if (!current) {
|
|
throw new Error('Order not found');
|
|
}
|
|
|
|
const summary = shapeSummary(ORDER_TYPE, current);
|
|
|
|
let number = summary.number;
|
|
if (hasNumber) {
|
|
number = patch.number?.trim?.() ? patch.number.trim() : null;
|
|
if (!number) {
|
|
throw new Error('Invalid order');
|
|
}
|
|
}
|
|
|
|
let date = summary.date;
|
|
if (hasDate) {
|
|
date = parseDate(patch.date);
|
|
if (!date) {
|
|
throw new Error('Invalid order');
|
|
}
|
|
}
|
|
|
|
let entityId = summary.entityId;
|
|
if (hasEntityId) {
|
|
entityId = await assertCustomerEntity(
|
|
pool,
|
|
tx,
|
|
companyId,
|
|
patch.entityId ?? null,
|
|
);
|
|
}
|
|
|
|
let externalNumber = summary.externalNumber;
|
|
if (hasExternalNumber) {
|
|
externalNumber = patch.externalNumber?.trim?.()
|
|
? patch.externalNumber.trim()
|
|
: 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);
|
|
|
|
if (hasLines) {
|
|
await request(pool, tx)
|
|
.input('manifestId', id)
|
|
.query(DELETE_LINES_BY_MANIFEST);
|
|
|
|
await insertLines(pool, tx, {
|
|
manifestId: id,
|
|
lines: normalizedLines,
|
|
ensureTax,
|
|
});
|
|
}
|
|
|
|
const doc = await loadDoc(pool, tx, { type: ORDER_TYPE, companyId, id });
|
|
await tx.commit();
|
|
return doc;
|
|
} catch (err) {
|
|
try {
|
|
await tx.rollback();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
throw err;
|
|
}
|
|
};
|
|
}
|