Skip to content

Commit

Permalink
feat: add addInstruction to the Venue class
Browse files Browse the repository at this point in the history
  • Loading branch information
monitz87 committed Oct 9, 2020
1 parent b5fb041 commit 155b3a3
Show file tree
Hide file tree
Showing 13 changed files with 698 additions and 16 deletions.
26 changes: 25 additions & 1 deletion src/api/entities/Identity/__tests__/index.ts
Expand Up @@ -7,7 +7,7 @@ import { Entity, Identity } from '~/api/entities';
import { Context } from '~/base';
import { tokensByTrustedClaimIssuer, tokensHeldByDid } from '~/middleware/queries';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { Order, Role, RoleType, TickerOwnerRole, TokenOwnerRole } from '~/types';
import { Order, Role, RoleType, TickerOwnerRole, TokenOwnerRole, VenueOwnerRole } from '~/types';
import * as utilsModule from '~/utils';

jest.mock(
Expand All @@ -20,6 +20,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 @@ -129,6 +133,26 @@ describe('Identity class', () => {
expect(hasRole).toBe(false);
});

test('hasRole should check whether the Identity has the Venue Owner role', async () => {
const did = 'someDid';
const identity = new Identity({ did }, context);
const role: VenueOwnerRole = { type: RoleType.VenueOwner, venueId: new BigNumber(10) };

entityMockUtils.configureMocks({
venueOptions: { details: { owner: new Identity({ did }, context) } },
});

let hasRole = await identity.hasRole(role);

expect(hasRole).toBe(true);

identity.did = 'otherDid';

hasRole = await identity.hasRole(role);

expect(hasRole).toBe(false);
});

test('hasRole should throw an error if the role is not recognized', () => {
const identity = new Identity({ did: 'someDid' }, context);
const type = 'Fake' as RoleType;
Expand Down
9 changes: 8 additions & 1 deletion src/api/entities/Identity/index.ts
@@ -1,7 +1,7 @@
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 @@ -11,6 +11,7 @@ import {
isCddProviderRole,
isTickerOwnerRole,
isTokenOwnerRole,
isVenueOwnerRole,
Order,
ResultSet,
Role,
Expand Down Expand Up @@ -102,6 +103,12 @@ export class Identity extends Entity<UniqueIdentifiers> {
const memberDids = activeMembers.map(identityIdToString);

return memberDids.includes(did);
} else if (isVenueOwnerRole(role)) {
const venue = new Venue({ id: role.venueId }, context);

const { owner } = await venue.details();

return owner.did === did;
}

throw new PolymeshError({
Expand Down
4 changes: 2 additions & 2 deletions src/api/entities/Instruction/__tests__/index.ts
Expand Up @@ -72,9 +72,9 @@ describe('Instruction class', () => {
const createdAt = new Date('10/14/1987');
const validFrom = new Date('11/17/1987');
let type = InstructionType.SettleOnAuthorization;
const creator = 'someDid';
const owner = 'someDid';

entityMockUtils.configureMocks({ identityOptions: { did: creator } });
entityMockUtils.configureMocks({ identityOptions: { did: owner } });
sinon
.stub(utilsModule, 'numberToU64')
.withArgs(id, context)
Expand Down
8 changes: 4 additions & 4 deletions src/api/entities/Venue/__tests__/index.ts
Expand Up @@ -61,9 +61,9 @@ describe('Venue class', () => {
test('should return the Venue details', async () => {
const description = 'someDescription';
const type = VenueType.Other;
const creator = 'someDid';
const owner = 'someDid';

entityMockUtils.configureMocks({ identityOptions: { did: creator } });
entityMockUtils.configureMocks({ identityOptions: { did: owner } });
sinon
.stub(utilsModule, 'numberToU64')
.withArgs(id, context)
Expand All @@ -73,7 +73,7 @@ describe('Venue class', () => {
.createQueryStub('settlement', 'venueInfo')
.withArgs(rawId)
.resolves({
creator: dsMockUtils.createMockIdentityId(creator),
creator: dsMockUtils.createMockIdentityId(owner),
instructions: [],
details: dsMockUtils.createMockVenueDetails(description),
// eslint-disable-next-line @typescript-eslint/camelcase
Expand All @@ -83,7 +83,7 @@ describe('Venue class', () => {
const result = await venue.details();

expect(result).toEqual({
creator: entityMockUtils.getIdentityInstance(),
owner: entityMockUtils.getIdentityInstance(),
description,
type,
});
Expand Down
2 changes: 1 addition & 1 deletion src/api/entities/Venue/index.ts
Expand Up @@ -64,7 +64,7 @@ export class Venue extends Entity<UniqueIdentifiers> {
);

return {
creator: new Identity({ did: identityIdToString(creator) }, context),
owner: new Identity({ did: identityIdToString(creator) }, context),
description: venueDetailsToString(details),
type: meshVenueTypeToVenueType(type),
};
Expand Down
2 changes: 1 addition & 1 deletion src/api/entities/Venue/types.ts
Expand Up @@ -18,6 +18,6 @@ export enum VenueType {

export interface VenueDetails {
type: VenueType;
creator: Identity;
owner: Identity;
description: string;
}

0 comments on commit 155b3a3

Please sign in to comment.