120 lines
4.6 KiB
JavaScript
120 lines
4.6 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',
|
|
SALES: 'sales@royalenfield.com',
|
|
SERVICE: 'service@royalenfield.com',
|
|
SPARES: 'spares@royalenfield.com',
|
|
FINANCE: 'finance@royalenfield.com',
|
|
ACCOUNTS: 'accounts@royalenfield.com',
|
|
WARRANTY: 'warranty@royalenfield.com',
|
|
MARKETING: 'marketing@royalenfield.com',
|
|
HR: 'hr@royalenfield.com',
|
|
IT: 'it@royalenfield.com',
|
|
LOGISTICS: 'logistics@royalenfield.com',
|
|
QUALITY: 'quality@royalenfield.com',
|
|
APPAREL: 'apparel@royalenfield.com',
|
|
DMS: 'dms@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
|
|
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 Review', email: EMAILS.LEGAL },
|
|
{ name: 'Legal Finalize', 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);
|
|
|
|
if (finalDetails.request.status === 'Completed' || finalDetails.request.currentStage === 'Completed') {
|
|
console.log(`[STEP ${currentStep}] SUCCESS: Request reached COMPLETED state.`);
|
|
} else {
|
|
console.error(`Verification Failed: Final stage is ${finalDetails.request.currentStage}. Status: ${finalDetails.request.status}`);
|
|
throw new Error('Constitutional change completion check failed.');
|
|
}
|
|
|
|
console.log('\n--- VERIFICATION RESULTS ---');
|
|
console.log('Outcome: CONSTITUTIONAL CHANGE FLOW COMPLETED SUCCESSFULLY');
|
|
process.exit(0);
|
|
|
|
} catch (error) {
|
|
console.error('Workflow failed:', error.message);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
run();
|