Dealer_Onboarding_Backend/trigger-termination.js

110 lines
4.2 KiB
JavaScript

import fs from 'fs';
const BASE_URL = 'http://localhost:5000/api';
const PASSWORD = 'Admin@123';
const EMAILS = {
DD_ADMIN: 'lince@gmail.com',
ASM: 'asm.sdelhi@royalenfield.com',
RBM: 'rbm.ncr@royalenfield.com',
ZBH: 'yashwin@gmail.com',
DD_LEAD: 'ddlead@royalenfield.com',
LEGAL: 'legal@royalenfield.com',
NBH: 'nbh@royalenfield.com',
CCO: 'cco@royalenfield.com',
CEO: 'ceo@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: PASSWORD });
return data.token;
}
const delay = (ms = 500) => new Promise(res => setTimeout(res, ms));
async function run() {
try {
console.log('--- STARTING DEALER TERMINATION E2E FLOW ---');
const adminToken = await login(EMAILS.DD_ADMIN);
const dealersRes = await apiRequest('/dealer', 'GET', null, adminToken);
const targetDealer = dealersRes.data[0];
if (!targetDealer) throw new Error('No dealer profiles found for termination test. Run seed first.');
console.log(`Targeting Dealer: ${targetDealer.legalName} (${targetDealer.id})`);
// STEP 1: Submission (ASM)
console.log('[STEP 1] ASM Initiating Termination...');
const asmToken = await login(EMAILS.ASM);
const createRes = await apiRequest('/termination', 'POST', {
dealerId: targetDealer.id,
category: 'Performance',
reason: 'Consistently failed to meet commitment targets.',
proposedLwd: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString(),
comments: 'Auto termination for testing flow ending with Legal Team, CCO, CEO.'
}, asmToken);
const terminationId = createRes.termination.id;
console.log(`[STEP 1] Termination Request Created. ID: ${terminationId}`);
const approvals = [
{ name: 'RBM Review', email: EMAILS.RBM },
{ name: 'ZBH Review', email: EMAILS.ZBH },
{ name: 'DD Lead Review', email: EMAILS.DD_LEAD },
{ name: 'Legal Verification', email: EMAILS.LEGAL },
{ name: 'NBH Evaluation', email: EMAILS.NBH },
{ name: 'SCN Issued', email: EMAILS.NBH },
{ name: 'Personal Hearing Outcome', email: EMAILS.DD_LEAD },
{ name: 'NBH Final Approval', email: EMAILS.NBH },
{ name: 'CCO Approval', email: EMAILS.CCO },
{ name: 'CEO Final Approval', email: EMAILS.CEO },
{ name: 'Legal Termination Letter', email: EMAILS.LEGAL }
];
let currentStep = 2;
for (const actor of approvals) {
console.log(`[STEP ${currentStep}] ${actor.name} (${actor.email}) processing approval...`);
const token = await login(actor.email);
await apiRequest(`/termination/${terminationId}/status`, 'PUT', {
action: 'approve',
remarks: `${actor.name} verification completed.`
}, token);
console.log(`[STEP ${currentStep}] ${actor.name} Result: SUCCESS`);
currentStep++;
await delay(500);
}
console.log('[FINAL STEP] Verifying Terminated Status...');
const finalDetails = await apiRequest(`/termination/${terminationId}`, 'GET', null, adminToken);
console.log(`Final Stage REACHED: ${finalDetails.termination.currentStage}`);
console.log('\n--- VERIFICATION SUCCESSFUL ---');
console.log('Role Participation: LEGAL TEAM, CCO, and CEO roles verified.');
console.log('Outcome: DEALER TERMINATED SUCCESSFULLY');
process.exit(0);
} catch (error) {
console.error('Workflow failed:', error.message);
process.exit(1);
}
}
run();