diff --git a/apps/agent-service/src/agent-service.controller.ts b/apps/agent-service/src/agent-service.controller.ts index caa1a54c2..fe5d3e2de 100644 --- a/apps/agent-service/src/agent-service.controller.ts +++ b/apps/agent-service/src/agent-service.controller.ts @@ -15,7 +15,9 @@ export class AgentServiceController { } @MessagePattern({ cmd: 'create-tenant' }) - async createTenant(payload: { createTenantDto: ITenantDto, user: IUserRequestInterface }): Promise { + async createTenant(payload: { createTenantDto: ITenantDto, user: IUserRequestInterface }): Promise<{ + agentSpinupStatus: number; + }> { return this.agentServiceService.createTenant(payload.createTenantDto, payload.user); } diff --git a/apps/agent-service/src/agent-service.service.ts b/apps/agent-service/src/agent-service.service.ts index 59d68e10a..62262c893 100644 --- a/apps/agent-service/src/agent-service.service.ts +++ b/apps/agent-service/src/agent-service.service.ts @@ -454,49 +454,124 @@ export class AgentServiceService { } } - async createTenant(payload: ITenantDto, user: IUserRequestInterface): Promise { - try { - const { label, seed } = payload; - const createTenantOptions = { - config: { - label - }, - seed - }; + async createTenant(payload: ITenantDto, user: IUserRequestInterface): Promise<{ + agentSpinupStatus: number; + }> { + const agentStatusResponse = { + agentSpinupStatus: 1 + }; - const platformAdminSpinnedUp = await this.agentServiceRepository.platformAdminAgent(parseInt(process.env.PLATFORM_ID)); + await this._createTenant(payload, user); - if (2 !== platformAdminSpinnedUp.org_agents[0].agentSpinUpStatus) { - throw new NotFoundException('Platform-admin agent is not spun-up'); - } + return agentStatusResponse; + } + + async _createTenant(payload: ITenantDto, user: IUserRequestInterface): Promise { + try { - const apiKey = ''; - const url = `${platformAdminSpinnedUp.org_agents[0].agentEndPoint}${CommonConstants.URL_SHAGENT_CREATE_TENANT}`; - const tenantDetails = await this.commonService - .httpPost(url, createTenantOptions, { headers: { 'x-api-key': apiKey } }) - .then(async (tenant) => { - this.logger.debug(`API Response Data: ${JSON.stringify(tenant)}`); - return tenant; + const sharedAgentSpinUpResponse = new Promise(async (resolve, _reject) => { + const { label, seed } = payload; + const createTenantOptions = { + config: { + label + }, + seed + }; + + const socket = await io(`${process.env.SOCKET_HOST}`, { + reconnection: true, + reconnectionDelay: 5000, + reconnectionAttempts: Infinity, + autoConnect: true, + transports: ['websocket'] }); - const storeOrgAgentData: IStoreOrgAgentDetails = { - did: tenantDetails.did, - verkey: tenantDetails.verkey, - isDidPublic: true, - agentSpinUpStatus: 2, - agentsTypeId: AgentType.AFJ, - orgId: payload.orgId, - agentEndPoint: platformAdminSpinnedUp.org_agents[0].agentEndPoint, - orgAgentTypeId: OrgAgentType.SHARED, - tenantId: tenantDetails.tenantRecord.id, - walletName: label - }; - const saveTenant = await this.agentServiceRepository.storeOrgAgentDetails(storeOrgAgentData); - await this._createLegacyConnectionInvitation(payload.orgId, user, storeOrgAgentData.walletName); - return saveTenant; + if (payload.clientSocketId) { + socket.emit('agent-spinup-process-initiated', { clientId: payload.clientSocketId }); + } + const platformAdminSpinnedUp = await this.agentServiceRepository.platformAdminAgent(parseInt(process.env.PLATFORM_ID)); + + if (!platformAdminSpinnedUp) { + throw new InternalServerErrorException('Agent not able to spin-up'); + } else { + resolve(platformAdminSpinnedUp); + } + + return sharedAgentSpinUpResponse.then(async (agentDetails) => { + if (agentDetails) { + if (2 !== platformAdminSpinnedUp.org_agents[0].agentSpinUpStatus) { + throw new NotFoundException('Platform-admin agent is not spun-up'); + } + + const apiKey = ''; + const url = `${platformAdminSpinnedUp.org_agents[0].agentEndPoint}${CommonConstants.URL_SHAGENT_CREATE_TENANT}`; + const tenantDetails = await this.commonService + .httpPost(url, createTenantOptions, { headers: { 'x-api-key': apiKey } }) + .then(async (tenant) => { + this.logger.debug(`API Response Data: ${JSON.stringify(tenant)}`); + return tenant; + }); + + const storeOrgAgentData: IStoreOrgAgentDetails = { + did: tenantDetails.did, + verkey: tenantDetails.verkey, + isDidPublic: true, + agentSpinUpStatus: 2, + agentsTypeId: AgentType.AFJ, + orgId: payload.orgId, + agentEndPoint: platformAdminSpinnedUp.org_agents[0].agentEndPoint, + orgAgentTypeId: OrgAgentType.SHARED, + tenantId: tenantDetails.tenantRecord.id, + walletName: label + }; + + if (payload.clientSocketId) { + socket.emit('agent-spinup-process-completed', { clientId: payload.clientSocketId }); + } + + const saveTenant = await this.agentServiceRepository.storeOrgAgentDetails(storeOrgAgentData); + + if (payload.clientSocketId) { + socket.emit('invitation-url-creation-started', { clientId: payload.clientSocketId }); + } + await this._createLegacyConnectionInvitation(payload.orgId, user, storeOrgAgentData.walletName); + + if (payload.clientSocketId) { + socket.emit('invitation-url-creation-success', { clientId: payload.clientSocketId }); + } + + resolve(saveTenant); + } else { + throw new InternalServerErrorException('Agent not able to spin-up'); + } + }) + .catch(async (error) => { + if (payload.clientSocketId) { + const socket = await io(`${process.env.SOCKET_HOST}`, { + reconnection: true, + reconnectionDelay: 5000, + reconnectionAttempts: Infinity, + autoConnect: true, + transports: ['websocket'] + }); + socket.emit('error-in-wallet-creation-process', { clientId: payload.clientSocketId, error }); + } + _reject(error); + }); + }); } catch (error) { this.logger.error(`Error in creating tenant: ${error}`); + if (payload.clientSocketId) { + const socket = await io(`${process.env.SOCKET_HOST}`, { + reconnection: true, + reconnectionDelay: 5000, + reconnectionAttempts: Infinity, + autoConnect: true, + transports: ['websocket'] + }); + socket.emit('error-in-wallet-creation-process', { clientId: payload.clientSocketId, error }); + } throw new RpcException(error.response); } } diff --git a/apps/agent-service/src/interface/agent-service.interface.ts b/apps/agent-service/src/interface/agent-service.interface.ts index 3e00e2245..e2b76d098 100644 --- a/apps/agent-service/src/interface/agent-service.interface.ts +++ b/apps/agent-service/src/interface/agent-service.interface.ts @@ -19,6 +19,7 @@ export interface ITenantDto { seed: string; tenantId?: string; orgId: number; + clientSocketId?: string; } export interface ITenantSchema { diff --git a/apps/api-gateway/src/agent-service/dto/create-tenant.dto.ts b/apps/api-gateway/src/agent-service/dto/create-tenant.dto.ts index b51d8debd..6ed0aa6ad 100644 --- a/apps/api-gateway/src/agent-service/dto/create-tenant.dto.ts +++ b/apps/api-gateway/src/agent-service/dto/create-tenant.dto.ts @@ -1,7 +1,7 @@ import { trim } from '@credebl/common/cast.helper'; import { ApiProperty } from '@nestjs/swagger'; import { Transform } from 'class-transformer'; -import { IsNotEmpty, IsNumber, IsString, Matches, MaxLength, MinLength } from 'class-validator'; +import { IsNotEmpty, IsNumber, IsString, Matches, MaxLength, MinLength, IsOptional } from 'class-validator'; const labelRegex = /^[a-zA-Z0-9 ]*$/; export class CreateTenantDto { @ApiProperty() @@ -28,4 +28,8 @@ export class CreateTenantDto { @ApiProperty() @IsNumber() orgId: number; + + @ApiProperty() + @IsOptional() + clientSocketId?: string; } \ No newline at end of file