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]);
|
||||
};
|
||||
}
|
||||
27
src/store/entityaddress/delete.js
Normal file
27
src/store/entityaddress/delete.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { DELETE_BY_ENTITY_AND_ID } from '../../sql/entityaddress.js';
|
||||
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
|
||||
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');
|
||||
}
|
||||
};
|
||||
}
|
||||
35
src/store/entityaddress/list.js
Normal file
35
src/store/entityaddress/list.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import { LIST_BY_ENTITY } 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,
|
||||
};
|
||||
}
|
||||
|
||||
export default function list(pool) {
|
||||
const access = resolveCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
|
||||
return async (googleAuthId, companyId, entityId) => {
|
||||
await access(googleAuthId, companyId);
|
||||
await entityAccess(companyId, entityId);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
.query(LIST_BY_ENTITY);
|
||||
|
||||
return result.recordset.map(shape);
|
||||
};
|
||||
}
|
||||
104
src/store/entityaddress/update.js
Normal file
104
src/store/entityaddress/update.js
Normal file
@@ -0,0 +1,104 @@
|
||||
import { GET_BY_ENTITY_AND_ID, UPDATE } 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 update(pool) {
|
||||
const access = resolveCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
|
||||
return async (googleAuthId, companyId, entityId, id, patch = {}) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Address not found');
|
||||
}
|
||||
|
||||
await access(googleAuthId, companyId);
|
||||
await entityAccess(companyId, entityId);
|
||||
|
||||
const existing = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('entityId', entityId)
|
||||
.query(GET_BY_ENTITY_AND_ID);
|
||||
|
||||
const current = existing.recordset[0];
|
||||
if (!current) {
|
||||
throw new Error('Address not found');
|
||||
}
|
||||
|
||||
const firstName = Object.prototype.hasOwnProperty.call(patch, 'firstName')
|
||||
? optionalString(patch.firstName)
|
||||
: current.firstName ?? null;
|
||||
const name = Object.prototype.hasOwnProperty.call(patch, 'name')
|
||||
? optionalString(patch.name)
|
||||
: current.name ?? null;
|
||||
const street = Object.prototype.hasOwnProperty.call(patch, 'street')
|
||||
? optionalString(patch.street)
|
||||
: current.street ?? null;
|
||||
const streetNumber = Object.prototype.hasOwnProperty.call(
|
||||
patch,
|
||||
'streetNumber',
|
||||
)
|
||||
? optionalString(patch.streetNumber)
|
||||
: current.streetNumber ?? null;
|
||||
const zip = Object.prototype.hasOwnProperty.call(patch, 'zip')
|
||||
? optionalString(patch.zip)
|
||||
: current.zip ?? null;
|
||||
const city = Object.prototype.hasOwnProperty.call(patch, 'city')
|
||||
? optionalString(patch.city)
|
||||
: current.city ?? null;
|
||||
const county = Object.prototype.hasOwnProperty.call(patch, 'county')
|
||||
? optionalString(patch.county)
|
||||
: current.county ?? null;
|
||||
|
||||
let addressType = current.addressType;
|
||||
if (Object.prototype.hasOwnProperty.call(patch, 'addressType')) {
|
||||
addressType = patch.addressType?.trim?.()
|
||||
? patch.addressType.trim()
|
||||
: null;
|
||||
if (!addressType) {
|
||||
throw new Error('Address type required');
|
||||
}
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('entityId', entityId)
|
||||
.input('firstName', firstName)
|
||||
.input('name', name)
|
||||
.input('street', street)
|
||||
.input('streetNumber', streetNumber)
|
||||
.input('zip', zip)
|
||||
.input('city', city)
|
||||
.input('county', county)
|
||||
.input('addressType', addressType)
|
||||
.query(UPDATE);
|
||||
|
||||
const row = result.recordset[0];
|
||||
if (!row) {
|
||||
throw new Error('Address not found');
|
||||
}
|
||||
return shape(row);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user