feat(receipt-import): refactor product creation and category update to use server actions

This commit is contained in:
Nils-Johan Gynther
2026-04-19 17:17:41 +02:00
parent 2b6fe71e53
commit 184ecaad61
2 changed files with 46 additions and 16 deletions
+5 -16
View File
@@ -195,26 +195,15 @@ export default function ReceiptImportClient({ isAdmin }: { isAdmin: boolean }) {
// eslint-disable-next-line no-console
console.log('handleCreateProduct: isAdmin =', isAdmin, 'endpoint = /api/products');
try {
// Admin skapar aktiv produkt direkt
const createRes = await fetch('/api/products', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: row.rawName }),
});
if (!createRes.ok) {
const e = await createRes.json().catch(() => ({}));
throw new Error(e.message ?? `HTTP ${createRes.status}`);
}
const product = await createRes.json() as { id: number; name: string; canonicalName: string | null };
const { createProductAction, updateProductCategoryAction } = await import('./actions');
// Admin skapar aktiv produkt direkt via Server Action
const product = await createProductAction(row.rawName);
// Sätt kategori: AI-förslag har prioritet, annars manuellt val
const categoryId = row.categorySuggestion?.categoryId ?? (row.selectedCategoryId !== '' ? row.selectedCategoryId : null);
if (categoryId) {
await fetch(`/api/products/${product.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ categoryId }),
});
await updateProductCategoryAction(product.id, categoryId);
}
// Uppdatera produktlistan lokalt