Files
recipe-app/frontend/app/api/admin/bulk-categorize/route.ts
T
Nils-Johan Gynther 1ae9b336d8 feat(api): implement bulk categorization and suggestion endpoints with authentication
refactor(actions): remove unused imports and console logs from product actions
refactor(EditProductForm): update category suggestion logic to use new API endpoint
2026-04-19 19:04:04 +02:00

44 lines
1.4 KiB
TypeScript

import { auth } from '../../../../auth';
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
async function getAuthHeaders(): Promise<Record<string, string>> {
const session = await auth();
if (!session?.accessToken) return {};
return { Authorization: `Bearer ${session.accessToken}` };
}
// POST /api/admin/bulk-categorize
// Body: { productIds?: number[] }
export async function POST(req: Request) {
try {
const body = await req.json().catch(() => ({}));
const { productIds } = body;
const authHeaders = await getAuthHeaders();
if (!authHeaders.Authorization) {
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
const res = await fetch(`${API_BASE}/api/products/ai-categorize-bulk`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...authHeaders },
body: JSON.stringify({ productIds }),
});
if (!res.ok) {
const text = await res.text();
console.error('[api/admin/bulk-categorize] failed:', res.status, text);
return Response.json({ error: `Bulk-AI-kategorisering misslyckades: ${text}` }, { status: res.status });
}
return Response.json(await res.json());
} catch (err) {
console.error('[api/admin/bulk-categorize] error:', err);
return Response.json(
{ error: err instanceof Error ? err.message : 'Unknown error' },
{ status: 500 },
);
}
}