/** * Communication Hub Usage Examples * * This file demonstrates how to use the hierarchical communication system. */ import { IntegrationService } from '../service/integration.service'; export class CommunicationUsageExamples { constructor(private readonly communicationService: IntegrationService) {} /** * Example 1: Send an email using Gmail API */ async sendEmailViaGmailApi() { // First, create a configuration for Gmail API const emailConfig = { clientId: 'your-gmail-client-id', clientSecret: 'your-gmail-client-secret', refreshToken: 'your-refresh-token', }; const hub = await this.communicationService.createIntegrationConfig( 1, // levelId 'organization', // levelType 'MY_APP_001', // app_code 'EMAIL', // integration_type 'gmail', // integration_provider 1, // integration_source_id emailConfig, ); console.log('Created email config:', hub); // Send a message const result = await this.communicationService.sendMessage({ levelId: 1, levelType: 'organization', app_code: 'MY_APP_001', to: 'recipient@example.com', message: 'Hello from Gmail API!', mode: 'EMAIL', }); console.log('Email sent:', result); } /** * Example 2: Send Email using Outlook API */ async sendEmailViaOutlookApi() { const outlookConfig = { clientId: 'your-outlook-client-id', clientSecret: 'your-outlook-client-secret', tenantId: 'your-tenant-id', email: 'user@outlook.com', }; await this.communicationService.createIntegrationConfig( 1, 'organization', 'MY_APP_001', 'EMAIL', 'outlook', 2, outlookConfig, ); const result = await this.communicationService.sendMessage({ levelId: 1, levelType: 'organization', app_code: 'MY_APP_001', to: 'recipient@example.com', message: 'Hello from Outlook API!', mode: 'EMAIL', }); console.log('Email sent:', result); } /** * Example 3: Send WhatsApp message */ async sendWhatsAppMessage() { const whatsappConfig = { accessToken: 'your-whatsapp-access-token', phoneNumberId: 'your-phone-number-id', businessAccountId: 'your-business-account-id', }; await this.communicationService.createIntegrationConfig( 1, 'organization', 'MY_APP_001', 'WA', 'whatsapp', 3, whatsappConfig, ); const result = await this.communicationService.sendMessage({ levelId: 1, levelType: 'organization', app_code: 'MY_APP_001', to: '+9876543210', message: 'Hello from WhatsApp!', mode: 'WA', }); console.log('WhatsApp sent:', result); } /** * Example 4: Make a voice call using Ozonetel */ async makeVoiceCall() { const voiceConfig = { apiKey: 'your-ozonetel-api-key', callerId: '+1234567890', voiceUrl: 'https://your-voice-script-url.com', }; await this.communicationService.createIntegrationConfig( 1, 'organization', 'MY_APP_001', 'TELEPHONE', 'ozonetel', 4, voiceConfig, ); const result = await this.communicationService.sendMessage({ levelId: 1, levelType: 'organization', app_code: 'MY_APP_001', to: '+9876543210', message: 'Voice message content', mode: 'TELEPHONE', }); console.log('Voice call initiated:', result); } /** * Example 5: Get all supported combinations */ getSupportedCombinations() { const combinations = this.communicationService.getSupportedCombinations(); console.log('Supported combinations:', combinations); // Output will be: // [ // { mode: 'EMAIL', service: 'API', provider: 'gmail' }, // { mode: 'EMAIL', service: 'SMTP', provider: 'gmail' }, // { mode: 'EMAIL', service: 'API', provider: 'outlook' }, // { mode: 'EMAIL', service: 'API', provider: 'sendgrid' }, // { mode: 'WA', service: 'API', provider: 'whatsapp' }, // { mode: 'TELEPHONE', service: 'THIRD_PARTY', provider: 'ozonetel' }, // { mode: 'TELEPHONE', service: 'THIRD_PARTY', provider: 'tubelight' } // ] } /** * Example 6: Send Gmail email with attachments */ async sendGmailWithAttachments() { const result = await this.communicationService.sendGenericMessage({ levelId: 1, levelType: 'organization', app_code: 'MY_APP_001', to: 'recipient@example.com', subject: 'Email with Attachments', message: 'Please find the attached documents.', type: 'EMAIL', attachments: [ { filename: 'document.pdf', content: 'base64-encoded-content-here', contentType: 'application/pdf', }, { filename: 'image.jpg', path: '/path/to/local/image.jpg', contentType: 'image/jpeg', }, ], }); console.log('Gmail with attachments sent:', result); } /** * Example 7: Send Gmail email using HTML template */ async sendGmailWithTemplate() { const htmlTemplate = `
Thank you for joining our platform.
Your account ID is: {{accountId}}
Verify your email `; const result = await this.communicationService.sendGenericMessage({ levelId: 1, levelType: 'organization', app_code: 'MY_APP_001', to: 'recipient@example.com', subject: 'Welcome to Our Platform', message: 'Welcome to Our Platform', html: htmlTemplate, type: 'EMAIL', variables: { userName: 'John Doe', accountId: 'ACC_12345', verificationLink: 'https://example.com/verify/token123', }, }); console.log('Gmail template email sent:', result); } /** * Example 8: Send SendGrid email with attachments */ async sendSendGridWithAttachments() { // First create SendGrid config const sendGridConfig = { apiKey: 'your-sendgrid-api-key', fromEmail: 'noreply@yourdomain.com', fromName: 'Your Company', }; await this.communicationService.createIntegrationConfig( 1, 'organization', 'MY_APP_001', 'EMAIL', 'sendgrid', 5, sendGridConfig, ); const result = await this.communicationService.sendGenericMessage({ levelId: 1, levelType: 'organization', app_code: 'MY_APP_001', to: 'recipient@example.com', subject: 'Invoice and Receipt', message: 'Please find your invoice and receipt attached.', type: 'EMAIL', attachments: [ { filename: 'invoice.pdf', content: 'base64-encoded-invoice-content', contentType: 'application/pdf', disposition: 'attachment', }, { filename: 'receipt.pdf', content: 'base64-encoded-receipt-content', contentType: 'application/pdf', disposition: 'attachment', }, ], }); console.log('SendGrid with attachments sent:', result); } /** * Example 9: Send SendGrid email using dynamic template */ async sendSendGridWithDynamicTemplate() { const result = await this.communicationService.sendGenericMessage({ levelId: 1, levelType: 'organization', app_code: 'MY_APP_001', to: 'recipient@example.com', message: '', // Required field, can be empty for template emails type: 'EMAIL', templateId: 'd-1234567890abcdef1234567890abcdef', // SendGrid template ID variables: { firstName: 'John', lastName: 'Doe', companyName: 'Acme Corp', orderNumber: 'ORD-2024-001', orderDate: '2024-01-15', items: [ { name: 'Product A', quantity: 2, price: '$29.99' }, { name: 'Product B', quantity: 1, price: '$49.99' }, ], totalAmount: '$109.97', }, }); console.log('SendGrid dynamic template email sent:', result); } /** * Example 10: Send SendGrid email with custom headers and categories */ async sendSendGridWithCustomOptions() { const result = await this.communicationService.sendGenericMessage({ levelId: 1, levelType: 'organization', app_code: 'MY_APP_001', to: ['recipient1@example.com', 'recipient2@example.com'], cc: ['manager@example.com'], bcc: ['archive@example.com'], subject: 'Monthly Newsletter', message: 'Our Monthly Newsletter', html: 'Latest updates from our team...
', type: 'EMAIL', }); console.log('SendGrid custom email sent:', result); } /** * Example 11: Send message without specifying mode (uses first available) */ async sendMessageAutoMode() { const result = await this.communicationService.sendMessage({ levelId: 1, levelType: 'organization', app_code: 'MY_APP_001', to: 'recipient@example.com', message: 'Hello! This will use the first available communication method.', }); console.log('Message sent via auto-selected provider:', result); } }