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

feat: multi ecosystem support #194

Merged
merged 2 commits into from
Oct 26, 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
23 changes: 21 additions & 2 deletions apps/ecosystem/src/ecosystem.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,17 @@ export class EcosystemRepository {
* @returns Get specific organization details from ecosystem
*/
// eslint-disable-next-line camelcase
async checkEcosystemOrgs(orgId: string): Promise<ecosystem_orgs> {
async checkEcosystemOrgs(orgId: string): Promise<ecosystem_orgs[]> {
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) {
Expand Down Expand Up @@ -205,6 +208,22 @@ export class EcosystemRepository {
}
}

// eslint-disable-next-line camelcase
async getSpecificEcosystemConfig(key: string): Promise<ecosystem_config> {
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<number> {
try {
const membersCount = await this.prisma.ecosystem_orgs.count(
Expand Down
39 changes: 26 additions & 13 deletions apps/ecosystem/src/ecosystem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -38,10 +38,18 @@ export class EcosystemService {

// eslint-disable-next-line camelcase
async createEcosystem(createEcosystemDto: CreateEcosystem): Promise<object> {
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);
Expand Down Expand Up @@ -186,11 +194,16 @@ export class EcosystemService {
*/
async acceptRejectEcosystemInvitations(acceptRejectInvitation: AcceptRejectEcosystemInvitationDto): Promise<string> {
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);

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down
22 changes: 13 additions & 9 deletions apps/user/src/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -331,18 +332,21 @@ export class UserService {
async getProfile(payload: { id }): Promise<object> {
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)}`);
Expand Down
9 changes: 9 additions & 0 deletions libs/enum/src/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions libs/prisma-service/prisma/data/credebl-master-table.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@
{
"key": "participateInEcosystem",
"value": "false"
},
{
"key": "multiEcosystemSupport",
"value": "false"
}
]
}