Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added socket in shared agent #46

Merged
merged 3 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}