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 * Sales Statistics API
7 *
8 * Provides various sales statistics endpoints:
9 * - ?type=today - Today's overall stats
10 * - ?type=department - Today's stats by department
11 * - ?type=hourly - Sales broken down by hour
12 * - ?type=store - Sales by store/location
13 * - ?type=payment - Sales by payment method
14 * - ?type=staff - Sales by staff member
15 */
16export async function GET(request: NextRequest) {
17 try {
18 const authData = await getStoredAuth();
19 if (!authData) {
20 return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
21 }
22
23 const { searchParams } = new URL(request.url);
24 const type = searchParams.get("type") || "today";
25
26 let endpoint: string;
27 let params: Record<string, string> = {};
28
29 switch (type) {
30 case "today":
31 // Overall today's statistics
32 params = {
33 "3": "retailmax.elink.stats.today"
34 };
35 break;
36
37 case "department":
38 // Today's stats by department
39 params = {
40 "3": "retailmax.elink.stats.today.department"
41 };
42 break;
43
44 case "hourly":
45 // Sales grouped by hour - use sale.totals.group with hour dimension
46 params = {
47 "3": "retailmax.elink.sale.totals.group",
48 "15": "hour", // Group by hour
49 "9": "f110,5,today" // Filter: sales >= today
50 };
51 break;
52
53 case "daily":
54 // Sales grouped by day for the last 7 days
55 params = {
56 "3": "retailmax.elink.sale.totals.group",
57 "15": "day", // Group by day
58 "9": "f110,5,today-7" // Filter: sales >= 7 days ago
59 };
60 break;
61
62 case "store":
63 // Sales grouped by store/location
64 params = {
65 "3": "retailmax.elink.sale.totals.group",
66 "15": "location", // Group by location
67 "9": "f110,5,today"
68 };
69 break;
70
71 case "payment":
72 // Get flat sale list with payment details
73 params = {
74 "3": "retailmax.elink.saleflat.list",
75 "9": "f110,5,today",
76 "10": "110,120,130,140" // Include sale fields + payment info
77 };
78 break;
79
80 case "staff":
81 // Sales grouped by staff/teller
82 params = {
83 "3": "retailmax.elink.sale.totals.group",
84 "15": "teller", // Group by staff
85 "9": "f110,5,today"
86 };
87 break;
88
89 default:
90 return NextResponse.json(
91 { success: false, error: "Invalid type parameter" },
92 { status: 400 }
93 );
94 }
95
96 console.log(`[Sales Stats API] Fetching ${type} stats`);
97
98 const result = await fieldpineServerApi.buckApiCall(params, authData.apiKey!);
99
100 console.log(`[Sales Stats API] Result for ${type}:`, {
101 hasDats: !!result.DATS,
102 datsLength: result.DATS?.length,
103 resultKeys: Object.keys(result)
104 });
105
106 return NextResponse.json({
107 success: true,
108 data: result,
109 type,
110 source: "elink",
111 });
112 } catch (error: any) {
113 console.error("[Sales Stats API] Error:", error);
114 return NextResponse.json(
115 { success: false, error: error.message || "Failed to fetch sales statistics" },
116 { status: error.status || 500 }
117 );
118 }
119}