import { DELETE_MANIFEST, DELIVERY_TYPE } from '../../sql/delivery.js'; import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js'; /** * Deletes a delivery by its id within a company. * * @param {object} pool - The mssql connection pool. * @returns {Function} An async function that deletes a delivery. * * @example * const fn = del(pool); * await fn(42, 1, 5); */ export default function del(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 result = await pool .request() .input('id', id) .input('companyId', companyId) .input('type', DELIVERY_TYPE) .query(DELETE_MANIFEST); if (!result.rowsAffected[0]) { throw new Error('Delivery not found'); } }; }