feat(api): implement new API routes for bulk category updates, inventory consumption, and product management with authentication

This commit is contained in:
Nils-Johan Gynther
2026-04-19 19:11:09 +02:00
parent 1ae9b336d8
commit 6f24aee18d
17 changed files with 440 additions and 85 deletions
+27 -2
View File
@@ -1,7 +1,7 @@
'use client';
import { useState } from 'react';
import { createInventoryItem } from './actions';
import { useRouter } from 'next/navigation';
import type { Product } from '../../features/inventory/types';
import { UNIT_OPTIONS } from '../../lib/units';
@@ -13,6 +13,7 @@ export default function InventoryForm({ products }: Props) {
const [isPending, setIsPending] = useState(false);
const [error, setError] = useState<string | null>(null);
const [isOpen, setIsOpen] = useState(false);
const router = useRouter();
const LOCATION_OPTIONS = [
{ value: '', label: 'Välj plats' },
@@ -82,8 +83,32 @@ export default function InventoryForm({ products }: Props) {
formData.set('quantity', String(quantity));
formData.set('unit', parsedUnit);
try {
await createInventoryItem(formData);
const payload: Record<string, unknown> = {
productId: Number(formData.get('productId')),
quantity,
unit: parsedUnit,
};
const location = String(formData.get('location') || '').trim();
if (location) payload.location = location;
payload.opened = formData.get('opened') === 'on';
const brand = String(formData.get('brand') || '').trim();
if (brand) payload.brand = brand;
const suitableFor = String(formData.get('suitableFor') || '').trim();
if (suitableFor) payload.suitableFor = suitableFor;
const bestBeforeDate = String(formData.get('bestBeforeDate') || '').trim();
if (bestBeforeDate) payload.bestBeforeDate = bestBeforeDate;
const res = await fetch('/api/admin/inventory-item', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(data?.error || 'Kunde inte spara');
}
form.reset();
router.refresh();
} catch (err) {
setError(err instanceof Error ? err.message : 'Okänt fel');
} finally {