u
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import * as db from '../../db/shipping.js';
|
||||
import { emit } from '../sse.js';
|
||||
|
||||
// #region Helpers
|
||||
|
||||
@@ -23,9 +24,23 @@ function shippingErrorStatus(error) {
|
||||
if (error.message === 'Not authorized') return 403;
|
||||
if (error.message?.endsWith(' not found')) return 404;
|
||||
if (error.message?.endsWith(' required')) return 400;
|
||||
if (
|
||||
error.message?.includes('cannot be deleted') ||
|
||||
error.message?.includes('cannot be undone') ||
|
||||
error.message?.includes('already shipped')
|
||||
) {
|
||||
return 409;
|
||||
}
|
||||
return 500;
|
||||
}
|
||||
|
||||
function emitShippingEvents(companyId) {
|
||||
emit('stock', { companyId });
|
||||
emit('ledger', { companyId });
|
||||
emit('articles', { companyId });
|
||||
emit('order', { companyId });
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
/**
|
||||
@@ -115,6 +130,7 @@ export function register(app, basePath = '') {
|
||||
* description: |
|
||||
* Creates a shipping for the order. The shipping number is allocated from the
|
||||
* `shipping` number sequence (prefix and counter from the numbers API).
|
||||
* If `state` is `shipped`, stock is adjusted via the ledger and article history is written.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
@@ -136,6 +152,9 @@ export function register(app, basePath = '') {
|
||||
* type: string
|
||||
* state:
|
||||
* type: string
|
||||
* warehouseId:
|
||||
* type: integer
|
||||
* description: Optional warehouse ID for stock deduction when state is shipped
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Created shipping
|
||||
@@ -165,7 +184,13 @@ export function register(app, basePath = '') {
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 404:
|
||||
* description: Order not found
|
||||
* description: Order or warehouse not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 409:
|
||||
* description: Order already shipped
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
@@ -189,6 +214,9 @@ export function register(app, basePath = '') {
|
||||
...(req.body ?? {}),
|
||||
manifestId: parsedId.id,
|
||||
});
|
||||
if (String(shipping.state || '').trim().toLowerCase() === 'shipped') {
|
||||
emitShippingEvents(selected.companyId);
|
||||
}
|
||||
res.status(201).json({ shipping });
|
||||
} catch (error) {
|
||||
const status = shippingErrorStatus(error);
|
||||
@@ -199,13 +227,116 @@ export function register(app, basePath = '') {
|
||||
|
||||
// #endregion
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /shipping/{id}:
|
||||
* patch:
|
||||
* tags: [Shipping]
|
||||
* summary: Update shipping
|
||||
* description: |
|
||||
* Updates a shipping record. If transitioned to `shipped`, stock is adjusted
|
||||
* via ledger and article history is written. Once shipped, it cannot be undone.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: Shipping ID
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* type:
|
||||
* type: string
|
||||
* state:
|
||||
* type: string
|
||||
* warehouseId:
|
||||
* type: integer
|
||||
* description: Warehouse ID for stock deduction when transitioning to shipped
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated shipping
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* shipping:
|
||||
* $ref: '#/components/schemas/Shipping'
|
||||
* 400:
|
||||
* description: Validation error 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: Shipping or warehouse not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 409:
|
||||
* description: Shipped shipping cannot be undone, or order already shipped
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
// #region PATCH /shipping/:id
|
||||
|
||||
app.patch(`${basePath}/shipping/:id`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const parsedId = parsePositiveInt(req.params.id, 'id');
|
||||
if (parsedId.error) {
|
||||
return res.status(400).json({ error: parsedId.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const shipping = await db.update(req.user.id, parsedId.id, req.body ?? {});
|
||||
if (String(shipping.state || '').trim().toLowerCase() === 'shipped') {
|
||||
emitShippingEvents(selected.companyId);
|
||||
}
|
||||
res.status(200).json({ shipping });
|
||||
} catch (error) {
|
||||
const status = shippingErrorStatus(error);
|
||||
if (status === 500) console.error('Update shipping failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// #endregion
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /shipping/{id}:
|
||||
* delete:
|
||||
* tags: [Shipping]
|
||||
* summary: Delete shipping
|
||||
* description: Deletes a shipping of the selected company. Its packages are removed as well.
|
||||
* description: |
|
||||
* Deletes a shipping of the selected company. Its packages are removed as well.
|
||||
* Shipped shippings cannot be deleted because stock movements have been ledgered.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
@@ -236,6 +367,18 @@ export function register(app, basePath = '') {
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 404:
|
||||
* description: Shipping not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 409:
|
||||
* description: Shipped shipping cannot be deleted
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
// #region DELETE /shipping/:id
|
||||
|
||||
|
||||
Reference in New Issue
Block a user