This commit is contained in:
seb
2026-10-20 05:33:14 +02:00
parent de3e74c928
commit fde381937c
9 changed files with 137 additions and 29 deletions

View File

@@ -1,4 +1,5 @@
import * as db from '../../db/shipment.js';
import * as dbStock from '../../db/stock.js';
import { createAndAttachManifestPdf } from '../../services/manifestPdf.js';
import { emit } from '../sse.js';
@@ -37,10 +38,12 @@ function shipmentErrorStatus(error) {
}
if (
error.message?.startsWith('Shipment amount exceeds') ||
error.message?.startsWith('Insufficient stock') ||
error.message === 'Shipment number already exists' ||
error.message?.includes('cannot be deleted') ||
error.message?.includes('cannot be undone') ||
error.message?.includes('already shipped')
error.message?.includes('already shipped') ||
error.message?.includes('already cancelled')
) {
return 409;
}
@@ -143,8 +146,9 @@ export function register(app, basePath = '') {
* If `lines` is omitted or empty, all remaining unshipped items from the order are included automatically.
* On individual line items, `amount` is optional and defaults to the remaining unshipped quantity for that article.
* Auto-assigns the shipment number formatted as `<order_number><shipment_prefix><index>`, e.g. `AUF-000001-L-1`.
* Automatically books shipment lines out on the ledger and deducts warehouse stock for stock-tracked articles.
* Updates the materialized `ordersummary` table (`shipped_quantity`, `is_shipped`) and emits SSE events
* (`order`, `shipment`) so that the order list and summary tables are rerendered in real time.
* (`order`, `shipment`, `stock`, `ledger`, `articles`) so that inventory and summaries are rerendered in real time.
* security:
* - cookieAuth: []
* parameters:
@@ -155,22 +159,26 @@ export function register(app, basePath = '') {
* type: integer
* description: Order ID
* requestBody:
* required: false
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [lines]
* properties:
* date:
* type: string
* format: date
* description: Optional shipment date (defaults to current date)
* warehouseId:
* type: integer
* description: Optional target warehouse ID to deduct stock from (defaults to company default warehouse)
* externalNumber:
* type: string
* description: Optional external delivery note reference (defaults to order.externalNumber or order.number)
* lines:
* type: array
* description: Optional positions to ship. If omitted, all remaining unshipped items are shipped.
* description: Positions to ship
* items:
* type: object
* required: [sku]
@@ -222,7 +230,7 @@ export function register(app, basePath = '') {
* schema:
* $ref: '#/components/schemas/Error'
* 409:
* description: Cumulative shipment amount exceeds ordered amount, or all items are already shipped
* description: Cumulative shipment amount exceeds ordered amount, or insufficient stock
* content:
* application/json:
* schema:
@@ -246,7 +254,24 @@ export function register(app, basePath = '') {
...(req.body ?? {}),
orderId: parsedId.id,
});
if (shipment?.id) {
try {
await dbStock.ship(req.user.id, {
shipmentId: shipment.id,
warehouseId: req.body?.warehouseId,
});
} catch (shipErr) {
await db.remove(req.user.id, shipment.id).catch(() => {});
throw shipErr;
}
}
emitShipmentEvents(selected.companyId);
emit('stock', { companyId: selected.companyId });
emit('ledger', { companyId: selected.companyId });
emit('articles', { companyId: selected.companyId });
res.status(201).json({ shipment });
} catch (error) {
const status = shipmentErrorStatus(error);
@@ -265,8 +290,7 @@ export function register(app, basePath = '') {
* summary: List shipments
* description: |
* Company-wide paginated list of shipment manifests (`manifest` rows with `type = 'shipment'`)
* with delivery state,
entityId, package tracking info, and order references.
* with delivery state, entityId, package tracking info, and order references.
* security:
* - cookieAuth: []
* parameters:
@@ -294,8 +318,8 @@ export function register(app, basePath = '') {
* name: state
* schema:
* type: string
* enum: [new, delivered, returned]
* description: Optional filter by delivery state
* enum: [new, delivered, returned, cancelled, all]
* description: Optional filter by delivery state (cancelled shipments hidden by default)
* - in: query
* name: seekMonths
* schema:
@@ -376,7 +400,7 @@ export function register(app, basePath = '') {
let state = undefined;
if (typeof req.query.state === 'string' && req.query.state.trim() !== '') {
const trimmedState = req.query.state.trim().toLowerCase();
if (!['new', 'delivered', 'returned'].includes(trimmedState)) {
if (!['new', 'delivered', 'returned', 'cancelled', 'all'].includes(trimmedState)) {
return res.status(400).json({ error: 'Invalid state' });
}
state = trimmedState;
@@ -507,7 +531,7 @@ export function register(app, basePath = '') {
* properties:
* state:
* type: string
* enum: [new, delivered, returned]
* enum: [new, delivered, returned, cancelled]
* description: Delivery state
* example:
* state: delivered
@@ -581,7 +605,8 @@ export function register(app, basePath = '') {
* delete:
* tags: [Shipment]
* summary: Delete shipment
* description: Deletes an unshipped shipment manifest. Shipped shipments cannot be deleted.
* description: Deletes an unshipped shipment manifest or cancels a shipped shipment restoring stock. Shipped packages cannot be deleted.
* x-signals: [shipment, order, stock, ledger, articles]
* security:
* - cookieAuth: []
* parameters:
@@ -613,7 +638,7 @@ export function register(app, basePath = '') {
* schema:
* $ref: '#/components/schemas/Error'
* 409:
* description: Shipped shipment cannot be deleted
* description: Shipped shipment cannot be deleted or shipment already cancelled
* content:
* application/json:
* schema:
@@ -635,6 +660,9 @@ export function register(app, basePath = '') {
try {
await db.remove(req.user.id, parsedId.id);
emitShipmentEvents(selected.companyId);
emit('stock', { companyId: selected.companyId });
emit('ledger', { companyId: selected.companyId });
emit('articles', { companyId: selected.companyId });
res.status(204).end();
} catch (error) {
const status = shipmentErrorStatus(error);

View File

@@ -19,7 +19,8 @@ function stockErrorStatus(error) {
error.message?.includes('already accepted') ||
error.message?.includes('already shipped') ||
error.message?.includes('already booked') ||
error.message?.includes('Stock tracking is disabled')
error.message?.includes('Stock tracking is disabled') ||
error.message?.startsWith('Insufficient stock')
) {
return 409;
}