From 5cfaea45155242ddef32fd196c49355265658a2c Mon Sep 17 00:00:00 2001 From: Mateusz Koteja Date: Fri, 19 Nov 2021 15:01:03 +0100 Subject: [PATCH] fix(origin-247-certificate): handle getAll in certificate service for unit tests --- .../src/certificateForUnitTests.service.ts | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/origin-247-certificate/src/certificateForUnitTests.service.ts b/packages/origin-247-certificate/src/certificateForUnitTests.service.ts index e03845b6..36fdf1e1 100644 --- a/packages/origin-247-certificate/src/certificateForUnitTests.service.ts +++ b/packages/origin-247-certificate/src/certificateForUnitTests.service.ts @@ -1,5 +1,5 @@ import { CertificateService } from './certificate.service'; -import { EventBus } from '@nestjs/cqrs'; +import { IGetAllCertificatesOptions } from '@energyweb/issuer-api'; import { BigNumber } from 'ethers'; import { ICertificate, @@ -17,8 +17,29 @@ export class CertificateForUnitTestsService implements PublicPart[] = []; - public async getAll(): Promise[]> { - return [...this.db]; + public async getAll(options: IGetAllCertificatesOptions = {}): Promise[]> { + const lastDate = new Date('2030-01-01T00:00:00.000Z'); + const generationEndFrom = options.generationEndFrom ?? new Date(0); + const generationEndTo = options.generationEndTo ?? lastDate; + const generationStartFrom = options.generationStartFrom ?? new Date(0); + const generationStartTo = options.generationStartTo ?? lastDate; + const creationTimeFrom = options.creationTimeFrom ?? new Date(0); + const creationTimeTo = options.creationTimeTo ?? lastDate; + const deviceId = options.deviceId; + + return this.db.filter((entry) => { + const isDateOk = + new Date(entry.generationStartTime * 1000) >= generationStartFrom && + new Date(entry.generationStartTime * 1000) <= generationStartTo; + new Date(entry.generationEndTime * 1000) >= generationEndFrom && + new Date(entry.generationEndTime * 1000) <= generationEndTo; + new Date(entry.creationTime * 1000) >= creationTimeFrom && + new Date(entry.creationTime * 1000) <= creationTimeTo; + + const isDeviceOk = deviceId ? entry.deviceId === entry.deviceId : true; + + return isDateOk && isDeviceOk; + }); } public async getById(id: number): Promise | null> {