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 ShelfVisits Endpoint
7 * GET /api/v1/openapi/shelf-visits - Retrieve shelf visit records
8 * POST /api/v1/openapi/shelf-visits - Record shelf visit/observation
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 locid = searchParams.get('locid');
24 if (locid) params.locid = parseInt(locid);
25
26 const pid = searchParams.get('pid');
27 if (pid) params.pid = parseInt(pid);
28
29 const from = searchParams.get('from');
30 if (from) params.from = from;
31
32 const to = searchParams.get('to');
33 if (to) params.to = to;
34
35 const shelfVisits = await fieldpineServerApi.apiCall("/ShelfVisits", {
36 params,
37 cookie: authData.apiKey,
38 useOpenApi: true
39 });
40
41 return NextResponse.json({
42 success: true,
43 data: shelfVisits,
44 source: 'openapi'
45 });
46
47 } catch (error) {
48 console.error('ShelfVisits API error:', error);
49 return NextResponse.json(
50 { error: 'Failed to fetch shelf visits' },
51 { status: 500 }
52 );
53 }
54}
55
56export async function POST(request: NextRequest) {
57 try {
58 const authData = await getStoredAuth();
59 if (!authData || !authData.authenticated) {
60 return NextResponse.json(
61 { error: 'Authentication required' },
62 { status: 401 }
63 );
64 }
65
66 const { searchParams } = new URL(request.url);
67 const body = await request.json();
68
69 const params: Record<string, string> = {};
70 const cardNo = searchParams.get('CardNo');
71 if (cardNo) params.CardNo = cardNo;
72
73 const pass = searchParams.get('Pass');
74 if (pass) params.Pass = pass;
75
76 const result = await fieldpineServerApi.apiCall("/ShelfVisits", {
77 method: 'POST',
78 params,
79 body,
80 cookie: authData.apiKey,
81 useOpenApi: true
82 });
83
84 return NextResponse.json({
85 success: true,
86 data: result,
87 source: 'openapi'
88 });
89
90 } catch (error) {
91 console.error('ShelfVisits POST error:', error);
92 return NextResponse.json(
93 { error: 'Failed to record shelf visit' },
94 { status: 500 }
95 );
96 }
97}