import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq'; import { Controller } from '@nestjs/common'; import { Ad } from '../../domain/entities/ad'; import { InjectMapper } from '@automapper/nestjs'; import { Mapper } from '@automapper/core'; import { CommandBus } from '@nestjs/cqrs'; import { CreateAdCommand } from '../../commands/create-ad.command'; import { AdPresenter } from './ad.presenter'; import { CreateAdRequest } from '../../domain/dtos/create-ad.request'; @Controller() export class AdMessagerController { constructor( private readonly _commandBus: CommandBus, @InjectMapper() private readonly _mapper: Mapper, ) {} @RabbitSubscribe({ name: 'adCreated', }) async adCreatedHandler(message: string): Promise { try { const createAdRequest: CreateAdRequest = JSON.parse(message); const ad: Ad = await this._commandBus.execute( new CreateAdCommand(createAdRequest), ); return this._mapper.map(ad, Ad, AdPresenter); } catch (e) { console.log('error', e); } } }