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,43 @@
|
||||
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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -33,8 +33,6 @@ export async function PATCH(
|
||||
return Response.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
console.log('[api/admin/product] PATCH product', productId);
|
||||
|
||||
// 1. Update product fields
|
||||
const patchRes = await fetch(`${API_BASE}/api/products/${productId}`, {
|
||||
method: 'PATCH',
|
||||
@@ -55,8 +53,6 @@ export async function PATCH(
|
||||
return Response.json({ error: `Kunde inte uppdatera produkt: ${text}` }, { status: patchRes.status });
|
||||
}
|
||||
|
||||
console.log('[api/admin/product] PATCH OK');
|
||||
|
||||
// 2. Update tags
|
||||
const tagsRes = await fetch(`${API_BASE}/api/products/${productId}/tags`, {
|
||||
method: 'PUT',
|
||||
@@ -70,8 +66,6 @@ export async function PATCH(
|
||||
return Response.json({ error: `Kunde inte uppdatera taggar: ${text}` }, { status: tagsRes.status });
|
||||
}
|
||||
|
||||
console.log('[api/admin/product] tags PUT OK');
|
||||
|
||||
// 3. Return the complete updated product
|
||||
const fullRes = await fetch(`${API_BASE}/api/products/${productId}`, {
|
||||
headers: authHeaders,
|
||||
@@ -82,7 +76,6 @@ export async function PATCH(
|
||||
}
|
||||
|
||||
const product = await fullRes.json();
|
||||
console.log('[api/admin/product] returning full product id:', product?.id);
|
||||
return Response.json(product);
|
||||
} catch (err) {
|
||||
console.error('[api/admin/product] PATCH error:', err);
|
||||
@@ -108,8 +101,6 @@ export async function DELETE(
|
||||
return Response.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
console.log('[api/admin/product] DELETE product', productId);
|
||||
|
||||
const res = await fetch(`${API_BASE}/api/products/${productId}`, {
|
||||
method: 'DELETE',
|
||||
headers: authHeaders,
|
||||
@@ -121,7 +112,6 @@ export async function DELETE(
|
||||
return Response.json({ error: `Kunde inte ta bort produkt: ${text}` }, { status: res.status });
|
||||
}
|
||||
|
||||
console.log('[api/admin/product] DELETE OK');
|
||||
return new Response(null, { status: 204 });
|
||||
} catch (err) {
|
||||
console.error('[api/admin/product] DELETE error:', err);
|
||||
|
||||
@@ -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