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 PurchaseOrders Endpoint
7 * GET /api/v1/openapi/purchase-orders - List purchase orders
8 * POST /api/v1/openapi/purchase-orders - Create new purchase order
9 */
10export async function GET(request: NextRequest) {
11 try {
12 const authData = await getStoredAuth();
13 if (!authData || !authData.authenticated) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
16 { status: 401 }
17 );
18 }
19
20 const { searchParams } = new URL(request.url);
21 const params: Record<string, string | number> = {};
22
23 const location = searchParams.get('location');
24 if (location) params.location = parseInt(location);
25
26 const spid = searchParams.get('spid');
27 if (spid) params.spid = parseInt(spid);
28
29 const state = searchParams.get('state');
30 if (state) params.state = state;
31
32 const purchaseOrders = await fieldpineServerApi.apiCall("/PurchaseOrders", {
33 params,
34 cookie: authData.apiKey,
35 useOpenApi: true
36 });
37
38 return NextResponse.json({
39 success: true,
40 data: purchaseOrders,
41 source: 'openapi'
42 });
43
44 } catch (error) {
45 console.error('PurchaseOrders API error:', error);
46 return NextResponse.json(
47 { error: 'Failed to fetch purchase orders' },
48 { status: 500 }
49 );
50 }
51}
52
53export async function POST(request: NextRequest) {
54 try {
55 const authData = await getStoredAuth();
56 if (!authData || !authData.authenticated) {
57 return NextResponse.json(
58 { error: 'Authentication required' },
59 { status: 401 }
60 );
61 }
62
63 const body = await request.json();
64
65 const result = await fieldpineServerApi.apiCall("/PurchaseOrders", {
66 method: 'POST',
67 body,
68 cookie: authData.apiKey,
69 useOpenApi: true
70 });
71
72 return NextResponse.json({
73 success: true,
74 data: result,
75 source: 'openapi'
76 });
77
78 } catch (error) {
79 console.error('PurchaseOrders POST error:', error);
80 return NextResponse.json(
81 { error: 'Failed to create purchase order' },
82 { status: 500 }
83 );
84 }
85}