u
This commit is contained in:
251
src/api/routes/attributevalue.js
Normal file
251
src/api/routes/attributevalue.js
Normal file
@@ -0,0 +1,251 @@
|
||||
import * as db from '../../db/attributevalue.js';
|
||||
import { emit } from '../sse.js';
|
||||
|
||||
function requireSelectedCompany(req) {
|
||||
const companyId = req.user?.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
return { error: 'No company selected' };
|
||||
}
|
||||
return { companyId };
|
||||
}
|
||||
|
||||
function attributeValueErrorStatus(error) {
|
||||
if (error.message === 'No company selected') return 400;
|
||||
if (error.message === 'Not authorized') return 403;
|
||||
if (error.message === 'Company not found' || error.message === 'Attribute name not found' || error.message === 'Attribute value not found') return 404;
|
||||
if (error.message === 'Value required') return 400;
|
||||
if (error.message === 'Invalid attribute value order') return 400;
|
||||
if (error.message === 'Invalid id' || error.message === 'Invalid attributeId') return 400;
|
||||
return 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers attributevalue routes under `/attributename/:attributeId/value` and `/attributevalue/:id`.
|
||||
* @param {import('express').Application} app
|
||||
* @param {string} [basePath='']
|
||||
*/
|
||||
export function register(app, basePath = '') {
|
||||
/**
|
||||
* @openapi
|
||||
* /attributename/{attributeId}/value:
|
||||
* get:
|
||||
* tags: [Article]
|
||||
* summary: List values for an attribute
|
||||
* description: Lists all values for a specific attribute.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: attributeId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: List of attribute values
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* values:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/AttributeValue'
|
||||
* post:
|
||||
* tags: [Article]
|
||||
* summary: Create value for an attribute
|
||||
* description: Adds a new predefined value to an attribute.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: attributeId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [value]
|
||||
* properties:
|
||||
* value:
|
||||
* type: string
|
||||
* sort:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Created attribute value
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* attributevalue:
|
||||
* $ref: '#/components/schemas/AttributeValue'
|
||||
*/
|
||||
app.get(`${basePath}/attributename/:attributeId/value`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const attributeId = Number(req.params.attributeId);
|
||||
const values = await db.listByAttributeId(req.user.id, attributeId);
|
||||
res.json({ values });
|
||||
} catch (error) {
|
||||
res.status(attributeValueErrorStatus(error)).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post(`${basePath}/attributename/:attributeId/value`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const attributeId = Number(req.params.attributeId);
|
||||
const attributevalue = await db.create(req.user.id, attributeId, req.body ?? {});
|
||||
emit('attributename', { companyId: selected.companyId });
|
||||
res.status(201).json({ attributevalue });
|
||||
} catch (error) {
|
||||
res.status(attributeValueErrorStatus(error)).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /attributename/{attributeId}/value/order:
|
||||
* put:
|
||||
* tags: [Article]
|
||||
* summary: Reorder values of an attribute
|
||||
* description: Updates the display sort order of values belonging to an attribute.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: attributeId
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [attributevalueIds]
|
||||
* properties:
|
||||
* attributevalueIds:
|
||||
* type: array
|
||||
* items:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Reordered successfully
|
||||
*/
|
||||
app.put(`${basePath}/attributename/:attributeId/value/order`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const attributeId = Number(req.params.attributeId);
|
||||
await db.order(req.user.id, attributeId, req.body ?? {});
|
||||
emit('attributename', { companyId: selected.companyId });
|
||||
res.json({ ok: true });
|
||||
} catch (error) {
|
||||
res.status(attributeValueErrorStatus(error)).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /attributevalue/{id}:
|
||||
* patch:
|
||||
* tags: [Article]
|
||||
* summary: Update attribute value
|
||||
* description: Updates value text and/or sort for an attribute value.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* value:
|
||||
* type: string
|
||||
* sort:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated attribute value
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* attributevalue:
|
||||
* $ref: '#/components/schemas/AttributeValue'
|
||||
* delete:
|
||||
* tags: [Article]
|
||||
* summary: Delete attribute value
|
||||
* description: Deletes an attribute value and cascades to article assignments.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Deleted successfully
|
||||
*/
|
||||
app.patch(`${basePath}/attributevalue/:id`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const id = Number(req.params.id);
|
||||
const attributevalue = await db.update(req.user.id, id, req.body ?? {});
|
||||
emit('attributename', { companyId: selected.companyId });
|
||||
res.json({ attributevalue });
|
||||
} catch (error) {
|
||||
res.status(attributeValueErrorStatus(error)).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete(`${basePath}/attributevalue/:id`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const id = Number(req.params.id);
|
||||
await db.remove(req.user.id, id);
|
||||
emit('attributename', { companyId: selected.companyId });
|
||||
res.json({ ok: true });
|
||||
} catch (error) {
|
||||
res.status(attributeValueErrorStatus(error)).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user