1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
6 * OpenAPI Product Writeoff Endpoint
7 * POST /api/v1/openapi/products/[id]/writeoff?location=1&qty=5&reason=1
8 * Record stock writeoff for damaged/expired items
10export async function POST(
12 { params }: { params: Promise<{ id: string }> }
15 const authData = await getStoredAuth();
16 if (!authData || !authData.authenticated) {
17 return NextResponse.json(
18 { error: 'Authentication required' },
23 const { searchParams } = new URL(request.url);
24 const queryParams: Record<string, string | number> = {};
26 const location = searchParams.get('location');
27 if (location) queryParams.location = parseInt(location);
29 const qty = searchParams.get('qty');
30 if (qty) queryParams.qty = parseFloat(qty);
32 const reason = searchParams.get('reason');
33 if (reason) queryParams.reason = parseInt(reason);
35 const result = await fieldpineServerApi.apiCall(
36 `/Products/${await params.then(p => p.id)}/writeoff`,
40 cookie: authData.apiKey,
45 return NextResponse.json({
52 console.error('Product writeoff error:', error);
53 return NextResponse.json(
54 { error: 'Failed to record writeoff' },