Skip to content

Commit

Permalink
chore: 🤖 Add findOne to extended identities service
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantasdeveloper committed Jul 22, 2024
1 parent ae795c7 commit f64cc27
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { ConfidentialProofsModule } from '~/confidential-proofs/confidential-pro
import { ConfidentialTransactionsController } from '~/confidential-transactions/confidential-transactions.controller';
import { ConfidentialTransactionsService } from '~/confidential-transactions/confidential-transactions.service';
import { ConfidentialVenuesController } from '~/confidential-transactions/confidential-venues.controller';
import { ExtendedIdentitiesModule } from '~/extended-identities/identities.module';
import { PolymeshModule } from '~/polymesh/polymesh.module';
import { IdentitiesModule } from '~/polymesh-rest-api/src/identities/identities.module';
import { TransactionsModule } from '~/transactions/transactions.module';

@Module({
Expand All @@ -17,7 +17,7 @@ import { TransactionsModule } from '~/transactions/transactions.module';
TransactionsModule,
ConfidentialAccountsModule,
forwardRef(() => ConfidentialProofsModule.register()),
IdentitiesModule,
forwardRef(() => ExtendedIdentitiesModule),
],
providers: [ConfidentialTransactionsService],
controllers: [ConfidentialTransactionsController, ConfidentialVenuesController],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import { ObserverAffirmConfidentialTransactionDto } from '~/confidential-transac
import { SenderAffirmConfidentialTransactionDto } from '~/confidential-transactions/dto/sender-affirm-confidential-transaction.dto copy';
import { ConfidentialAssetAuditorModel } from '~/confidential-transactions/models/confidential-asset-auditor.model';
import { ConfidentialTransactionModel } from '~/confidential-transactions/models/confidential-transaction.model';
import { ExtendedIdentitiesService } from '~/extended-identities/identities.service';
import { POLYMESH_API } from '~/polymesh/polymesh.consts';
import { PolymeshModule } from '~/polymesh/polymesh.module';
import { PolymeshService } from '~/polymesh/polymesh.service';
import { TransactionBaseDto } from '~/polymesh-rest-api/src/common/dto/transaction-base-dto';
import { ProcessMode } from '~/polymesh-rest-api/src/common/types';
import { IdentitiesService } from '~/polymesh-rest-api/src/identities/identities.service';
import { testValues } from '~/test-utils/consts';
import {
createMockConfidentialAccount,
Expand Down Expand Up @@ -70,12 +70,12 @@ describe('ConfidentialTransactionsService', () => {
mockTransactionsProvider,
mockConfidentialProofsServiceProvider,
mockConfidentialAccountsServiceProvider,
IdentitiesService,
ExtendedIdentitiesService,
],
})
.overrideProvider(POLYMESH_API)
.useValue(mockPolymeshApi)
.overrideProvider(IdentitiesService)
.overrideProvider(ExtendedIdentitiesService)
.useValue(mockIdentitiesService)
.compile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { createConfidentialTransactionModel } from '~/confidential-transactions/
import { CreateConfidentialTransactionDto } from '~/confidential-transactions/dto/create-confidential-transaction.dto';
import { ObserverAffirmConfidentialTransactionDto } from '~/confidential-transactions/dto/observer-affirm-confidential-transaction.dto';
import { SenderAffirmConfidentialTransactionDto } from '~/confidential-transactions/dto/sender-affirm-confidential-transaction.dto copy';
import { ExtendedIdentitiesService } from '~/extended-identities/identities.service';
import { PolymeshService } from '~/polymesh/polymesh.service';
import { TransactionBaseDto } from '~/polymesh-rest-api/src/common/dto/transaction-base-dto';
import { AppValidationError } from '~/polymesh-rest-api/src/common/errors';
import { extractTxOptions, ServiceReturn } from '~/polymesh-rest-api/src/common/utils/functions';
import { IdentitiesService } from '~/polymesh-rest-api/src/identities/identities.service';
import { TransactionsService } from '~/transactions/transactions.service';
import { handleSdkError } from '~/transactions/transactions.util';

Expand All @@ -35,7 +35,7 @@ export class ConfidentialTransactionsService {
private readonly transactionsService: TransactionsService,
private readonly confidentialAccountsService: ConfidentialAccountsService,
private readonly confidentialProofsService: ConfidentialProofsService,
private readonly identitiesService: IdentitiesService
private readonly extendedIdentitiesService: ExtendedIdentitiesService
) {}

public async findOne(id: BigNumber): Promise<ConfidentialTransaction> {
Expand Down Expand Up @@ -181,7 +181,7 @@ export class ConfidentialTransactionsService {
}

public async findVenuesByOwner(did: string): Promise<ConfidentialVenue[]> {
const identity = await this.identitiesService.findOne(did);
const identity = await this.extendedIdentitiesService.findOne(did);

return identity.getConfidentialVenues();
}
Expand Down
4 changes: 2 additions & 2 deletions src/extended-identities/identities.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { forwardRef, Module } from '@nestjs/common';
import { ConfidentialTransactionsModule } from '~/confidential-transactions/confidential-transactions.module';
import { ExtendedIdentitiesController } from '~/extended-identities/identities.controller';
import { ExtendedIdentitiesService } from '~/extended-identities/identities.service';
import { IdentitiesModule } from '~/polymesh-rest-api/src/identities/identities.module';
import { PolymeshModule } from '~/polymesh/polymesh.module';

@Module({
imports: [IdentitiesModule, forwardRef(() => ConfidentialTransactionsModule)],
imports: [PolymeshModule, forwardRef(() => ConfidentialTransactionsModule)],
controllers: [ExtendedIdentitiesController],
providers: [ExtendedIdentitiesService],
exports: [ExtendedIdentitiesService],
Expand Down
58 changes: 47 additions & 11 deletions src/extended-identities/identities.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,66 @@ import { BigNumber } from '@polymeshassociation/polymesh-private-sdk';
import { ConfidentialLegParty } from '@polymeshassociation/polymesh-private-sdk/types';

import { ExtendedIdentitiesService } from '~/extended-identities/identities.service';
import { IdentitiesService } from '~/polymesh-rest-api/src/identities/identities.service';
import { MockIdentitiesService } from '~/polymesh-rest-api/src/test-utils/service-mocks';
import { createMockConfidentialTransaction, MockIdentity } from '~/test-utils/mocks';
import { POLYMESH_API } from '~/polymesh/polymesh.consts';
import { PolymeshModule } from '~/polymesh/polymesh.module';
import { PolymeshService } from '~/polymesh/polymesh.service';
import { createMockConfidentialTransaction, MockIdentity, MockPolymesh } from '~/test-utils/mocks';
import * as transactionsUtilModule from '~/transactions/transactions.util';

describe('IdentitiesService', () => {
describe('ExtendedIdentitiesService', () => {
let service: ExtendedIdentitiesService;
let mockIdentitiesService: MockIdentitiesService;
let mockPolymeshApi: MockPolymesh;
let polymeshService: PolymeshService;

beforeEach(async () => {
mockIdentitiesService = new MockIdentitiesService();

mockPolymeshApi = new MockPolymesh();
const module: TestingModule = await Test.createTestingModule({
providers: [ExtendedIdentitiesService, IdentitiesService],
imports: [PolymeshModule],
providers: [ExtendedIdentitiesService],
})
.overrideProvider(IdentitiesService)
.useValue(mockIdentitiesService)
.overrideProvider(POLYMESH_API)
.useValue(mockPolymeshApi)
.compile();

mockPolymeshApi = module.get<MockPolymesh>(POLYMESH_API);
polymeshService = module.get<PolymeshService>(PolymeshService);

service = module.get<ExtendedIdentitiesService>(ExtendedIdentitiesService);
});

afterEach(async () => {
await polymeshService.close();
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('findOne', () => {
it('should return the Identity for a valid DID', async () => {
const fakeResult = 'identity';

mockPolymeshApi.identities.getIdentity.mockResolvedValue(fakeResult);

const result = await service.findOne('realDid');

expect(result).toBe(fakeResult);
});

describe('otherwise', () => {
it('should call the handleSdkError method and throw an error', async () => {
const mockError = new Error('Some Error');
mockPolymeshApi.identities.getIdentity.mockRejectedValue(mockError);

const handleSdkErrorSpy = jest.spyOn(transactionsUtilModule, 'handleSdkError');

await expect(() => service.findOne('invalidDID')).rejects.toThrowError();

expect(handleSdkErrorSpy).toHaveBeenCalledWith(mockError);
});
});
});

describe('getInvolvedConfidentialTransactions', () => {
const mockAffirmations = {
data: [
Expand All @@ -46,7 +81,8 @@ describe('IdentitiesService', () => {
const mockIdentity = new MockIdentity();
mockIdentity.getInvolvedConfidentialTransactions.mockResolvedValue(mockAffirmations);

mockIdentitiesService.findOne.mockResolvedValue(mockIdentity);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
jest.spyOn(service, 'findOne').mockResolvedValue(mockIdentity as any);
});

it('should return the list of involved confidential affirmations', async () => {
Expand Down
20 changes: 17 additions & 3 deletions src/extended-identities/identities.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@ import { Injectable } from '@nestjs/common';
import { BigNumber } from '@polymeshassociation/polymesh-private-sdk';
import {
ConfidentialAffirmation,
Identity,
ResultSet,
} from '@polymeshassociation/polymesh-private-sdk/types';

import { IdentitiesService } from '~/polymesh-rest-api/src/identities/identities.service';
import { PolymeshService } from '~/polymesh/polymesh.service';
import { handleSdkError } from '~/transactions/transactions.util';

@Injectable()
export class ExtendedIdentitiesService {
constructor(private readonly identitiesService: IdentitiesService) {}
constructor(private readonly polymeshService: PolymeshService) {}

/**
* Method to get identity for a specific did
*/
public async findOne(did: string): Promise<Identity> {
const {
polymeshService: { polymeshApi },
} = this;
return await polymeshApi.identities.getIdentity({ did }).catch(error => {
throw handleSdkError(error);
});
}

public async getInvolvedConfidentialTransactions(
did: string,
size: BigNumber,
start?: string
): Promise<ResultSet<ConfidentialAffirmation>> {
const identity = await this.identitiesService.findOne(did);
const identity = await this.findOne(did);

return identity.getInvolvedConfidentialTransactions({ size, start });
}
Expand Down
6 changes: 6 additions & 0 deletions src/test-utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export class MockPolymesh extends MockPublicPolymesh {
getVenue: jest.fn(),
createVenue: jest.fn(),
};

public identities = {
isIdentityValid: jest.fn(),
getIdentity: jest.fn(),
createPortfolio: jest.fn(),
};
}

export class MockTransaction {
Expand Down
1 change: 1 addition & 0 deletions src/test-utils/service-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class MockHttpService extends MockHttpServiceRestApi {
}

export class MockIdentitiesService extends MockIdentitiesServiceRestApi {
findOne = jest.fn();
getInvolvedConfidentialTransactions = jest.fn();
}

Expand Down

0 comments on commit f64cc27

Please sign in to comment.