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 PATCH(
6 request: NextRequest,
7 context: { params: Promise<{ id: string }> }
8) {
9 try {
10 const authData = await getStoredAuth();
11 if (!authData || !authData.authenticated) {
12 return NextResponse.json(
13 { error: 'Authentication required' },
14 { status: 401 }
15 );
16 }
17
18 const { id: saleId } = await context.params;
19 const updates = await request.json();
20
21 console.log(`[Sale Update API] Updating sale ${saleId}`, updates);
22
23 // Build DATI packet for sale update
24 let packet = '<DATI><f8_s>retailmax.elink.sale.edit</f8_s>';
25 packet += '<f11_B>E</f11_B>';
26 packet += `<f100_E>${saleId}</f100_E>`;
27
28 // Add fields to update
29 if (updates.f7102 !== undefined) {
30 packet += `<f7102>${updates.f7102}</f7102>`; // Store assignment
31 }
32 if (updates.f7120 !== undefined) {
33 packet += `<f7120>${updates.f7120}</f7120>`; // Picking comments
34 }
35 if (updates.f7121 !== undefined) {
36 packet += `<f7121_E>${updates.f7121}</f7121>`; // Row color
37 }
38 if (updates.f7123 !== undefined) {
39 packet += `<f7123>${updates.f7123}</f7123>`; // Customer comments
40 }
41 if (updates.f108 !== undefined) {
42 packet += `<f108>${updates.f108}</f108>`; // Status
43 }
44
45 // Handle line item updates (if provided)
46 if (updates.lineItems && Array.isArray(updates.lineItems)) {
47 console.log(`[Sale Update API] Updating ${updates.lineItems.length} line items`);
48 // Note: Line item updates may need separate DATI packets or different approach
49 // For now, log this - full implementation depends on Fieldpine's line item edit capability
50 // This would typically require retailmax.elink.saleline.edit or similar
51 }
52
53 packet += '</DATI>';
54
55 console.log(`[Sale Update API] DATI packet:`, packet);
56
57 // Send update to Fieldpine
58 const baseUrl = process.env.FIELDPINE_BASE_URL || 'https://iig.cwanz.online';
59 const response = await fetch(`${baseUrl}/DATI`, {
60 method: 'POST',
61 headers: {
62 'Content-Type': 'application/xml',
63 'Cookie': `FieldpineApiKey=${authData.apiKey}`
64 },
65 body: packet
66 });
67
68 if (!response.ok) {
69 throw new Error(`Fieldpine update failed: ${response.status}`);
70 }
71
72 console.log(`[Sale Update API] Updated sale ${saleId}`);
73
74 return NextResponse.json({
75 success: true,
76 message: 'Sale updated successfully'
77 });
78 } catch (error: any) {
79 console.error('[Sale Update API] Error:', error);
80 return NextResponse.json(
81 { success: false, error: error.message || 'Failed to update sale' },
82 { status: 500 }
83 );
84 }
85}