import { QUOTE_TYPE as MSSQL_QUOTE_TYPE } from '../../providers/mssql/queries/manifestDoc.js'; import { QUOTE_TYPE as SQLITE_QUOTE_TYPE } from '../../providers/sqlite3/queries/manifestDoc.js'; import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js'; import { loadDoc } from '../manifestDoc/helpers.js'; import makeRunner from '../runner.js'; export default function getById(provider) { const access = resolveSelectedCompanyAccess(provider); const isSqlite = provider?.dialect === 'sqlite3'; const quoteType = isSqlite ? SQLITE_QUOTE_TYPE : MSSQL_QUOTE_TYPE; /** * 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. * @param {object} [tx] - Optional transaction. * @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 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, externalNumber, date, lines: [...] } */ return async (googleAuthId, id, tx) => { if (!Number.isFinite(id)) { throw new Error('Quote not found'); } const runner = makeRunner(provider, tx); const { companyId } = await access(googleAuthId, runner); const doc = await loadDoc(runner, { type: quoteType, companyId, id }); if (!doc) { throw new Error('Quote not found'); } return doc; }; }