sqlite
This commit is contained in:
@@ -1,13 +1,24 @@
|
||||
import { CREATE_MANIFEST, QUOTE_TYPE } from '../../providers/mssql/queries/manifestDoc.js';
|
||||
import request from '../request.js';
|
||||
import {
|
||||
CREATE_MANIFEST as MSSQL_CREATE_MANIFEST,
|
||||
QUOTE_TYPE as MSSQL_QUOTE_TYPE,
|
||||
} from '../../providers/mssql/queries/manifestDoc.js';
|
||||
import {
|
||||
CREATE_MANIFEST as SQLITE_CREATE_MANIFEST,
|
||||
QUOTE_TYPE as SQLITE_QUOTE_TYPE,
|
||||
} 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 } from '../manifestDoc/helpers.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function create(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const ensureTax = taxEnsure(pool);
|
||||
export default function create(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const ensureTax = taxEnsure(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
|
||||
const createManifestSql = isSqlite ? SQLITE_CREATE_MANIFEST : MSSQL_CREATE_MANIFEST;
|
||||
const quoteType = isSqlite ? SQLITE_QUOTE_TYPE : MSSQL_QUOTE_TYPE;
|
||||
|
||||
/**
|
||||
* Creates a new quote for the user's selected company with its lines.
|
||||
@@ -19,6 +30,7 @@ export default function create(pool) {
|
||||
* @param {number} [body.entityId] - The id of the customer entity.
|
||||
* @param {string} [body.externalNumber] - An external (customer) reference number.
|
||||
* @param {Array<{sku?: string, name?: string, pu?: number, amount: number, net: number, rebate?: number, taxrate: number, comment?: string|null}>} [body.lines] - The quote 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 created quote.
|
||||
* @throws {Error} If the quote is invalid.
|
||||
*
|
||||
@@ -26,7 +38,7 @@ export default function create(pool) {
|
||||
* const quote = await store.quote.create(42, { number: 'A-2026-001', date: '2026-09-01', entityId: 12, lines: [...] });
|
||||
* // => { id, number, type, entityId, externalNumber, date, lines: [...] }
|
||||
*/
|
||||
return async (googleAuthId, body = {}) => {
|
||||
return async (googleAuthId, body = {}, tx) => {
|
||||
const number = body.number?.trim?.() ? body.number.trim() : null;
|
||||
const date = parseDate(body.date);
|
||||
if (!number || !date) {
|
||||
@@ -37,50 +49,49 @@ export default function create(pool) {
|
||||
: null;
|
||||
|
||||
const lines = normalizeLines(body.lines ?? []);
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
const runCreate = async (runner) => {
|
||||
const { companyId } = await access(googleAuthId, runner);
|
||||
|
||||
try {
|
||||
const entityId = await assertCustomerEntity(
|
||||
pool,
|
||||
tx,
|
||||
runner,
|
||||
companyId,
|
||||
body.entityId ?? null,
|
||||
);
|
||||
|
||||
const created = await request(pool, tx)
|
||||
.input('companyId', companyId)
|
||||
.input('number', number)
|
||||
.input('type', QUOTE_TYPE)
|
||||
.input('entityId', entityId)
|
||||
.input('externalNumber', externalNumber)
|
||||
.input('date', date)
|
||||
.query(CREATE_MANIFEST);
|
||||
const created = await runner.query(createManifestSql, {
|
||||
companyId,
|
||||
number,
|
||||
type: quoteType,
|
||||
entityId,
|
||||
externalNumber,
|
||||
date,
|
||||
});
|
||||
|
||||
const manifestId = created.recordset[0].id;
|
||||
const manifestId = created.rows[0]?.id ?? created.insertId;
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
await insertLines(runner, {
|
||||
manifestId,
|
||||
lines,
|
||||
ensureTax,
|
||||
});
|
||||
|
||||
const doc = await loadDoc(pool, tx, {
|
||||
type: QUOTE_TYPE,
|
||||
const doc = await loadDoc(runner, {
|
||||
type: quoteType,
|
||||
companyId,
|
||||
id: manifestId,
|
||||
});
|
||||
await tx.commit();
|
||||
|
||||
return doc;
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return runCreate(makeRunner(provider, tx));
|
||||
}
|
||||
if (typeof provider?.transaction === 'function') {
|
||||
return provider.transaction(runCreate);
|
||||
}
|
||||
return runCreate(makeRunner(provider));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user