Dealer_Onboarding_Backend/trigger-constitutional.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',
DEALER: 'dealer@royalenfield.com',
ASM: 'asm.sdelhi@royalenfield.com',
RBM_L1: 'rbm.ncr@royalenfield.com',
ZBH: 'yashwin@gmail.com',
DD_LEAD: 'ddlead@royalenfield.com',
DD_HEAD: 'ddhead@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: PASSWORD });
return data.token;
}
const delay = (ms = 500) => new Promise(res => setTimeout(res, ms));
async function run() {
try {
console.log('--- STARTING CONSTITUTIONAL CHANGE E2E FLOW ---');
console.log(`[STEP 0] Logging in as Dealer: ${EMAILS.DEALER}...`);
const dealerToken = await login(EMAILS.DEALER);
console.log('[STEP 1] Dealer Submitting Constitutional Change...');
const createRes = await apiRequest('/self-service/constitutional', 'POST', {
changeType: 'LLP Conversion',
reason: 'Converting to LLP for better operational governance.',
currentConstitution: 'Proprietorship',
newPartnersDetails: 'John Doe, Jane Smith',
shareholdingPattern: '60/40'
}, dealerToken);
const requestId = createRes.requestId;
console.log(`[STEP 1] Request Created. RequestID: ${requestId}`);
// Sequence of users taking actions to advance stages
// 1. Dealer SUBMITTED -> ASM_REVIEW
// 2. ASM moves it to ZM_RBM_REVIEW
// 3. ZM moves it to ZBH_REVIEW
// 4. ZBH moves it to LEAD_REVIEW
// 5. LEAD moves it to HEAD_REVIEW
// 6. HEAD moves it to NBH_APPROVAL
// 7. NBH moves it to LEGAL_REVIEW
// 8. LEGAL moves it to COMPLETED
const approvalSequence = [
{ name: 'ASM', email: EMAILS.ASM },
{ name: 'ZM/RBM', email: EMAILS.RBM_L1 },
{ name: 'ZBH', email: EMAILS.ZBH },
{ name: 'DD Lead', email: EMAILS.DD_LEAD },
{ name: 'DD Head', email: EMAILS.DD_HEAD },
{ name: 'NBH', email: EMAILS.NBH },
{ name: 'Legal Admin', email: EMAILS.LEGAL }
];
let currentStep = 2;
for (const actor of approvalSequence) {
console.log(`[STEP ${currentStep}] ${actor.name} (${actor.email}) processing approval...`);
const token = await login(actor.email);
const res = await apiRequest(`/self-service/constitutional/${requestId}/action`, 'POST', {
action: 'Approve',
comments: `${actor.name} verified the request.`
}, token);
console.log(`[STEP ${currentStep}] ${actor.name} Result: ${res.message}`);
currentStep++;
await delay(500);
}
console.log('[FINAL STEP] Verifying Completion Status...');
const adminToken = await login(EMAILS.DD_ADMIN);
const finalDetails = await apiRequest(`/self-service/constitutional/${requestId}`, 'GET', null, adminToken);
console.log(`Final Stage REACHED: ${finalDetails.request.currentStage}`);
console.log(`Final Status: ${finalDetails.request.status}`);
console.log('\n--- VERIFICATION RESULTS ---');
console.log('Legal Team Participation: CONFIRMED (Participated at Step 8)');
console.log('Final Outcome: SUCCESS (Workflow reached COMPLETED stage)');
console.log('--- CONSTITUTIONAL CHANGE FLOW COMPLETED SUCCESSFULLY! ---');
} catch (error) {
console.error('Workflow failed:', error.message);
process.exit(1);
}
}
run();