u
This commit is contained in:
@@ -38,6 +38,10 @@ function articleErrorStatus(error) {
|
||||
if (error.message === 'POS customer group required') return 400;
|
||||
if (error.message === 'Invalid unitamount') return 400;
|
||||
if (error.message === 'Unit not found') return 404;
|
||||
if (error.message === 'Invalid eans') return 400;
|
||||
if (error.message === 'Brand not found') return 404;
|
||||
if (error.message === 'Invalid weight') return 400;
|
||||
if (error.message === 'Invalid categoryIds') return 400;
|
||||
return 500;
|
||||
}
|
||||
|
||||
@@ -724,4 +728,350 @@ export function register(app, basePath = '') {
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /article/{sku}/eans:
|
||||
* put:
|
||||
* tags: [Article]
|
||||
* summary: Replace article EANs by SKU
|
||||
* description: |
|
||||
* Replace-all EANs for the article. EANs must be non-empty unique strings;
|
||||
* empty array clears all EANs. Returns the same shape as GET by SKU.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: article/set-eans.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: sku
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [eans]
|
||||
* properties:
|
||||
* eans:
|
||||
* type: array
|
||||
* items:
|
||||
* type: string
|
||||
* example:
|
||||
* eans: ["4001234567890"]
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated article detail
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* article:
|
||||
* $ref: '#/components/schemas/ArticleDetail'
|
||||
* 400:
|
||||
* description: Invalid eans
|
||||
* 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'
|
||||
*/
|
||||
app.put(`${basePath}/article/:sku/eans`, async (req, res) => {
|
||||
const skuParsed = parseSku(req.params.sku);
|
||||
if (skuParsed.error) {
|
||||
return res.status(400).json({ error: skuParsed.error });
|
||||
}
|
||||
|
||||
if (!Array.isArray(req.body?.eans)) {
|
||||
return res.status(400).json({ error: 'eans is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const article = await db.setEansBySku(req.user.id, skuParsed.sku, req.body.eans);
|
||||
res.status(200).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Set article eans failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /article/{sku}/brand:
|
||||
* put:
|
||||
* tags: [Article]
|
||||
* summary: Set or clear article brand by SKU
|
||||
* description: |
|
||||
* Sets the article's `brandId` (must belong to the article's company).
|
||||
* Pass `null` to clear the brand. Returns the same shape as GET by SKU.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: article/set-brand.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: sku
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [brandId]
|
||||
* properties:
|
||||
* brandId:
|
||||
* type: integer
|
||||
* nullable: true
|
||||
* example:
|
||||
* brandId: 3
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated article detail
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* article:
|
||||
* $ref: '#/components/schemas/ArticleDetail'
|
||||
* 400:
|
||||
* description: Validation error
|
||||
* 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 or brand not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.put(`${basePath}/article/:sku/brand`, async (req, res) => {
|
||||
const skuParsed = parseSku(req.params.sku);
|
||||
if (skuParsed.error) {
|
||||
return res.status(400).json({ error: skuParsed.error });
|
||||
}
|
||||
|
||||
const brandId = req.body?.brandId ?? null;
|
||||
|
||||
try {
|
||||
const article = await db.setBrandIdBySku(req.user.id, skuParsed.sku, brandId);
|
||||
res.status(200).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Set article brand failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /article/{sku}/weight:
|
||||
* put:
|
||||
* tags: [Article]
|
||||
* summary: Set article weight by SKU
|
||||
* description: |
|
||||
* Sets the article's `weight`. Must be finite and `>= 0`.
|
||||
* Returns the same shape as GET by SKU.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: article/set-weight.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: sku
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [weight]
|
||||
* properties:
|
||||
* weight:
|
||||
* type: number
|
||||
* example:
|
||||
* weight: 1.5
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated article detail
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* article:
|
||||
* $ref: '#/components/schemas/ArticleDetail'
|
||||
* 400:
|
||||
* description: Invalid weight
|
||||
* 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'
|
||||
*/
|
||||
app.put(`${basePath}/article/:sku/weight`, async (req, res) => {
|
||||
const skuParsed = parseSku(req.params.sku);
|
||||
if (skuParsed.error) {
|
||||
return res.status(400).json({ error: skuParsed.error });
|
||||
}
|
||||
|
||||
const weight = req.body?.weight;
|
||||
|
||||
try {
|
||||
const article = await db.setWeightBySku(req.user.id, skuParsed.sku, weight);
|
||||
res.status(200).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Set article weight failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /article/{sku}/categories:
|
||||
* put:
|
||||
* tags: [Article]
|
||||
* summary: Replace article categories by SKU
|
||||
* description: |
|
||||
* Replace-all the article's category list. Each category must belong to
|
||||
* the article's company; ids must be unique. Empty array removes the
|
||||
* article from all categories. Returns the same shape as GET by SKU.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: article/set-categories.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: sku
|
||||
* required: true
|
||||
* schema:
|
||||
* type: string
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [categoryIds]
|
||||
* properties:
|
||||
* categoryIds:
|
||||
* type: array
|
||||
* items:
|
||||
* type: integer
|
||||
* example:
|
||||
* categoryIds: [4, 7]
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated article detail
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* article:
|
||||
* $ref: '#/components/schemas/ArticleDetail'
|
||||
* 400:
|
||||
* description: Invalid categoryIds
|
||||
* 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'
|
||||
*/
|
||||
app.put(`${basePath}/article/:sku/categories`, async (req, res) => {
|
||||
const skuParsed = parseSku(req.params.sku);
|
||||
if (skuParsed.error) {
|
||||
return res.status(400).json({ error: skuParsed.error });
|
||||
}
|
||||
|
||||
if (!Array.isArray(req.body?.categoryIds)) {
|
||||
return res.status(400).json({ error: 'categoryIds is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const article = await db.setCategoryIdsBySku(
|
||||
req.user.id,
|
||||
skuParsed.sku,
|
||||
req.body.categoryIds,
|
||||
);
|
||||
res.status(200).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Set article categories failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
239
src/api/routes/brand.js
Normal file
239
src/api/routes/brand.js
Normal file
@@ -0,0 +1,239 @@
|
||||
import * as db from '../../db/brand.js';
|
||||
|
||||
function requireSelectedCompany(req) {
|
||||
const companyId = req.user.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
return { error: 'No company selected' };
|
||||
}
|
||||
return { companyId };
|
||||
}
|
||||
|
||||
function brandErrorStatus(error) {
|
||||
if (error.message === 'Not authorized') return 403;
|
||||
if (error.message === 'Company not found') return 404;
|
||||
if (error.message === 'Brand not found') return 404;
|
||||
if (error.message === 'Invalid brand') return 400;
|
||||
if (error.message === 'Brand in use') return 409;
|
||||
return 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers brand routes for the authenticated user's selected company.
|
||||
* @param {import('express').Application} app
|
||||
* @param {string} [basePath='']
|
||||
*/
|
||||
export function register(app, basePath = '') {
|
||||
/**
|
||||
* @openapi
|
||||
* /brand:
|
||||
* get:
|
||||
* tags: [Brand]
|
||||
* summary: List brands
|
||||
* description: Lists brands for the authenticated user's selected company, sorted by name.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: brand/list.md
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Brand list
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* brands:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Brand'
|
||||
* 400:
|
||||
* description: 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: Company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* put:
|
||||
* tags: [Brand]
|
||||
* summary: Upsert brand
|
||||
* description: |
|
||||
* Insert or update brand by `name` for the selected company. `gsprpinfo` optional.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: brand/upsert.md
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [name]
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* gsprpinfo:
|
||||
* type: string
|
||||
* example:
|
||||
* name: Acme
|
||||
* gsprpinfo: 'Acme GmbH'
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Upserted brand
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* brand:
|
||||
* $ref: '#/components/schemas/Brand'
|
||||
* 400:
|
||||
* description: Invalid brand 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: Company not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.get(`${basePath}/brand`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const brands = await db.list(req.user.id, selected.companyId);
|
||||
res.status(200).json({ brands });
|
||||
} catch (error) {
|
||||
const status = brandErrorStatus(error);
|
||||
if (status === 500) console.error('List brands failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.put(`${basePath}/brand`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
const { name, gsprpinfo } = req.body ?? {};
|
||||
|
||||
try {
|
||||
const brand = await db.upsert(req.user.id, selected.companyId, {
|
||||
name,
|
||||
gsprpinfo,
|
||||
});
|
||||
res.status(200).json({ brand });
|
||||
} catch (error) {
|
||||
const status = brandErrorStatus(error);
|
||||
if (status === 500) console.error('Upsert brand failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /brand/{id}:
|
||||
* delete:
|
||||
* tags: [Brand]
|
||||
* summary: Delete brand
|
||||
* description: |
|
||||
* Deletes the brand for the selected company. Fails with `Brand in use`
|
||||
* if any article still references it.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: brand/delete.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 204:
|
||||
* description: Deleted
|
||||
* 400:
|
||||
* description: 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: Brand not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 409:
|
||||
* description: Brand in use
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
app.delete(`${basePath}/brand/:id`, 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' });
|
||||
}
|
||||
|
||||
try {
|
||||
await db.remove(req.user.id, selected.companyId, id);
|
||||
res.sendStatus(204);
|
||||
} catch (error) {
|
||||
const status = brandErrorStatus(error);
|
||||
if (status === 500) console.error('Delete brand failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -13,6 +13,8 @@ function categoryErrorStatus(error) {
|
||||
if (error.message === 'Company not found') return 404;
|
||||
if (error.message === 'Parent not found') return 404;
|
||||
if (error.message === 'Cannot delete root category') return 400;
|
||||
if (error.message === 'Name required') return 400;
|
||||
if (error.message === 'Category not found') return 404;
|
||||
return 500;
|
||||
}
|
||||
|
||||
@@ -246,6 +248,74 @@ export function register(app, basePath = '') {
|
||||
/**
|
||||
* @openapi
|
||||
* /category/{id}:
|
||||
* patch:
|
||||
* tags: [Category]
|
||||
* summary: Rename category
|
||||
* description: Renames a category the user is authorized for (updates name and re-slugifies in the company default language).
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: category/rename.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: Category id
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [name]
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* example:
|
||||
* name: Renamed category
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Renamed category
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* category:
|
||||
* type: object
|
||||
* required: [id, name, slug]
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* name:
|
||||
* type: string
|
||||
* slug:
|
||||
* type: string
|
||||
* 400:
|
||||
* description: Validation error (name required)
|
||||
* 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: Category not found
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* delete:
|
||||
* tags: [Category]
|
||||
* summary: Delete category
|
||||
@@ -300,4 +370,29 @@ export function register(app, basePath = '') {
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.patch(`${basePath}/category/:id`, async (req, res) => {
|
||||
const parsed = parsePositiveInt(req.params.id, 'id');
|
||||
if (parsed.error) {
|
||||
return res.status(400).json({ error: parsed.error });
|
||||
}
|
||||
|
||||
const name = typeof req.body?.name === 'string' ? req.body.name.trim() : '';
|
||||
if (!name) {
|
||||
return res.status(400).json({ error: 'name is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const category = await db.rename({
|
||||
googleAuthId: req.user.id,
|
||||
id: parsed.id,
|
||||
name,
|
||||
});
|
||||
res.status(200).json({ category });
|
||||
} catch (error) {
|
||||
const status = categoryErrorStatus(error);
|
||||
if (status === 500) console.error('Rename category failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user