Add entity address/contact tables and CRUD for entity resources
This commit is contained in:
39
src/store/entitycontact/create.js
Normal file
39
src/store/entitycontact/create.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { CREATE } from '../../sql/entitycontact.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
entityId: row.entityId,
|
||||
contactType: row.contactType,
|
||||
contact: row.contact,
|
||||
};
|
||||
}
|
||||
|
||||
export default function create(pool) {
|
||||
const access = resolveCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
|
||||
return async (googleAuthId, companyId, entityId, input = {}) => {
|
||||
const contactType = input.contactType?.trim?.()
|
||||
? input.contactType.trim()
|
||||
: null;
|
||||
const contact = input.contact?.trim?.() ? input.contact.trim() : null;
|
||||
if (!contactType || !contact) {
|
||||
throw new Error('Contact required');
|
||||
}
|
||||
|
||||
await access(googleAuthId, companyId);
|
||||
await entityAccess(companyId, entityId);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
.input('contactType', contactType)
|
||||
.input('contact', contact)
|
||||
.query(CREATE);
|
||||
|
||||
return shape(result.recordset[0]);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user