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.
This commit is contained in:
seb
2026-08-07 06:22:13 +02:00
parent 2767568f5a
commit e6c24654dd
36 changed files with 496 additions and 189 deletions

View File

@@ -9,11 +9,15 @@ function requireSelectedCompany(req) {
}
function entityErrorStatus(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 === 'Customer group not found') return 404;
if (error.message === 'Name required') return 400;
if (error.message === 'Number required') return 400;
if (error.message === 'Number already in use') return 409;
if (error.message === 'Invalid role') return 400;
return 500;
}
@@ -31,7 +35,8 @@ export function register(app, basePath = '') {
* summary: List entities
* description: |
* Lists entities for the selected company. Optional `customergroupId`
* query filters to one group (must belong to the company).
* filters to one group. Optional `role=customer` excludes Lieferanten/POS
* groups (for quote/order pickers). `role` wins when both are set.
* security:
* - cookieAuth: []
* x-markdown: entity/list.md
@@ -41,6 +46,12 @@ export function register(app, basePath = '') {
* required: false
* schema:
* type: integer
* - in: query
* name: role
* required: false
* schema:
* type: string
* enum: [customer]
* responses:
* 200:
* description: Entity list
@@ -54,7 +65,7 @@ export function register(app, basePath = '') {
* items:
* $ref: '#/components/schemas/Entity'
* 400:
* description: No company selected or invalid customergroupId
* description: No company selected, invalid customergroupId, or invalid role
* content:
* application/json:
* schema:
@@ -82,6 +93,7 @@ export function register(app, basePath = '') {
* summary: Create entity
* description: |
* Creates an entity in a company customer group and writes `entityhistory` (`Erstellt`).
* Optional `number`: omit to allocate the next customer number (`type` entity).
* security:
* - cookieAuth: []
* x-markdown: entity/create.md
@@ -100,10 +112,14 @@ export function register(app, basePath = '') {
* firstName:
* type: string
* nullable: true
* number:
* type: string
* description: Customer number; omit to auto-allocate
* example:
* customergroupId: 1
* name: Müller
* firstName: Anna
* number: '10042'
* responses:
* 201:
* description: Created entity
@@ -115,7 +131,7 @@ export function register(app, basePath = '') {
* entity:
* $ref: '#/components/schemas/Entity'
* 400:
* description: Name required or no company selected
* description: Name/number required or no company selected
* content:
* application/json:
* schema:
@@ -138,6 +154,12 @@ export function register(app, basePath = '') {
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 409:
* description: Number already in use
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.get(`${basePath}/entity`, async (req, res) => {
const selected = requireSelectedCompany(req);
@@ -153,9 +175,12 @@ export function register(app, basePath = '') {
}
options.customergroupId = customergroupId;
}
if (Object.prototype.hasOwnProperty.call(req.query, 'role')) {
options.role = req.query.role;
}
try {
const entities = await db.list(req.user.id, selected.companyId, options);
const entities = await db.list(req.user.id, options);
res.status(200).json({ entities });
} catch (error) {
const status = entityErrorStatus(error);
@@ -170,14 +195,18 @@ export function register(app, basePath = '') {
return res.status(400).json({ error: selected.error });
}
const { customergroupId, name, firstName } = req.body ?? {};
const body = req.body ?? {};
const data = {
customergroupId: body.customergroupId,
name: body.name,
firstName: body.firstName,
};
if (Object.prototype.hasOwnProperty.call(body, 'number')) {
data.number = body.number;
}
try {
const entity = await db.create(req.user.id, selected.companyId, {
customergroupId,
name,
firstName,
});
const entity = await db.create(req.user.id, data);
res.status(201).json({ entity });
} catch (error) {
const status = entityErrorStatus(error);
@@ -186,6 +215,72 @@ export function register(app, basePath = '') {
}
});
/**
* @openapi
* /entity/next-number:
* get:
* tags: [Entity]
* summary: Peek next customer number
* description: |
* Returns the next customer number that would be allocated (`type` entity)
* without consuming it. Use on the customer add form.
* security:
* - cookieAuth: []
* x-markdown: entity/next-number.md
* responses:
* 200:
* description: Next number
* content:
* application/json:
* schema:
* type: object
* required: [number]
* properties:
* number:
* type: string
* example:
* number: '1'
* 400:
* description: 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: Company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.get(`${basePath}/entity/next-number`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
try {
const number = await db.peekNextNumber(req.user.id);
res.status(200).json({ number });
} catch (error) {
const status = entityErrorStatus(error);
if (status === 500) console.error('Peek next entity number failed:', error);
res.status(status).json({ error: error.message });
}
});
/**
* @openapi
* /entity/{id}:
@@ -240,7 +335,9 @@ export function register(app, basePath = '') {
* tags: [Entity]
* summary: Update entity
* description: |
* Patches `name` and/or `firstName`. Writes `entityhistory` when values change.
* Patches fields that are present (`name`, `firstName`, `number`).
* `number` is only changed when the key is present (no auto-allocate).
* Writes `entityhistory` when values change.
* security:
* - cookieAuth: []
* x-markdown: entity/update.md
@@ -262,9 +359,13 @@ export function register(app, basePath = '') {
* firstName:
* type: string
* nullable: true
* number:
* type: string
* nullable: true
* example:
* name: Müller
* firstName: Anna
* number: '10042'
* responses:
* 200:
* description: Updated entity
@@ -276,7 +377,7 @@ export function register(app, basePath = '') {
* entity:
* $ref: '#/components/schemas/Entity'
* 400:
* description: Name required, invalid id, or no company selected
* description: Name/number required, invalid id, or no company selected
* content:
* application/json:
* schema:
@@ -299,6 +400,12 @@ export function register(app, basePath = '') {
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 409:
* description: Number already in use
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* delete:
* tags: [Entity]
* summary: Delete entity
@@ -352,7 +459,7 @@ export function register(app, basePath = '') {
}
try {
const entity = await db.getById(req.user.id, selected.companyId, id);
const entity = await db.getById(req.user.id, id);
res.status(200).json({ entity });
} catch (error) {
const status = entityErrorStatus(error);
@@ -380,9 +487,12 @@ export function register(app, basePath = '') {
if (Object.prototype.hasOwnProperty.call(body, 'firstName')) {
patch.firstName = body.firstName;
}
if (Object.prototype.hasOwnProperty.call(body, 'number')) {
patch.number = body.number;
}
try {
const entity = await db.update(req.user.id, selected.companyId, id, patch);
const entity = await db.update(req.user.id, id, patch);
res.status(200).json({ entity });
} catch (error) {
const status = entityErrorStatus(error);
@@ -403,7 +513,7 @@ export function register(app, basePath = '') {
}
try {
await db.remove(req.user.id, selected.companyId, id);
await db.remove(req.user.id, id);
res.sendStatus(204);
} catch (error) {
const status = entityErrorStatus(error);