Files
dashback/src/api/routes/entityaddress.js
seb e6c24654dd Align store calls with selected-company scoping and add entity numbers.
Drop companyId from company-scoped store wrappers and routes, add GET /entity/next-number plus optional number on entity create/update, and document dashdbschema API.md lookup in sibling-repos.
2026-08-07 06:22:13 +02:00

417 lines
14 KiB
JavaScript

import * as db from '../../db/entityaddress.js';
function requireSelectedCompany(req) {
const companyId = req.user.selectedCompanyId;
if (companyId == null) {
return { error: 'No company selected' };
}
return { companyId };
}
function addressErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Entity not found') return 404;
if (error.message === 'Address not found') return 404;
if (error.message === 'Address type required') return 400;
return 500;
}
const ADDRESS_FIELDS = [
'addressType',
'firstName',
'name',
'street',
'streetNumber',
'zip',
'city',
'county',
];
/**
* Registers entity address routes nested under `/entity/:entityId/address`.
* @param {import('express').Application} app
* @param {string} [basePath='']
*/
export function register(app, basePath = '') {
/**
* @openapi
* /entity/{entityId}/address:
* get:
* tags: [Entity]
* summary: List entity addresses
* description: Lists addresses for an entity of the selected company.
* security:
* - cookieAuth: []
* x-markdown: entity/address/list.md
* parameters:
* - in: path
* name: entityId
* required: true
* schema:
* type: integer
* responses:
* 200:
* description: Address list
* content:
* application/json:
* schema:
* type: object
* properties:
* addresses:
* type: array
* items:
* $ref: '#/components/schemas/EntityAddress'
* 400:
* description: No company selected or invalid entityId
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Not authorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Entity or company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* post:
* tags: [Entity]
* summary: Create entity address
* description: Creates an address for the entity. `addressType` is required.
* security:
* - cookieAuth: []
* x-markdown: entity/address/create.md
* parameters:
* - in: path
* name: entityId
* required: true
* schema:
* type: integer
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [addressType]
* properties:
* addressType:
* type: string
* firstName:
* type: string
* nullable: true
* name:
* type: string
* nullable: true
* street:
* type: string
* nullable: true
* streetNumber:
* type: string
* nullable: true
* zip:
* type: string
* nullable: true
* city:
* type: string
* nullable: true
* county:
* type: string
* nullable: true
* example:
* addressType: billing
* street: Hauptstraße
* streetNumber: '12'
* zip: '10115'
* city: Berlin
* responses:
* 201:
* description: Created address
* content:
* application/json:
* schema:
* type: object
* properties:
* address:
* $ref: '#/components/schemas/EntityAddress'
* 400:
* description: Address type required, invalid entityId, or no company selected
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Not authorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Entity or company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.get(`${basePath}/entity/:entityId/address`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const entityId = Number(req.params.entityId);
if (!Number.isInteger(entityId) || entityId <= 0) {
return res.status(400).json({ error: 'entityId is required' });
}
try {
const addresses = await db.list(req.user.id, entityId);
res.status(200).json({ addresses });
} catch (error) {
const status = addressErrorStatus(error);
if (status === 500) console.error('List entity addresses failed:', error);
res.status(status).json({ error: error.message });
}
});
app.post(`${basePath}/entity/:entityId/address`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const entityId = Number(req.params.entityId);
if (!Number.isInteger(entityId) || entityId <= 0) {
return res.status(400).json({ error: 'entityId is required' });
}
const body = req.body ?? {};
const data = {};
for (const key of ADDRESS_FIELDS) {
if (Object.prototype.hasOwnProperty.call(body, key)) {
data[key] = body[key];
}
}
try {
const address = await db.create(req.user.id, entityId, data);
res.status(201).json({ address });
} catch (error) {
const status = addressErrorStatus(error);
if (status === 500) console.error('Create entity address failed:', error);
res.status(status).json({ error: error.message });
}
});
/**
* @openapi
* /entity/{entityId}/address/{id}:
* patch:
* tags: [Entity]
* summary: Update entity address
* description: Patches address fields that are present.
* security:
* - cookieAuth: []
* x-markdown: entity/address/update.md
* parameters:
* - in: path
* name: entityId
* required: true
* schema:
* type: integer
* - in: path
* name: id
* required: true
* schema:
* type: integer
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* addressType:
* type: string
* firstName:
* type: string
* nullable: true
* name:
* type: string
* nullable: true
* street:
* type: string
* nullable: true
* streetNumber:
* type: string
* nullable: true
* zip:
* type: string
* nullable: true
* city:
* type: string
* nullable: true
* county:
* type: string
* nullable: true
* example:
* city: München
* zip: '80331'
* responses:
* 200:
* description: Updated address
* content:
* application/json:
* schema:
* type: object
* properties:
* address:
* $ref: '#/components/schemas/EntityAddress'
* 400:
* description: Address type required, invalid ids, or no company selected
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Not authorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Address, entity, or company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* delete:
* tags: [Entity]
* summary: Delete entity address
* description: Deletes one address of the entity.
* security:
* - cookieAuth: []
* x-markdown: entity/address/delete.md
* parameters:
* - in: path
* name: entityId
* required: true
* schema:
* type: integer
* - in: path
* name: id
* required: true
* schema:
* type: integer
* responses:
* 204:
* description: Deleted
* 400:
* description: No company selected or invalid ids
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Not authorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Address or entity not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.patch(`${basePath}/entity/:entityId/address/:id`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const entityId = Number(req.params.entityId);
const id = Number(req.params.id);
if (!Number.isInteger(entityId) || entityId <= 0) {
return res.status(400).json({ error: 'entityId is required' });
}
if (!Number.isInteger(id) || id <= 0) {
return res.status(400).json({ error: 'id is required' });
}
const body = req.body ?? {};
const patch = {};
for (const key of ADDRESS_FIELDS) {
if (Object.prototype.hasOwnProperty.call(body, key)) {
patch[key] = body[key];
}
}
try {
const address = await db.update(req.user.id, entityId, id, patch);
res.status(200).json({ address });
} catch (error) {
const status = addressErrorStatus(error);
if (status === 500) console.error('Update entity address failed:', error);
res.status(status).json({ error: error.message });
}
});
app.delete(`${basePath}/entity/:entityId/address/:id`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const entityId = Number(req.params.entityId);
const id = Number(req.params.id);
if (!Number.isInteger(entityId) || entityId <= 0) {
return res.status(400).json({ error: 'entityId is required' });
}
if (!Number.isInteger(id) || id <= 0) {
return res.status(400).json({ error: 'id is required' });
}
try {
await db.remove(req.user.id, entityId, id);
res.sendStatus(204);
} catch (error) {
const status = addressErrorStatus(error);
if (status === 500) console.error('Delete entity address failed:', error);
res.status(status).json({ error: error.message });
}
});
}