Skip to content

Commit

Permalink
feat: change inputs for addClaims, editClaims and revokeClaims
Browse files Browse the repository at this point in the history
These functions now receive an array of `ClaimTarget` (an object with a target DID, a claim and an
optional expiry)

BREAKING CHANGE: The `ClaimTargets` interface has been replaced with `ClaimTarget`. Instead of
representing a set of identities with one claim, it now represents a single identity/claim pair
  • Loading branch information
monitz87 committed Aug 17, 2020
1 parent e1bb0d8 commit 212ab8a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
14 changes: 7 additions & 7 deletions src/__tests__/Polymesh.ts
Expand Up @@ -16,7 +16,7 @@ import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import {
AccountBalance,
ClaimData,
ClaimTargets,
ClaimTarget,
ClaimType,
ResultSet,
Signer,
Expand Down Expand Up @@ -911,9 +911,9 @@ describe('Polymesh Class', () => {
accountUri: '//uri',
});

const claims: ClaimTargets[] = [
const claims: ClaimTarget[] = [
{
targets: ['someDid'],
target: 'someDid',
claim: {
type: ClaimType.Accredited,
scope: 'someIdentityId',
Expand Down Expand Up @@ -949,9 +949,9 @@ describe('Polymesh Class', () => {
accountUri: '//uri',
});

const claims: ClaimTargets[] = [
const claims: ClaimTarget[] = [
{
targets: ['someDid'],
target: 'someDid',
claim: {
type: ClaimType.Accredited,
scope: 'someIdentityId',
Expand Down Expand Up @@ -987,9 +987,9 @@ describe('Polymesh Class', () => {
accountUri: '//uri',
});

const claims: ClaimTargets[] = [
const claims: ClaimTarget[] = [
{
targets: ['someDid'],
target: 'someDid',
claim: {
type: ClaimType.Accredited,
scope: 'someIdentityId',
Expand Down
12 changes: 8 additions & 4 deletions src/api/procedures/__tests__/modifyClaims.ts
Expand Up @@ -62,11 +62,15 @@ describe('modifyClaims procedure', () => {
args = {
claims: [
{
targets: [someDid, otherDid],
target: someDid,
claim: cddClaim,
},
{
targets: [someDid],
target: otherDid,
claim: cddClaim,
},
{
target: someDid,
claim: buyLockupClaim,
expiry,
},
Expand Down Expand Up @@ -280,7 +284,7 @@ describe('getRequiredRoles', () => {
const args = {
claims: [
{
targets: ['someDid'],
target: 'someDid',
claim: { type: ClaimType.CustomerDueDiligence },
},
],
Expand All @@ -293,7 +297,7 @@ describe('getRequiredRoles', () => {
const args = {
claims: [
{
targets: ['someDid'],
target: 'someDid',
claim: { type: ClaimType.Accredited, scope: 'someIdentityId' },
},
],
Expand Down
48 changes: 22 additions & 26 deletions src/api/procedures/modifyClaims.ts
Expand Up @@ -7,7 +7,7 @@ import { didsWithClaims } from '~/middleware/queries';
import { Claim as MiddlewareClaim, Query } from '~/middleware/types';
import {
Claim,
ClaimTargets,
ClaimTarget,
ClaimType,
Ensured,
ErrorCode,
Expand All @@ -32,17 +32,17 @@ interface AddClaimItem {
}

interface AddClaimsParams {
claims: ClaimTargets[];
claims: ClaimTarget[];
operation: ClaimOperation.Add;
}

interface EditClaimsParams {
claims: ClaimTargets[];
claims: ClaimTarget[];
operation: ClaimOperation.Edit;
}

interface RevokeClaimsParams {
claims: Omit<ClaimTargets, 'expiry'>[];
claims: Omit<ClaimTarget, 'expiry'>[];
operation: ClaimOperation.Revoke;
}

Expand All @@ -69,14 +69,12 @@ export async function prepareModifyClaims(
const modifyClaimItems: AddClaimItem[] = [];
let allTargets: string[] = [];

claims.forEach(({ targets, expiry, claim }: ClaimTargets) => {
targets.forEach(target => {
allTargets.push(valueToDid(target));
modifyClaimItems.push({
target: stringToIdentityId(valueToDid(target), context),
claim: claimToMeshClaim(claim, context),
expiry: expiry ? dateToMoment(expiry, context) : null,
});
claims.forEach(({ target, expiry, claim }: ClaimTarget) => {
allTargets.push(valueToDid(target));
modifyClaimItems.push({
target: stringToIdentityId(valueToDid(target), context),
claim: claimToMeshClaim(claim, context),
expiry: expiry ? dateToMoment(expiry, context) : null,
});
});

Expand Down Expand Up @@ -117,24 +115,22 @@ export async function prepareModifyClaims(
);

const nonExistentClaims: Claim[] = [];
claims.forEach(({ targets, claim }) => {
targets.forEach(target => {
const targetClaims = claimsByDid[valueToDid(target)] ?? [];

const claimExists = !!targetClaims.find(({ scope, type }) => {
let isSameScope = true;

if (isScopedClaim(claim)) {
isSameScope = claim.scope === scope;
}
claims.forEach(({ target, claim }) => {
const targetClaims = claimsByDid[valueToDid(target)] ?? [];

return isSameScope && ClaimType[type] === claim.type;
});
const claimExists = !!targetClaims.find(({ scope, type }) => {
let isSameScope = true;

if (!claimExists) {
nonExistentClaims.push(claim);
if (isScopedClaim(claim)) {
isSameScope = claim.scope === scope;
}

return isSameScope && ClaimType[type] === claim.type;
});

if (!claimExists) {
nonExistentClaims.push(claim);
}
});

if (nonExistentClaims.length) {
Expand Down
4 changes: 2 additions & 2 deletions src/types/index.ts
Expand Up @@ -317,8 +317,8 @@ export enum TransferStatus {
FundsLimitReached = 'FundsLimitReached', // 168
}

export interface ClaimTargets {
targets: (string | Identity)[];
export interface ClaimTarget {
target: string | Identity;
claim: Claim;
expiry?: Date;
}
Expand Down

0 comments on commit 212ab8a

Please sign in to comment.