feat(api): implement PATCH and DELETE routes for product management with error handling and logging

This commit is contained in:
Nils-Johan Gynther
2026-04-19 18:31:19 +02:00
parent f12e673af5
commit 051fb11714
4 changed files with 205 additions and 35 deletions
+8 -4
View File
@@ -61,9 +61,8 @@ export async function setProductTags(productId: number, tags: string[]) {
export async function updateProductWithTags(formData: FormData, tags: string[]) {
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 canonicalName = String(formData.get('canonicalName') || '').trim();
const category = String(formData.get('category') || '').trim();
const subcategory = String(formData.get('subcategory') || '').trim();
const brand = String(formData.get('brand') || '').trim();
const categoryIdRaw = formData.get('categoryId');
@@ -77,7 +76,7 @@ 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.');
const authHeaders = await getAuthHeaders();
console.log('[actions:updateProductWithTags] auth headers present:', !!authHeaders.Authorization);\n
const res = await fetch(`${API_BASE}/api/products/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json', ...authHeaders },
@@ -98,6 +97,7 @@ export async function updateProductWithTags(formData: FormData, tags: string[])
}
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`, {
method: 'PUT',
@@ -111,13 +111,17 @@ export async function updateProductWithTags(formData: FormData, tags: string[])
throw new Error(`Kunde inte uppdatera taggar: ${text}`);
}
console.log('[actions:updateProductWithTags] tags PUT OK, fetching full product...');
// Fetch the complete product (includes tags, categoryRef) after both updates
const fullRes = await fetch(`${API_BASE}/api/products/${id}`, {
headers: authHeaders,
cache: 'no-store',
});
console.log('[actions:updateProductWithTags] full product fetch HTTP', fullRes.status);
if (!fullRes.ok) return updatedProduct;
return fullRes.json();
const result = await fullRes.json();
console.log('[actions:updateProductWithTags] returning full product id:', (result as any)?.id);
return result;
}
export async function deleteProduct(id: number) {