- Add `external_number NVARCHAR(100)` column to the `manifest` table. - Thread `externalNumber` through manifestDoc and delivery SQL (list, header, create, update). - Include `externalNumber` in shapeSummary and quote/order/delivery create/update/list/getById handlers (optional, clearable on update). - Update README type definitions and regenerate API/schema docs.
147 lines
4.5 KiB
JavaScript
147 lines
4.5 KiB
JavaScript
import {
|
|
DELETE_LINES_BY_MANIFEST,
|
|
DELIVERY_TYPE,
|
|
GET_DELIVERY_HEADER,
|
|
UPDATE_MANIFEST,
|
|
} from '../../sql/delivery.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 { loadDelivery, shapeSummary } from './helpers.js';
|
|
import { assertSupplier } from './writeLines.js';
|
|
|
|
export default function update(pool) {
|
|
const access = resolveSelectedCompanyAccess(pool);
|
|
const ensureTax = taxEnsure(pool);
|
|
|
|
/**
|
|
* Updates a delivery's header and/or lines.
|
|
*
|
|
* @param {number} googleAuthId - The id of the Google auth user.
|
|
* @param {number} id - The id of the delivery to update.
|
|
* @param {object} patch - The update patch.
|
|
* @param {string} [patch.number] - The new delivery number.
|
|
* @param {string} [patch.date] - The new delivery date.
|
|
* @param {number} [patch.supplierId] - The new supplier id.
|
|
* @param {string} [patch.externalNumber] - The new external (supplier) reference number.
|
|
* @param {Array<object>} [patch.lines] - The new delivery lines.
|
|
* @returns {Promise<{id: number, number: string, type: string, entityId: number, externalNumber: string|null, date: string, lines: Array<object>}>} The updated delivery.
|
|
* @throws {Error} If the delivery is not found.
|
|
*
|
|
* @example
|
|
* const delivery = await store.delivery.update(42, 5, { number: 'D-2', lines: [...] });
|
|
* // => { id, number, type, entityId, date, lines: [...] }
|
|
*/
|
|
return async (googleAuthId, id, patch = {}) => {
|
|
if (!Number.isFinite(id)) {
|
|
throw new Error('Delivery not found');
|
|
}
|
|
|
|
const hasNumber = Object.prototype.hasOwnProperty.call(patch, 'number');
|
|
const hasDate = Object.prototype.hasOwnProperty.call(patch, 'date');
|
|
const hasSupplierId = Object.prototype.hasOwnProperty.call(
|
|
patch,
|
|
'supplierId',
|
|
);
|
|
const hasExternalNumber = Object.prototype.hasOwnProperty.call(patch, 'externalNumber');
|
|
const hasLines = Object.prototype.hasOwnProperty.call(patch, 'lines');
|
|
|
|
if (!hasNumber && !hasDate && !hasSupplierId && !hasExternalNumber && !hasLines) {
|
|
throw new Error('Invalid delivery');
|
|
}
|
|
|
|
let normalizedLines;
|
|
if (hasLines) {
|
|
normalizedLines = normalizeLines(patch.lines);
|
|
}
|
|
|
|
const { companyId, rootCategoryId } = 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', DELIVERY_TYPE)
|
|
.query(GET_DELIVERY_HEADER);
|
|
|
|
const current = existing.recordset[0];
|
|
if (!current) {
|
|
throw new Error('Delivery not found');
|
|
}
|
|
|
|
const summary = shapeSummary(current);
|
|
|
|
let number = summary.number;
|
|
if (hasNumber) {
|
|
number = patch.number?.trim?.() ? patch.number.trim() : null;
|
|
if (!number) {
|
|
throw new Error('Invalid delivery');
|
|
}
|
|
}
|
|
|
|
let date = summary.date;
|
|
if (hasDate) {
|
|
date = parseDate(patch.date);
|
|
if (!date) {
|
|
throw new Error('Invalid delivery');
|
|
}
|
|
}
|
|
|
|
let supplierId = summary.supplierId;
|
|
if (hasSupplierId) {
|
|
supplierId = await assertSupplier(
|
|
pool,
|
|
tx,
|
|
companyId,
|
|
patch.supplierId ?? 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', DELIVERY_TYPE)
|
|
.input('number', number)
|
|
.input('entityId', supplierId)
|
|
.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,
|
|
rootCategoryId,
|
|
lines: normalizedLines,
|
|
ensureTax,
|
|
});
|
|
}
|
|
|
|
const delivery = await loadDelivery(pool, tx, companyId, id);
|
|
await tx.commit();
|
|
return delivery;
|
|
} catch (err) {
|
|
try {
|
|
await tx.rollback();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
throw err;
|
|
}
|
|
};
|
|
}
|