u
This commit is contained in:
235
src/api/routes/attributename.js
Normal file
235
src/api/routes/attributename.js
Normal file
@@ -0,0 +1,235 @@
|
||||
import * as db from '../../db/attributename.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 attributeErrorStatus(error) {
|
||||
if (error.message === 'No company selected') return 400;
|
||||
if (error.message === 'Not authorized') return 403;
|
||||
if (error.message === 'Company not found') return 404;
|
||||
if (error.message === 'Attribute name not found') return 404;
|
||||
if (error.message === 'Name required') return 400;
|
||||
if (error.message === 'Invalid attribute name order') return 400;
|
||||
if (error.message === 'Invalid id') return 400;
|
||||
return 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers attributename routes.
|
||||
* @param {import('express').Application} app
|
||||
* @param {string} [basePath='']
|
||||
*/
|
||||
export function register(app, basePath = '') {
|
||||
/**
|
||||
* @openapi
|
||||
* /attributename:
|
||||
* get:
|
||||
* tags: [Article]
|
||||
* summary: List attribute names
|
||||
* description: Lists all attribute names defined for the selected company, including nested values.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* responses:
|
||||
* 200:
|
||||
* description: List of attribute names
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* attributenames:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Attributename'
|
||||
* post:
|
||||
* tags: [Article]
|
||||
* summary: Create attribute name
|
||||
* description: Creates an attribute name for the selected company.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [name]
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* sort:
|
||||
* type: integer
|
||||
* exclusive:
|
||||
* type: boolean
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Created attribute name
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* attributename:
|
||||
* $ref: '#/components/schemas/Attributename'
|
||||
*/
|
||||
app.get(`${basePath}/attributename`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const attributenames = await db.list(req.user.id);
|
||||
res.json({ attributenames });
|
||||
} catch (error) {
|
||||
res.status(attributeErrorStatus(error)).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post(`${basePath}/attributename`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const attributename = await db.create(req.user.id, req.body ?? {});
|
||||
emit('attributename', { companyId: selected.companyId });
|
||||
res.status(201).json({ attributename });
|
||||
} catch (error) {
|
||||
res.status(attributeErrorStatus(error)).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /attributename/order:
|
||||
* put:
|
||||
* tags: [Article]
|
||||
* summary: Reorder attribute names
|
||||
* description: Updates the display sort order of attribute names for the selected company.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [attributenameIds]
|
||||
* properties:
|
||||
* attributenameIds:
|
||||
* type: array
|
||||
* items:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Reordered successfully
|
||||
*/
|
||||
app.put(`${basePath}/attributename/order`, async (req, res) => {
|
||||
const selected = requireSelectedCompany(req);
|
||||
if (selected.error) {
|
||||
return res.status(400).json({ error: selected.error });
|
||||
}
|
||||
|
||||
try {
|
||||
await db.order(req.user.id, req.body ?? {});
|
||||
emit('attributename', { companyId: selected.companyId });
|
||||
res.json({ ok: true });
|
||||
} catch (error) {
|
||||
res.status(attributeErrorStatus(error)).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /attributename/{id}:
|
||||
* patch:
|
||||
* tags: [Article]
|
||||
* summary: Update attribute name
|
||||
* description: Updates name, sort, and/or exclusive flag for an attribute name.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* sort:
|
||||
* type: integer
|
||||
* exclusive:
|
||||
* type: boolean
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Updated attribute name
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* attributename:
|
||||
* $ref: '#/components/schemas/Attributename'
|
||||
* delete:
|
||||
* tags: [Article]
|
||||
* summary: Delete attribute name
|
||||
* description: Deletes an attribute name and cascades to attribute values and article assignments.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Deleted successfully
|
||||
*/
|
||||
app.patch(`${basePath}/attributename/: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 attributename = await db.update(req.user.id, id, req.body ?? {});
|
||||
emit('attributename', { companyId: selected.companyId });
|
||||
res.json({ attributename });
|
||||
} catch (error) {
|
||||
res.status(attributeErrorStatus(error)).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete(`${basePath}/attributename/: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(attributeErrorStatus(error)).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user