Add entity address/contact tables and CRUD for entity resources

This commit is contained in:
seb
2026-08-06 05:43:50 +02:00
parent 0b7e40ac9c
commit 3b77a1c946
32 changed files with 3078 additions and 1363 deletions

View File

@@ -0,0 +1,24 @@
import { CREATE } from '../../sql/customergroup.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function create(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, { name } = {}) => {
const cleaned = name?.trim();
if (!cleaned) {
throw new Error('Name required');
}
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('companyId', companyId)
.input('name', cleaned)
.query(CREATE);
const row = result.recordset[0];
return { id: row.id, name: row.name };
};
}

View File

@@ -0,0 +1,31 @@
import { DELETE_BY_COMPANY_AND_ID } from '../../sql/customergroup.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function del(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, id) => {
if (!Number.isFinite(id)) {
throw new Error('Customer group not found');
}
await access(googleAuthId, companyId);
try {
const result = await pool
.request()
.input('id', id)
.input('companyId', companyId)
.query(DELETE_BY_COMPANY_AND_ID);
if (!result.rowsAffected[0]) {
throw new Error('Customer group not found');
}
} catch (err) {
if (err.number === 547) {
throw new Error('Customer group in use');
}
throw err;
}
};
}

View File

@@ -0,0 +1,20 @@
import { LIST_BY_COMPANY } from '../../sql/customergroup.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function list(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId) => {
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('companyId', companyId)
.query(LIST_BY_COMPANY);
return result.recordset.map((row) => ({
id: row.id,
name: row.name,
}));
};
}

View File

@@ -0,0 +1,22 @@
import { GET_BY_COMPANY_AND_ID } from '../../sql/customergroup.js';
import request from '../request.js';
/** Ensure customergroup belongs to company. Throws Customer group not found. */
export default function resolveCustomerGroupAccess(pool) {
return async (companyId, customergroupId, tx) => {
if (!Number.isFinite(customergroupId)) {
throw new Error('Customer group not found');
}
const result = await request(pool, tx)
.input('id', customergroupId)
.input('companyId', companyId)
.query(GET_BY_COMPANY_AND_ID);
const row = result.recordset[0];
if (!row) {
throw new Error('Customer group not found');
}
return row;
};
}

View File

@@ -0,0 +1,32 @@
import { UPDATE_NAME } from '../../sql/customergroup.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function update(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, id, { name } = {}) => {
if (!Number.isFinite(id)) {
throw new Error('Customer group not found');
}
const cleaned = name?.trim();
if (!cleaned) {
throw new Error('Name required');
}
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('id', id)
.input('companyId', companyId)
.input('name', cleaned)
.query(UPDATE_NAME);
const row = result.recordset[0];
if (!row) {
throw new Error('Customer group not found');
}
return { id: row.id, name: row.name };
};
}

View File

@@ -0,0 +1,69 @@
import { CREATE } from '../../sql/entity.js';
import { INSERT as INSERT_HISTORY } from '../../sql/entityhistory.js';
import request from '../request.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveCustomerGroupAccess from '../customergroup/resolveCustomerGroupAccess.js';
function shape(row) {
return {
id: row.id,
customergroupId: row.customergroupId,
firstName: row.firstName ?? null,
name: row.name,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
export default function create(pool) {
const access = resolveCompanyAccess(pool);
const groupAccess = resolveCustomerGroupAccess(pool);
return async (
googleAuthId,
companyId,
{ customergroupId, name, firstName = null } = {},
) => {
const cleaned = name?.trim();
if (!cleaned) {
throw new Error('Name required');
}
const first =
firstName == null || firstName === ''
? null
: String(firstName).trim() || null;
const { companyuserId } = await access(googleAuthId, companyId);
await groupAccess(companyId, customergroupId);
const tx = pool.transaction();
await tx.begin();
try {
const created = await request(pool, tx)
.input('customergroupId', customergroupId)
.input('firstName', first)
.input('name', cleaned)
.query(CREATE);
const row = created.recordset[0];
await request(pool, tx)
.input('entityId', row.id)
.input('companyuserId', companyuserId)
.input('text', 'Erstellt')
.query(INSERT_HISTORY);
await tx.commit();
return shape(row);
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}

View File

@@ -0,0 +1,24 @@
import { DELETE_BY_COMPANY_AND_ID } from '../../sql/entity.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
export default function del(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, id) => {
if (!Number.isFinite(id)) {
throw new Error('Entity not found');
}
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('id', id)
.input('companyId', companyId)
.query(DELETE_BY_COMPANY_AND_ID);
if (!result.rowsAffected[0]) {
throw new Error('Entity not found');
}
};
}

View File

@@ -0,0 +1,37 @@
import { GET_BY_COMPANY_AND_ID } from '../../sql/entity.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
function shape(row) {
return {
id: row.id,
customergroupId: row.customergroupId,
firstName: row.firstName ?? null,
name: row.name,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
export default function getById(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, id) => {
if (!Number.isFinite(id)) {
throw new Error('Entity not found');
}
await access(googleAuthId, companyId);
const result = await pool
.request()
.input('id', id)
.input('companyId', companyId)
.query(GET_BY_COMPANY_AND_ID);
const row = result.recordset[0];
if (!row) {
throw new Error('Entity not found');
}
return shape(row);
};
}

43
src/store/entity/list.js Normal file
View File

@@ -0,0 +1,43 @@
import {
LIST_BY_COMPANY,
LIST_BY_COMPANY_AND_GROUP,
} from '../../sql/entity.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
import resolveCustomerGroupAccess from '../customergroup/resolveCustomerGroupAccess.js';
function shape(row) {
return {
id: row.id,
customergroupId: row.customergroupId,
firstName: row.firstName ?? null,
name: row.name,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
export default function list(pool) {
const access = resolveCompanyAccess(pool);
const groupAccess = resolveCustomerGroupAccess(pool);
return async (googleAuthId, companyId, { customergroupId } = {}) => {
await access(googleAuthId, companyId);
if (customergroupId != null) {
await groupAccess(companyId, customergroupId);
const result = await pool
.request()
.input('companyId', companyId)
.input('customergroupId', customergroupId)
.query(LIST_BY_COMPANY_AND_GROUP);
return result.recordset.map(shape);
}
const result = await pool
.request()
.input('companyId', companyId)
.query(LIST_BY_COMPANY);
return result.recordset.map(shape);
};
}

View File

@@ -0,0 +1,22 @@
import { GET_BY_COMPANY_AND_ID } from '../../sql/entity.js';
import request from '../request.js';
/** Ensure entity belongs to company. Throws Entity not found. */
export default function resolveEntityAccess(pool) {
return async (companyId, entityId, tx) => {
if (!Number.isFinite(entityId)) {
throw new Error('Entity not found');
}
const result = await request(pool, tx)
.input('id', entityId)
.input('companyId', companyId)
.query(GET_BY_COMPANY_AND_ID);
const row = result.recordset[0];
if (!row) {
throw new Error('Entity not found');
}
return row;
};
}

110
src/store/entity/update.js Normal file
View File

@@ -0,0 +1,110 @@
import { GET_BY_COMPANY_AND_ID, UPDATE } from '../../sql/entity.js';
import { INSERT as INSERT_HISTORY } from '../../sql/entityhistory.js';
import request from '../request.js';
import resolveCompanyAccess from '../companyuser/resolveCompanyAccess.js';
function shape(row) {
return {
id: row.id,
customergroupId: row.customergroupId,
firstName: row.firstName ?? null,
name: row.name,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
export default function update(pool) {
const access = resolveCompanyAccess(pool);
return async (googleAuthId, companyId, id, patch = {}) => {
if (!Number.isFinite(id)) {
throw new Error('Entity not found');
}
const hasName = Object.prototype.hasOwnProperty.call(patch, 'name');
const hasFirstName = Object.prototype.hasOwnProperty.call(patch, 'firstName');
if (!hasName && !hasFirstName) {
throw new Error('Name required');
}
const { companyuserId } = await access(googleAuthId, companyId);
const existing = await pool
.request()
.input('id', id)
.input('companyId', companyId)
.query(GET_BY_COMPANY_AND_ID);
const current = existing.recordset[0];
if (!current) {
throw new Error('Entity not found');
}
let name = current.name;
if (hasName) {
name = patch.name?.trim();
if (!name) {
throw new Error('Name required');
}
}
let firstName = current.firstName ?? null;
if (hasFirstName) {
firstName =
patch.firstName == null || patch.firstName === ''
? null
: String(patch.firstName).trim() || null;
}
const historyLines = [];
const oldFirst = current.firstName ?? '';
const newFirst = firstName ?? '';
if (oldFirst !== newFirst) {
historyLines.push(
`Vorname geändert von "${oldFirst}" auf "${newFirst}"`,
);
}
if (current.name !== name) {
historyLines.push(
`Name geändert von "${current.name}" auf "${name}"`,
);
}
if (!historyLines.length) {
return shape(current);
}
const tx = pool.transaction();
await tx.begin();
try {
const updated = await request(pool, tx)
.input('id', id)
.input('firstName', firstName)
.input('name', name)
.query(UPDATE);
const row = updated.recordset[0];
if (!row) {
throw new Error('Entity not found');
}
await request(pool, tx)
.input('entityId', id)
.input('companyuserId', companyuserId)
.input('text', historyLines.join('\n'))
.query(INSERT_HISTORY);
await tx.commit();
return shape(row);
} catch (err) {
try {
await tx.rollback();
} catch {
// ignore
}
throw err;
}
};
}

View 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]);
};
}

View 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');
}
};
}

View 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);
};
}

View 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);
};
}

View 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]);
};
}

View File

@@ -0,0 +1,27 @@
import { DELETE_BY_ENTITY_AND_ID } from '../../sql/entitycontact.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('Contact 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('Contact not found');
}
};
}

View File

@@ -0,0 +1,29 @@
import { LIST_BY_ENTITY } 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 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);
};
}

View File

@@ -0,0 +1,67 @@
import { GET_BY_ENTITY_AND_ID, UPDATE } 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 update(pool) {
const access = resolveCompanyAccess(pool);
const entityAccess = resolveEntityAccess(pool);
return async (googleAuthId, companyId, entityId, id, patch = {}) => {
if (!Number.isFinite(id)) {
throw new Error('Contact 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('Contact not found');
}
let contactType = current.contactType;
if (Object.prototype.hasOwnProperty.call(patch, 'contactType')) {
contactType = patch.contactType?.trim?.()
? patch.contactType.trim()
: null;
}
let contact = current.contact;
if (Object.prototype.hasOwnProperty.call(patch, 'contact')) {
contact = patch.contact?.trim?.() ? patch.contact.trim() : null;
}
if (!contactType || !contact) {
throw new Error('Contact required');
}
const result = await pool
.request()
.input('id', id)
.input('entityId', entityId)
.input('contactType', contactType)
.input('contact', contact)
.query(UPDATE);
const row = result.recordset[0];
if (!row) {
throw new Error('Contact not found');
}
return shape(row);
};
}

View File

@@ -23,6 +23,7 @@ export default function create(pool) {
const created = await request(pool, tx)
.input('customergroupId', customergroupId)
.input('firstName', null)
.input('name', cleaned)
.query(CREATE);