u
This commit is contained in:
@@ -23,7 +23,12 @@ function shippingErrorStatus(error) {
|
||||
if (error.message === 'No company selected') return 400;
|
||||
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?.endsWith(' required') ||
|
||||
error.message?.includes('not a shipment')
|
||||
) {
|
||||
return 400;
|
||||
}
|
||||
if (
|
||||
error.message?.includes('cannot be deleted') ||
|
||||
error.message?.includes('cannot be undone') ||
|
||||
@@ -227,6 +232,178 @@ export function register(app, basePath = '') {
|
||||
|
||||
// #endregion
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /shipment/{id}/shipping:
|
||||
* get:
|
||||
* tags: [Shipping]
|
||||
* summary: List packages of a shipment
|
||||
* description: Lists all packages (shippings) belonging to the specified shipment manifest.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: Shipment ID
|
||||
* responses:
|
||||
* 200:
|
||||
* description: List of packages
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* shippings:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Shipping'
|
||||
* 400:
|
||||
* description: 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'
|
||||
* 404:
|
||||
* description: Shipment not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
// #region GET /shipment/:id/shipping
|
||||
|
||||
app.get(`${basePath}/shipment/:id/shipping`, 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 shippings = await db.listByManifestId(req.user.id, parsedId.id);
|
||||
res.status(200).json({ shippings });
|
||||
} catch (error) {
|
||||
const status = shippingErrorStatus(error);
|
||||
if (status === 500) console.error('List shipment packages failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// #endregion
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /shipment/{id}/shipping:
|
||||
* post:
|
||||
* tags: [Shipping]
|
||||
* summary: Create package for a shipment
|
||||
* description: |
|
||||
* Creates a physical package (shipping) for the shipment manifest.
|
||||
* Accepts carrier and parcel tracking code. If state is `shipped`,
|
||||
* deducts stock for the shipment's lines via ledger movements.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: Shipment ID
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [type, state]
|
||||
* properties:
|
||||
* type:
|
||||
* type: string
|
||||
* state:
|
||||
* type: string
|
||||
* trackingCode:
|
||||
* type: string
|
||||
* description: Carrier parcel tracking code
|
||||
* carrier:
|
||||
* type: string
|
||||
* description: Carrier name (e.g. DHL, UPS, DPD)
|
||||
* warehouseId:
|
||||
* type: integer
|
||||
* description: Optional warehouse ID for stock deduction when state is shipped
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Created package
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* shipping:
|
||||
* $ref: '#/components/schemas/Shipping'
|
||||
* 400:
|
||||
* description: Validation error, manifest is not a shipment, or no company selected
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 401:
|
||||
* description: Unauthorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 404:
|
||||
* description: Shipment or warehouse not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
// #region POST /shipment/:id/shipping
|
||||
|
||||
app.post(`${basePath}/shipment/:id/shipping`, 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.create(req.user.id, {
|
||||
...(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);
|
||||
if (status === 500) console.error('Create shipment package failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// #endregion
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /shipping/{id}:
|
||||
@@ -256,6 +433,12 @@ export function register(app, basePath = '') {
|
||||
* type: string
|
||||
* state:
|
||||
* type: string
|
||||
* trackingCode:
|
||||
* type: string
|
||||
* description: Carrier parcel tracking code
|
||||
* carrier:
|
||||
* type: string
|
||||
* description: Carrier name (e.g. DHL, UPS, DPD)
|
||||
* warehouseId:
|
||||
* type: integer
|
||||
* description: Warehouse ID for stock deduction when transitioning to shipped
|
||||
|
||||
Reference in New Issue
Block a user