diff --git a/src/services/rabbit.service.ts b/src/services/rabbit.service.ts index 7f6dbc4..0559bdc 100644 --- a/src/services/rabbit.service.ts +++ b/src/services/rabbit.service.ts @@ -14,11 +14,11 @@ export class RabbitMQService implements OnModuleInit { } async connect(): Promise { - const user = this.configService.get('rabbitmq.user'); - const pass = this.configService.get('rabbitmq.pass'); - const host = this.configService.get('rabbitmq.host'); - const port = this.configService.get('rabbitmq.port'); - + const user = this.configService.get('appConfig.user'); + const pass = this.configService.get('appConfig.pass'); + const host = this.configService.get('appConfig.host'); + const port = this.configService.get('appConfig.port'); + this.connection = await connect(`amqp://${user}:${pass}@${host}:${port}`); this.channel = await this.connection.createChannel(); console.log('Connected to RabbitMQ'); diff --git a/src/services/webhok.service.spec.ts b/src/services/webhok.service.spec.ts new file mode 100644 index 0000000..1f16c43 --- /dev/null +++ b/src/services/webhok.service.spec.ts @@ -0,0 +1,117 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { WebhookService } from './webhook.service'; +import { RabbitMQService } from './rabbit.service'; +import { ConfigService } from '@nestjs/config'; +import { InboundSMSMessageNotificationWrapperDto } from 'src/dtos/sms.mo.dto'; +import { + EventType, + OrderState, + SubscriptionDto, +} from 'src/dtos/subscription.dto'; + +describe('WebhookService', () => { + let service: WebhookService; + const mockRabbitMQ = { sendToQueue: jest.fn() }; + const mockConfigService = { + get: jest.fn((key: string) => { + const config = { + RABBITMQ_QUEUE_WEBHOOK: 'RABBITMQ_QUEUE_WEBHOOK', + RABBITMQ_QUEUE_NOTIFICATION: 'RABBITMQ_QUEUE_NOTIFICATION', + RABBITMQ_QUEUE_PAYMENT: 'RABBITMQ_QUEUE_PAYMENT', + }; + return config[key]; + }), + }; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + WebhookService, + { provide: RabbitMQService, useValue: mockRabbitMQ }, + { provide: ConfigService, useValue: mockConfigService }, + ], + }).compile(); + + service = module.get(WebhookService); + jest.clearAllMocks(); + }); + + //unit test for sms-mo notification + it('should send SMS MO payload to the correct queue', async () => { + const dto: InboundSMSMessageNotificationWrapperDto = { + inboundSMSMessageNotification: { + callbackData: 'cb-123', + inboundSMSMessage: { + dateTime: new Date().toISOString(), + destinationAddress: '12345', + messageId: 'msg-001', + message: 'Hello world!', + senderAddress: '987654321', + }, + }, + }; + + await service.smsMoNotification('FR', 'Orange', '123', dto); + + expect(mockRabbitMQ.sendToQueue).toHaveBeenCalledWith( + 'webhook_queue', + expect.objectContaining({ + operator: 'Orange', + country: 'FR', + ise2: '123', + data: dto, + }), + ); + }); + + //unit test subscription mangement + it('should send subscription payload to the correct queue', async () => { + const dto: SubscriptionDto = { + note: { text: 'User subscribed' }, + event: { + id: 1, + relatedParty: [{ id: '123', name: 'ISE2', role: 'subscriber' }], + order: { + id: 1, + state: OrderState.Completed, + orderItem: { + product: { id: 'WIDO access' }, + }, + }, + }, + eventType: EventType.creation, + eventTime: new Date().toISOString(), + }; + + await service.manageSubscription('FR', 'Orange', dto); + + expect(mockRabbitMQ.sendToQueue).toHaveBeenCalledWith( + 'payment_queue', + expect.objectContaining({ + operator: 'Orange', + country: 'FR', + data: dto, + }), + ); + }); + + //unit test for he notification + it('should send HE notification payload to the correct queue', async () => { + await service.handleHeNotification( + 'FR', + 'Orange', + 'callbackURL', + 'ISE2CODE', + ); + + expect(mockRabbitMQ.sendToQueue).toHaveBeenCalledWith( + 'notification_queue', + expect.objectContaining({ + operator: 'Orange', + country: 'FR', + callback: 'callbackURL', + ise2: 'ISE2CODE', + }), + ); + }); +}); diff --git a/src/services/webhook.service.ts b/src/services/webhook.service.ts index 3ddf65a..4b99937 100644 --- a/src/services/webhook.service.ts +++ b/src/services/webhook.service.ts @@ -2,10 +2,27 @@ import { Injectable } from '@nestjs/common'; import { RabbitMQService } from 'src/services/rabbit.service'; import { InboundSMSMessageNotificationWrapperDto } from '../dtos/sms.mo.dto'; import { SubscriptionDto } from '../dtos/subscription.dto'; +import { ConfigService } from '@nestjs/config'; @Injectable() export class WebhookService { - constructor(private readonly rabbitMQService: RabbitMQService) {} + private smsMoQueue: string; + private heQueue: string; + private subscriptionEventQueue: string; + constructor( + private readonly rabbitMQService: RabbitMQService, + private configService: ConfigService, + ) { + this.smsMoQueue = this.configService.get( + 'RABBITMQ_QUEUE_WEBHOOK', + ) as string; + this.heQueue = this.configService.get( + 'RABBITMQ_QUEUE_NOTIFICATION', + ) as string; + this.subscriptionEventQueue = this.configService.get( + 'RABBITMQ_QUEUE_PAYMENT', + ) as string; + } async smsMoNotification( country: string, @@ -21,7 +38,7 @@ export class WebhookService { receivedAt: new Date().toISOString(), }; - await this.rabbitMQService.sendToQueue('sms_mo', payload); + await this.rabbitMQService.sendToQueue(this.smsMoQueue, payload); } async manageSubscription( @@ -37,7 +54,10 @@ export class WebhookService { }; // send message to queue "subscription_events" - await this.rabbitMQService.sendToQueue('subscription_events', payload); + await this.rabbitMQService.sendToQueue( + this.subscriptionEventQueue, + payload, + ); console.log('payload sent to rabbitMQ'); } @@ -54,7 +74,7 @@ export class WebhookService { callback, receivedAt: new Date().toISOString(), }; - await this.rabbitMQService.sendToQueue('he_notifications', payload); + await this.rabbitMQService.sendToQueue(this.heQueue, payload); console.log('payload sent to rabbitMQ'); } }