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