import fs from 'fs'; const BASE_URL = 'http://localhost:5000/api'; const PASSWORD = 'Admin@123'; const EMAILS = { DD_ADMIN: 'lince@gmail.com', DEALER: 'dealer@royalenfield.com', ASM: 'asm.sdelhi@royalenfield.com', RBM: 'rbm.ncr@royalenfield.com', ZBH: 'yashwin@gmail.com', DD_LEAD: 'ddlead@royalenfield.com', NBH: 'nbh@royalenfield.com', LEGAL: 'legal@royalenfield.com' }; async function apiRequest(endpoint, method = 'GET', body = null, token = null) { const headers = { 'Content-Type': 'application/json' }; if (token) headers['Authorization'] = `Bearer ${token}`; const config = { method, headers }; if (body) config.body = JSON.stringify(body); const response = await fetch(`${BASE_URL}${endpoint}`, config); const data = await response.json(); if (!response.ok) { throw new Error(`API Error ${method} ${endpoint}: ${JSON.stringify(data)}`); } return data; } async function login(email) { const data = await apiRequest('/auth/login', 'POST', { email, password: (email === 'dealer@royalenfield.com' ? 'Admin@123' : PASSWORD) }); return data.token; } const delay = (ms = 500) => new Promise(res => setTimeout(res, ms)); async function run() { try { console.log('--- STARTING DEALER RESIGNATION E2E FLOW ---'); const adminToken = await login(EMAILS.DD_ADMIN); const appsRes = await apiRequest('/onboarding/applications', 'GET', null, adminToken); const targetApp = appsRes.data.find(a => a.status === 'Onboarded') || appsRes.data[0]; if (!targetApp) throw new Error('No onboarded applications found for resignation test.'); console.log(`Targeting Application: ${targetApp.applicantName} (${targetApp.id}) - Email: ${targetApp.email}`); // 1.0 Login as the Dealer console.log(`[STEP 1.0] Logging in as Dealer (${targetApp.email})...`); const dealerData = await apiRequest('/auth/login', 'POST', { email: targetApp.email, password: 'Dealer@123' // Standard default password from onboarding }); const dealerToken = dealerData.token; // 1.1 Discover Dealer's Outlet console.log(`[STEP 1.1] Discovering Outlets for Dealer...`); const dealerDashboard = await apiRequest('/dealers/dashboard', 'GET', null, dealerToken); const targetOutlet = dealerDashboard.data.outlets[0]; if (!targetOutlet) throw new Error('No outlets found for this dealer. Ensure they are fully onboarded.'); console.log(`Found Target Outlet: ${targetOutlet.name} (${targetOutlet.code})`); console.log(`[STEP 1.2] Dealer Submitting Resignation for Outlet...`); const createRes = await apiRequest('/self-service/resignations', 'POST', { outletId: targetOutlet.id, resignationType: 'Voluntary', lastOperationalDateSales: new Date().toISOString().split('T')[0], lastOperationalDateServices: new Date().toISOString().split('T')[0], reason: 'Focusing on other business ventures', remarks: 'Initiating voluntary resignation for E2E validation.' }, dealerToken); const resignationId = createRes.resignation.id; console.log(`[STEP 1] Resignation Created. ID: ${resignationId}`); const approvals = [ { name: 'ASM', email: EMAILS.ASM }, { name: 'RBM', email: EMAILS.RBM }, { name: 'ZBH', email: EMAILS.ZBH }, { name: 'DD Lead', email: EMAILS.DD_LEAD }, { name: 'NBH', email: EMAILS.NBH }, { name: 'DD Admin', email: EMAILS.DD_ADMIN }, { name: 'Legal Admin', email: EMAILS.LEGAL } ]; let currentStep = 2; for (const actor of approvals) { console.log(`[STEP ${currentStep}] ${actor.name} (${actor.email}) approving...`); const token = await login(actor.email); const res = await apiRequest(`/self-service/resignations/${resignationId}/approve`, 'PUT', { remarks: `${actor.name} approved the resignation request.` }, token); console.log(`[STEP ${currentStep}] ${actor.name} Result: ${res.message}`); currentStep++; await delay(500); } console.log('[FINAL STEP] Verifying Acceptance Status...'); const finalRes = await apiRequest(`/self-service/resignations/${resignationId}`, 'GET', null, adminToken); console.log(`Final Stage: ${finalRes.resignation.currentStage}`); console.log(`Final Status: ${finalRes.resignation.status}`); console.log('\n--- VERIFICATION SUCCESSFUL ---'); console.log('Outcome: RESIGNATION ACCEPTED BY ALL STAKEHOLDERS (Includng Legal).'); process.exit(0); } catch (error) { console.error('Workflow failed:', error.message); process.exit(1); } } run();