u
This commit is contained in:
7
package-lock.json
generated
7
package-lock.json
generated
@@ -33,8 +33,13 @@
|
||||
"@clack/prompts": "^1.7.0",
|
||||
"@hpcc-js/wasm-graphviz": "^1.28.0",
|
||||
"chalk": "^6.0.0",
|
||||
"dotenv": "^17.4.2",
|
||||
"dotenv": "^17.4.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"mssql": "^12.7.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"mssql": "^12.6.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@alcalzone/ansi-tokenize": {
|
||||
|
||||
104
src/api/routes/article.js
Normal file
104
src/api/routes/article.js
Normal file
@@ -0,0 +1,104 @@
|
||||
import * as db from '../../db/article.js';
|
||||
|
||||
function parsePositiveInt(value, field) {
|
||||
const id = Number(value);
|
||||
if (!Number.isInteger(id) || id <= 0) {
|
||||
return { error: `${field} is required` };
|
||||
}
|
||||
return { id };
|
||||
}
|
||||
|
||||
function articleErrorStatus(error) {
|
||||
if (error.message === 'Not authorized') return 403;
|
||||
if (error.message === 'Parent not found') return 404;
|
||||
if (error.message === 'Article not found') return 404;
|
||||
if (error.message === 'Name required') return 400;
|
||||
return 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers article routes (requires auth middleware).
|
||||
* @param {import('express').Application} app - Express app
|
||||
* @param {string} [basePath=''] - Optional URL prefix for all routes in this module
|
||||
*/
|
||||
export function register(app, basePath = '') {
|
||||
/**
|
||||
* @route POST /article
|
||||
* @description Creates an article under an authorized category (`parentId`).
|
||||
* @body {number} parentId - Category id
|
||||
* @body {string} name
|
||||
* @returns {Object} 201 - `{ article: { id, parentId, name, sku } }`
|
||||
*/
|
||||
app.post(`${basePath}/article`, async (req, res) => {
|
||||
const parent = parsePositiveInt(req.body?.parentId, 'parentId');
|
||||
if (parent.error) {
|
||||
return res.status(400).json({ error: parent.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 article = await db.createUnderParent({
|
||||
googleAuthId: req.user.id,
|
||||
parentId: parent.id,
|
||||
name,
|
||||
});
|
||||
res.status(201).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Create article failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @route GET /article/:sku
|
||||
* @description Returns an article by SKU if the user may access its category.
|
||||
* @returns {Object} 200 - `{ article }`
|
||||
*/
|
||||
app.get(`${basePath}/article/:sku`, async (req, res) => {
|
||||
const sku = typeof req.params.sku === 'string' ? req.params.sku.trim() : '';
|
||||
if (!sku) {
|
||||
return res.status(400).json({ error: 'sku is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const article = await db.getBySku(req.user.id, decodeURIComponent(sku));
|
||||
res.status(200).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Get article by sku failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @route PATCH /article/:sku
|
||||
* @description Updates an article name by SKU if the user may access its category.
|
||||
* @body {string} name
|
||||
* @returns {Object} 200 - `{ article }`
|
||||
*/
|
||||
app.patch(`${basePath}/article/:sku`, async (req, res) => {
|
||||
const sku = typeof req.params.sku === 'string' ? req.params.sku.trim() : '';
|
||||
if (!sku) {
|
||||
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' });
|
||||
}
|
||||
|
||||
try {
|
||||
const article = await db.updateBySku(req.user.id, decodeURIComponent(sku), { name });
|
||||
res.status(200).json({ article });
|
||||
} catch (error) {
|
||||
const status = articleErrorStatus(error);
|
||||
if (status === 500) console.error('Update article by sku failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -24,18 +24,17 @@ function categoryErrorStatus(error) {
|
||||
export function register(app, basePath = '') {
|
||||
/**
|
||||
* @route GET /category/tree
|
||||
* @description Returns the category tree for a company the user belongs to.
|
||||
* @query {number} companyId
|
||||
* @description Returns the category tree for the authenticated user's selected company.
|
||||
* @returns {Object} 200 - `{ languageId, tree, articles }`
|
||||
*/
|
||||
app.get(`${basePath}/category/tree`, async (req, res) => {
|
||||
const parsed = parsePositiveInt(req.query.companyId, 'companyId');
|
||||
if (parsed.error) {
|
||||
return res.status(400).json({ error: parsed.error });
|
||||
const companyId = req.user.selectedCompanyId;
|
||||
if (companyId == null) {
|
||||
return res.status(400).json({ error: 'No company selected' });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await db.getTreeForCompany(req.user.id, parsed.id);
|
||||
const result = await db.getTreeForCompany(req.user.id, companyId);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
const status = categoryErrorStatus(error);
|
||||
@@ -76,6 +75,27 @@ export function register(app, basePath = '') {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @route GET /category/:id/articles
|
||||
* @description Returns articles in a category the user is authorized for.
|
||||
* @returns {Object} 200 - `{ articles }`
|
||||
*/
|
||||
app.get(`${basePath}/category/:id/articles`, async (req, res) => {
|
||||
const parsed = parsePositiveInt(req.params.id, 'id');
|
||||
if (parsed.error) {
|
||||
return res.status(400).json({ error: parsed.error });
|
||||
}
|
||||
|
||||
try {
|
||||
const articles = await db.getCategoryArticles(req.user.id, parsed.id);
|
||||
res.status(200).json({ articles });
|
||||
} catch (error) {
|
||||
const status = categoryErrorStatus(error);
|
||||
if (status === 500) console.error('Get category articles failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @route DELETE /category/:id
|
||||
* @description Deletes a category the user is authorized for (not the company root).
|
||||
|
||||
16
src/db/article.js
Normal file
16
src/db/article.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { getStore } from './store.js';
|
||||
|
||||
export async function createUnderParent({ googleAuthId, parentId, name }) {
|
||||
const store = await getStore();
|
||||
return store.article.createUnderParent({ googleAuthId, parentId, name });
|
||||
}
|
||||
|
||||
export async function getBySku(googleAuthId, sku) {
|
||||
const store = await getStore();
|
||||
return store.article.getBySku(googleAuthId, sku);
|
||||
}
|
||||
|
||||
export async function updateBySku(googleAuthId, sku, data) {
|
||||
const store = await getStore();
|
||||
return store.article.updateBySku(googleAuthId, sku, data);
|
||||
}
|
||||
@@ -14,3 +14,8 @@ export async function deleteCategory({ googleAuthId, id }) {
|
||||
const store = await getStore();
|
||||
await store.category.delete({ googleAuthId, id });
|
||||
}
|
||||
|
||||
export async function getCategoryArticles(googleAuthId, categoryId) {
|
||||
const store = await getStore();
|
||||
return store.category.getCategoryArticles(googleAuthId, categoryId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user