Skip to content

Commit

Permalink
Merge pull request #12 from Bhutan-NDI/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ankita-p17 authored Jul 11, 2024
2 parents 9544142 + e36cdf7 commit 7405399
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 6 deletions.
5 changes: 5 additions & 0 deletions apps/agent-service/src/agent-service.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,10 @@ export class AgentServiceController {
async createConnectionInvitation(payload: { url: string; orgId: string; connectionPayload: ICreateConnectionInvitation }): Promise<object> {
return this.agentServiceService.createConnectionInvitation(payload.url, payload.orgId, payload.connectionPayload);
}

@MessagePattern({ cmd: 'agent-send-basic-message' })
async sendBasicMessage(payload: { url, orgId, content }): Promise<object> {
return this.agentServiceService.sendBasicMessage(payload.content, payload.url, payload.orgId);
}
}

16 changes: 15 additions & 1 deletion apps/agent-service/src/agent-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ import {
IWallet,
ITenantRecord,
LedgerListResponse,
ICreateConnectionInvitation
ICreateConnectionInvitation,
IBasicMessage
} from './interface/agent-service.interface';
import { AgentSpinUpStatus, AgentType, DidMethod, Ledgers, OrgAgentType } from '@credebl/enum/enum';
import { AgentServiceRepository } from './repositories/agent-service.repository';
Expand Down Expand Up @@ -1673,4 +1674,17 @@ export class AgentServiceService {
throw error;
}
}

async sendBasicMessage(questionPayload: IBasicMessage, url: string, orgId: string): Promise<object> {
try {
const getApiKey = await this.getOrgAgentApiKey(orgId);
const sendQuestionRes = await this.commonService
.httpPost(url, questionPayload, { headers: { authorization: getApiKey } })
.then(async (response) => response);
return sendQuestionRes;
} catch (error) {
this.logger.error(`Error in sendBasicMessage in agent service : ${JSON.stringify(error)}`);
throw error;
}
}
}
4 changes: 4 additions & 0 deletions apps/agent-service/src/interface/agent-service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,7 @@ export interface ICreateConnectionInvitation {
appendedAttachments?: object[];
orgId?: string;
}

export interface IBasicMessage {
content: string;
}
27 changes: 26 additions & 1 deletion apps/api-gateway/src/connection/connection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ApiResponseDto } from '../dtos/apiResponse.dto';
import { IConnectionSearchCriteria } from '../interfaces/IConnectionSearch.interface';
import { SortFields } from 'apps/connection/src/enum/connection.enum';
import { ClientProxy} from '@nestjs/microservices';
import { QuestionAnswerWebhookDto, QuestionDto} from './dtos/question-answer.dto';
import { BasicMessageDto, QuestionAnswerWebhookDto, QuestionDto} from './dtos/question-answer.dto';

@UseFilters(CustomExceptionFilter)
@Controller()
Expand Down Expand Up @@ -332,4 +332,29 @@ export class ConnectionController {
}
return res.status(HttpStatus.CREATED).json(finalResponse);
}

@Post('/orgs/:orgId/basic-message/:connectionId')
@ApiOperation({ summary: '', description: 'Send basic message to connection' })
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
@Roles(OrgRoles.OWNER, OrgRoles.ADMIN, OrgRoles.ISSUER, OrgRoles.VERIFIER, OrgRoles.MEMBER, OrgRoles.HOLDER, OrgRoles.SUPER_ADMIN, OrgRoles.PLATFORM_ADMIN)
@ApiResponse({ status: HttpStatus.CREATED, description: 'Created', type: ApiResponseDto })
async sendBasicMessage(
@Param('orgId') orgId: string,
@Param('connectionId') connectionId: string,
@Body() basicMessageDto: BasicMessageDto,
@User() reqUser: IUserRequestInterface,
@Res() res: Response
): Promise<Response> {

basicMessageDto.orgId = orgId;
basicMessageDto.connectionId = connectionId;
const basicMesgResponse = await this.connectionService.sendBasicMessage(basicMessageDto);
const finalResponse: IResponse = {
statusCode: HttpStatus.CREATED,
message: ResponseMessages.connection.success.basicMessage, // TODO
data: basicMesgResponse
};
return res.status(HttpStatus.CREATED).json(finalResponse);

}
}
12 changes: 11 additions & 1 deletion apps/api-gateway/src/connection/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ConnectionDto, CreateOutOfBandConnectionInvitation, ReceiveInvitationDt
import { IReceiveInvitationRes, IUserRequestInterface } from './interfaces';
import { IConnectionList } from '@credebl/common/interfaces/connection.interface';
import { AgentConnectionSearchCriteria, IConnectionDetailsById, IConnectionSearchCriteria } from '../interfaces/IConnectionSearch.interface';
import { QuestionDto } from './dtos/question-answer.dto';
import { BasicMessageDto, QuestionDto } from './dtos/question-answer.dto';

@Injectable()
export class ConnectionService extends BaseService {
Expand Down Expand Up @@ -130,4 +130,14 @@ export class ConnectionService extends BaseService {
const payload = { user, createOutOfBandConnectionInvitation };
return this.sendNatsMessage(this.connectionServiceProxy, 'create-connection-invitation', payload);
}

sendBasicMessage(
basicMessageDto: BasicMessageDto
): Promise<object> {
try {
return this.sendNatsMessage(this.connectionServiceProxy, 'send-basic-message', basicMessageDto);
} catch (error) {
throw new RpcException(error.response);
}
}
}
11 changes: 11 additions & 0 deletions apps/api-gateway/src/connection/dtos/question-answer.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,15 @@ export class QuestionAnswerWebhookDto {
@IsOptional()
type: string;

}

export class BasicMessageDto {
@ApiPropertyOptional()
@IsOptional()
@IsString({ message: 'content must be a string' })
@IsNotEmpty({ message: 'please provide valid content' })
content: string;

orgId: string;
connectionId: string;
}
5 changes: 5 additions & 0 deletions apps/connection/src/connection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ export class ConnectionController {
async createConnectionInvitation(payload: ICreateOutOfbandConnectionInvitation): Promise<object> {
return this.connectionService.createConnectionInvitation(payload);
}

@MessagePattern({ cmd: 'send-basic-message' })
async sendBasicMessage(payload: {content: string, orgId: string, connectionId: string}): Promise<object> {
return this.connectionService.sendBasicMesage(payload);
}
}
88 changes: 87 additions & 1 deletion apps/connection/src/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Cache } from 'cache-manager';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { IConnectionList, ICreateConnectionUrl } from '@credebl/common/interfaces/connection.interface';
import { IConnectionDetailsById } from 'apps/api-gateway/src/interfaces/IConnectionSearch.interface';
import { IQuestionPayload } from './interfaces/question-answer.interfaces';
import { IBasicMessage, IQuestionPayload } from './interfaces/question-answer.interfaces';
import { agent_invitations } from '@prisma/client';

@Injectable()
Expand Down Expand Up @@ -781,4 +781,90 @@ export class ConnectionService {
throw new RpcException(error.response ? error.response : error);
}
}

async sendBasicMesage(payload: IBasicMessage): Promise<object> {
const { content, orgId, connectionId } = payload;
try {
const agentDetails = await this.connectionRepository.getAgentEndPoint(orgId);

const { agentEndPoint } = agentDetails;

if (!agentDetails) {
throw new NotFoundException(ResponseMessages.connection.error.agentEndPointNotFound);
}

const questionPayload = {
content
};

const orgAgentType = await this.connectionRepository.getOrgAgentType(agentDetails?.orgAgentTypeId);
const label = 'send-basic-message';
const url = await this.sendBasicMessageAgentUrl(
label,
orgAgentType,
agentEndPoint,
agentDetails?.tenantId,
connectionId
);

const sendBasicMessage = await this._sendBasicMessage(questionPayload, url, orgId);
return sendBasicMessage;
} catch (error) {
this.logger.error(`[sendBasicMesage] - error in sending basic message: ${error}`);
if (error && error?.status && error?.status?.message && error?.status?.message?.error) {
throw new RpcException({
message: error?.status?.message?.error?.reason
? error?.status?.message?.error?.reason
: error?.status?.message?.error,
statusCode: error?.status?.code
});
} else {
throw new RpcException(error.response ? error.response : error);
}
}
}

async _sendBasicMessage(content: IBasicMessage, url: string, orgId: string): Promise<object> {
const pattern = { cmd: 'agent-send-basic-message' };
const payload = { content, url, orgId };
// eslint-disable-next-line no-return-await
return await this.natsCall(pattern, payload);
}

async sendBasicMessageAgentUrl(
label: string,
orgAgentType: string,
agentEndPoint: string,
tenantId?: string,
connectionId?: string
): Promise<string> {
try {
let url;
switch (label) {
case 'send-basic-message': {
url =
orgAgentType === OrgAgentType.DEDICATED
? `${agentEndPoint}${CommonConstants.URL_SEND_BASIC_MESSAGE}`.replace('#', connectionId)
: orgAgentType === OrgAgentType.SHARED
? `${agentEndPoint}${CommonConstants.URL_SHARED_SEND_BASIC_MESSAGE}`
.replace('#', connectionId)
.replace('@', tenantId)
: null;
break;
}

default: {
break;
}
}

if (!url) {
throw new NotFoundException(ResponseMessages.issuance.error.agentUrlNotFound);
}
return url;
} catch (error) {
this.logger.error(`Error in getting basic-message Url: ${JSON.stringify(error)}`);
throw error;
}
}
}
7 changes: 7 additions & 0 deletions apps/connection/src/interfaces/question-answer.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ export interface IValidResponses {
connectionId?: string;
tenantId?: string;
}

export interface IBasicMessage {
content: string;
orgId?: string;
connectionId?: string;
tenantId?: string;
}
4 changes: 3 additions & 1 deletion libs/common/src/common.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export enum CommonConstants {
URL_ACCEPT_CREDENTIALS = '/credentials/accept-offer',
URL_SEND_QUESTION = '/question-answer/question/#',
URL_QUESTION_ANSWER_RECORD = '/question-answer',
URL_SEND_BASIC_MESSAGE = '/basic-messages/#',

// SCHEMA & CRED DEF SERVICES
URL_SCHM_CREATE_SCHEMA = '/schemas',
Expand Down Expand Up @@ -117,7 +118,8 @@ export enum CommonConstants {
URL_SHAGENT_SEND_QUESTION = '/multi-tenancy/question-answer/question/#/@',
URL_SHAGENT_SEND_ANSWER = '/multi-tenancy/question-answer/answer/#/@',
URL_SHAGENT_QUESTION_ANSWER_RECORD = '/multi-tenancy/question-answer/#',

URL_SHARED_SEND_BASIC_MESSAGE = '/multi-tenancy/basic-message/#/@',

// PROOF SERVICES
URL_SEND_PROOF_REQUEST = '/proofs/request-proof',
URL_GET_PROOF_PRESENTATIONS = '/proofs',
Expand Down
3 changes: 2 additions & 1 deletion libs/common/src/response-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ export const ResponseMessages = {
fetchConnection: 'Connection details fetched successfully',
fetch: 'Connections details fetched successfully',
questionAnswerRecord: 'Question Answer record fetched successfully',
questionSend:'Question sent successfully'
questionSend:'Question sent successfully',
basicMessage:'Basic message sent successfully'
},
error: {
exists: 'Connection is already exist',
Expand Down

0 comments on commit 7405399

Please sign in to comment.