Skip to content

Commit

Permalink
feat: get the portfolio creation event
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Nov 5, 2020
1 parent 088b63f commit 1dc8df6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/api/entities/NumberedPortfolio.ts
Expand Up @@ -3,6 +3,9 @@ import BigNumber from 'bignumber.js';
import { Portfolio } from '~/api/entities';
import { deletePortfolio, renamePortfolio, RenamePortfolioParams } from '~/api/procedures';
import { Context, TransactionQueue } from '~/base';
import { eventByIndexedArgs } from '~/middleware/queries';
import { EventIdEnum, ModuleIdEnum, Query } from '~/middleware/types';
import { Ensured, EventIdentifier } from '~/types';
import { bytesToString, numberToU64 } from '~/utils';

export interface UniqueIdentifiers {
Expand Down Expand Up @@ -83,4 +86,40 @@ export class NumberedPortfolio extends Portfolio {
const rawPortfolioName = await portfolio.portfolios(did, numberToU64(id, context));
return bytesToString(rawPortfolioName);
}

/**
* Retrieve the identifier data (block number, date and event index) of the event that was emitted when this portfolio was created
*
* @note uses the middleware
* @note there is a possibility that the data is not ready by the time it is requested. In that case, `null` is returned
*/
public async createdAt(): Promise<EventIdentifier | null> {
const {
owner: { did },
id,
context,
} = this;

const result = await context.queryMiddleware<Ensured<Query, 'eventByIndexedArgs'>>(
eventByIndexedArgs({
moduleId: ModuleIdEnum.Portfolio,
eventId: EventIdEnum.PortfolioCreated,
eventArg0: did,
eventArg1: id.toString(),
})
);

if (result.data.eventByIndexedArgs) {
// TODO remove null check once types fixed
/* eslint-disable @typescript-eslint/no-non-null-assertion */
return {
blockNumber: new BigNumber(result.data.eventByIndexedArgs.block_id!),
blockDate: result.data.eventByIndexedArgs.block!.datetime!,
eventIndex: result.data.eventByIndexedArgs.event_idx!,
};
/* eslint-enabled @typescript-eslint/no-non-null-assertion */
}

return null;
}
}
43 changes: 43 additions & 0 deletions src/api/entities/__tests__/NumberedPortfolio.ts
Expand Up @@ -4,6 +4,8 @@ import sinon from 'sinon';
import { Entity, Identity, NumberedPortfolio } from '~/api/entities';
import { deletePortfolio, renamePortfolio } from '~/api/procedures';
import { Context, TransactionQueue } from '~/base';
import { eventByIndexedArgs } from '~/middleware/queries';
import { EventIdEnum, ModuleIdEnum } from '~/middleware/types';
import { dsMockUtils } from '~/testUtils/mocks';

describe('Numberedortfolio class', () => {
Expand Down Expand Up @@ -107,4 +109,45 @@ describe('Numberedortfolio class', () => {
expect(result).toEqual(portfolioName);
});
});

describe('method: createdAt', () => {
const id = new BigNumber(1);
const did = 'someDid';
const variables = {
moduleId: ModuleIdEnum.Portfolio,
eventId: EventIdEnum.PortfolioCreated,
eventArg0: did,
eventArg1: id.toString(),
};

test('should return the event identifier object of the portfolio creation', async () => {
const blockNumber = new BigNumber(1234);
const blockDate = new Date('4/14/2020');
const eventIdx = 1;
const fakeResult = { blockNumber, blockDate, eventIndex: eventIdx };
const numberedPortfolio = new NumberedPortfolio({ id, did }, context);

dsMockUtils.createApolloQueryStub(eventByIndexedArgs(variables), {
/* eslint-disable @typescript-eslint/camelcase */
eventByIndexedArgs: {
block_id: blockNumber.toNumber(),
block: { datetime: blockDate },
event_idx: eventIdx,
},
/* eslint-enable @typescript-eslint/camelcase */
});

const result = await numberedPortfolio.createdAt();

expect(result).toEqual(fakeResult);
});

test('should return null if the query result is empty', async () => {
const numberedPortfolio = new NumberedPortfolio({ id, did }, context);

dsMockUtils.createApolloQueryStub(eventByIndexedArgs(variables), {});
const result = await numberedPortfolio.createdAt();
expect(result).toBeNull();
});
});
});

0 comments on commit 1dc8df6

Please sign in to comment.