123 lines
3.8 KiB
TypeScript
123 lines
3.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { createServerClient } from '@/lib/supabase/server';
|
|
import { z } from 'zod';
|
|
|
|
const addItemSchema = z.object({
|
|
householdId: z.string().min(1).default('demo-household-id'),
|
|
shoppingListId: z.string().optional().nullable(),
|
|
productId: z.string().optional().nullable(),
|
|
customName: z.string().optional().nullable(),
|
|
quantity: z.coerce.number().positive().default(1),
|
|
unitId: z.string().optional().nullable(),
|
|
estimatedUnitPriceCents: z.coerce.number().int().nonnegative().optional().nullable(),
|
|
selectedStore: z.string().optional().nullable(),
|
|
notes: z.string().optional().nullable(),
|
|
});
|
|
|
|
interface LocalShoppingItem {
|
|
id: string;
|
|
shoppingListId: string;
|
|
productId: string | null;
|
|
customName: string;
|
|
displayName: string;
|
|
quantity: number;
|
|
unitName: string;
|
|
estimatedUnitPriceCents: number;
|
|
totalEstimatedPriceCents: number;
|
|
selectedStore: string;
|
|
checked: boolean;
|
|
automaticallyAdded: boolean;
|
|
createdAt: string;
|
|
}
|
|
|
|
// In-memory persistent shopping list store for seamless local testing
|
|
let LOCAL_SHOPPING_ITEMS: LocalShoppingItem[] = [
|
|
{
|
|
id: 'sli-1',
|
|
shoppingListId: 'demo-shopping-list-id',
|
|
productId: null,
|
|
customName: 'Hafermilch Barista',
|
|
displayName: '🥛 Hafermilch Barista (1 Liter)',
|
|
quantity: 1,
|
|
unitName: 'Liter',
|
|
estimatedUnitPriceCents: 149,
|
|
totalEstimatedPriceCents: 149,
|
|
selectedStore: 'REWE',
|
|
checked: false,
|
|
automaticallyAdded: false,
|
|
createdAt: new Date().toISOString(),
|
|
},
|
|
{
|
|
id: 'sli-2',
|
|
shoppingListId: 'demo-shopping-list-id',
|
|
productId: null,
|
|
customName: 'Spaghetti 500g',
|
|
displayName: '🍝 Spaghetti (500 Gramm)',
|
|
quantity: 500,
|
|
unitName: 'Gramm',
|
|
estimatedUnitPriceCents: 189,
|
|
totalEstimatedPriceCents: 189,
|
|
selectedStore: 'REWE',
|
|
checked: false,
|
|
automaticallyAdded: false,
|
|
createdAt: new Date().toISOString(),
|
|
},
|
|
];
|
|
|
|
export async function GET(request: NextRequest) {
|
|
return NextResponse.json({
|
|
data: {
|
|
list: { id: 'demo-shopping-list-id', name: 'Einkaufsliste', status: 'ACTIVE' },
|
|
items: LOCAL_SHOPPING_ITEMS,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const requestId = crypto.randomUUID();
|
|
try {
|
|
const body = await request.json();
|
|
const parsed = addItemSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ error: { code: 'VALIDATION_ERROR', message: 'Ungültige Eingabedaten.', requestId } },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const newItem: LocalShoppingItem = {
|
|
id: crypto.randomUUID(),
|
|
shoppingListId: 'demo-shopping-list-id',
|
|
productId: parsed.data.productId || null,
|
|
customName: parsed.data.customName || 'Neuer Artikel',
|
|
displayName: `${parsed.data.customName || 'Neuer Artikel'} (${parsed.data.quantity} ${parsed.data.unitId || 'Stk.'})`,
|
|
quantity: parsed.data.quantity,
|
|
unitName: parsed.data.unitId || 'Stk.',
|
|
estimatedUnitPriceCents: parsed.data.estimatedUnitPriceCents || 149,
|
|
totalEstimatedPriceCents: (parsed.data.estimatedUnitPriceCents || 149) * parsed.data.quantity,
|
|
selectedStore: parsed.data.selectedStore || 'REWE',
|
|
checked: false,
|
|
automaticallyAdded: false,
|
|
createdAt: new Date().toISOString(),
|
|
};
|
|
|
|
LOCAL_SHOPPING_ITEMS.unshift(newItem);
|
|
|
|
return NextResponse.json({ data: newItem }, { status: 201 });
|
|
} catch (err: any) {
|
|
return NextResponse.json(
|
|
{ error: { code: 'SERVER_ERROR', message: err.message, requestId } },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function DELETE(request: NextRequest) {
|
|
const { searchParams } = new URL(request.url);
|
|
const id = searchParams.get('id');
|
|
if (id) {
|
|
LOCAL_SHOPPING_ITEMS = LOCAL_SHOPPING_ITEMS.filter(i => i.id !== id);
|
|
}
|
|
return NextResponse.json({ success: true });
|
|
}
|