import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js'; import { loadDelivery } from './helpers.js'; /** * Returns a single delivery by its id within a company. * * @param {object} pool - The mssql connection pool. * @returns {Function} An async function that fetches a delivery by id. * * @example * const fn = getById(pool); * const delivery = await fn(42, 1, 5); * // => { id, number, type, entityId, date, lines: [...] } */ export default function getById(pool) { const access = resolveCompanyAccess(pool); return async (googleAuthId, companyId, id) => { if (!Number.isFinite(id)) { throw new Error('Delivery not found'); } await access(googleAuthId, companyId); const delivery = await loadDelivery(pool, null, companyId, id); if (!delivery) { throw new Error('Delivery not found'); } return delivery; }; }