Skip to content

Commit

Permalink
Merge pull request #52 from energywebfoundation/fix/fix-get-all-certs…
Browse files Browse the repository at this point in the history
…-in-unit-service

fix(origin-247-certificate): handle getAll in certificate service for unit tests
  • Loading branch information
soanvig authored Nov 22, 2021
2 parents 2a89604 + 5cfaea4 commit cfa65bb
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,8 +17,29 @@ export class CertificateForUnitTestsService<T> implements PublicPart<Certificate
private serial = 0;
private db: ICertificate<T>[] = [];

public async getAll(): Promise<ICertificate<T>[]> {
return [...this.db];
public async getAll(options: IGetAllCertificatesOptions = {}): Promise<ICertificate<T>[]> {
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<ICertificate<T> | null> {
Expand Down

0 comments on commit cfa65bb

Please sign in to comment.