sqlite
This commit is contained in:
@@ -1,42 +1,19 @@
|
||||
import {
|
||||
DELETE_LINES_BY_MANIFEST,
|
||||
DELIVERY_TYPE,
|
||||
GET_DELIVERY_HEADER,
|
||||
GET_NEXT_ATTACHMENT_SORT,
|
||||
CREATE_FILE,
|
||||
CREATE_ATTACHMENT,
|
||||
UPDATE_MANIFEST,
|
||||
} from '../../providers/mssql/queries/delivery.js';
|
||||
import request from '../request.js';
|
||||
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(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 queries = isSqlite ? sqliteQueries : mssqlQueries;
|
||||
|
||||
/**
|
||||
* 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<{sku?: string, name?: string, pu?: number, amount: number, net: number, rebate?: number, taxrate: number, comment?: string|null}>} [patch.lines] - The new delivery lines.
|
||||
* @returns {Promise<{id: number, number: string, type: string, entityId: number, 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 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');
|
||||
@@ -63,17 +40,14 @@ export default function update(pool) {
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
const run = async (runner, tx) => {
|
||||
const existing = await runner(queries.GET_DELIVERY_HEADER, {
|
||||
id,
|
||||
companyId,
|
||||
type: queries.DELIVERY_TYPE,
|
||||
});
|
||||
|
||||
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];
|
||||
const current = existing.rows[0];
|
||||
if (!current) {
|
||||
throw new Error('Delivery not found');
|
||||
}
|
||||
@@ -99,7 +73,7 @@ export default function update(pool) {
|
||||
let supplierId = summary.supplierId;
|
||||
if (hasSupplierId) {
|
||||
supplierId = await assertSupplier(
|
||||
pool,
|
||||
provider,
|
||||
tx,
|
||||
companyId,
|
||||
patch.supplierId ?? null,
|
||||
@@ -113,22 +87,22 @@ export default function update(pool) {
|
||||
: 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);
|
||||
await runner(queries.UPDATE_MANIFEST, {
|
||||
id,
|
||||
companyId,
|
||||
type: queries.DELIVERY_TYPE,
|
||||
number,
|
||||
entityId: supplierId,
|
||||
externalNumber,
|
||||
date,
|
||||
});
|
||||
|
||||
if (hasLines) {
|
||||
await request(pool, tx)
|
||||
.input('manifestId', id)
|
||||
.query(DELETE_LINES_BY_MANIFEST);
|
||||
await runner(queries.DELETE_LINES_BY_MANIFEST, {
|
||||
manifestId: id,
|
||||
});
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
await insertLines(provider, tx, {
|
||||
manifestId: id,
|
||||
lines: normalizedLines,
|
||||
ensureTax,
|
||||
@@ -137,28 +111,24 @@ export default function update(pool) {
|
||||
|
||||
if (hasFiles) {
|
||||
await insertAttachments(
|
||||
pool,
|
||||
provider,
|
||||
tx,
|
||||
{
|
||||
GET_NEXT_ATTACHMENT_SORT,
|
||||
CREATE_FILE,
|
||||
CREATE_ATTACHMENT,
|
||||
GET_NEXT_ATTACHMENT_SORT: queries.GET_NEXT_ATTACHMENT_SORT,
|
||||
CREATE_FILE: queries.CREATE_FILE,
|
||||
CREATE_ATTACHMENT: queries.CREATE_ATTACHMENT,
|
||||
},
|
||||
id,
|
||||
patch.files,
|
||||
);
|
||||
}
|
||||
|
||||
const delivery = await loadDelivery(pool, tx, companyId, id);
|
||||
await tx.commit();
|
||||
return delivery;
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
return loadDelivery(provider, tx, companyId, id);
|
||||
};
|
||||
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction((runner, tx) => run(runner, tx));
|
||||
}
|
||||
return run(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user