Skip to content

Commit f0d4070

Browse files
GHkrishnaRinkalBhojani
authored andcommitted
fix: nats call microservices (#1514)
* fix: nats call from nats client for verification service Signed-off-by: Krishna Waske <[email protected]> * fix: ledger nats fix in agent-service Signed-off-by: Krishna Waske <[email protected]> * fix: remove unused variable Signed-off-by: Krishna Waske <[email protected]> * fix: nats calls in connection service Signed-off-by: Krishna Waske <[email protected]> * fix: webhook service unwanted imports Signed-off-by: Krishna Waske <[email protected]> * chore: add todos to remove nested mapping Signed-off-by: Krishna Waske <[email protected]> * fix: sonar cube issues Signed-off-by: Krishna Waske <[email protected]> * fix: coderabbit suggestions Signed-off-by: Krishna Waske <[email protected]> --------- Signed-off-by: Krishna Waske <[email protected]>
1 parent 901e784 commit f0d4070

File tree

11 files changed

+184
-310
lines changed

11 files changed

+184
-310
lines changed

apps/agent-service/src/agent-service.service.ts

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,16 @@ import {
4747
IDidCreate,
4848
IWallet,
4949
ITenantRecord,
50-
LedgerListResponse,
5150
ICreateConnectionInvitation,
5251
IStoreAgent,
5352
AgentHealthData,
5453
IAgentStore,
5554
IAgentConfigure,
5655
OrgDid,
5756
IBasicMessage,
58-
WalletDetails
57+
WalletDetails,
58+
ILedger,
59+
IStoreOrgAgent
5960
} from './interface/agent-service.interface';
6061
import { AgentSpinUpStatus, AgentType, DidMethod, Ledgers, OrgAgentType, PromiseResult } from '@credebl/enum/enum';
6162
import { AgentServiceRepository } from './repositories/agent-service.repository';
@@ -497,8 +498,15 @@ export class AgentServiceService {
497498
if (agentSpinupDto.method !== DidMethod.KEY && agentSpinupDto.method !== DidMethod.WEB) {
498499
const { network } = agentSpinupDto;
499500
const ledger = await ledgerName(network);
500-
const ledgerList = (await this._getALlLedgerDetails()) as unknown as LedgerListResponse;
501-
const isLedgerExist = ledgerList.response.find((existingLedgers) => existingLedgers.name === ledger);
501+
const ledgerList = await this._getALlLedgerDetails();
502+
if (!ledgerList) {
503+
throw new BadRequestException(ResponseMessages.agent.error.invalidLedger, {
504+
cause: new Error(),
505+
description: ResponseMessages.errorMessages.notFound
506+
});
507+
}
508+
509+
const isLedgerExist = ledgerList.find((existingLedgers) => existingLedgers.name === ledger);
502510
if (!isLedgerExist) {
503511
throw new BadRequestException(ResponseMessages.agent.error.invalidLedger, {
504512
cause: new Error(),
@@ -511,15 +519,14 @@ export class AgentServiceService {
511519
/**
512520
* Invoke wallet create and provision with agent
513521
*/
514-
const walletProvision = await this._walletProvision(walletProvisionPayload);
515-
if (!walletProvision?.response) {
522+
const agentDetails = await this._walletProvision(walletProvisionPayload);
523+
if (!agentDetails) {
516524
this.logger.error(`Agent not able to spin-up`);
517525
throw new BadRequestException(ResponseMessages.agent.error.notAbleToSpinup, {
518526
cause: new Error(),
519527
description: ResponseMessages.errorMessages.badRequest
520528
});
521529
}
522-
const agentDetails = walletProvision.response;
523530
const agentEndPoint = `${process.env.API_GATEWAY_PROTOCOL}://${agentDetails.agentEndPoint}`;
524531
/**
525532
* Socket connection
@@ -693,48 +700,43 @@ export class AgentServiceService {
693700
}
694701
}
695702

696-
async _createConnectionInvitation(
697-
orgId: string,
698-
user: IUserRequestInterface,
699-
label: string
700-
): Promise<{
701-
response;
702-
}> {
703+
async _createConnectionInvitation(orgId: string, user: IUserRequestInterface, label: string): Promise<object> {
703704
try {
704705
const pattern = {
705706
cmd: 'create-connection-invitation'
706707
};
707708
const payload = { createOutOfBandConnectionInvitation: { orgId, user, label } };
708-
return await this.natsCall(pattern, payload);
709+
const result = await this.natsClient.send<object>(this.agentServiceProxy, pattern, payload);
710+
return result;
709711
} catch (error) {
710-
this.logger.error(`error in create-connection in wallet provision : ${JSON.stringify(error)}`);
712+
this.logger.error(`[natsCall] - error in create-connection in wallet provision : ${JSON.stringify(error)}`);
713+
throw new RpcException(error?.response ? error.response : error);
711714
}
712715
}
713716

714-
async _getALlLedgerDetails(): Promise<{
715-
response;
716-
}> {
717+
async _getALlLedgerDetails(): Promise<ILedger[]> {
717718
try {
718719
const pattern = {
719720
cmd: 'get-all-ledgers'
720721
};
721722
const payload = {};
722-
return await this.natsCall(pattern, payload);
723+
const result = await this.natsClient.send<ILedger[]>(this.agentServiceProxy, pattern, payload);
724+
return result;
723725
} catch (error) {
724-
this.logger.error(`error in while fetching all the ledger details : ${JSON.stringify(error)}`);
726+
this.logger.error(`[natsCall] - error in while fetching all the ledger details : ${JSON.stringify(error)}`);
727+
throw new RpcException(error?.response ? error.response : error);
725728
}
726729
}
727730

728-
async _walletProvision(payload: IWalletProvision): Promise<{
729-
response;
730-
}> {
731+
async _walletProvision(payload: IWalletProvision): Promise<Partial<IStoreOrgAgent>> {
731732
try {
732733
const pattern = {
733734
cmd: 'wallet-provisioning'
734735
};
735-
return await this.natsCall(pattern, payload);
736+
const result = await this.natsClient.send<Partial<IStoreOrgAgent>>(this.agentServiceProxy, pattern, payload);
737+
return result;
736738
} catch (error) {
737-
this.logger.error(`error in wallet provision : ${JSON.stringify(error)}`);
739+
this.logger.error(`[natsCall] - error in wallet provision : ${JSON.stringify(error)}`);
738740
throw error;
739741
}
740742
}
@@ -795,8 +797,14 @@ export class AgentServiceService {
795797
ledger = Ledgers.Not_Applicable;
796798
}
797799

798-
const ledgerList = (await this._getALlLedgerDetails()) as unknown as LedgerListResponse;
799-
const isLedgerExist = ledgerList.response.find((existingLedgers) => existingLedgers.name === ledger);
800+
const ledgerList = await this._getALlLedgerDetails();
801+
if (!ledgerList) {
802+
throw new BadRequestException(ResponseMessages.agent.error.invalidLedger, {
803+
cause: new Error(),
804+
description: ResponseMessages.errorMessages.notFound
805+
});
806+
}
807+
const isLedgerExist = ledgerList.find((existingLedgers) => existingLedgers.name === ledger);
800808
if (!isLedgerExist) {
801809
throw new BadRequestException(ResponseMessages.agent.error.invalidLedger, {
802810
cause: new Error(),
@@ -2191,32 +2199,6 @@ export class AgentServiceService {
21912199
}
21922200
}
21932201

2194-
async natsCall(
2195-
pattern: object,
2196-
payload: object
2197-
): Promise<{
2198-
response: string;
2199-
}> {
2200-
try {
2201-
return from(this.natsClient.send<string>(this.agentServiceProxy, pattern, payload))
2202-
.pipe(map((response) => ({ response })))
2203-
.toPromise()
2204-
.catch((error) => {
2205-
this.logger.error(`catch: ${JSON.stringify(error)}`);
2206-
throw new HttpException(
2207-
{
2208-
status: error.statusCode,
2209-
error: error.message
2210-
},
2211-
error.error
2212-
);
2213-
});
2214-
} catch (error) {
2215-
this.logger.error(`[natsCall] - error in nats call : ${JSON.stringify(error)}`);
2216-
throw error;
2217-
}
2218-
}
2219-
22202202
private async tokenEncryption(token: string): Promise<string> {
22212203
try {
22222204
const encryptedToken = CryptoJS.AES.encrypt(JSON.stringify(token), process.env.CRYPTO_PRIVATE_KEY).toString();

apps/agent-service/src/interface/agent-service.interface.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ export interface IStoreOrgAgent {
213213
id?: string;
214214
clientSocketId?: string;
215215
agentEndPoint?: string;
216+
agentToken?: string;
216217
apiKey?: string;
217218
seed?: string;
218219
did?: string;
@@ -556,7 +557,7 @@ export interface IQuestionPayload {
556557
export interface IBasicMessage {
557558
content: string;
558559
}
559-
interface Ledger {
560+
export interface ILedger {
560561
id: string;
561562
createDateTime: string;
562563
lastChangedDateTime: string;
@@ -570,10 +571,6 @@ interface Ledger {
570571
networkUrl: string | null;
571572
}
572573

573-
export interface LedgerListResponse {
574-
response: Ledger[];
575-
}
576-
577574
export interface ICreateConnectionInvitation {
578575
label?: string;
579576
alias?: string;

apps/api-gateway/src/connection/connection.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class ConnectionService extends BaseService {
3232
try {
3333
return this.natsClient.sendNatsMessage(this.connectionServiceProxy, 'send-question', questionDto);
3434
} catch (error) {
35-
throw new RpcException(error.response);
35+
throw new RpcException(error?.response ?? error);
3636
}
3737
}
3838

apps/cloud-wallet/src/cloud-wallet.service.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ import { CommonService } from '@credebl/common';
33
import {
44
BadRequestException,
55
ConflictException,
6-
Inject,
76
Injectable,
87
InternalServerErrorException,
98
Logger,
109
NotFoundException
1110
} from '@nestjs/common';
12-
import { Cache } from 'cache-manager';
13-
import { CACHE_MANAGER } from '@nestjs/cache-manager';
1411
import {
1512
IAcceptOffer,
1613
ICreateCloudWalletDid,
@@ -40,17 +37,13 @@ import { CloudWalletRepository } from './cloud-wallet.repository';
4037
import { ResponseMessages } from '@credebl/common/response-messages';
4138
import { CloudWalletType } from '@credebl/enum/enum';
4239
import { CommonConstants } from '@credebl/common/common.constant';
43-
import { ClientProxy } from '@nestjs/microservices';
4440

4541
@Injectable()
4642
export class CloudWalletService {
4743
constructor(
4844
private readonly commonService: CommonService,
49-
@Inject('NATS_CLIENT') private readonly cloudWalletServiceProxy: ClientProxy,
5045
private readonly cloudWalletRepository: CloudWalletRepository,
51-
private readonly logger: Logger,
52-
// TODO: Remove duplicate, unused variable
53-
@Inject(CACHE_MANAGER) private cacheService: Cache
46+
private readonly logger: Logger
5447
) {}
5548

5649
/**

apps/connection/src/connection.controller.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ export class ConnectionController {
4646
}
4747

4848
@MessagePattern({ cmd: 'get-all-agent-connection-list' })
49-
async getConnectionListFromAgent(payload: GetAllConnections): Promise<string> {
50-
const {orgId, connectionSearchCriteria } = payload;
49+
async getConnectionListFromAgent(payload: GetAllConnections): Promise<IConnectionList> {
50+
const { orgId, connectionSearchCriteria } = payload;
5151
return this.connectionService.getAllConnectionListFromAgent(orgId, connectionSearchCriteria);
5252
}
5353

5454
/**
55-
*
55+
*
5656
* @param connectionId
57-
* @param orgId
57+
* @param orgId
5858
* @returns connection details by connection Id
5959
*/
6060
@MessagePattern({ cmd: 'get-connection-details-by-connectionId' })
@@ -64,7 +64,7 @@ export class ConnectionController {
6464
}
6565

6666
@MessagePattern({ cmd: 'get-connection-records' })
67-
async getConnectionRecordsByOrgId(payload: { orgId: string, userId: string }): Promise<number> {
67+
async getConnectionRecordsByOrgId(payload: { orgId: string; userId: string }): Promise<number> {
6868
const { orgId } = payload;
6969
return this.connectionService.getConnectionRecords(orgId);
7070
}
@@ -80,7 +80,7 @@ export class ConnectionController {
8080
const { user, receiveInvitation, orgId } = payload;
8181
return this.connectionService.receiveInvitation(user, receiveInvitation, orgId);
8282
}
83-
83+
8484
@MessagePattern({ cmd: 'send-question' })
8585
async sendQuestion(payload: IQuestionPayload): Promise<object> {
8686
return this.connectionService.sendQuestion(payload);
@@ -97,13 +97,13 @@ export class ConnectionController {
9797
}
9898

9999
@MessagePattern({ cmd: 'delete-connection-records' })
100-
async deleteConnectionRecords(payload: {orgId: string, userDetails: user}): Promise<IDeletedConnectionsRecord> {
100+
async deleteConnectionRecords(payload: { orgId: string; userDetails: user }): Promise<IDeletedConnectionsRecord> {
101101
const { orgId, userDetails } = payload;
102102
return this.connectionService.deleteConnectionRecords(orgId, userDetails);
103103
}
104104

105105
@MessagePattern({ cmd: 'send-basic-message-on-connection' })
106-
async sendBasicMessage(payload: {content: string, orgId: string, connectionId: string}): Promise<object> {
107-
return this.connectionService.sendBasicMesage(payload);
106+
async sendBasicMessage(payload: { content: string; orgId: string; connectionId: string }): Promise<object> {
107+
return this.connectionService.sendBasicMessage(payload);
108108
}
109109
}

0 commit comments

Comments
 (0)