Skip to content

Commit

Permalink
feat: move getVenues to Identity
Browse files Browse the repository at this point in the history
  • Loading branch information
monitz87 committed Oct 8, 2020
1 parent 97da907 commit 1ffd1d8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 102 deletions.
37 changes: 0 additions & 37 deletions src/api/entities/CurrentIdentity.ts
@@ -1,5 +1,3 @@
import { u64 } from '@polkadot/types';

import { Identity, Venue } from '~/api/entities';
import {
createVenue,
Expand All @@ -10,7 +8,6 @@ import {
} from '~/api/procedures';
import { TransactionQueue } from '~/base';
import { SecondaryKey, Signer, SubCallback, UnsubCallback } from '~/types';
import { stringToIdentityId, u64ToBigNumber } from '~/utils';

/**
* Represents the Identity associated to the current [[Account]]
Expand All @@ -37,40 +34,6 @@ export class CurrentIdentity extends Identity {
return context.getSecondaryKeys();
}

/**
* Retrieve all Venues created by this Identity
*
* @note can be subscribed to
*/
public async getVenues(): Promise<Venue[]>;
public async getVenues(callback: SubCallback<Venue[]>): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async getVenues(callback?: SubCallback<Venue[]>): Promise<Venue[] | UnsubCallback> {
const {
context: {
polymeshApi: {
query: { settlement },
},
},
did,
context,
} = this;

const assembleResult = (ids: u64[]): Venue[] =>
ids.map(id => new Venue({ id: u64ToBigNumber(id) }, context));

const rawDid = stringToIdentityId(did, context);

if (callback) {
return settlement.userVenues(rawDid, ids => callback(assembleResult(ids)));
}

const venueIds = await settlement.userVenues(rawDid);

return assembleResult(venueIds);
}

/**
* Remove a list of secondary keys associated with the Identity
*/
Expand Down
61 changes: 61 additions & 0 deletions src/api/entities/Identity/__tests__/index.ts
@@ -1,3 +1,4 @@
import { u64 } from '@polkadot/types';
import { AccountId, Balance } from '@polkadot/types/interfaces';
import BigNumber from 'bignumber.js';
import { DidRecord, IdentityId, Ticker } from 'polymesh-types/types';
Expand All @@ -20,6 +21,10 @@ jest.mock(
'~/api/entities/SecurityToken',
require('~/testUtils/mocks/entities').mockSecurityTokenModule('~/api/entities/SecurityToken')
);
jest.mock(
'~/api/entities/Venue',
require('~/testUtils/mocks/entities').mockVenueModule('~/api/entities/Venue')
);

describe('Identity class', () => {
let context: Context;
Expand Down Expand Up @@ -423,4 +428,60 @@ describe('Identity class', () => {
expect(result.data[1].ticker).toBe(tickers[1]);
});
});

describe('method: getVenues', () => {
let did: string;
let venueId: BigNumber;

let rawDid: IdentityId;
let rawVenueId: u64;

beforeAll(() => {
did = 'someDid';
venueId = new BigNumber(10);

rawDid = dsMockUtils.createMockIdentityId(did);
rawVenueId = dsMockUtils.createMockU64(venueId.toNumber());
});

beforeEach(() => {
stringToIdentityIdStub.withArgs(did, context).returns(rawDid);
});

afterAll(() => {
sinon.restore();
});

test('should return a list of Venues', async () => {
const fakeResult = [entityMockUtils.getVenueInstance({ id: venueId })];

dsMockUtils
.createQueryStub('settlement', 'userVenues')
.withArgs(rawDid)
.resolves([rawVenueId]);

const identity = new Identity({ did }, context);

const result = await identity.getVenues();
expect(result).toEqual(fakeResult);
});

test('should allow subscription', async () => {
const unsubCallback = 'unsubCallBack';

const fakeResult = [entityMockUtils.getVenueInstance({ id: venueId })];

dsMockUtils.createQueryStub('settlement', 'userVenues').callsFake(async (_, cbFunc) => {
cbFunc([rawVenueId]);
return unsubCallback;
});

const identity = new Identity({ did }, context);

const callback = sinon.stub();
const result = await identity.getVenues(callback);
expect(result).toEqual(unsubCallback);
sinon.assert.calledWithExactly(callback, fakeResult);
});
});
});
38 changes: 37 additions & 1 deletion src/api/entities/Identity/index.ts
@@ -1,7 +1,8 @@
import { u64 } from '@polkadot/types';
import { BigNumber } from 'bignumber.js';
import { CddStatus, DidRecord } from 'polymesh-types/types';

import { Entity, SecurityToken, TickerReservation } from '~/api/entities';
import { Entity, SecurityToken, TickerReservation, Venue } from '~/api/entities';
import { Context, PolymeshError } from '~/base';
import { tokensByTrustedClaimIssuer, tokensHeldByDid } from '~/middleware/queries';
import { Query } from '~/middleware/types';
Expand All @@ -26,6 +27,7 @@ import {
removePadding,
stringToIdentityId,
stringToTicker,
u64ToBigNumber,
} from '~/utils';

import { IdentityAuthorizations } from './IdentityAuthorizations';
Expand Down Expand Up @@ -316,4 +318,38 @@ export class Identity extends Entity<UniqueIdentifiers> {

return tickers.map(ticker => new SecurityToken({ ticker: removePadding(ticker) }, context));
}

/**
* Retrieve all Venues created by this Identity
*
* @note can be subscribed to
*/
public async getVenues(): Promise<Venue[]>;
public async getVenues(callback: SubCallback<Venue[]>): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async getVenues(callback?: SubCallback<Venue[]>): Promise<Venue[] | UnsubCallback> {
const {
context: {
polymeshApi: {
query: { settlement },
},
},
did,
context,
} = this;

const assembleResult = (ids: u64[]): Venue[] =>
ids.map(id => new Venue({ id: u64ToBigNumber(id) }, context));

const rawDid = stringToIdentityId(did, context);

if (callback) {
return settlement.userVenues(rawDid, ids => callback(assembleResult(ids)));
}

const venueIds = await settlement.userVenues(rawDid);

return assembleResult(venueIds);
}
}
64 changes: 0 additions & 64 deletions src/api/entities/__tests__/CurrentIdentity.ts
@@ -1,14 +1,10 @@
import { u64 } from '@polkadot/types';
import BigNumber from 'bignumber.js';
import sinon from 'sinon';

import { CurrentIdentity, Identity, Venue } from '~/api/entities';
import { createVenue, inviteAccount, removeSecondaryKeys } from '~/api/procedures';
import { Context, TransactionQueue } from '~/base';
import { IdentityId } from '~/polkadot';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { SecondaryKey, SubCallback, VenueType } from '~/types';
import * as utilsModule from '~/utils';

jest.mock(
'~/api/entities/Venue',
Expand Down Expand Up @@ -78,66 +74,6 @@ describe('CurrentIdentity class', () => {
});
});

describe('method: getVenues', () => {
let did: string;
let venueId: BigNumber;

let rawDid: IdentityId;
let rawVenueId: u64;

let stringToIdentityIdStub: sinon.SinonStub;

beforeAll(() => {
did = 'someDid';
venueId = new BigNumber(10);

rawDid = dsMockUtils.createMockIdentityId(did);
rawVenueId = dsMockUtils.createMockU64(venueId.toNumber());

stringToIdentityIdStub = sinon.stub(utilsModule, 'stringToIdentityId');
});

beforeEach(() => {
stringToIdentityIdStub.withArgs(did, context).returns(rawDid);
});

afterAll(() => {
sinon.restore();
});

test('should return a list of Venues', async () => {
const fakeResult = [entityMockUtils.getVenueInstance({ id: venueId })];

dsMockUtils
.createQueryStub('settlement', 'userVenues')
.withArgs(rawDid)
.resolves([rawVenueId]);

const identity = new CurrentIdentity({ did }, context);

const result = await identity.getVenues();
expect(result).toEqual(fakeResult);
});

test('should allow subscription', async () => {
const unsubCallback = 'unsubCallBack';

const fakeResult = [entityMockUtils.getVenueInstance({ id: venueId })];

dsMockUtils.createQueryStub('settlement', 'userVenues').callsFake(async (_, cbFunc) => {
cbFunc([rawVenueId]);
return unsubCallback;
});

const identity = new CurrentIdentity({ did }, context);

const callback = sinon.stub();
const result = await identity.getVenues(callback);
expect(result).toEqual(unsubCallback);
sinon.assert.calledWithExactly(callback, fakeResult);
});
});

describe('method: removeSecondaryKeys', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const did = 'someDid';
Expand Down

0 comments on commit 1ffd1d8

Please sign in to comment.