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
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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}` };
|
||||
}
|
||||
|
||||
// GET /api/admin/suggest-category/[id]
|
||||
export async function GET(
|
||||
_req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
try {
|
||||
const { id } = await params;
|
||||
const productId = Number(id);
|
||||
if (!productId) return Response.json({ error: 'Invalid id' }, { status: 400 });
|
||||
|
||||
const authHeaders = await getAuthHeaders();
|
||||
if (!authHeaders.Authorization) {
|
||||
return Response.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_BASE}/api/products/${productId}/suggest-category`, {
|
||||
headers: authHeaders,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
console.error('[api/admin/suggest-category] failed:', res.status, text);
|
||||
return Response.json({ error: `AI-kategorisering misslyckades: ${text}` }, { status: res.status });
|
||||
}
|
||||
|
||||
return Response.json(await res.json());
|
||||
} catch (err) {
|
||||
console.error('[api/admin/suggest-category] error:', err);
|
||||
return Response.json(
|
||||
{ error: err instanceof Error ? err.message : 'Unknown error' },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user