EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
route.ts
Go to the documentation of this file.
1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
4
5/**
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
9 */
10export async function POST(
11 request: NextRequest,
12 { params }: { params: Promise<{ id: string }> }
13) {
14 try {
15 const authData = await getStoredAuth();
16 if (!authData || !authData.authenticated) {
17 return NextResponse.json(
18 { error: 'Authentication required' },
19 { status: 401 }
20 );
21 }
22
23 const { searchParams } = new URL(request.url);
24 const queryParams: Record<string, string | number> = {};
25
26 const location = searchParams.get('location');
27 if (location) queryParams.location = parseInt(location);
28
29 const qty = searchParams.get('qty');
30 if (qty) queryParams.qty = parseFloat(qty);
31
32 const reason = searchParams.get('reason');
33 if (reason) queryParams.reason = parseInt(reason);
34
35 const result = await fieldpineServerApi.apiCall(
36 `/Products/${await params.then(p => p.id)}/writeoff`,
37 {
38 method: 'POST',
39 params: queryParams,
40 cookie: authData.apiKey,
41 useOpenApi: true
42 }
43 );
44
45 return NextResponse.json({
46 success: true,
47 data: result,
48 source: 'openapi'
49 });
50
51 } catch (error) {
52 console.error('Product writeoff error:', error);
53 return NextResponse.json(
54 { error: 'Failed to record writeoff' },
55 { status: 500 }
56 );
57 }
58}