Files
dashback/src/api/routes/attributename.js
2026-10-11 18:55:06 +02:00

293 lines
10 KiB
JavaScript

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;
if (error.message === 'Attribute name already exists') return 409;
if (error.message === 'Attribute name in use' || error.message?.includes('FOREIGN KEY constraint')) return 409;
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. Each attribute name includes `articleCount` (the number of parent articles configured with this attribute) and each value includes `articleCount` (the number of articles having this attribute value).
* 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: []
* x-signals: [attributename]
* 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: []
* x-signals: [attributename]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [attributenameIds]
* properties:
* attributenameIds:
* type: array
* items:
* type: integer
* responses:
* 200:
* description: Reordered successfully
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/SuccessResponse'
*/
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: []
* x-signals: [attributename]
* 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: []
* x-signals: [attributename]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* responses:
* 200:
* description: Deleted successfully
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/SuccessResponse'
* 400:
* description: Bad request 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: Forbidden
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Attribute name not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 409:
* description: Attribute name in use
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
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) {
const status = attributeErrorStatus(error);
if (status === 500) console.error('Update attribute name failed:', error);
res.status(status).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) {
const status = attributeErrorStatus(error);
const message = (error.message === 'Attribute name in use' || error.message?.includes('FOREIGN KEY constraint'))
? 'Attribute name in use'
: error.message;
if (status === 500) console.error('Delete attribute name failed:', error);
res.status(status).json({ error: message });
}
});
}