import { Controller, Inject } from '@nestjs/common'; import { CommandBus } from '@nestjs/cqrs'; import { CreateAdCommand } from '../../commands/create-ad.command'; import { CreateAdRequest } from '../../domain/dtos/create-ad.request'; import { validateOrReject } from 'class-validator'; import { plainToInstance } from 'class-transformer'; import { DatabaseException } from 'src/modules/database/exceptions/database.exception'; import { ExceptionCode } from 'src/modules/utils/exception-code.enum'; import { IPublishMessage } from 'src/interfaces/message-publisher'; import { MESSAGE_PUBLISHER } from 'src/app.constants'; import { RabbitSubscribe } from '@mobicoop/message-broker-module'; @Controller() export class AdMessagerService { constructor( @Inject(MESSAGE_PUBLISHER) private readonly messagePublisher: IPublishMessage, private readonly commandBus: CommandBus, ) {} @RabbitSubscribe({ name: 'adCreated', }) async adCreatedHandler(message: string): Promise { let createAdRequest: CreateAdRequest; // parse message to request instance try { createAdRequest = plainToInstance(CreateAdRequest, JSON.parse(message)); // validate instance await validateOrReject(createAdRequest); // validate nested objects (fixes direct nested validation bug) for (const waypoint of createAdRequest.addresses) { try { await validateOrReject(waypoint); } catch (e) { throw e; } } } catch (e) { this.messagePublisher.publish( 'matcher.ad.crit', JSON.stringify({ message: `Can't validate message : ${message}`, error: e, }), ); } try { await this.commandBus.execute(new CreateAdCommand(createAdRequest)); } catch (e) { if (e instanceof DatabaseException) { if (e.message.includes('already exists')) { this.messagePublisher.publish( 'matcher.ad.crit', JSON.stringify({ code: ExceptionCode.ALREADY_EXISTS, message: 'Already exists', uuid: createAdRequest.uuid, }), ); } if (e.message.includes("Can't reach database server")) { this.messagePublisher.publish( 'matcher.ad.crit', JSON.stringify({ code: ExceptionCode.UNAVAILABLE, message: 'Database server unavailable', uuid: createAdRequest.uuid, }), ); } } this.messagePublisher.publish( 'logging.matcher.ad.crit', JSON.stringify({ message, error: e, }), ); } } }