import { Repository } from 'typeorm'; import { CommandBus, CommandHandler, ICommandHandler } from '@nestjs/cqrs'; import { InjectRepository } from '@nestjs/typeorm'; import { GetOrganizationCommand } from '@energyweb/origin-backend'; import { Beneficiary } from '../beneficiary.entity'; import { GetBeneficiaryCommand } from '../commands'; import { BeneficiaryDTO } from '../dto'; @CommandHandler(GetBeneficiaryCommand) export class GetBeneficiaryHandler implements ICommandHandler { constructor( @InjectRepository(Beneficiary) private readonly repository: Repository, private readonly commandBus: CommandBus ) {} public async execute({ id }: GetBeneficiaryCommand): Promise { const beneficiary = await this.repository.findOne(id); if (!beneficiary) { return; } return BeneficiaryDTO.wrap({ ...beneficiary, organization: beneficiary.organizationId ? await this.commandBus.execute( new GetOrganizationCommand(String(beneficiary.ownerId)) ) : null }); } }