import { DELETE_BY_COMPANY_AND_SI } from '../../providers/mssql/queries/unit.js'; import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js'; export default function del(pool) { const access = resolveSelectedCompanyAccess(pool); /** * Deletes a unit for the user's selected company by its SI code. * * @param {number} googleAuthId - The id of the Google auth user. * @param {string} si - The SI code of the unit to delete. * @returns {Promise} * @throws {Error} If the unit is not found. * * @example * await store.unit.delete(42, 'l'); */ return async (googleAuthId, si) => { if (!si?.trim()) { throw new Error('Unit not found'); } const { companyId } = await access(googleAuthId); try { const result = await pool .request() .input('companyId', companyId) .input('si', si.trim()) .query(DELETE_BY_COMPANY_AND_SI); if (!result.rowsAffected[0]) { throw new Error('Unit not found'); } } catch (err) { if (err.number === 547) { throw new Error('Unit in use'); } throw err; } }; }