From e4d2f6bf72922210b94d4de5347d2a58baa12ff4 Mon Sep 17 00:00:00 2001 From: Shuffledex Date: Thu, 17 Dec 2020 13:43:11 -0300 Subject: [PATCH] feat: refactor --- src/base/Context.ts | 79 +++++++++++++++++++++++------------ src/base/__tests__/Context.ts | 23 +++++++++- 2 files changed, 75 insertions(+), 27 deletions(-) diff --git a/src/base/Context.ts b/src/base/Context.ts index f069cf7b3b..49836e5231 100644 --- a/src/base/Context.ts +++ b/src/base/Context.ts @@ -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'; @@ -589,6 +590,48 @@ export class Context { return assembleResult(didRecords); } + /** + * @hidden + */ + private async identityClaimsEntries(args: { + targets: (string | Identity)[]; + claimTypes: ClaimType[]; + }): Promise { + 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 * @@ -611,18 +654,13 @@ export class Context { start?: number; } = {} ): Promise | 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>( didsWithClaims({ dids: targets?.map(target => signerToString(target)), @@ -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; } } diff --git a/src/base/__tests__/Context.ts b/src/base/__tests__/Context.ts index 963fa1e86f..d846ad7934 100644 --- a/src/base/__tests__/Context.ts +++ b/src/base/__tests__/Context.ts @@ -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', () => {