import { DELETE_BY_ENTITY_AND_ID } from '../../sql/entitycontact.js'; import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js'; import resolveEntityAccess from '../entity/resolveEntityAccess.js'; /** * Deletes a contact of an entity. * * @param {object} pool - The mssql connection pool. * @returns {Function} An async function that deletes an entity contact. * * @example * const fn = del(pool); * await fn(42, 5, 9); */ export default function del(pool) { const access = resolveSelectedCompanyAccess(pool); const entityAccess = resolveEntityAccess(pool); return async (googleAuthId, entityId, id) => { if (!Number.isFinite(id)) { throw new Error('Contact not found'); } const { companyId } = await access(googleAuthId); 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('Contact not found'); } }; }