38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
import { DELETE_BY_ENTITY_AND_ID } from '../../sql/entityaddress.js';
|
|
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
|
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
|
|
|
/**
|
|
* Deletes an address of an entity.
|
|
*
|
|
* @param {object} pool - The mssql connection pool.
|
|
* @returns {Function} An async function that deletes an entity address.
|
|
*
|
|
* @example
|
|
* const fn = del(pool);
|
|
* await fn(42, 1, 5, 9);
|
|
*/
|
|
export default function del(pool) {
|
|
const access = resolveCompanyAccess(pool);
|
|
const entityAccess = resolveEntityAccess(pool);
|
|
|
|
return async (googleAuthId, companyId, entityId, id) => {
|
|
if (!Number.isFinite(id)) {
|
|
throw new Error('Address not found');
|
|
}
|
|
|
|
await access(googleAuthId, companyId);
|
|
await entityAccess(companyId, entityId);
|
|
|
|
const result = await pool
|
|
.request()
|
|
.input('id', id)
|
|
.input('entityId', entityId)
|
|
.query(DELETE_BY_ENTITY_AND_ID);
|
|
|
|
if (!result.rowsAffected[0]) {
|
|
throw new Error('Address not found');
|
|
}
|
|
};
|
|
}
|