Rewrite store JSDoc, split quote/order into modules, and generate typed API docs
- 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
This commit is contained in:
132
src/store/quote/update.js
Normal file
132
src/store/quote/update.js
Normal file
@@ -0,0 +1,132 @@
|
||||
import {
|
||||
DELETE_LINES_BY_MANIFEST,
|
||||
GET_MANIFEST_HEADER,
|
||||
QUOTE_TYPE,
|
||||
UPDATE_MANIFEST,
|
||||
} 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, shapeSummary } from '../manifestDoc/helpers.js';
|
||||
|
||||
export default function update(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const ensureTax = taxEnsure(pool);
|
||||
|
||||
/**
|
||||
* Updates a quote's header and/or lines.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The id of the quote to update.
|
||||
* @param {object} patch - The update patch.
|
||||
* @param {string} [patch.number] - The new quote number.
|
||||
* @param {string} [patch.date] - The new quote date.
|
||||
* @param {number} [patch.entityId] - The new customer entity id.
|
||||
* @param {Array<object>} [patch.lines] - The new quote lines.
|
||||
* @returns {Promise<{id: number, number: string, type: string, entityId: number|null, date: string, lines: Array<object>}>} The updated quote.
|
||||
* @throws {Error} If the quote is not found.
|
||||
*
|
||||
* @example
|
||||
* const quote = await store.quote.update(42, 5, { number: 'A-2026-002', lines: [...] });
|
||||
* // => { id, number, type, entityId, date, lines: [...] }
|
||||
*/
|
||||
return async (googleAuthId, id, patch = {}) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Quote not found');
|
||||
}
|
||||
|
||||
const hasNumber = Object.prototype.hasOwnProperty.call(patch, 'number');
|
||||
const hasDate = Object.prototype.hasOwnProperty.call(patch, 'date');
|
||||
const hasEntityId = Object.prototype.hasOwnProperty.call(patch, 'entityId');
|
||||
const hasLines = Object.prototype.hasOwnProperty.call(patch, 'lines');
|
||||
|
||||
if (!hasNumber && !hasDate && !hasEntityId && !hasLines) {
|
||||
throw new Error('Invalid quote');
|
||||
}
|
||||
|
||||
let normalizedLines;
|
||||
if (hasLines) {
|
||||
normalizedLines = normalizeLines(patch.lines);
|
||||
}
|
||||
|
||||
const { companyId, rootCategoryId } = await access(googleAuthId);
|
||||
|
||||
const tx = pool.transaction();
|
||||
await tx.begin();
|
||||
|
||||
try {
|
||||
const existing = await request(pool, tx)
|
||||
.input('id', id)
|
||||
.input('companyId', companyId)
|
||||
.input('type', QUOTE_TYPE)
|
||||
.query(GET_MANIFEST_HEADER);
|
||||
|
||||
const current = existing.recordset[0];
|
||||
if (!current) {
|
||||
throw new Error('Quote not found');
|
||||
}
|
||||
|
||||
const summary = shapeSummary(QUOTE_TYPE, current);
|
||||
|
||||
let number = summary.number;
|
||||
if (hasNumber) {
|
||||
number = patch.number?.trim?.() ? patch.number.trim() : null;
|
||||
if (!number) {
|
||||
throw new Error('Invalid quote');
|
||||
}
|
||||
}
|
||||
|
||||
let date = summary.date;
|
||||
if (hasDate) {
|
||||
date = parseDate(patch.date);
|
||||
if (!date) {
|
||||
throw new Error('Invalid quote');
|
||||
}
|
||||
}
|
||||
|
||||
let entityId = summary.entityId;
|
||||
if (hasEntityId) {
|
||||
entityId = await assertCustomerEntity(
|
||||
pool,
|
||||
tx,
|
||||
companyId,
|
||||
patch.entityId ?? null,
|
||||
);
|
||||
}
|
||||
|
||||
await request(pool, tx)
|
||||
.input('id', id)
|
||||
.input('companyId', companyId)
|
||||
.input('type', QUOTE_TYPE)
|
||||
.input('number', number)
|
||||
.input('entityId', entityId)
|
||||
.input('date', date)
|
||||
.query(UPDATE_MANIFEST);
|
||||
|
||||
if (hasLines) {
|
||||
await request(pool, tx)
|
||||
.input('manifestId', id)
|
||||
.query(DELETE_LINES_BY_MANIFEST);
|
||||
|
||||
await insertLines(pool, tx, {
|
||||
manifestId: id,
|
||||
rootCategoryId,
|
||||
lines: normalizedLines,
|
||||
ensureTax,
|
||||
});
|
||||
}
|
||||
|
||||
const doc = await loadDoc(pool, tx, { type: QUOTE_TYPE, companyId, id });
|
||||
await tx.commit();
|
||||
return doc;
|
||||
} catch (err) {
|
||||
try {
|
||||
await tx.rollback();
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user