import * as mssqlQueries from '../../providers/mssql/queries/delivery.js'; import * as sqliteQueries from '../../providers/sqlite3/queries/delivery.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 { insertAttachments } from './attachments.js'; import { assertSupplier } from './writeLines.js'; import makeRunner from '../runner.js'; export default function update(provider) { const access = resolveSelectedCompanyAccess(provider); const ensureTax = taxEnsure(provider); const isSqlite = provider?.dialect === 'sqlite3'; const queries = isSqlite ? sqliteQueries : mssqlQueries; 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'); const hasFiles = Object.prototype.hasOwnProperty.call(patch, 'files'); if (!hasNumber && !hasDate && !hasSupplierId && !hasExternalNumber && !hasLines && !hasFiles) { throw new Error('Invalid delivery'); } let normalizedLines; if (hasLines) { normalizedLines = normalizeLines(patch.lines); } const { companyId } = await access(googleAuthId); const run = async (runner, tx) => { const existing = await runner(queries.GET_DELIVERY_HEADER, { id, companyId, type: queries.DELIVERY_TYPE, }); const current = existing.rows[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( provider, tx, companyId, patch.supplierId ?? null, ); } let externalNumber = summary.externalNumber; if (hasExternalNumber) { externalNumber = patch.externalNumber?.trim?.() ? patch.externalNumber.trim() : null; } await runner(queries.UPDATE_MANIFEST, { id, companyId, type: queries.DELIVERY_TYPE, number, entityId: supplierId, externalNumber, date, }); if (hasLines) { await runner(queries.DELETE_LINES_BY_MANIFEST, { manifestId: id, }); await insertLines(provider, tx, { manifestId: id, lines: normalizedLines, ensureTax, }); } if (hasFiles) { await insertAttachments( provider, tx, { GET_NEXT_ATTACHMENT_SORT: queries.GET_NEXT_ATTACHMENT_SORT, CREATE_FILE: queries.CREATE_FILE, CREATE_ATTACHMENT: queries.CREATE_ATTACHMENT, }, id, patch.files, ); } return loadDelivery(provider, tx, companyId, id); }; if (typeof provider?.transaction === 'function') { return provider.transaction((runner, tx) => run(runner, tx)); } return run(makeRunner(provider)); }; }