u
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import * as db from '../../db/quote.js';
|
||||
import * as orderDb from '../../db/order.js';
|
||||
import { createAndAttachManifestPdf } from '../../services/manifestPdf.js';
|
||||
import { emit } from '../sse.js';
|
||||
|
||||
@@ -17,12 +18,14 @@ function quoteErrorStatus(error) {
|
||||
if (error.message === 'Not authorized') return 403;
|
||||
if (error.message === 'Company not found') return 404;
|
||||
if (error.message === 'Quote not found') return 404;
|
||||
if (error.message === 'Order not found') return 404;
|
||||
if (error.message === 'Manifest not found') return 404;
|
||||
if (error.message === 'Attachment not found') return 404;
|
||||
if (error.message === 'Entity not found') return 404;
|
||||
if (error.message === 'Article not found') return 404;
|
||||
if (error.message === 'Invalid entityId') return 400;
|
||||
if (error.message === 'Invalid quote') return 400;
|
||||
if (error.message === 'Invalid order') return 400;
|
||||
if (error.message === 'Invalid lines') return 400;
|
||||
if (error.message === 'File name required') return 400;
|
||||
if (error.message === 'Mimetype required') return 400;
|
||||
@@ -570,6 +573,134 @@ export function register(app, basePath = '') {
|
||||
|
||||
// #endregion
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /quote/{id}/order:
|
||||
* post:
|
||||
* tags: [Quote]
|
||||
* summary: Create order from quote
|
||||
* description: |
|
||||
* Creates a sales order from an existing quote for the authenticated user's selected company.
|
||||
* Copies lines, customer entity, and external number from the quote unless explicitly overridden.
|
||||
* If the order number is omitted, it is auto-allocated from the sequence.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: Quote ID
|
||||
* requestBody:
|
||||
* required: false
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* number:
|
||||
* type: string
|
||||
* description: Optional sales order number. Auto-allocated from sequence if omitted.
|
||||
* date:
|
||||
* type: string
|
||||
* description: Optional order date (ISO format). Defaults to current date.
|
||||
* createdAt:
|
||||
* type: string
|
||||
* format: date-time
|
||||
* description: Optional creation timestamp.
|
||||
* entityId:
|
||||
* type: integer
|
||||
* nullable: true
|
||||
* description: Customer entity ID override. Defaults to quote's entityId.
|
||||
* externalNumber:
|
||||
* type: string
|
||||
* nullable: true
|
||||
* description: External customer reference override. Defaults to quote's externalNumber.
|
||||
* lines:
|
||||
* type: array
|
||||
* description: Optional line items override. Defaults to quote's lines.
|
||||
* items:
|
||||
* $ref: '#/components/schemas/ManifestLineInput'
|
||||
* x-markdown: quote/order.md
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Created order
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* order:
|
||||
* $ref: '#/components/schemas/Order'
|
||||
* 400:
|
||||
* description: No company selected, invalid quote id, or invalid order data
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 401:
|
||||
* description: Unauthorized
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 403:
|
||||
* description: Not authorized for company
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 404:
|
||||
* description: Quote not found or entity not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 500:
|
||||
* description: Server error
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
// #region POST /quote/:id/order
|
||||
|
||||
app.post(`${basePath}/quote/:id/order`, 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 { number, date, createdAt, entityId, externalNumber, lines } = req.body ?? {};
|
||||
if (entityId !== undefined && entityId !== null && (typeof entityId !== 'number' || !Number.isInteger(entityId) || entityId <= 0)) {
|
||||
return res.status(400).json({ error: 'Invalid entityId' });
|
||||
}
|
||||
|
||||
try {
|
||||
const order = await orderDb.createFromQuote(req.user.id, id, {
|
||||
number,
|
||||
date,
|
||||
createdAt,
|
||||
entityId,
|
||||
externalNumber: externalNumber !== undefined && externalNumber !== null ? String(externalNumber) : externalNumber,
|
||||
lines,
|
||||
});
|
||||
res.status(201).json({ order });
|
||||
} catch (error) {
|
||||
const status = quoteErrorStatus(error);
|
||||
if (status === 500) console.error('Create order from quote failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// #endregion
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /quote/{id}/pdf:
|
||||
|
||||
Reference in New Issue
Block a user