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
@@ -0,0 +1,31 @@
import { NextResponse } from 'next/server';
import { auth } from '../../../../auth';
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
export async function POST(req: Request) {
const session = await auth();
if (!session?.accessToken) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const body = await req.json();
const { ids, categoryId } = body as { ids: number[]; categoryId: number | null };
const res = await fetch(`${API_BASE}/api/products/bulk-update`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify({ ids, categoryId }),
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
return NextResponse.json({ error: text || 'Bulk-uppdatering misslyckades' }, { status: res.status });
}
return NextResponse.json({ ok: true });
}
@@ -0,0 +1,31 @@
import { NextResponse } from 'next/server';
import { auth } from '../../../../../../auth';
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
export async function POST(req: Request, { params }: { params: Promise<{ id: string }> }) {
const session = await auth();
if (!session?.accessToken) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { id } = await params;
const body = await req.json();
const res = await fetch(`${API_BASE}/api/inventory/${id}/consume`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify(body),
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
return NextResponse.json({ error: text || 'Kunde inte förbruka inventory-rad' }, { status: res.status });
}
return NextResponse.json({ ok: true });
}
@@ -0,0 +1,31 @@
import { NextResponse } from 'next/server';
import { auth } from '../../../../../auth';
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
export async function PATCH(req: Request, { params }: { params: Promise<{ id: string }> }) {
const session = await auth();
if (!session?.accessToken) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { id } = await params;
const body = await req.json();
const res = await fetch(`${API_BASE}/api/inventory/${id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify(body),
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
return NextResponse.json({ error: text || 'Kunde inte uppdatera inventory-rad' }, { status: res.status });
}
return NextResponse.json({ ok: true });
}
@@ -0,0 +1,30 @@
import { NextResponse } from 'next/server';
import { auth } from '../../../../auth';
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
export async function POST(req: Request) {
const session = await auth();
if (!session?.accessToken) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const body = await req.json();
const res = await fetch(`${API_BASE}/api/inventory`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify(body),
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
return NextResponse.json({ error: text || 'Kunde inte skapa inventory-rad' }, { status: res.status });
}
return NextResponse.json({ ok: true });
}
@@ -0,0 +1,34 @@
import { NextResponse } from 'next/server';
import { auth } from '../../../../auth';
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
export async function POST(req: Request) {
const session = await auth();
if (!session?.accessToken) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const body = await req.json();
const { sourceProductId, targetProductId } = body as {
sourceProductId: number;
targetProductId: number;
};
const res = await fetch(`${API_BASE}/api/products/merge`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify({ sourceProductId, targetProductId }),
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
return NextResponse.json({ error: text || 'Sammanslagning misslyckades' }, { status: res.status });
}
return NextResponse.json({ ok: true });
}
@@ -0,0 +1,28 @@
import { NextResponse } from 'next/server';
import { auth } from '../../../../../auth';
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
export async function DELETE(_req: Request, { params }: { params: Promise<{ id: string }> }) {
const session = await auth();
if (!session?.accessToken) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { id } = await params;
const res = await fetch(`${API_BASE}/api/pantry/${id}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${session.accessToken}`,
},
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
return NextResponse.json({ error: text || 'Kunde inte ta bort baslager-vara' }, { status: res.status });
}
return NextResponse.json({ ok: true });
}
@@ -0,0 +1,31 @@
import { NextResponse } from 'next/server';
import { auth } from '../../../../auth';
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
export async function POST(req: Request) {
const session = await auth();
if (!session?.accessToken) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const body = await req.json();
const { productId } = body as { productId: number };
const res = await fetch(`${API_BASE}/api/pantry`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify({ productId }),
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
return NextResponse.json({ error: text || 'Kunde inte lägga till baslager-vara' }, { status: res.status });
}
return NextResponse.json({ ok: true });
}
@@ -0,0 +1,27 @@
import { NextResponse } from 'next/server';
import { auth } from '../../../../auth';
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
export async function POST() {
const session = await auth();
if (!session?.accessToken) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const res = await fetch(`${API_BASE}/api/products/reset-all`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
cache: 'no-store',
});
if (!res.ok) {
const text = await res.text();
return NextResponse.json({ error: text || 'Återställning misslyckades' }, { status: res.status });
}
return NextResponse.json({ ok: true });
}