import { IQueryHandler , QueryHandler } from '@nestjs/cqrs' ; import { InjectRepository } from '@nestjs/typeorm' ; import { Repository } from 'typeorm' ; import { CertificateUtils } from '@energyweb/issuer' ; import { GetAllCertificateEventsQuery } from '../queries/get-all-certificate-events.query' ; import { Certificate } from '../certificate.entity' ; import { CertificateEvent } from '../../../types' ; import { BlockchainPropertiesService } from '../../blockchain/blockchain-properties.service' ; @ QueryHandler ( GetAllCertificateEventsQuery ) export class GetAllCertificateEventsHandler implements IQueryHandler < GetAllCertificateEventsQuery > { constructor ( @ InjectRepository ( Certificate ) private readonly repository , private readonly blockchainPropertiesService ) { } async execute ( { id } ) { const certificate = await this . repository . findOne ( id ) ; if ( ! certificate ) { return [ ] ; } const blockchainProperties = await this . blockchainPropertiesService . get ( ) ; const allCertificateEvents = await CertificateUtils . getAllCertificateEvents ( certificate . id , blockchainProperties . wrap ( ) ) ; return allCertificateEvents . map ( ( blockchainEvent ) => ( { name : blockchainEvent . name , timestamp : blockchainEvent . timestamp , ... blockchainEvent } ) ) ; } }