#!/usr/bin/env node
// Test script to verify Dealer.K API response processing
const { logger } = require('./src/utils/logger');
console.log('🔗 Testing Dealer.K API Response Processing\n');
// Simulate the actual API response structure from the logs
const mockApiResponse = {
"request": {
"action": "list",
"controller": "lead",
"apiKey": "b50e31aaacfa73f7be1275ae1df39425",
"dateFrom": "2025-06-22",
"dateTo": null,
"format": "json"
},
"response": [
{
"cap": "9401 AS",
"channel": "marketplace.bnp.devk",
"surname": "Alvarez",
"dateCreated": "2025-06-23 18:19:17",
"descOptional": "",
"email": "test@dealerk.com",
"maker": "Alfa Romeo",
"model": "Giulia",
"nome": "Lucas",
"note": "",
"internalNotes": null,
"vat": "",
"price": 28975,
"county": "N.D.",
"version": "2.2 Turbo AT8 210CV AWD Q4 Veloce",
"phone": "001234567",
"type": "Usato",
"typeCode": "used",
"vehicleType": "USED",
"vehicleClass": "auto",
"vehicle_id": 9476489,
"sub_type": "preventivo",
"source_url": "http://marketplace.bnp.devk/cars/used/beek/hyundai/tucson/benzine/1-6t-gdi-klass-multimedia-met-navi-app-camera/244/",
"cellphone": "",
"city": "",
"extraFields": "extra_leadWeb:",
"originCampaign": "",
"originSource": "",
"originDetail": "",
"originTerm": "",
"originContent": "",
"originGclid": "",
"privacyMarketingSms": 0,
"privacyMarketingPhone": 0,
"privacyMarketingMail": 0,
"privacyFinancing": "ND",
"privacyCommercial": "ND",
"privacyLegal": 1,
"isCompany": 0,
"country": null,
"job": "",
"companyName": "",
"qualified": 0,
"dealer_id": 196036,
"dealer_name": "AutoItalia Finance",
"dealer_address": "Via Nazionale 100",
"dealer_dms_code": null,
"dealer_city": "Rome",
"external_id": null,
"form_name": "Richiedi preventivo",
"form_code": "USED_ESTIMATE",
"company": {
"externalId": null,
"name": "Automotive Dealer Day 2025",
"id": 35430
},
"id": "f7273059254051e2f69750f11cb3e5ec"
}
]
};
// Test the processing logic from our fixed implementation
function processLeadsResponse(data) {
let leads;
if (data && data.response && Array.isArray(data.response)) {
leads = data.response;
logger.info(`Leads API returned ${leads.length} leads from nested response structure`);
} else if (Array.isArray(data)) {
leads = data;
logger.info(`Leads API returned ${leads.length} leads from direct array`);
} else {
logger.warn('Unexpected leads API response format', {
responseStructure: typeof data,
hasResponse: data && typeof data.response !== 'undefined',
responseType: data && data.response ? typeof data.response : 'undefined'
});
leads = [];
}
return leads;
}
// Test the field mapping
function mapLeadFields(leads) {
return leads.map(lead => ({
id: lead.id,
vehicleId: lead.vehicle_id || lead.vehicleId || lead.stockNumber,
customerName: `${lead.nome || ''} ${lead.surname || ''}`.trim() || lead.customerName || lead.customer_name,
email: lead.email,
phone: lead.phone || lead.cellphone,
message: lead.note || lead.message || lead.inquiry,
source: lead.channel || lead.source || lead.lead_source,
createdAt: lead.dateCreated || lead.created_at || lead.date || lead.createdAt,
status: lead.qualified ? 'qualified' : 'active',
vehicleInfo: {
make: lead.maker || lead.vehicleMake || lead.make,
model: lead.model || lead.vehicleModel,
version: lead.version || lead.vehicleVersion,
type: lead.type || lead.vehicleType,
price: lead.price
},
dealerInfo: {
dealerId: lead.dealer_id,
dealerName: lead.dealer_name,
dealerAddress: lead.dealer_address,
dealerCity: lead.dealer_city
},
formInfo: {
formName: lead.form_name,
formCode: lead.form_code
}
}));
}
// Test vehicle filtering
function filterForVehicle(leads, vehicleId) {
return leads.filter(lead => {
return lead.vehicle_id === vehicleId ||
lead.vehicleId === vehicleId ||
lead.stockNumber === vehicleId ||
lead.stock_number === vehicleId;
});
}
console.log('1. Testing response processing...');
const processedLeads = processLeadsResponse(mockApiResponse);
console.log(` ✅ Extracted ${processedLeads.length} leads from nested response structure`);
console.log('\n2. Testing field mapping...');
const mappedLeads = mapLeadFields(processedLeads);
console.log(' ✅ Mapped lead fields:', JSON.stringify(mappedLeads[0], null, 2));
console.log('\n3. Testing vehicle filtering...');
const vehicleLeads = filterForVehicle(processedLeads, 9476489);
console.log(` ✅ Found ${vehicleLeads.length} leads for vehicle 9476489`);
console.log('\n4. Testing date processing...');
const lead = processedLeads[0];
const leadDate = new Date(lead.dateCreated);
console.log(` ✅ Parsed date: ${leadDate.toISOString()} from "${lead.dateCreated}"`);
console.log('\n✅ All Dealer.K API response processing tests passed!');
console.log('\n🎯 Expected behavior when calling get_vehicle_leads:');
console.log(' • API response will be properly unwrapped from nested structure');
console.log(' • Lead fields will be correctly mapped to standard format');
console.log(' • Vehicle filtering will work with vehicle_id field');
console.log(' • Date processing will handle "YYYY-MM-DD HH:MM:SS" format');
console.log(' • All customer and vehicle information will be properly extracted');