1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
6 * OpenAPI PurchaseOrders Endpoint
7 * GET /api/v1/openapi/purchase-orders - List purchase orders
8 * POST /api/v1/openapi/purchase-orders - Create new purchase order
10export async function GET(request: NextRequest) {
12 const authData = await getStoredAuth();
13 if (!authData || !authData.authenticated) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
20 const { searchParams } = new URL(request.url);
21 const params: Record<string, string | number> = {};
23 const location = searchParams.get('location');
24 if (location) params.location = parseInt(location);
26 const spid = searchParams.get('spid');
27 if (spid) params.spid = parseInt(spid);
29 const state = searchParams.get('state');
30 if (state) params.state = state;
32 const purchaseOrders = await fieldpineServerApi.apiCall("/PurchaseOrders", {
34 cookie: authData.apiKey,
38 return NextResponse.json({
45 console.error('PurchaseOrders API error:', error);
46 return NextResponse.json(
47 { error: 'Failed to fetch purchase orders' },
53export async function POST(request: NextRequest) {
55 const authData = await getStoredAuth();
56 if (!authData || !authData.authenticated) {
57 return NextResponse.json(
58 { error: 'Authentication required' },
63 const body = await request.json();
65 const result = await fieldpineServerApi.apiCall("/PurchaseOrders", {
68 cookie: authData.apiKey,
72 return NextResponse.json({
79 console.error('PurchaseOrders POST error:', error);
80 return NextResponse.json(
81 { error: 'Failed to create purchase order' },