/** * Real Scenario Email Test * * Test emails with realistic workflow data * Simulates: User 10 creates request, User 12 is approver */ import { emailNotificationService } from '../services/emailNotification.service'; import logger from '../utils/logger'; const appDomain = process.env.APP_DOMAIN || 'royalenfield.com'; /** * Simulate real workflow scenario */ async function testRealScenario() { console.log('๐Ÿงช Testing Real Workflow Scenario\n'); console.log('Scenario: User 10 creates request, assigns to User 12 as approver\n'); // Mock user data (simulating real database records) const user10 = { userId: 'user-10-uuid', email: `john.doe@${appDomain}`, displayName: 'John Doe', department: 'Engineering', designation: 'Senior Engineer' }; const user12 = { userId: 'user-12-uuid', email: `jane.smith@${appDomain}`, displayName: 'Jane Smith', department: 'Management', designation: 'Engineering Manager', levelNumber: 1 }; // Mock request data (simulating real workflow request) const requestData = { requestId: 'req-uuid-12345', requestNumber: 'REQ-2025-12-0013', title: 'New Equipment Purchase Request - Testing Department', description: `

Equipment Purchase Request

Requesting approval for the following equipment for the testing department:

Budget Breakdown

Total estimated cost: โ‚น15,00,000

This purchase is critical for our Q1 2025 testing schedule and has been pre-approved by the department head.

Please review and approve at your earliest convenience.

For questions, contact: john.doe@${appDomain}

`, requestType: 'Purchase', priority: 'HIGH', createdAt: new Date(), tatHours: 48, totalApprovers: 3, initiatorName: user10.displayName, initiatorId: user10.userId }; // Mock approval chain for multi-level scenario const approvalChain = [ { levelNumber: 1, approverName: 'Jane Smith', approverEmail: `jane.smith@${appDomain}`, status: 'PENDING', approvedAt: null }, { levelNumber: 2, approverName: 'Michael Brown', approverEmail: `michael.brown@${appDomain}`, status: 'PENDING', approvedAt: null }, { levelNumber: 3, approverName: 'Sarah Johnson', approverEmail: `sarah.johnson@${appDomain}`, status: 'PENDING', approvedAt: null } ]; try { console.log('โ”€'.repeat(80)); console.log('๐Ÿ“ง Test 1: Request Created Email (to Initiator - User 10)'); console.log('โ”€'.repeat(80)); await emailNotificationService.sendRequestCreated( requestData, user10, user12 ); console.log('\n'); console.log('โ”€'.repeat(80)); console.log('๐Ÿ“ง Test 2: Multi-Level Approval Request Email (to Approver - User 12)'); console.log('โ”€'.repeat(80)); await emailNotificationService.sendApprovalRequest( requestData, user12, user10, true, // isMultiLevel approvalChain ); console.log('\n'); console.log('โ”€'.repeat(80)); console.log('๐Ÿ“ง Test 3: TAT Reminder at 40% (Configurable Threshold)'); console.log('โ”€'.repeat(80)); await emailNotificationService.sendTATReminder( requestData, user12, { thresholdPercentage: 40, timeRemaining: '28 hours', tatDeadline: new Date(Date.now() + 28 * 60 * 60 * 1000), assignedDate: requestData.createdAt } ); console.log('\n'); console.log('โ”€'.repeat(80)); console.log('๐Ÿ“ง Test 4: TAT Reminder at 80% (Configurable Threshold)'); console.log('โ”€'.repeat(80)); await emailNotificationService.sendTATReminder( requestData, user12, { thresholdPercentage: 80, timeRemaining: '9 hours', tatDeadline: new Date(Date.now() + 9 * 60 * 60 * 1000), assignedDate: requestData.createdAt } ); console.log('\n'); console.log('โ”€'.repeat(80)); console.log('๐Ÿ“ง Test 5: Approval Confirmation (User 12 approved)'); console.log('โ”€'.repeat(80)); const approvedUser12 = { ...user12, approvedAt: new Date(), comments: `

Approved! The equipment purchase is justified for the testing schedule.

Conditions:

` }; await emailNotificationService.sendApprovalConfirmation( requestData, approvedUser12, user10, false, // not final approval { displayName: 'Michael Brown', email: `michael.brown@${appDomain}` } ); console.log('\n'); console.log('โ”€'.repeat(80)); console.log('๐Ÿ“ง Test 6: TAT Breached (100%)'); console.log('โ”€'.repeat(80)); await emailNotificationService.sendTATBreached( requestData, user12, { timeOverdue: '6 hours overdue', tatDeadline: new Date(Date.now() - 6 * 60 * 60 * 1000), assignedDate: requestData.createdAt } ); console.log('\n'); console.log('โ”€'.repeat(80)); console.log('๐Ÿ“ง Test 7: Request Closed (with conclusion)'); console.log('โ”€'.repeat(80)); const closedRequestData = { ...requestData, closedAt: new Date(), totalApprovals: 3 }; await emailNotificationService.sendRequestClosed( closedRequestData, user10, { conclusionRemark: `

Project Successfully Completed

All equipment has been purchased and delivered to the testing department.

Final Summary:

Total cost: โ‚น14,85,000 (within budget)

Testing department is now fully equipped for Q1 2025 schedule. All deliverables met.
`, workNotesCount: 15, documentsCount: 8 } ); console.log('\n'); console.log('โ•'.repeat(80)); console.log('โœ… All Real Scenario Tests Complete!'); console.log('โ•'.repeat(80)); console.log('\n๐Ÿ’ก Check the Preview URLs above to see emails with REAL data!'); console.log('๐Ÿ“Œ All emails use actual request information, rich text content, and workflow data'); console.log('\n'); } catch (error) { console.error('โŒ Test failed:', error); throw error; } } // Run the test console.log('\n'); console.log('โ•'.repeat(80)); console.log(' REAL SCENARIO EMAIL TEST'); console.log(' User 10 (John Doe) โ†’ Creates Request'); console.log(' User 12 (Jane Smith) โ†’ Assigned as Approver'); console.log('โ•'.repeat(80)); console.log('\n'); testRealScenario() .then(() => { console.log('โœ… Test completed successfully\n'); process.exit(0); }) .catch((error) => { console.error('โŒ Test failed:', error); process.exit(1); });