Skip to content

Commit

Permalink
feat: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Dec 17, 2020
1 parent 3d3189e commit e4d2f6b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 27 deletions.
79 changes: 53 additions & 26 deletions src/base/Context.ts
Expand Up @@ -7,6 +7,7 @@ import { hexToU8a } from '@polkadot/util';
import { NormalizedCacheObject } from 'apollo-cache-inmemory';
import ApolloClient, { ApolloQueryResult } from 'apollo-client';
import BigNumber from 'bignumber.js';
import P from 'bluebird';
import { polymesh } from 'polymesh-types/definitions';
import { Claim1stKey, DidRecord, ProtocolOp, TxTag } from 'polymesh-types/types';

Expand Down Expand Up @@ -589,6 +590,48 @@ export class Context {
return assembleResult(didRecords);
}

/**
* @hidden
*/
private async identityClaimsEntries(args: {
targets: (string | Identity)[];
claimTypes: ClaimType[];
}): Promise<ClaimData[]> {
const {
polymeshApi: {
query: { identity },
},
} = this;

const { targets, claimTypes } = args;
const data: ClaimData[] = [];

await P.each(targets, async rawTarget => {
await P.each(claimTypes, async rawClaimType => {
const entries = await identity.claims.entries({
target: signerToString(rawTarget),
// eslint-disable-next-line @typescript-eslint/camelcase
claim_type: claimTypeToMeshClaimType(rawClaimType, this),
});

entries.forEach(
([key, { claim_issuer: claimissuer, issuance_date: issuanceDate, expiry, claim }]) => {
const { target } = key.args[0] as Claim1stKey;
data.push({
target: new Identity({ did: identityIdToString(target) }, this),
issuer: new Identity({ did: identityIdToString(claimissuer) }, this),
issuedAt: momentToDate(issuanceDate),
expiry: expiry ? momentToDate(expiry.unwrap()) : null,
claim: meshClaimToClaim(claim),
});
}
);
});
});

return data;
}

/**
* Retrieve a list of claims. Can be filtered using parameters
*
Expand All @@ -611,18 +654,13 @@ export class Context {
start?: number;
} = {}
): Promise<ResultSet<ClaimData> | ClaimData[]> {
const {
polymeshApi: {
query: { identity },
},
} = this;

const { targets, trustedClaimIssuers, claimTypes, includeExpired = true, size, start } = opts;

const data: ClaimData[] = [];
const isMiddlewareAvailable = await this.isMiddlewareAvailable();

if (isMiddlewareAvailable) {
const data: ClaimData[] = [];

const result = await this.queryMiddleware<Ensured<Query, 'didsWithClaims'>>(
didsWithClaims({
dids: targets?.map(target => signerToString(target)),
Expand Down Expand Up @@ -673,27 +711,16 @@ export class Context {
count,
};
} else {
const { did } = await this.getCurrentIdentity();
const entries = await identity.claims.entries({
target: stringToIdentityId(did, this),
// eslint-disable-next-line @typescript-eslint/camelcase
claim_type: claimTypeToMeshClaimType(ClaimType.CustomerDueDiligence, this),
});
if (!targets || !claimTypes) {
throw new PolymeshError({
code: ErrorCode.FatalError,
message: 'Cannot perform this action without an active middleware connection',
});
}

entries.forEach(
([key, { claim_issuer: claimissuer, issuance_date: issuanceDate, expiry, claim }]) => {
const { target } = key.args[0] as Claim1stKey;
data.push({
target: new Identity({ did: identityIdToString(target) }, this),
issuer: new Identity({ did: identityIdToString(claimissuer) }, this),
issuedAt: momentToDate(issuanceDate),
expiry: expiry ? momentToDate(expiry.unwrap()) : null,
claim: meshClaimToClaim(claim),
});
}
);
const identityClaimsEntries = await this.identityClaimsEntries({ targets, claimTypes });

return data;
return identityClaimsEntries;
}
}

Expand Down
23 changes: 22 additions & 1 deletion src/base/__tests__/Context.ts
Expand Up @@ -1285,10 +1285,31 @@ describe('Context class', () => {

dsMockUtils.createQueryStub('identity', 'claims').entries = entriesStub;

const result = (await context.issuedClaims()) as ClaimData[];
const result = (await context.issuedClaims({
targets: [targetDid],
claimTypes: [ClaimType.CustomerDueDiligence],
})) as ClaimData[];

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

test('should throw if the middleware query fails and targets or claimTypes are not seted', async () => {
const context = await Context.create({
polymeshApi: dsMockUtils.getApiInstance(),
middlewareApi: dsMockUtils.getMiddlewareApi(),
seed: '0x6'.padEnd(66, '0'),
});

dsMockUtils.configureMocks({
contextOptions: {
middlewareAvailable: false,
},
});

await expect(context.issuedClaims()).rejects.toThrow(
'Cannot perform this action without an active middleware connection'
);
});
});

describe('method: queryMiddleware', () => {
Expand Down

0 comments on commit e4d2f6b

Please sign in to comment.