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:
82
src/store/quote/create.js
Normal file
82
src/store/quote/create.js
Normal file
@@ -0,0 +1,82 @@
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
36
src/store/quote/delete.js
Normal file
36
src/store/quote/delete.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import { DELETE_MANIFEST, QUOTE_TYPE } from '../../sql/manifestDoc.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
|
||||
export default function del(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
|
||||
/**
|
||||
* Deletes a quote by its id within the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The id of the quote to delete.
|
||||
* @returns {Promise<void>}
|
||||
* @throws {Error} If the quote is not found.
|
||||
*
|
||||
* @example
|
||||
* await store.quote.delete(42, 5);
|
||||
*/
|
||||
return async (googleAuthId, id) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Quote not found');
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('companyId', companyId)
|
||||
.input('type', QUOTE_TYPE)
|
||||
.query(DELETE_MANIFEST);
|
||||
|
||||
if (!result.rowsAffected[0]) {
|
||||
throw new Error('Quote not found');
|
||||
}
|
||||
};
|
||||
}
|
||||
33
src/store/quote/getById.js
Normal file
33
src/store/quote/getById.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { QUOTE_TYPE } from '../../sql/manifestDoc.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import { loadDoc } from '../manifestDoc/helpers.js';
|
||||
|
||||
export default function getById(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
|
||||
/**
|
||||
* Returns a single quote by its id within the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} id - The id of the quote.
|
||||
* @returns {Promise<{id: number, number: string, type: string, entityId: number|null, date: string, lines: Array<object>}>} The quote with its lines.
|
||||
* @throws {Error} If the quote is not found.
|
||||
*
|
||||
* @example
|
||||
* const quote = await store.quote.getById(42, 5);
|
||||
* // => { id, number, type, entityId, date, lines: [...] }
|
||||
*/
|
||||
return async (googleAuthId, id) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Quote not found');
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const doc = await loadDoc(pool, null, { type: QUOTE_TYPE, companyId, id });
|
||||
if (!doc) {
|
||||
throw new Error('Quote not found');
|
||||
}
|
||||
return doc;
|
||||
};
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { QUOTE_TYPE } from '../../sql/manifestDoc.js';
|
||||
import createManifestDocStore from '../manifestDoc/createStore.js';
|
||||
|
||||
/**
|
||||
* Creates a quote store (list, getById, create, update, delete) backed by the
|
||||
* manifest / manifestarticle tables.
|
||||
*
|
||||
* @param {object} pool - The mssql connection pool.
|
||||
* @returns {object} An object with `list`, `getById`, `create`, `update` and `delete` functions.
|
||||
*
|
||||
* @example
|
||||
* const store = createQuoteStore(pool);
|
||||
* const quotes = await store.list(42);
|
||||
* // => [{ id, number, type, entityId, date }]
|
||||
*/
|
||||
export default function createQuoteStore(pool) {
|
||||
return createManifestDocStore(pool, { type: QUOTE_TYPE, label: 'Quote' });
|
||||
}
|
||||
29
src/store/quote/list.js
Normal file
29
src/store/quote/list.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { LIST_MANIFESTS, QUOTE_TYPE } from '../../sql/manifestDoc.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import { shapeSummary } from '../manifestDoc/helpers.js';
|
||||
|
||||
export default function list(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
|
||||
/**
|
||||
* Lists the quotes of the user's selected company.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @returns {Promise<Array<{id: number, number: string, type: string, entityId: number|null, date: string}>>} The quotes.
|
||||
*
|
||||
* @example
|
||||
* const quotes = await store.quote.list(42);
|
||||
* // => [{ id, number, type, entityId, date }]
|
||||
*/
|
||||
return async (googleAuthId) => {
|
||||
const { companyId } = await access(googleAuthId);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('companyId', companyId)
|
||||
.input('type', QUOTE_TYPE)
|
||||
.query(LIST_MANIFESTS);
|
||||
|
||||
return result.recordset.map((row) => shapeSummary(QUOTE_TYPE, row));
|
||||
};
|
||||
}
|
||||
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