From 569cf84cde72588070aa3adb30c6e9b3151eddcf Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 30 Oct 2025 16:40:27 +0100 Subject: [PATCH] Add comprehensive server-side logging for Wren API calls - Added detailed request/response logging for createWrenOffsetOrder - Added detailed request/response logging for getWrenOffsetOrder - Logs include timestamp, duration, status, parameters, and full responses - API token is masked for security (shows first 8 and last 4 chars) - Logs will appear in Docker container logs with [WREN API SERVER] prefix - Makes it easier to track and debug Wren API calls from webhooks --- server/utils/wrenClient.js | 61 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/server/utils/wrenClient.js b/server/utils/wrenClient.js index f3d3bcb..3d4dd3c 100644 --- a/server/utils/wrenClient.js +++ b/server/utils/wrenClient.js @@ -21,9 +21,25 @@ export async function createWrenOffsetOrder({ amountCents, dryRun = false }) { + const startTime = Date.now(); const apiToken = process.env.WREN_API_TOKEN; + console.log('🔵 [WREN API SERVER] ========================================'); + console.log('🔵 [WREN API SERVER] POST /offset_orders - Request initiated'); + console.log('🔵 [WREN API SERVER] Timestamp:', new Date().toISOString()); + console.log('🔵 [WREN API SERVER] API Token:', apiToken ? `${apiToken.substring(0, 8)}...${apiToken.substring(apiToken.length - 4)}` : 'NOT SET'); + console.log('🔵 [WREN API SERVER] Request URL:', `${WREN_API_BASE_URL}/offset_orders`); + console.log('🔵 [WREN API SERVER] Parameters:', JSON.stringify({ + tons: parseFloat(tons), + portfolioId, + customerEmail, + currency: currency.toUpperCase(), + amountCents, + dryRun + }, null, 2)); + if (!apiToken) { + console.error('❌ [WREN API SERVER] WREN_API_TOKEN not configured'); throw new Error('WREN_API_TOKEN environment variable is required'); } @@ -49,10 +65,25 @@ export async function createWrenOffsetOrder({ } ); - console.log('✅ Wren offset order created:', response.data.id); + const duration = Date.now() - startTime; + console.log('✅ [WREN API SERVER] POST /offset_orders - Success'); + console.log('✅ [WREN API SERVER] Status:', response.status); + console.log('✅ [WREN API SERVER] Duration:', duration + 'ms'); + console.log('✅ [WREN API SERVER] Order ID:', response.data.id); + console.log('✅ [WREN API SERVER] Response:', JSON.stringify(response.data, null, 2)); + console.log('🔵 [WREN API SERVER] ========================================'); + return response.data; } catch (error) { - console.error('❌ Wren API error:', error.response?.data || error.message); + const duration = Date.now() - startTime; + console.error('❌ [WREN API SERVER] POST /offset_orders - Failed'); + console.error('❌ [WREN API SERVER] Status:', error.response?.status || 'No response'); + console.error('❌ [WREN API SERVER] Duration:', duration + 'ms'); + console.error('❌ [WREN API SERVER] Error message:', error.message); + console.error('❌ [WREN API SERVER] Error response:', JSON.stringify(error.response?.data, null, 2)); + console.error('❌ [WREN API SERVER] Full error:', error); + console.log('🔵 [WREN API SERVER] ========================================'); + throw new Error(`Wren API failed: ${error.response?.data?.message || error.message}`); } } @@ -63,9 +94,18 @@ export async function createWrenOffsetOrder({ * @returns {Promise} Wren order details */ export async function getWrenOffsetOrder(orderId) { + const startTime = Date.now(); const apiToken = process.env.WREN_API_TOKEN; + console.log('🔵 [WREN API SERVER] ========================================'); + console.log('🔵 [WREN API SERVER] GET /offset_orders/:id - Request initiated'); + console.log('🔵 [WREN API SERVER] Timestamp:', new Date().toISOString()); + console.log('🔵 [WREN API SERVER] API Token:', apiToken ? `${apiToken.substring(0, 8)}...${apiToken.substring(apiToken.length - 4)}` : 'NOT SET'); + console.log('🔵 [WREN API SERVER] Request URL:', `${WREN_API_BASE_URL}/offset_orders/${orderId}`); + console.log('🔵 [WREN API SERVER] Order ID:', orderId); + if (!apiToken) { + console.error('❌ [WREN API SERVER] WREN_API_TOKEN not configured'); throw new Error('WREN_API_TOKEN environment variable is required'); } @@ -80,9 +120,24 @@ export async function getWrenOffsetOrder(orderId) { } ); + const duration = Date.now() - startTime; + console.log('✅ [WREN API SERVER] GET /offset_orders/:id - Success'); + console.log('✅ [WREN API SERVER] Status:', response.status); + console.log('✅ [WREN API SERVER] Duration:', duration + 'ms'); + console.log('✅ [WREN API SERVER] Response:', JSON.stringify(response.data, null, 2)); + console.log('🔵 [WREN API SERVER] ========================================'); + return response.data; } catch (error) { - console.error('❌ Wren API error:', error.response?.data || error.message); + const duration = Date.now() - startTime; + console.error('❌ [WREN API SERVER] GET /offset_orders/:id - Failed'); + console.error('❌ [WREN API SERVER] Status:', error.response?.status || 'No response'); + console.error('❌ [WREN API SERVER] Duration:', duration + 'ms'); + console.error('❌ [WREN API SERVER] Error message:', error.message); + console.error('❌ [WREN API SERVER] Error response:', JSON.stringify(error.response?.data, null, 2)); + console.error('❌ [WREN API SERVER] Full error:', error); + console.log('🔵 [WREN API SERVER] ========================================'); + throw new Error(`Wren API failed: ${error.response?.data?.message || error.message}`); } }