Add delivery and supplier HTTP APIs with expanded response docs.

Wire store-backed CRUD for goods receipts and suppliers, and teach the API doc generator to expand $ref/allOf/array samples so route markdown shows full JSON shapes.
This commit is contained in:
seb
2026-08-06 05:32:48 +02:00
parent a748bd5f5c
commit 1860f38640
52 changed files with 2515 additions and 66 deletions

View File

@@ -110,6 +110,96 @@
* gsprpinfo:
* type: string
* nullable: true
* Supplier:
* type: object
* required: [id, name, createdAt, updatedAt]
* properties:
* id:
* type: integer
* name:
* type: string
* createdAt:
* type: string
* format: date-time
* updatedAt:
* type: string
* format: date-time
* DeliverySummary:
* type: object
* required: [id, number, type, supplierId, date]
* properties:
* id:
* type: integer
* number:
* type: string
* type:
* type: string
* enum: [delivery]
* supplierId:
* type: integer
* nullable: true
* description: manifest.entity_id
* date:
* type: string
* format: date-time
* DeliveryLine:
* type: object
* required: [id, articleId, sku, amount, net, transactedAmount, rebate, tax, comment]
* properties:
* id:
* type: integer
* articleId:
* type: integer
* nullable: true
* sku:
* type: string
* nullable: true
* amount:
* type: number
* net:
* type: number
* transactedAmount:
* type: number
* rebate:
* type: number
* tax:
* type: number
* description: tax.rate
* comment:
* type: string
* nullable: true
* DeliveryLineInput:
* type: object
* required: [sku, amount, net, tax]
* properties:
* sku:
* type: string
* amount:
* type: number
* net:
* type: number
* tax:
* type: number
* description: Tax rate (resolved via tax.ensure)
* rebate:
* type: number
* description: Defaults to 0
* transactedAmount:
* type: number
* description: Defaults to amount
* comment:
* type: string
* nullable: true
* Delivery:
* allOf:
* - $ref: '#/components/schemas/DeliverySummary'
* - type: object
* required: [lines]
* properties:
* lines:
* type: array
* items:
* $ref: '#/components/schemas/DeliveryLine'
* ArticleImage:
* type: object
* required: [picId, sort, full, thumb, hash, thumbHash]

429
src/api/routes/delivery.js Normal file
View File

@@ -0,0 +1,429 @@
import * as db from '../../db/delivery.js';
function requireSelectedCompany(req) {
const companyId = req.user.selectedCompanyId;
if (companyId == null) {
return { error: 'No company selected' };
}
return { companyId };
}
function deliveryErrorStatus(error) {
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Delivery not found') return 404;
if (error.message === 'Supplier not found') return 404;
if (error.message === 'Article not found') return 404;
if (error.message === 'Invalid delivery') return 400;
if (error.message === 'Invalid lines') return 400;
return 500;
}
/**
* Registers delivery (goods receipt) routes for the authenticated user's selected company.
* @param {import('express').Application} app
* @param {string} [basePath='']
*/
export function register(app, basePath = '') {
/**
* @openapi
* /delivery:
* get:
* tags: [Delivery]
* summary: List deliveries
* description: |
* Lists goods receipts (`manifest` rows with `type = 'delivery'`)
* for the authenticated user's selected company.
* security:
* - cookieAuth: []
* x-markdown: delivery/list.md
* responses:
* 200:
* description: Delivery list
* content:
* application/json:
* schema:
* type: object
* properties:
* deliveries:
* type: array
* items:
* $ref: '#/components/schemas/DeliverySummary'
* 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'
* post:
* tags: [Delivery]
* summary: Create delivery
* description: |
* Creates a goods receipt with header and lines in one transaction.
* Each line requires `sku`, `amount`, `net`, and `tax` (rate).
* `rebate` defaults to `0`; `transactedAmount` defaults to `amount`.
* security:
* - cookieAuth: []
* x-markdown: delivery/create.md
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [number, date, lines]
* properties:
* number:
* type: string
* date:
* type: string
* format: date-time
* supplierId:
* type: integer
* nullable: true
* lines:
* type: array
* items:
* $ref: '#/components/schemas/DeliveryLineInput'
* example:
* number: DHL-123
* date: '2026-08-10'
* supplierId: 1
* lines:
* - sku: '1'
* amount: 10
* net: 99.5
* tax: 19
* responses:
* 201:
* description: Created delivery
* content:
* application/json:
* schema:
* type: object
* properties:
* delivery:
* $ref: '#/components/schemas/Delivery'
* 400:
* description: Invalid delivery, invalid lines, 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: Company, supplier, or article not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.get(`${basePath}/delivery`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
try {
const deliveries = await db.list(req.user.id, selected.companyId);
res.status(200).json({ deliveries });
} catch (error) {
const status = deliveryErrorStatus(error);
if (status === 500) console.error('List deliveries failed:', error);
res.status(status).json({ error: error.message });
}
});
app.post(`${basePath}/delivery`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const { number, date, supplierId, lines } = req.body ?? {};
try {
const delivery = await db.create(req.user.id, selected.companyId, {
number,
date,
supplierId,
lines,
});
res.status(201).json({ delivery });
} catch (error) {
const status = deliveryErrorStatus(error);
if (status === 500) console.error('Create delivery failed:', error);
res.status(status).json({ error: error.message });
}
});
/**
* @openapi
* /delivery/{id}:
* get:
* tags: [Delivery]
* summary: Get delivery
* description: Returns a goods receipt with lines for the selected company.
* security:
* - cookieAuth: []
* x-markdown: delivery/get.md
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* responses:
* 200:
* description: Delivery
* content:
* application/json:
* schema:
* type: object
* properties:
* delivery:
* $ref: '#/components/schemas/Delivery'
* 400:
* description: No company selected or invalid id
* 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: Delivery or company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* patch:
* tags: [Delivery]
* summary: Update delivery
* description: |
* Patches header fields that are present. If `lines` is present, replaces
* all lines; if omitted, leaves lines unchanged. Returns the full delivery.
* security:
* - cookieAuth: []
* x-markdown: delivery/update.md
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* number:
* type: string
* date:
* type: string
* format: date-time
* supplierId:
* type: integer
* nullable: true
* lines:
* type: array
* items:
* $ref: '#/components/schemas/DeliveryLineInput'
* example:
* number: DHL-124
* lines: []
* responses:
* 200:
* description: Updated delivery
* content:
* application/json:
* schema:
* type: object
* properties:
* delivery:
* $ref: '#/components/schemas/Delivery'
* 400:
* description: Invalid delivery, invalid lines, invalid id, 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: Delivery, supplier, article, or company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* delete:
* tags: [Delivery]
* summary: Delete delivery
* description: |
* Deletes the goods receipt (`manifestarticle` rows cascade).
* security:
* - cookieAuth: []
* x-markdown: delivery/delete.md
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* responses:
* 204:
* description: Deleted
* 400:
* description: No company selected or invalid id
* 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: Delivery not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.get(`${basePath}/delivery/:id`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const id = Number(req.params.id);
if (!Number.isInteger(id) || id <= 0) {
return res.status(400).json({ error: 'id is required' });
}
try {
const delivery = await db.getById(req.user.id, selected.companyId, id);
res.status(200).json({ delivery });
} catch (error) {
const status = deliveryErrorStatus(error);
if (status === 500) console.error('Get delivery failed:', error);
res.status(status).json({ error: error.message });
}
});
app.patch(`${basePath}/delivery/:id`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const id = Number(req.params.id);
if (!Number.isInteger(id) || id <= 0) {
return res.status(400).json({ error: 'id is required' });
}
const body = req.body ?? {};
const patch = {};
if (Object.prototype.hasOwnProperty.call(body, 'number')) {
patch.number = body.number;
}
if (Object.prototype.hasOwnProperty.call(body, 'date')) {
patch.date = body.date;
}
if (Object.prototype.hasOwnProperty.call(body, 'supplierId')) {
patch.supplierId = body.supplierId;
}
if (Object.prototype.hasOwnProperty.call(body, 'lines')) {
patch.lines = body.lines;
}
try {
const delivery = await db.update(req.user.id, selected.companyId, id, patch);
res.status(200).json({ delivery });
} catch (error) {
const status = deliveryErrorStatus(error);
if (status === 500) console.error('Update delivery failed:', error);
res.status(status).json({ error: error.message });
}
});
app.delete(`${basePath}/delivery/:id`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const id = Number(req.params.id);
if (!Number.isInteger(id) || id <= 0) {
return res.status(400).json({ error: 'id is required' });
}
try {
await db.remove(req.user.id, selected.companyId, id);
res.sendStatus(204);
} catch (error) {
const status = deliveryErrorStatus(error);
if (status === 500) console.error('Delete delivery failed:', error);
res.status(status).json({ error: error.message });
}
});
}

310
src/api/routes/supplier.js Normal file
View File

@@ -0,0 +1,310 @@
import * as db from '../../db/supplier.js';
function requireSelectedCompany(req) {
const companyId = req.user.selectedCompanyId;
if (companyId == null) {
return { error: 'No company selected' };
}
return { companyId };
}
function supplierErrorStatus(error) {
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Supplier not found') return 404;
if (error.message === 'Name required') return 400;
return 500;
}
/**
* Registers supplier routes for the authenticated user's selected company.
* @param {import('express').Application} app
* @param {string} [basePath='']
*/
export function register(app, basePath = '') {
/**
* @openapi
* /supplier:
* get:
* tags: [Supplier]
* summary: List suppliers
* description: |
* Lists suppliers (`entity` rows in the company `Lieferanten` customergroup)
* for the authenticated user's selected company.
* security:
* - cookieAuth: []
* x-markdown: supplier/list.md
* responses:
* 200:
* description: Supplier list
* content:
* application/json:
* schema:
* type: object
* properties:
* suppliers:
* type: array
* items:
* $ref: '#/components/schemas/Supplier'
* 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'
* post:
* tags: [Supplier]
* summary: Create supplier
* description: |
* Creates a supplier in the company `Lieferanten` group (created on first use).
* security:
* - cookieAuth: []
* x-markdown: supplier/create.md
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [name]
* properties:
* name:
* type: string
* example:
* name: Großhandel Müller
* responses:
* 201:
* description: Created supplier
* content:
* application/json:
* schema:
* type: object
* properties:
* supplier:
* $ref: '#/components/schemas/Supplier'
* 400:
* description: Name required 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: Company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.get(`${basePath}/supplier`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
try {
const suppliers = await db.list(req.user.id, selected.companyId);
res.status(200).json({ suppliers });
} catch (error) {
const status = supplierErrorStatus(error);
if (status === 500) console.error('List suppliers failed:', error);
res.status(status).json({ error: error.message });
}
});
app.post(`${basePath}/supplier`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const { name } = req.body ?? {};
try {
const supplier = await db.create(req.user.id, selected.companyId, { name });
res.status(201).json({ supplier });
} catch (error) {
const status = supplierErrorStatus(error);
if (status === 500) console.error('Create supplier failed:', error);
res.status(status).json({ error: error.message });
}
});
/**
* @openapi
* /supplier/{id}:
* patch:
* tags: [Supplier]
* summary: Update supplier
* description: Renames a supplier for the selected company.
* security:
* - cookieAuth: []
* x-markdown: supplier/update.md
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [name]
* properties:
* name:
* type: string
* example:
* name: Großhandel Müller GmbH
* responses:
* 200:
* description: Updated supplier
* content:
* application/json:
* schema:
* type: object
* properties:
* supplier:
* $ref: '#/components/schemas/Supplier'
* 400:
* description: Name required, invalid id, 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: Supplier or company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* delete:
* tags: [Supplier]
* summary: Delete supplier
* description: |
* Deletes the supplier entity for the selected company.
* Related `entityhistory` cascades; `manifest.entity_id` is set null.
* security:
* - cookieAuth: []
* x-markdown: supplier/delete.md
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* responses:
* 204:
* description: Deleted
* 400:
* description: No company selected or invalid id
* 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: Supplier not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.patch(`${basePath}/supplier/:id`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const id = Number(req.params.id);
if (!Number.isInteger(id) || id <= 0) {
return res.status(400).json({ error: 'id is required' });
}
const { name } = req.body ?? {};
try {
const supplier = await db.update(req.user.id, selected.companyId, id, { name });
res.status(200).json({ supplier });
} catch (error) {
const status = supplierErrorStatus(error);
if (status === 500) console.error('Update supplier failed:', error);
res.status(status).json({ error: error.message });
}
});
app.delete(`${basePath}/supplier/:id`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const id = Number(req.params.id);
if (!Number.isInteger(id) || id <= 0) {
return res.status(400).json({ error: 'id is required' });
}
try {
await db.remove(req.user.id, selected.companyId, id);
res.sendStatus(204);
} catch (error) {
const status = supplierErrorStatus(error);
if (status === 500) console.error('Delete supplier failed:', error);
res.status(status).json({ error: error.message });
}
});
}