diff --git a/apps/ecosystem/src/ecosystem.repository.ts b/apps/ecosystem/src/ecosystem.repository.ts index 96c265473..64470f0bf 100644 --- a/apps/ecosystem/src/ecosystem.repository.ts +++ b/apps/ecosystem/src/ecosystem.repository.ts @@ -157,14 +157,17 @@ export class EcosystemRepository { * @returns Get specific organization details from ecosystem */ // eslint-disable-next-line camelcase - async checkEcosystemOrgs(orgId: string): Promise { + async checkEcosystemOrgs(orgId: string): Promise { try { if (!orgId) { throw new BadRequestException(ResponseMessages.ecosystem.error.invalidOrgId); } - return this.prisma.ecosystem_orgs.findFirst({ + return this.prisma.ecosystem_orgs.findMany({ where: { orgId + }, + include:{ + ecosystemRole: true } }); } catch (error) { @@ -205,6 +208,22 @@ export class EcosystemRepository { } } + // eslint-disable-next-line camelcase + async getSpecificEcosystemConfig(key: string): Promise { + try { + return await this.prisma.ecosystem_config.findFirst( + { + where: { + key + } + } + ); + } catch (error) { + this.logger.error(`error: ${JSON.stringify(error)}`); + throw error; + } + } + async getEcosystemMembersCount(ecosystemId: string): Promise { try { const membersCount = await this.prisma.ecosystem_orgs.count( diff --git a/apps/ecosystem/src/ecosystem.service.ts b/apps/ecosystem/src/ecosystem.service.ts index 57e4368e8..f10d79601 100644 --- a/apps/ecosystem/src/ecosystem.service.ts +++ b/apps/ecosystem/src/ecosystem.service.ts @@ -9,7 +9,7 @@ import { EcosystemInviteTemplate } from '../templates/EcosystemInviteTemplate'; import { EmailDto } from '@credebl/common/dtos/email.dto'; import { sendEmail } from '@credebl/common/send-grid-helper-file'; import { AcceptRejectEcosystemInvitationDto } from '../dtos/accept-reject-ecosysteminvitation.dto'; -import { Invitation, OrgAgentType } from '@credebl/enum/enum'; +import { EcosystemConfigSettings, Invitation, OrgAgentType } from '@credebl/enum/enum'; import { EcosystemOrgStatus, EcosystemRoles, endorsementTransactionStatus, endorsementTransactionType } from '../enums/ecosystem.enum'; import { FetchInvitationsPayload } from '../interfaces/invitations.interface'; import { EcosystemMembersPayload } from '../interfaces/ecosystemMembers.interface'; @@ -38,10 +38,18 @@ export class EcosystemService { // eslint-disable-next-line camelcase async createEcosystem(createEcosystemDto: CreateEcosystem): Promise { - const checkOrganization = await this.ecosystemRepository.checkEcosystemOrgs(createEcosystemDto.orgId); - if (checkOrganization) { - throw new ConflictException(ResponseMessages.ecosystem.error.ecosystemOrgAlready); - }; + + const isMultiEcosystemEnabled = await this.ecosystemRepository.getSpecificEcosystemConfig(EcosystemConfigSettings.MULTI_ECOSYSTEM); + + if (isMultiEcosystemEnabled && 'false' === isMultiEcosystemEnabled.value) { + const ecoOrganizationList = await this.ecosystemRepository.checkEcosystemOrgs(createEcosystemDto.orgId); + + for (const organization of ecoOrganizationList) { + if (organization['ecosystemRole']['name'] === EcosystemRoles.ECOSYSTEM_MEMBER) { + throw new ConflictException(ResponseMessages.ecosystem.error.ecosystemOrgAlready); + } + } + } const createEcosystem = await this.ecosystemRepository.createNewEcosystem(createEcosystemDto); if (!createEcosystem) { throw new NotFoundException(ResponseMessages.ecosystem.error.notCreated); @@ -186,11 +194,16 @@ export class EcosystemService { */ async acceptRejectEcosystemInvitations(acceptRejectInvitation: AcceptRejectEcosystemInvitationDto): Promise { try { - const checkOrganization = await this.ecosystemRepository.checkEcosystemOrgs(acceptRejectInvitation.orgId); + const isMultiEcosystemEnabled = await this.ecosystemRepository.getSpecificEcosystemConfig(EcosystemConfigSettings.MULTI_ECOSYSTEM); + if (isMultiEcosystemEnabled + && 'false' === isMultiEcosystemEnabled.value + && acceptRejectInvitation.status !== Invitation.REJECTED) { + const checkOrganization = await this.ecosystemRepository.checkEcosystemOrgs(acceptRejectInvitation.orgId); + if (0 < checkOrganization.length) { + throw new ConflictException(ResponseMessages.ecosystem.error.ecosystemOrgAlready); + }; + } - if (checkOrganization) { - throw new ConflictException(ResponseMessages.ecosystem.error.ecosystemOrgAlready); - }; const { orgId, status, invitationId, orgName, orgDid } = acceptRejectInvitation; const invitation = await this.ecosystemRepository.getEcosystemInvitationById(invitationId); @@ -216,7 +229,7 @@ export class EcosystemService { return ResponseMessages.ecosystem.success.invitationAccept; } catch (error) { - this.logger.error(`acceptRejectInvitations: ${error}`); + this.logger.error(`acceptRejectEcosystemInvitations: ${error}`); throw new RpcException(error.response ? error.response : error); } } @@ -484,7 +497,7 @@ export class EcosystemService { throw new NotFoundException(ResponseMessages.ecosystem.error.credentialDefinitionNotFound); } - requestCredDefPayload["credentialDefinition"] = requestBody; + requestCredDefPayload['credentialDefinition'] = requestBody; const schemaTransactionResponse = { endorserDid: ecosystemLeadAgentDetails.orgDid, authorDid: ecosystemMemberDetails.orgDid, @@ -792,7 +805,7 @@ export class EcosystemService { const submitTransactionRequest = await this._submitTransaction(payload, url, platformConfig.sgApiKey); - if ('failed' === submitTransactionRequest["message"].state) { + if ('failed' === submitTransactionRequest['message'].state) { throw new InternalServerErrorException(ResponseMessages.ecosystem.error.sumbitTransaction); } @@ -802,7 +815,7 @@ export class EcosystemService { return this.handleSchemaSubmission(endorsementTransactionPayload, ecosystemMemberDetails, submitTransactionRequest); } else if (endorsementTransactionPayload.type === endorsementTransactionType.CREDENTIAL_DEFINITION) { - if ('undefined' === submitTransactionRequest["message"].credentialDefinitionId.split(":")[3]) { + if ('undefined' === submitTransactionRequest['message'].credentialDefinitionId.split(':')[3]) { const autoEndorsement = `${CommonConstants.ECOSYSTEM_AUTO_ENDOSEMENT}`; const ecosystemConfigDetails = await this.ecosystemRepository.getEcosystemConfigDetails(autoEndorsement); diff --git a/apps/user/src/user.service.ts b/apps/user/src/user.service.ts index af7f869ba..588386276 100644 --- a/apps/user/src/user.service.ts +++ b/apps/user/src/user.service.ts @@ -39,6 +39,7 @@ import { UserActivityService } from '@credebl/user-activity'; import { SupabaseService } from '@credebl/supabase'; import { UserDevicesRepository } from '../repositories/user-device.repository'; import { v4 as uuidv4 } from 'uuid'; +import { EcosystemConfigSettings } from '@credebl/enum/enum'; @Injectable() export class UserService { @@ -331,18 +332,21 @@ export class UserService { async getProfile(payload: { id }): Promise { try { const userData = await this.userRepository.getUserById(payload.id); - const ecosystemDetails = await this.prisma.ecosystem_config.findFirst({ - where: { - key: 'enableEcosystem' + const ecosystemSettingsList = await this.prisma.ecosystem_config.findMany( + { + where:{ + OR: [ + { key: EcosystemConfigSettings.ENABLE_ECOSYSTEM }, + { key: EcosystemConfigSettings.MULTI_ECOSYSTEM } + ] + } } - }); + ); - if ('true' === ecosystemDetails.value) { - userData['enableEcosystem'] = true; - return userData; + for (const setting of ecosystemSettingsList) { + userData[setting.key] = 'true' === setting.value; } - - userData['enableEcosystem'] = false; + return userData; } catch (error) { this.logger.error(`get user: ${JSON.stringify(error)}`); diff --git a/libs/enum/src/enum.ts b/libs/enum/src/enum.ts index e47471927..6a9402c58 100644 --- a/libs/enum/src/enum.ts +++ b/libs/enum/src/enum.ts @@ -19,6 +19,15 @@ export enum EcosystemRoles { ECOSYSTEM_OWNER = 'Ecosystem Owner' } +export enum EcosystemConfigSettings { + URL = 'url', + ENABLE_ECOSYSTEM = 'enableEcosystem', + AUTO_ENDORSEMENT = 'autoEndorsement', + PARTICIPATE_IN_ECOSYSTEM = 'participateInEcosystem', + MULTI_ECOSYSTEM = 'multiEcosystemSupport' + +} + export enum EndorserTransactionType{ SCHEMA = 'schema', CREDENTIAL_DEFINITION = 'credential-definition', diff --git a/libs/prisma-service/prisma/data/credebl-master-table.json b/libs/prisma-service/prisma/data/credebl-master-table.json index a498c5929..4c6a60251 100644 --- a/libs/prisma-service/prisma/data/credebl-master-table.json +++ b/libs/prisma-service/prisma/data/credebl-master-table.json @@ -182,6 +182,10 @@ { "key": "participateInEcosystem", "value": "false" + }, + { + "key": "multiEcosystemSupport", + "value": "false" } ] } \ No newline at end of file