Skip to content

Commit

Permalink
fix: manual merge
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Aug 3, 2020
2 parents ace1a34 + 5335354 commit f3eed1d
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 105 deletions.
45 changes: 36 additions & 9 deletions src/Polymesh.ts
Expand Up @@ -33,6 +33,7 @@ import {
LinkType,
MiddlewareConfig,
NetworkProperties,
ResultSet,
SubCallback,
TickerReservationStatus,
UiKeyring,
Expand All @@ -41,6 +42,7 @@ import {
import { ClaimOperation, SignerType } from '~/types/internal';
import {
booleanToBool,
calculateNextKey,
createClaim,
linkTypeToMeshLinkType,
moduleAddressToString,
Expand Down Expand Up @@ -537,20 +539,27 @@ export class Polymesh {
/**
* Retrieve all claims issued by the current identity
*/
public async getIssuedClaims(): Promise<ClaimData[]> {
public async getIssuedClaims(
opts: {
size?: number;
start?: number;
} = {}
): Promise<ResultSet<ClaimData>> {
const {
context,
context: { middlewareApi },
} = this;

const { size, start } = opts;
const { did } = context.getCurrentIdentity();

let result: ApolloQueryResult<Ensured<Query, 'didsWithClaims'>>;
try {
result = await middlewareApi.query<Ensured<Query, 'didsWithClaims'>>(
didsWithClaims({
trustedClaimIssuers: [did],
count: 100,
count: size,
skip: start,
})
);
} catch (e) {
Expand All @@ -561,14 +570,16 @@ export class Polymesh {
}

const {
data: { didsWithClaims: didsWithClaimsList },
data: {
didsWithClaims: { items: didsWithClaimsList, totalCount: count },
},
} = result;
const claimData: ClaimData[] = [];
const data: ClaimData[] = [];

didsWithClaimsList.forEach(({ claims }) => {
claims.forEach(
({ targetDID, issuer, issuance_date: issuanceDate, expiry, type, jurisdiction, scope }) => {
claimData.push({
data.push({
target: new Identity({ did: targetDID }, context),
issuer: new Identity({ did: issuer }, context),
issuedAt: new Date(issuanceDate),
Expand All @@ -579,7 +590,13 @@ export class Polymesh {
);
});

return claimData;
const next = calculateNextKey(count, size, start);

return {
data,
next,
count,
};
}

/**
Expand All @@ -601,7 +618,7 @@ export class Polymesh {
size?: number;
start?: number;
} = {}
): Promise<IdentityWithClaims[]> {
): Promise<ResultSet<IdentityWithClaims>> {
const {
context,
context: { middlewareApi },
Expand Down Expand Up @@ -632,10 +649,12 @@ export class Polymesh {
}

const {
data: { didsWithClaims: didsWithClaimsList },
data: {
didsWithClaims: { items: didsWithClaimsList, totalCount: count },
},
} = result;

return didsWithClaimsList.map(({ did, claims }) => ({
const data = didsWithClaimsList.map(({ did, claims }) => ({
identity: new Identity({ did }, context),
claims: claims.map(
({
Expand All @@ -655,6 +674,14 @@ export class Polymesh {
})
),
}));

const next = calculateNextKey(count, size, start);

return {
data,
next,
count,
};
}

/**
Expand Down
109 changes: 60 additions & 49 deletions src/__tests__/Polymesh.ts
Expand Up @@ -10,7 +10,7 @@ import { Identity, TickerReservation } from '~/api/entities';
import { modifyClaims, reserveTicker, transferPolyX } from '~/api/procedures';
import { TransactionQueue } from '~/base';
import { didsWithClaims } from '~/middleware/queries';
import { ClaimTypeEnum, IdentityWithClaims } from '~/middleware/types';
import { ClaimTypeEnum, IdentityWithClaimsResult } from '~/middleware/types';
import { Polymesh } from '~/Polymesh';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import {
Expand Down Expand Up @@ -697,32 +697,35 @@ describe('Polymesh Class', () => {
issuance_date: date,
last_update_date: date,
};
const didsWithClaimsQueryResponse: IdentityWithClaims[] = [
{
did: targetDid,
claims: [
{
...commonClaimData,
expiry: null,
type: customerDueDiligenceType,
},
{
...commonClaimData,
expiry: date,
type: jurisdictionType,
jurisdiction: 'someJurisdiction',
scope: 'someScope',
},
{
...commonClaimData,
expiry: null,
type: exemptedType,
jurisdiction: null,
scope: 'someScope',
},
],
},
];
const didsWithClaimsQueryResponse: IdentityWithClaimsResult = {
totalCount: 1,
items: [
{
did: targetDid,
claims: [
{
...commonClaimData,
expiry: null,
type: customerDueDiligenceType,
},
{
...commonClaimData,
expiry: date,
type: jurisdictionType,
jurisdiction: 'someJurisdiction',
scope: 'someScope',
},
{
...commonClaimData,
expiry: null,
type: exemptedType,
jurisdiction: null,
scope: 'someScope',
},
],
},
],
};
/* eslint-enabled @typescript-eslint/camelcase */

dsMockUtils.configureMocks({ contextOptions: { withSeed: true } });
Expand All @@ -737,15 +740,17 @@ describe('Polymesh Class', () => {
});

dsMockUtils.createApolloQueryStub(
didsWithClaims({ trustedClaimIssuers: ['someDid'], count: 100 }),
didsWithClaims({ trustedClaimIssuers: ['someDid'], count: 30, skip: 1 }),
{
didsWithClaims: didsWithClaimsQueryResponse,
}
);

const result = await polymesh.getIssuedClaims();
const result = await polymesh.getIssuedClaims({ start: 1, size: 30 });

expect(result).toEqual(fakeClaims);
expect(result.data).toEqual(fakeClaims);
expect(result.count).toEqual(1);
expect(result.next).toBeNull();
});

test('should throw if the middleware query fails', async () => {
Expand Down Expand Up @@ -807,23 +812,26 @@ describe('Polymesh Class', () => {
issuance_date: date,
last_update_date: date,
};
const didsWithClaimsQueryResponse: IdentityWithClaims[] = [
{
did: targetDid,
claims: [
{
...commonClaimData,
expiry: date,
type: customerDueDiligenceType,
},
{
...commonClaimData,
expiry: null,
type: customerDueDiligenceType,
},
],
},
];
const didsWithClaimsQueryResponse: IdentityWithClaimsResult = {
totalCount: 25,
items: [
{
did: targetDid,
claims: [
{
...commonClaimData,
expiry: date,
type: customerDueDiligenceType,
},
{
...commonClaimData,
expiry: null,
type: customerDueDiligenceType,
},
],
},
],
};
/* eslint-enabled @typescript-eslint/camelcase */

dsMockUtils.configureMocks({ contextOptions: { withSeed: true } });
Expand All @@ -843,7 +851,7 @@ describe('Polymesh Class', () => {
scope: undefined,
trustedClaimIssuers: [targetDid],
claimTypes: [ClaimTypeEnum.Accredited],
count: undefined,
count: 1,
skip: undefined,
}),
{
Expand All @@ -855,9 +863,12 @@ describe('Polymesh Class', () => {
targets: [targetDid],
trustedClaimIssuers: [targetDid],
claimTypes: [ClaimType.Accredited],
size: 1,
});

expect(result).toEqual(fakeClaims);
expect(result.data).toEqual(fakeClaims);
expect(result.count).toEqual(25);
expect(result.next).toEqual(1);
});

test('should throw if the middleware query fails', async () => {
Expand Down
4 changes: 3 additions & 1 deletion src/api/entities/Proposal/__tests__/index.ts
Expand Up @@ -130,7 +130,9 @@ describe('Proposal class', () => {

const result = await proposal.getVotes();

expect(result).toEqual(fakeResult);
expect(result.data).toEqual(fakeResult);
expect(result.next).toBeNull();
expect(result.count).toBeUndefined();
});

test('should throw if the middleware query fails', async () => {
Expand Down
12 changes: 9 additions & 3 deletions src/api/entities/Proposal/index.ts
Expand Up @@ -7,7 +7,7 @@ import { Entity, PolymeshError, TransactionQueue } from '~/base';
import { Context } from '~/context';
import { eventByIndexedArgs, proposalVotes } from '~/middleware/queries';
import { EventIdEnum, ModuleIdEnum, Query } from '~/middleware/types';
import { Ensured, ErrorCode } from '~/types';
import { Ensured, ErrorCode, ResultSet } from '~/types';
import { valueToDid } from '~/utils';

import { ProposalVote, ProposalVotesOrderByInput } from './types';
Expand Down Expand Up @@ -108,7 +108,7 @@ export class Proposal extends Entity<UniqueIdentifiers> {
size?: number;
start?: number;
} = {}
): Promise<ProposalVote[]> {
): Promise<ResultSet<ProposalVote>> {
const {
context: { middlewareApi },
pipId,
Expand All @@ -135,13 +135,19 @@ export class Proposal extends Entity<UniqueIdentifiers> {
});
}

return result.data.proposalVotes.map(({ account: did, vote: proposalVote, weight }) => {
const data = result.data.proposalVotes.map(({ account: did, vote: proposalVote, weight }) => {
return {
identity: new Identity({ did }, context),
vote: proposalVote,
weight: new BigNumber(weight),
};
});

return {
data,
// TODO: replace by proper calculation once the query returns totalCount
next: null,
};
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/api/entities/Proposal/types.ts
Expand Up @@ -45,4 +45,9 @@ export interface ProposalTimeFrames {
coolOff: number;
}

export { ProposalOrderByInput, ProposalState, ProposalVotesOrderByInput } from '~/middleware/types';
export {
ProposalOrderByInput,
ProposalState,
ProposalOrderFields,
ProposalVotesOrderByInput,
} from '~/middleware/types';

0 comments on commit f3eed1d

Please sign in to comment.