Skip to content

Commit

Permalink
Merge pull request #46 from credebl/socket-shared-agent
Browse files Browse the repository at this point in the history
Added socket in shared agent
  • Loading branch information
nishad-ayanworks authored Aug 18, 2023
2 parents 3cee2dc + 9224220 commit da753b4
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 37 deletions.
4 changes: 3 additions & 1 deletion apps/agent-service/src/agent-service.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export class AgentServiceController {
}

@MessagePattern({ cmd: 'create-tenant' })
async createTenant(payload: { createTenantDto: ITenantDto, user: IUserRequestInterface }): Promise<object> {
async createTenant(payload: { createTenantDto: ITenantDto, user: IUserRequestInterface }): Promise<{
agentSpinupStatus: number;
}> {
return this.agentServiceService.createTenant(payload.createTenantDto, payload.user);
}

Expand Down
145 changes: 110 additions & 35 deletions apps/agent-service/src/agent-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,49 +454,124 @@ export class AgentServiceService {
}
}

async createTenant(payload: ITenantDto, user: IUserRequestInterface): Promise<org_agents> {
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<void> {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ITenantDto {
seed: string;
tenantId?: string;
orgId: number;
clientSocketId?: string;
}

export interface ITenantSchema {
Expand Down
6 changes: 5 additions & 1 deletion apps/api-gateway/src/agent-service/dto/create-tenant.dto.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -28,4 +28,8 @@ export class CreateTenantDto {
@ApiProperty()
@IsNumber()
orgId: number;

@ApiProperty()
@IsOptional()
clientSocketId?: string;
}

0 comments on commit da753b4

Please sign in to comment.