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
5export async function GET(request: NextRequest) {
6 try {
7 const authData = await getStoredAuth();
8 if (!authData?.authenticated) {
9 return NextResponse.json(
10 { success: false, error: 'Authentication required' },
11 { status: 401 }
12 );
13 }
14
15 // Fetch products with filter a5
16 const result = await fieldpineServerApi.buckApiCall({
17 "3": "retailmax.elink.products",
18 "8": "150000",
19 "9": "f503,0,a5", // Note: Using a5 filter as per user's request
20 "10": "113,349,100,101,105,103,113s,1101,1102",
21 "18": "1101,1102"
22 }, authData.apiKey);
23
24 const products = result.DATS || [];
25
26 // Group products by PLU code (f105)
27 const pluGroups = new Map<string, any[]>();
28
29 products.forEach((product: any) => {
30 const plu = product.f105;
31
32 // Only process products with a PLU
33 if (plu && plu.trim() !== '') {
34 if (!pluGroups.has(plu)) {
35 pluGroups.set(plu, []);
36 }
37 pluGroups.get(plu)!.push(product);
38 }
39 });
40
41 // Find PLUs with duplicates (more than one product)
42 const duplicates: any[] = [];
43
44 pluGroups.forEach((productList: any, plu: string) => {
45 if (productList.length > 1) {
46 duplicates.push({
47 plu: plu,
48 count: productList.length,
49 products: productList.map((p: any) => ({
50 id: p.f100,
51 name: p.f101,
52 price: p.f103,
53 cost: p.f108,
54 salesInfo: p.f1102 || 'No sales data'
55 }))
56 });
57 }
58 });
59
60 // Sort by count (most duplicates first)
61 duplicates.sort((a: any, b: any) => b.count - a.count);
62
63 return NextResponse.json({
64 success: true,
65 data: {
66 duplicates,
67 totalProducts: products.length,
68 duplicatePLUCount: duplicates.length,
69 totalDuplicateProducts: duplicates.reduce((sum, d) => sum + d.count, 0)
70 }
71 });
72
73 } catch (error: any) {
74 console.error('Error fetching duplicate PLU data:', error);
75 return NextResponse.json(
76 { success: false, error: error.message || 'Failed to fetch duplicate PLU data' },
77 { status: 500 }
78 );
79 }
80}