Files
dashlibstorage/src/store/quote/create.js
seb 29099b2a16 feat(manifest): add externalNumber reference to quote/order/delivery
- 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.
2026-08-08 09:13:20 +02:00

88 lines
2.9 KiB
JavaScript

import { CREATE_MANIFEST, QUOTE_TYPE } 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 } from '../manifestDoc/helpers.js';
export default function create(pool) {
const access = resolveSelectedCompanyAccess(pool);
const ensureTax = taxEnsure(pool);
/**
* Creates a new quote for the user's selected company with its lines.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} body - The quote input.
* @param {string} body.number - The quote number.
* @param {string} body.date - The quote date.
* @param {number} [body.entityId] - The id of the customer entity.
* @param {string} [body.externalNumber] - An external (customer) reference number.
* @param {Array<object>} [body.lines] - The quote lines.
* @returns {Promise<{id: number, number: string, type: string, entityId: number|null, externalNumber: string|null, date: string, lines: Array<object>}>} The created quote.
* @throws {Error} If the quote is invalid.
*
* @example
* 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 = {}) => {
const number = body.number?.trim?.() ? body.number.trim() : null;
const date = parseDate(body.date);
if (!number || !date) {
throw new Error('Invalid quote');
}
const externalNumber = body.externalNumber?.trim?.()
? body.externalNumber.trim()
: null;
const lines = normalizeLines(body.lines ?? []);
const { companyId, rootCategoryId } = await access(googleAuthId);
const tx = pool.transaction();
await tx.begin();
try {
const entityId = await assertCustomerEntity(
pool,
tx,
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 manifestId = created.recordset[0].id;
await insertLines(pool, tx, {
manifestId,
rootCategoryId,
lines,
ensureTax,
});
const doc = await loadDoc(pool, tx, {
type: QUOTE_TYPE,
companyId,
id: manifestId,
});
await tx.commit();
return doc;
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}