Units are isolated per company; article detail exposes full unit shape, and unused units can be deleted safely via FK NO ACTION.
32 lines
778 B
JavaScript
32 lines
778 B
JavaScript
import { DELETE_BY_COMPANY_AND_SI } from '../../sql/unit.js';
|
|
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
|
|
|
export default function del(pool) {
|
|
const access = resolveCompanyAccess(pool);
|
|
|
|
return async (googleAuthId, companyId, si) => {
|
|
if (!si?.trim()) {
|
|
throw new Error('Unit not found');
|
|
}
|
|
|
|
await access(googleAuthId, companyId);
|
|
|
|
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;
|
|
}
|
|
};
|
|
}
|