2026-05-20 13:57:11 +02:00
|
|
|
import { applyCors, isAllowedOrigin } from './_cors.js';
|
2026-05-14 17:17:10 +02:00
|
|
|
import { getBcConfig, getBCToken, findItem, patchItemCpnpNo } from '../bc-runtime.js';
|
|
|
|
|
|
|
|
|
|
function setCors(req, res) {
|
2026-05-20 13:57:11 +02:00
|
|
|
applyCors(req, res, 'POST, OPTIONS');
|
2026-05-14 17:17:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
|
|
|
|
setCors(req, res);
|
|
|
|
|
|
|
|
|
|
if (req.method === 'OPTIONS') return res.status(204).end();
|
|
|
|
|
|
|
|
|
|
const origin = req.headers.origin;
|
2026-05-20 13:57:11 +02:00
|
|
|
if (origin && !isAllowedOrigin(origin)) {
|
2026-05-14 17:17:10 +02:00
|
|
|
return res.status(403).json({ error: 'Forbidden' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.method !== 'POST') {
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { articleNo, cpnpNo } = req.body || {};
|
|
|
|
|
if (!articleNo || cpnpNo === undefined) {
|
|
|
|
|
return res.status(400).json({ error: 'Missing articleNo or cpnpNo' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const config = getBcConfig();
|
|
|
|
|
const token = await getBCToken(config);
|
|
|
|
|
const item = await findItem(config, token, articleNo);
|
|
|
|
|
await patchItemCpnpNo(config, token, item, String(cpnpNo));
|
|
|
|
|
return res.json({ success: true, articleNo, cpnpNo });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('[bc-proxy] error:', err.message);
|
|
|
|
|
return res.status(500).json({ success: false, error: err.message });
|
|
|
|
|
}
|
|
|
|
|
}
|