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 { getRequestContext } from '@/lib/server/sessionUtils';
4
5/**
6 * GET /api/v1/elink/sales/[id]
7 * Fetch detailed sale information including line items, payments, and location
8 */
9export async function GET(
10 request: NextRequest,
11 { params }: { params: Promise<{ id: string }> }
12) {
13 try {
14 const context = await getRequestContext(request);
15 if (!context?.session) {
16 return NextResponse.json(
17 { success: false, error: 'Not authenticated' },
18 { status: 401 }
19 );
20 }
21
22 const { id: saleId } = await params;
23 console.log(`[Sale Fetch] Fetching sale details for ID: ${saleId}`);
24
25 // Use BUCK API to fetch full sale details
26 // retailmax.elink.sale.fetch with 100=1 returns full details including LINE, PAYM, LOCN
27 const buckParams: Record<string, string> = {
28 '3': 'retailmax.elink.sale.fetch',
29 '100': '1', // Include all sub-records
30 '9': `f100,0,${saleId}` // Filter by sale ID
31 };
32
33 const result = await fieldpineServerApi.buckApiCall(buckParams, context.session.apiKey);
34
35 if (!result) {
36 return NextResponse.json(
37 { success: false, error: 'Sale not found' },
38 { status: 404 }
39 );
40 }
41
42 console.log(`[Sale Fetch] Successfully fetched sale ${saleId}`);
43
44 return NextResponse.json({
45 success: true,
46 data: result
47 });
48
49 } catch (error) {
50 console.error('[Sale Fetch] Error:', error);
51 return NextResponse.json(
52 {
53 success: false,
54 error: error instanceof Error ? error.message : 'Failed to fetch sale details'
55 },
56 { status: 500 }
57 );
58 }
59}
60
61/**
62 * DELETE /api/v1/elink/sales/[id]
63 * Void/delete a sale by setting phase to 10002
64 */
65export async function DELETE(
66 request: NextRequest,
67 { params }: { params: Promise<{ id: string }> }
68) {
69 try {
70 const context = await getRequestContext(request);
71 if (!context?.session) {
72 return NextResponse.json(
73 { success: false, error: 'Not authenticated' },
74 { status: 401 }
75 );
76 }
77
78 const { id: saleId } = await params;
79 console.log(`[Sale Delete] Voiding sale ID: ${saleId}`);
80
81 // First, parse the delete command (dry run)
82 const parseUrl = `${context.session.storeUrl}/OpenApi2/Sales/Edit/Parse?saleid=${saleId}&ref=${Date.now()}`;
83 const parseResponse = await fetch(parseUrl, {
84 method: 'POST',
85 headers: {
86 'Authorization': `Bearer ${context.session.apiKey}`,
87 'Content-Type': 'text/plain'
88 },
89 body: 'delete sale\n'
90 });
91
92 if (!parseResponse.ok) {
93 throw new Error(`Parse failed: ${parseResponse.status} ${parseResponse.statusText}`);
94 }
95
96 const parseResult = await parseResponse.json();
97 console.log('[Sale Delete] Parse result:', parseResult);
98
99 // If parse was successful, execute the actual delete
100 const editUrl = `${context.session.storeUrl}/OpenApi2/Sales/Edit?saleid=${saleId}&ref=${Date.now()}`;
101 const editResponse = await fetch(editUrl, {
102 method: 'POST',
103 headers: {
104 'Authorization': `Bearer ${context.session.apiKey}`,
105 'Content-Type': 'text/plain'
106 },
107 body: 'delete sale\n'
108 });
109
110 if (!editResponse.ok) {
111 throw new Error(`Delete failed: ${editResponse.status} ${editResponse.statusText}`);
112 }
113
114 const editResult = await editResponse.json();
115 console.log('[Sale Delete] Edit result:', editResult);
116
117 if (editResult.data?.Status?.includes('Changes applied')) {
118 return NextResponse.json({
119 success: true,
120 message: 'Sale voided successfully',
121 data: editResult.data
122 });
123 } else {
124 return NextResponse.json(
125 {
126 success: false,
127 error: 'Failed to void sale',
128 details: editResult
129 },
130 { status: 500 }
131 );
132 }
133
134 } catch (error) {
135 console.error('[Sale Delete] Error:', error);
136 return NextResponse.json(
137 {
138 success: false,
139 error: error instanceof Error ? error.message : 'Failed to void sale'
140 },
141 { status: 500 }
142 );
143 }
144}
145
146/**
147 * PUT /api/v1/elink/sales/[id]
148 * Update sale details (placeholder for future implementation)
149 */
150export async function PUT(
151 request: NextRequest,
152 { params }: { params: Promise<{ id: string }> }
153) {
154 return NextResponse.json(
155 { success: false, error: 'Sale updates not yet implemented' },
156 { status: 501 }
157 );
158}