| 1 | import { Controller, Get, Logger } from '@nestjs/common'; |
| 2 | import { NotificationsServiceService } from './notifications-service.service'; |
| 3 | import { EventPattern, Payload } from '@nestjs/microservices'; |
| 4 | import { KAFKA_TOPICS } from '@app/kafka'; |
| 5 | |
| 6 | @Controller() |
| 7 | export class NotificationsServiceController { |
| 8 | private readonly logger = new Logger(NotificationsServiceController.name); |
| 9 | |
| 10 | constructor( |
| 11 | private readonly notificationsServiceService: NotificationsServiceService, |
| 12 | ) {} |
| 13 | |
| 14 | @Get('health') |
| 15 | healthCheck() { |
| 16 | return { status: 'ok', service: 'notifications-service' }; |
| 17 | } |
| 18 | |
| 19 | @EventPattern(KAFKA_TOPICS.USER_REGISTERED) |
| 20 | async handleUserRegistered( |
| 21 | @Payload() data: { userId: string; email: string; name: string }, |
| 22 | ) { |
| 23 | this.logger.log(`Received user registered event: ${JSON.stringify(data)}`); |
| 24 | await this.notificationsServiceService.sendWelcomeEmail(data); |
| 25 | } |
| 26 | |
| 27 | @EventPattern(KAFKA_TOPICS.TICKET_PURCHASED) |
| 28 | async handleTicketPurchased( |
| 29 | @Payload() |
| 30 | data: { |
| 31 | ticketId: string; |
| 32 | ticketCode: string; |
| 33 | userId: string; |
| 34 | quantity: number; |
| 35 | totalPrice: number; |
| 36 | }, |
| 37 | ) { |
| 38 | this.logger.log(`Received ticket purchase event: ${JSON.stringify(data)}`); |
| 39 | await this.notificationsServiceService.sendTicketPurchasedEmail(data); |
| 40 | } |
| 41 | |
| 42 | @EventPattern(KAFKA_TOPICS.TICKET_CANCELLED) |
| 43 | async handleTicketCancelled( |
| 44 | @Payload() data: { ticketId: string; userId: string }, |
| 45 | ) { |
| 46 | this.logger.log( |
| 47 | `Received ticket cancellation event: ${JSON.stringify(data)}`, |
| 48 | ); |
| 49 | await this.notificationsServiceService.sendTicketCancelledEmail(data); |
| 50 | } |
| 51 | } |