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// GET: Look up product by barcode
6export async function GET(request: NextRequest) {
7 try {
8 const authData = await getStoredAuth();
9 if (!authData) {
10 return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
11 }
12
13 const { searchParams } = new URL(request.url);
14 const barcode = searchParams.get("barcode");
15
16 if (!barcode) {
17 return NextResponse.json(
18 { success: false, error: "Barcode parameter is required" },
19 { status: 400 }
20 );
21 }
22
23 // Look up product by barcode
24 const result = await fieldpineServerApi.buckApiCall({
25 "3": "retailmax.elink.products",
26 "10": "199",
27 "9": `f501,0,${barcode}`
28 }, authData.apiKey!);
29
30 return NextResponse.json({
31 success: true,
32 data: result,
33 source: "elink",
34 });
35 } catch (error: any) {
36 console.error("Error looking up product:", error);
37 return NextResponse.json(
38 { success: false, error: error.message || "Failed to look up product" },
39 { status: error.status || 500 }
40 );
41 }
42}
43
44// POST: Record stocktake count
45export async function POST(request: NextRequest) {
46 try {
47 const authData = await getStoredAuth();
48 if (!authData) {
49 return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
50 }
51
52 const body = await request.json();
53 const { productId, quantity, barcode, storeId, reference } = body;
54
55 if (!productId || !quantity || !barcode) {
56 return NextResponse.json(
57 { success: false, error: "Missing required fields: productId, quantity, barcode" },
58 { status: 400 }
59 );
60 }
61
62 // Build DATI XML for stocktake count
63 const escapeXml = (v: string) =>
64 v.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
65
66 const xml = [
67 "<DATI>",
68 "<f8>retailmax.elink.stocktake.count</f8>",
69 "<f11:B>I</f11:B>",
70 "<f26>2</f26>",
71 "<f110_E>2</f110_E>", // scanner type
72 `<f118_E>${productId}</f118_E>`,
73 `<f111_E>${quantity}</f111_E>`,
74 `<f120_s>${escapeXml(barcode)}</f120_s>`,
75 storeId && storeId !== "0" ? `<f121_E>${storeId}</f121_E>` : "",
76 reference ? `<f124_s>${escapeXml(reference)}</f124_s>` : "",
77 "</DATI>",
78 ].join("");
79
80 // POST DATI XML to BUCK endpoint
81 const url = `${process.env.FIELDPINE_BASE_URL || "https://iig.cwanz.online"}/GNAP/j/buck`;
82 const headers: Record<string, string> = {
83 'Accept': 'application/json',
84 'Content-Type': 'text/xml',
85 'Cookie': `FieldpineApiKey=${authData.apiKey}`
86 };
87
88 const response = await fetch(url, {
89 method: 'POST',
90 headers,
91 body: xml,
92 });
93
94 if (!response.ok) {
95 throw new Error(`Stocktake count failed: ${response.status} ${response.statusText}`);
96 }
97
98 const result = await response.json();
99
100 return NextResponse.json({
101 success: true,
102 data: result,
103 source: "dati",
104 });
105 } catch (error: any) {
106 console.error("Error recording stocktake:", error);
107 return NextResponse.json(
108 { success: false, error: error.message || "Failed to record stocktake" },
109 { status: error.status || 500 }
110 );
111 }
112}