This commit is contained in:
seb
2026-09-27 21:01:20 +02:00
parent bab15b0978
commit 68b793c9ba
17 changed files with 1695 additions and 23 deletions

View File

@@ -61,6 +61,7 @@ function articleErrorStatus(error) {
if (error.message === 'Company not found') return 404;
if (error.message === 'Brand not found') return 404;
if (error.message === 'Invalid weight') return 400;
if (error.message === 'Invalid track_stock') return 400;
if (error.message === 'Invalid categoryIds') return 400;
if (error.message?.includes('already exists') || error.code === 'SQLITE_CONSTRAINT_UNIQUE') return 409;
return 500;
@@ -96,6 +97,9 @@ export function register(app, basePath = '') {
* description: Category id
* name:
* type: string
* track_stock:
* type: boolean
* description: Whether to track inventory stock (defaults to true)
* example:
* parentId: 10
* name: New article
@@ -151,11 +155,18 @@ export function register(app, basePath = '') {
return res.status(400).json({ error: 'name is required' });
}
const rawTrack = req.body?.track_stock ?? req.body?.trackStock;
if (rawTrack !== undefined && typeof rawTrack !== 'boolean' && rawTrack !== 0 && rawTrack !== 1) {
return res.status(400).json({ error: 'Invalid track_stock' });
}
const track_stock = rawTrack !== undefined ? Boolean(rawTrack) : true;
try {
const article = await db.createUnderParent({
googleAuthId: req.user.id,
parentId: parent.id,
name,
track_stock,
});
emitArticlesSignal(req);
res.status(201).json({ article });
@@ -353,6 +364,9 @@ export function register(app, basePath = '') {
* unitSi:
* type: string
* description: Existing company unit `si`
* track_stock:
* type: boolean
* description: Whether to track inventory stock
* example:
* unitamount: 0.24
* unitSi: l
@@ -550,9 +564,14 @@ export function register(app, basePath = '') {
if (Object.prototype.hasOwnProperty.call(body, 'unitSi')) {
patch.unitSi = body.unitSi;
}
if (Object.prototype.hasOwnProperty.call(body, 'track_stock')) {
patch.track_stock = body.track_stock;
} else if (Object.prototype.hasOwnProperty.call(body, 'trackStock')) {
patch.track_stock = body.trackStock;
}
if (!('name' in patch) && !('unitamount' in patch) && !('unitSi' in patch)) {
return res.status(400).json({ error: 'name, unitamount, or unitSi is required' });
if (!('name' in patch) && !('unitamount' in patch) && !('unitSi' in patch) && !('track_stock' in patch)) {
return res.status(400).json({ error: 'name, unitamount, unitSi, or track_stock is required' });
}
try {
@@ -1218,6 +1237,97 @@ export function register(app, basePath = '') {
}
});
/**
* @openapi
* /article/{sku}/track_stock:
* put:
* tags: [Article]
* summary: Set article track_stock flag by SKU
* description: |
* Enables or disables inventory stock tracking for this article.
* security:
* - cookieAuth: []
* x-signals: [article-<sku>]
* parameters:
* - in: path
* name: sku
* required: true
* schema:
* type: string
* description: Article SKU
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [track_stock]
* properties:
* track_stock:
* type: boolean
* description: Whether to track inventory stock for this article
* example:
* track_stock: true
* responses:
* 200:
* description: Updated article detail
* content:
* application/json:
* schema:
* type: object
* properties:
* article:
* $ref: '#/components/schemas/ArticleDetail'
* 400:
* description: Invalid track_stock
* 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: Article not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
async function handleSetTrackStock(req, res) {
const skuParsed = parseSku(req.params.sku);
if (skuParsed.error) {
return res.status(400).json({ error: skuParsed.error });
}
const raw = req.body?.track_stock ?? req.body?.trackStock;
if (raw === undefined || (typeof raw !== 'boolean' && raw !== 0 && raw !== 1)) {
return res.status(400).json({ error: 'Invalid track_stock' });
}
try {
const article = await db.setTrackStockBySku(req.user.id, skuParsed.sku, Boolean(raw));
emitArticleSignal(req, skuParsed.sku);
res.status(200).json({ article });
} catch (error) {
const status = articleErrorStatus(error);
if (status === 500) console.error('Set article track_stock failed:', error);
res.status(status).json({ error: error.message });
}
}
app.put(`${basePath}/article/:sku/track_stock`, handleSetTrackStock);
app.put(`${basePath}/article/:sku/track-stock`, handleSetTrackStock);
/**
* @openapi
* /article/{sku}/categories: