- Rewrite JSDoc across all store methods to document the actual async methods (real params, returns, throws) instead of the internal wrappers - Split quote/order into individual module files backed by shared manifestDoc helpers; remove the createStore factory and thin wrappers - Deduplicate manifest line helpers into src/store/manifestLines.js - Extend generate-api-doc.js to render parameter/return/throws types and emit per-namespace API.<module>.md files with an API.md index - Regenerate all API docs
83 lines
2.6 KiB
JavaScript
83 lines
2.6 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 {Array<object>} [body.lines] - The quote lines.
|
|
* @returns {Promise<{id: number, number: string, type: string, entityId: number|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, 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 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('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;
|
|
}
|
|
};
|
|
}
|