import { CommandHandler , EventPublisher , ICommandHandler } from '@nestjs/cqrs' ; import { InjectRepository } from '@nestjs/typeorm' ; import { RpcException } from '@nestjs/microservices' ; import { ItemEntity } from '@infrastructure/entities' ; import { ItemRepository } from '@domain/repositories' ; import { ItemWriteRepository } from '@infrastructure/repositories' ; import { validateDbError } from '@database/helpers' ; import { UpdateItemCommand } from '../../impl' ; @ CommandHandler ( UpdateItemCommand ) export class UpdateItemHandler implements ICommandHandler < UpdateItemCommand > { constructor ( @ InjectRepository ( ItemWriteRepository ) private readonly itemWriteRepository , private readonly itemRepository , private readonly publisher ) { } async execute ( command ) { const { updateItemDto } = command ; const item = await this . itemWriteRepository . findOne ( updateItemDto . id ) ; if ( ! item ) throw new RpcException ( { statusCode : 404 , errorStatus : 'Item not found' , } ) ; item . name = updateItemDto . name ; try { await this . itemWriteRepository . update ( updateItemDto . id , { name : updateItemDto . name , } ) ; const itemModel = await this . publisher . mergeObjectContext ( this . itemRepository . updateItem ( updateItemDto ) ) ; itemModel . commit ( ) ; return item ; } catch ( error ) { const { code , message } = validateDbError ( error . code ) ; throw new RpcException ( { statusCode : code , errorStatus : message } ) ; } } }