Add unit API and article unitamount/unitSi patch.
Units use the selected company; article PATCH accepts name/unitamount/unitSi.
This commit is contained in:
@@ -36,6 +36,8 @@ function articleErrorStatus(error) {
|
||||
if (error.message === 'Article has children') return 400;
|
||||
if (error.message === 'Invalid prices') return 400;
|
||||
if (error.message === 'POS customer group required') return 400;
|
||||
if (error.message === 'Invalid unitamount') return 400;
|
||||
if (error.message === 'Unit not found') return 404;
|
||||
return 500;
|
||||
}
|
||||
|
||||
@@ -185,7 +187,9 @@ export function register(app, basePath = '') {
|
||||
* patch:
|
||||
* tags: [Article]
|
||||
* summary: Update article by SKU
|
||||
* description: Updates an article name by SKU; returns the same detail shape as GET.
|
||||
* description: |
|
||||
* Patches article fields. At least one of `name`, `unitamount`, `unitSi` required.
|
||||
* `unitSi` must already exist for the company. Returns the same detail shape as GET.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: article/update.md
|
||||
@@ -202,12 +206,18 @@ export function register(app, basePath = '') {
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [name]
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* unitamount:
|
||||
* type: number
|
||||
* description: Must be finite and `> 0`
|
||||
* unitSi:
|
||||
* type: string
|
||||
* description: Existing company unit `si`
|
||||
* example:
|
||||
* name: Updated name
|
||||
* unitamount: 0.24
|
||||
* unitSi: l
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated article detail
|
||||
@@ -219,7 +229,7 @@ export function register(app, basePath = '') {
|
||||
* article:
|
||||
* $ref: '#/components/schemas/ArticleDetail'
|
||||
* 400:
|
||||
* description: Validation error
|
||||
* description: Validation error (Name required / Invalid unitamount)
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
@@ -237,7 +247,7 @@ export function register(app, basePath = '') {
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 404:
|
||||
* description: Article not found
|
||||
* description: Article or unit not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
@@ -309,13 +319,24 @@ export function register(app, basePath = '') {
|
||||
return res.status(400).json({ error: 'sku is required' });
|
||||
}
|
||||
|
||||
const name = typeof req.body?.name === 'string' ? req.body.name.trim() : '';
|
||||
if (!name) {
|
||||
return res.status(400).json({ error: 'name is required' });
|
||||
const body = req.body ?? {};
|
||||
const patch = {};
|
||||
if (Object.prototype.hasOwnProperty.call(body, 'name')) {
|
||||
patch.name = body.name;
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(body, 'unitamount')) {
|
||||
patch.unitamount = body.unitamount;
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(body, 'unitSi')) {
|
||||
patch.unitSi = body.unitSi;
|
||||
}
|
||||
|
||||
if (!('name' in patch) && !('unitamount' in patch) && !('unitSi' in patch)) {
|
||||
return res.status(400).json({ error: 'name, unitamount, or unitSi is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const article = await db.updateBySku(req.user.id, decodeURIComponent(sku), { name });
|
||||
const article = await db.updateBySku(req.user.id, decodeURIComponent(sku), patch);
|
||||
res.status(200).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
|
||||
Reference in New Issue
Block a user