Entity gets an optional unique-per-company number with allocate/peek/isNumberFree helpers. Company-scoped store methods no longer take companyId and resolve google_auth.selected_company instead.
38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
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');
|
|
}
|
|
};
|
|
}
|