35 lines
1.3 KiB
JavaScript
35 lines
1.3 KiB
JavaScript
import { getArticle } from '../../../services/article/getArticle.js';
|
|
|
|
/**
|
|
* Registers the full article lookup route backed by MSSQL.
|
|
* @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 GET /article/get
|
|
* @description Look up a single article by exact article number, EAN, or barcode.
|
|
* @query {string} target - Article number, packaging EAN, or barcode
|
|
* @returns {Object} 200 - `{ article }` with enriched article data
|
|
* @returns {Object} 400 - `{ error }` when target is missing
|
|
* @returns {Object} 404 - `{ error }` when no article matches
|
|
*/
|
|
app.get(`${basePath}/article/get`, async (req, res) => {
|
|
const target = req.query.target;
|
|
|
|
if (!target || !String(target).trim()) {
|
|
return res.status(400).json({ error: 'Query parameter "target" is required' });
|
|
}
|
|
|
|
try {
|
|
const article = await getArticle(String(target));
|
|
if (!article) {
|
|
return res.status(404).json({ error: 'Article not found' });
|
|
}
|
|
return res.status(200).json({ article });
|
|
} catch (error) {
|
|
return res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
}
|