import { RabbitSubscribe } from '@mobicoop/message-broker-module'; import { DeleteAdCommand } from '@modules/ad/core/application/commands/delete-ad/delete-ad.command'; import { Injectable } from '@nestjs/common'; import { CommandBus } from '@nestjs/cqrs'; import { AD_DELETED_MESSAGE_HANDLER, AD_DELETED_ROUTING_KEY, } from '@src/app.constants'; import { AdReference } from './ad.types'; @Injectable() export class AdDeletedMessageHandler { constructor(private readonly commandBus: CommandBus) {} @RabbitSubscribe({ name: AD_DELETED_MESSAGE_HANDLER, routingKey: AD_DELETED_ROUTING_KEY, }) public async adDeleted(message: string): Promise { try { const deletedAd: AdReference = JSON.parse(message); await this.commandBus.execute( new DeleteAdCommand({ id: deletedAd.aggregateId, }), ); } catch (error: any) { // do not throw error to acknowledge incoming message // error handling should be done in the command handler, if relevant } } }