notifications-service.service.ts86 lines, 2.5 KB
| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | import { EmailService } from './email.service'; |
| 3 | |
| 4 | @Injectable() |
| 5 | export class NotificationsServiceService { |
| 6 | private readonly logger = new Logger(NotificationsServiceService.name); |
| 7 | |
| 8 | constructor(private readonly emailService: EmailService) {} |
| 9 | |
| 10 | async sendWelcomeEmail(data: { |
| 11 | userId: string; |
| 12 | email: string; |
| 13 | name: string; |
| 14 | }) { |
| 15 | this.logger.log(`Sending welcome email to ${data.email}`); |
| 16 | |
| 17 | const html = ` |
| 18 | <h1>Welcome to EventFlow, ${data.name}! 🎉</h1> |
| 19 | <p>Your account has been created successfully.</p> |
| 20 | <p>You can now:</p> |
| 21 | <ul> |
| 22 | <li>Browse and discover events</li> |
| 23 | <li>Purchase tickets</li> |
| 24 | <li>Create your own events</li> |
| 25 | </ul> |
| 26 | <p>Happy eventing!</p> |
| 27 | `; |
| 28 | |
| 29 | await this.emailService.sendEmail( |
| 30 | data.email, |
| 31 | 'Welcome to EventFlow!', |
| 32 | html, |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | async sendTicketPurchasedEmail(data: { |
| 37 | userId: string; |
| 38 | email?: string; |
| 39 | ticketCode: string; |
| 40 | eventTitle?: string; |
| 41 | quantity: number; |
| 42 | totalPrice: number; |
| 43 | }) { |
| 44 | const email = data.email || 'user@example.com'; // Fallback for demo |
| 45 | this.logger.log(`Sending ticket confirmation to ${email}`); |
| 46 | |
| 47 | const html = ` |
| 48 | <h1>Ticket Confirmed! 🎟️</h1> |
| 49 | <p>Your ticket purchase was successful.</p> |
| 50 | <div style="background: #f5f5f5; padding: 20px; border-radius: 8px; margin: 20px 0;"> |
| 51 | <p><strong>Event:</strong> ${data.eventTitle || 'Your Event'}</p> |
| 52 | <p><strong>Ticket Code:</strong> ${data.ticketCode}</p> |
| 53 | <p><strong>Quantity:</strong> ${data.quantity}</p> |
| 54 | <p><strong>Total:</strong> $${(data.totalPrice / 100).toFixed(2)}</p> |
| 55 | </div> |
| 56 | <p>Show this ticket code at the event entrance.</p> |
| 57 | `; |
| 58 | |
| 59 | await this.emailService.sendEmail(email, 'Your Ticket is Confirmed!', html); |
| 60 | } |
| 61 | |
| 62 | async sendTicketCancelledEmail(data: { |
| 63 | ticketId: string; |
| 64 | userId: string; |
| 65 | email?: string; |
| 66 | }) { |
| 67 | const email = data.email || 'user@example.com'; |
| 68 | this.logger.log(`Sending cancellation notice to ${email}`); |
| 69 | |
| 70 | const html = ` |
| 71 | <h1>Ticket Cancelled</h1> |
| 72 | <p>Your ticket has been cancelled as requested.</p> |
| 73 | <p>If you did not request this cancellation, please contact support.</p> |
| 74 | `; |
| 75 | |
| 76 | await this.emailService.sendEmail(email, 'Ticket Cancelled', html); |
| 77 | } |
| 78 | |
| 79 | sendEventCancelledEmail(data: { |
| 80 | eventId: string; |
| 81 | eventTitle?: string; |
| 82 | organizerId: string; |
| 83 | }) { |
| 84 | this.logger.log(`Sending event cancellation notice to ${data.organizerId}`); |
| 85 | } |
| 86 | } |