import { Repository } from 'typeorm' ; import { CommandBus , CommandHandler , ICommandHandler } from '@nestjs/cqrs' ; import { InjectRepository } from '@nestjs/typeorm' ; import { AddOrganizationBeneficiaryCommand , GetBeneficiaryCommand } from '../commands' ; import { Beneficiary } from '../beneficiary.entity' ; import { NotFoundException } from '@nestjs/common' ; import { BeneficiaryDTO } from '../dto/beneficiary.dto' ; @ CommandHandler ( AddOrganizationBeneficiaryCommand ) export class AddOrganizationBeneficiaryHandler implements ICommandHandler < AddOrganizationBeneficiaryCommand > { constructor ( @ InjectRepository ( Beneficiary ) private readonly repository , private readonly commandBus ) { } public async execute ( { ownerId , irecBeneficiaryId } ) { const beneficiary = await this . repository . findOne ( { ownerId : null , irecBeneficiaryId } ) ; if ( ! beneficiary ) { throw new NotFoundException ( 'Beneficiary not found' ) ; } const newBeneficiary = this . repository . create ( { irecBeneficiaryId : beneficiary . irecBeneficiaryId , organizationId : beneficiary . organizationId , ownerId : ownerId , name : beneficiary . name , countryCode : beneficiary . countryCode , location : beneficiary . location , active : true } ) ; const storedBeneficiary = await this . repository . save ( newBeneficiary ) ; return this . commandBus . execute ( new GetBeneficiaryCommand ( storedBeneficiary . id ) ) ; } }