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