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:
Nils-Johan Gynther
2026-04-19 19:04:04 +02:00
parent e0836cd269
commit 1ae9b336d8
6 changed files with 98 additions and 32 deletions
@@ -3,7 +3,7 @@
import { useState, useMemo, useEffect, useTransition, useCallback } from 'react'; import { useState, useMemo, useEffect, useTransition, useCallback } from 'react';
import type { Product, Category } from '../../../features/inventory/types'; import type { Product, Category } from '../../../features/inventory/types';
import EditProductForm from './EditProductForm'; import EditProductForm from './EditProductForm';
import { bulkSetCategory, suggestBulkCategories } from './actions'; import { bulkSetCategory } from './actions';
type CategoryNode = Category & { children: CategoryNode[] }; type CategoryNode = Category & { children: CategoryNode[] };
@@ -54,15 +54,12 @@ export default function AdminProductList() {
const [aiApplying, setAiApplying] = useState(false); const [aiApplying, setAiApplying] = useState(false);
const refetchProducts = useCallback(() => { const refetchProducts = useCallback(() => {
console.log('[AdminProductList] refetchProducts: starting fetch /api/products');
fetch('/api/products') fetch('/api/products')
.then(async (r) => { .then(async (r) => {
console.log('[AdminProductList] refetchProducts: HTTP', r.status);
if (!r.ok) throw new Error(`HTTP ${r.status}`); if (!r.ok) throw new Error(`HTTP ${r.status}`);
return r.json(); return r.json();
}) })
.then((data) => { .then((data) => {
console.log('[AdminProductList] refetchProducts: got', Array.isArray(data) ? data.length : 'non-array', 'products');
if (Array.isArray(data)) setProducts(data); if (Array.isArray(data)) setProducts(data);
}) })
.catch((e) => console.error('[AdminProductList] refetchProducts error:', e)) .catch((e) => console.error('[AdminProductList] refetchProducts error:', e))
@@ -157,9 +154,11 @@ export default function AdminProductList() {
setAiError(null); setAiError(null);
setAiSuggestions(null); setAiSuggestions(null);
try { try {
const results = await suggestBulkCategories(); const res = await fetch('/api/admin/bulk-categorize', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({}) });
setAiSuggestions(results); const data = await res.json();
setAiApproved(new Set(results.map((r) => r.productId))); if (!res.ok) throw new Error(data?.error ?? 'AI-kategorisering misslyckades');
setAiSuggestions(data);
setAiApproved(new Set(data.map((r: AiSuggestion) => r.productId)));
} catch (err) { } catch (err) {
setAiError(err instanceof Error ? err.message : 'AI-kategorisering misslyckades'); setAiError(err instanceof Error ? err.message : 'AI-kategorisering misslyckades');
} finally { } finally {
@@ -2,7 +2,6 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import type { Product } from '../../../features/inventory/types'; import type { Product } from '../../../features/inventory/types';
import { suggestProductCategory } from './actions';
type CategoryNode = { type CategoryNode = {
id: number; id: number;
@@ -84,8 +83,10 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
setAiError(null); setAiError(null);
setAiSuggestion(null); setAiSuggestion(null);
try { try {
const result = await suggestProductCategory(product.id); const res = await fetch(`/api/admin/suggest-category/${product.id}`);
setAiSuggestion(result); const data = await res.json();
if (!res.ok) throw new Error(data?.error ?? 'AI-kategorisering misslyckades');
setAiSuggestion(data);
} catch (err) { } catch (err) {
setAiError(err instanceof Error ? err.message : 'AI-kategorisering misslyckades'); setAiError(err instanceof Error ? err.message : 'AI-kategorisering misslyckades');
} finally { } finally {
@@ -97,7 +98,6 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
e.preventDefault(); e.preventDefault();
const formData = new FormData(e.currentTarget); const formData = new FormData(e.currentTarget);
const rawTags = tagInput.split(',').map((t) => t.trim().toLowerCase()).filter(Boolean); const rawTags = tagInput.split(',').map((t) => t.trim().toLowerCase()).filter(Boolean);
console.log('[EditProductForm] handleSubmit: PATCH /api/admin/product/', product.id);
setIsPending(true); setIsPending(true);
setError(null); setError(null);
setSuccess(false); setSuccess(false);
@@ -116,7 +116,6 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
}), }),
}); });
const data = await res.json(); const data = await res.json();
console.log('[EditProductForm] handleSubmit: HTTP', res.status, data);
if (!res.ok) { if (!res.ok) {
setError(data?.error ?? 'Okänt fel'); setError(data?.error ?? 'Okänt fel');
return; return;
@@ -125,7 +124,6 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
setIsOpen(false); setIsOpen(false);
onSaved(data as Product); onSaved(data as Product);
} catch (err) { } catch (err) {
console.error('[EditProductForm] handleSubmit error:', err);
setError(err instanceof Error ? err.message : 'Okänt fel'); setError(err instanceof Error ? err.message : 'Okänt fel');
} finally { } finally {
setIsPending(false); setIsPending(false);
@@ -137,7 +135,6 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
setError(null); setError(null);
setSuccess(false); setSuccess(false);
setIsPending(true); setIsPending(true);
console.log('[EditProductForm] handleDelete: DELETE /api/admin/product/', product.id);
try { try {
const res = await fetch(`/api/admin/product/${product.id}`, { method: 'DELETE' }); const res = await fetch(`/api/admin/product/${product.id}`, { method: 'DELETE' });
console.log('[EditProductForm] handleDelete: HTTP', res.status); console.log('[EditProductForm] handleDelete: HTTP', res.status);
@@ -148,7 +145,6 @@ export default function EditProductForm({ product, onSaved, onDeleted }: Props)
} }
onDeleted(product.id); onDeleted(product.id);
} catch (err) { } catch (err) {
console.error('[EditProductForm] handleDelete error:', err);
setError(err instanceof Error ? err.message : 'Okänt fel'); setError(err instanceof Error ? err.message : 'Okänt fel');
} finally { } finally {
setIsPending(false); setIsPending(false);
+1 -7
View File
@@ -61,7 +61,6 @@ export async function setProductTags(productId: number, tags: string[]) {
export async function updateProductWithTags(formData: FormData, tags: string[]) { export async function updateProductWithTags(formData: FormData, tags: string[]) {
const id = Number(formData.get('id')); const id = Number(formData.get('id'));
console.log('[actions:updateProductWithTags] called for product id:', id, 'tags:', tags);
const name = String(formData.get('name') || '').trim(); const name = String(formData.get('name') || '').trim();
const canonicalName = String(formData.get('canonicalName') || '').trim(); const canonicalName = String(formData.get('canonicalName') || '').trim();
const category = String(formData.get('category') || '').trim(); const category = String(formData.get('category') || '').trim();
@@ -78,7 +77,6 @@ export async function updateProductWithTags(formData: FormData, tags: string[])
if (brand.length > 100) throw new Error('Varumärke får inte vara längre än 100 tecken.'); if (brand.length > 100) throw new Error('Varumärke får inte vara längre än 100 tecken.');
const authHeaders = await getAuthHeaders(); const authHeaders = await getAuthHeaders();
console.log('[actions:updateProductWithTags] auth headers present:', !!authHeaders.Authorization);
const res = await fetch(`${API_BASE}/api/products/${id}`, { const res = await fetch(`${API_BASE}/api/products/${id}`, {
method: 'PATCH', method: 'PATCH',
headers: { 'Content-Type': 'application/json', ...authHeaders }, headers: { 'Content-Type': 'application/json', ...authHeaders },
@@ -99,7 +97,6 @@ export async function updateProductWithTags(formData: FormData, tags: string[])
} }
const updatedProduct = await res.json(); const updatedProduct = await res.json();
console.log('[actions:updateProductWithTags] PATCH OK, product id:', (updatedProduct as any)?.id);
const tagsRes = await fetch(`${API_BASE}/api/products/${id}/tags`, { const tagsRes = await fetch(`${API_BASE}/api/products/${id}/tags`, {
method: 'PUT', method: 'PUT',
@@ -113,16 +110,13 @@ export async function updateProductWithTags(formData: FormData, tags: string[])
throw new Error(`Kunde inte uppdatera taggar: ${text}`); throw new Error(`Kunde inte uppdatera taggar: ${text}`);
} }
console.log('[actions:updateProductWithTags] tags PUT OK, fetching full product...'); // Fetch the complete product
// Fetch the complete product (includes tags, categoryRef) after both updates
const fullRes = await fetch(`${API_BASE}/api/products/${id}`, { const fullRes = await fetch(`${API_BASE}/api/products/${id}`, {
headers: authHeaders, headers: authHeaders,
cache: 'no-store', cache: 'no-store',
}); });
console.log('[actions:updateProductWithTags] full product fetch HTTP', fullRes.status);
if (!fullRes.ok) return updatedProduct; if (!fullRes.ok) return updatedProduct;
const result = await fullRes.json(); const result = await fullRes.json();
console.log('[actions:updateProductWithTags] returning full product id:', (result as any)?.id);
return result; return result;
} }
@@ -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 }); return Response.json({ error: 'Unauthorized' }, { status: 401 });
} }
console.log('[api/admin/product] PATCH product', productId);
// 1. Update product fields // 1. Update product fields
const patchRes = await fetch(`${API_BASE}/api/products/${productId}`, { const patchRes = await fetch(`${API_BASE}/api/products/${productId}`, {
method: 'PATCH', method: 'PATCH',
@@ -55,8 +53,6 @@ export async function PATCH(
return Response.json({ error: `Kunde inte uppdatera produkt: ${text}` }, { status: patchRes.status }); return Response.json({ error: `Kunde inte uppdatera produkt: ${text}` }, { status: patchRes.status });
} }
console.log('[api/admin/product] PATCH OK');
// 2. Update tags // 2. Update tags
const tagsRes = await fetch(`${API_BASE}/api/products/${productId}/tags`, { const tagsRes = await fetch(`${API_BASE}/api/products/${productId}/tags`, {
method: 'PUT', method: 'PUT',
@@ -70,8 +66,6 @@ export async function PATCH(
return Response.json({ error: `Kunde inte uppdatera taggar: ${text}` }, { status: tagsRes.status }); 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 // 3. Return the complete updated product
const fullRes = await fetch(`${API_BASE}/api/products/${productId}`, { const fullRes = await fetch(`${API_BASE}/api/products/${productId}`, {
headers: authHeaders, headers: authHeaders,
@@ -82,7 +76,6 @@ export async function PATCH(
} }
const product = await fullRes.json(); const product = await fullRes.json();
console.log('[api/admin/product] returning full product id:', product?.id);
return Response.json(product); return Response.json(product);
} catch (err) { } catch (err) {
console.error('[api/admin/product] PATCH error:', err); console.error('[api/admin/product] PATCH error:', err);
@@ -108,8 +101,6 @@ export async function DELETE(
return Response.json({ error: 'Unauthorized' }, { status: 401 }); return Response.json({ error: 'Unauthorized' }, { status: 401 });
} }
console.log('[api/admin/product] DELETE product', productId);
const res = await fetch(`${API_BASE}/api/products/${productId}`, { const res = await fetch(`${API_BASE}/api/products/${productId}`, {
method: 'DELETE', method: 'DELETE',
headers: authHeaders, headers: authHeaders,
@@ -121,7 +112,6 @@ export async function DELETE(
return Response.json({ error: `Kunde inte ta bort produkt: ${text}` }, { status: res.status }); 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 }); return new Response(null, { status: 204 });
} catch (err) { } catch (err) {
console.error('[api/admin/product] DELETE error:', 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 },
);
}
}