Skip to content

Commit

Permalink
feat: delete ecosystem by org Id (#768)
Browse files Browse the repository at this point in the history
* feat: delete ecosystem members

Signed-off-by: bhavanakarwade <[email protected]>

* wip: delete ecosystem

Signed-off-by: bhavanakarwade <[email protected]>

* fix: eslint issues

Signed-off-by: bhavanakarwade <[email protected]>

* fix: resolved trimmedstring issue

Signed-off-by: bhavanakarwade <[email protected]>

* refactor: interface type

Signed-off-by: bhavanakarwade <[email protected]>

* refactor: get organization details function

Signed-off-by: bhavanakarwade <[email protected]>

* fix: resolved conflicts

Signed-off-by: bhavanakarwade <[email protected]>

* refactor: made description field required

Signed-off-by: bhavanakarwade <[email protected]>

* refactor: function name

Signed-off-by: bhavanakarwade <[email protected]>

---------

Signed-off-by: bhavanakarwade <[email protected]>
Signed-off-by: KulkarniShashank <[email protected]>
  • Loading branch information
bhavanakarwade authored and KulkarniShashank committed Sep 11, 2024
1 parent 4c7a3c3 commit 99dc0c6
Show file tree
Hide file tree
Showing 20 changed files with 500 additions and 29 deletions.
25 changes: 23 additions & 2 deletions apps/api-gateway/src/ecosystem/ecosystem.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,11 @@ export class EcosystemController {
@Roles(OrgRoles.OWNER, OrgRoles.ADMIN)
async addOrganizationsInEcosystem(
@Body() addOrganizationsDto: AddOrganizationsDto,
@Param('ecosystemId', new ParseUUIDPipe({exceptionFactory: (): Error => { throw new BadRequestException(ResponseMessages.ecosystem.error.invalidEcosystemId); }})) ecosystemId: string,
@Param('ecosystemId', new ParseUUIDPipe({exceptionFactory: (): Error => { throw new BadRequestException(ResponseMessages.ecosystem.error.invalidEcosystemId); }}), TrimStringParamPipe) ecosystemId: string,
@Param('orgId', new ParseUUIDPipe({exceptionFactory: (): Error => { throw new BadRequestException(ResponseMessages.organisation.error.invalidOrgId); }})) orgId: string,
@User() user: user,
@Res() res: Response
): Promise<Response> {
): Promise<Response> {

addOrganizationsDto.ecosystemId = ecosystemId;
addOrganizationsDto.orgId = orgId;
Expand Down Expand Up @@ -668,6 +668,27 @@ export class EcosystemController {
return res.status(HttpStatus.OK).json(finalResponse);
}

@Delete('/:orgId/ecosystems')
@ApiOperation({ summary: 'Delete ecosystems', description: 'Delete ecosystems by orgId' })
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
@Roles(OrgRoles.OWNER)
@ApiBearerAuth()
async deleteEcosystemAsMember(
@Param('orgId', new ParseUUIDPipe({exceptionFactory: (): Error => { throw new BadRequestException(ResponseMessages.organisation.error.invalidOrgId); }})) orgId: string,
@Res() res: Response,
@User() user: user
): Promise<Response> {

await this.ecosystemService.deleteEcosystemAsMember(orgId, user);

const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.ecosystem.success.deleteEcosystemMember
};
return res.status(HttpStatus.OK).json(finalResponse);
}


@Delete('/:ecosystemId/:orgId/invitations/:invitationId')
@ApiOperation({ summary: 'Delete ecosystem pending invitations', description: 'Delete ecosystem pending invitations' })
Expand Down
15 changes: 11 additions & 4 deletions apps/api-gateway/src/ecosystem/ecosystem.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Inject } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { BaseService } from 'libs/service/base.service';
import { BulkEcosystemInvitationDto } from './dtos/send-invitation.dto';
Expand All @@ -13,9 +12,10 @@ import { CreateEcosystemDto } from './dtos/create-ecosystem-dto';
import { EditEcosystemDto } from './dtos/edit-ecosystem-dto';
import { IEcosystemDashboard, IEcosystemInvitation, IEcosystemInvitations, IEcosystem, IEditEcosystem, IEndorsementTransaction, ISchemaResponse } from 'apps/ecosystem/interfaces/ecosystem.interfaces';
import { PaginationDto } from '@credebl/common/dtos/pagination.dto';
import { IEcosystemDetails } from '@credebl/common/interfaces/ecosystem.interface';
import { IEcosystemDataDeletionResults, IEcosystemDetails } from '@credebl/common/interfaces/ecosystem.interface';
import { AddOrganizationsDto } from './dtos/add-organizations.dto';
import { schemaRequestType } from '@credebl/enum/enum';
import { user } from '@prisma/client';

@Injectable()
export class EcosystemService extends BaseService {
Expand Down Expand Up @@ -130,6 +130,7 @@ export class EcosystemService extends BaseService {
const payload = { invitationId };
return this.sendNats(this.serviceProxy, 'delete-ecosystem-invitations', payload);
}

async acceptRejectEcosystemInvitaion(
acceptRejectInvitation: AcceptRejectEcosystemInvitationDto,
userEmail: string
Expand Down Expand Up @@ -205,4 +206,10 @@ export class EcosystemService extends BaseService {
const payload = { ecosystemId, endorsementId, orgId };
return this.sendNatsMessage(this.serviceProxy, 'decline-endorsement-transaction', payload);
}
}

async deleteEcosystemAsMember(orgId: string, userDetails: user): Promise<IEcosystemDataDeletionResults> {
const payload = { orgId, userDetails };
return this.sendNats(this.serviceProxy, 'delete-ecosystems-as-member', payload);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class CreateOrganizationDto {
@IsNotSQLInjection({ message: 'Organization name is required.' })
name: string;

@ApiPropertyOptional()
@ApiProperty()
@Transform(({ value }) => trim(value))
@IsNotEmpty({ message: 'Description is required.' })
@MinLength(2, { message: 'Description must be at least 2 characters.' })
Expand Down
37 changes: 35 additions & 2 deletions apps/ecosystem/interfaces/ecosystem.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,40 @@ export interface IEcosystemOrgDetails {
ecosystemOrgs: IEcosystemOrgsData[];
}


export interface IEcosystemEndorsementFlag {
autoEndorsement: boolean;
}
}


interface IEcosystemRole {
id: string;
name: string;
description: string;
createDateTime: Date;
lastChangedDateTime: Date;
deletedAt: Date;
}

interface IEcosystemMemberOrgs extends IEcosystemOrgs{
id: string;
createDateTime: Date;
lastChangedDateTime: Date;
deletedAt: Date;
ecosystemRole: IEcosystemRole;
}

export interface IEcosystemData {
id: string;
name: string;
description: string;
tags: string;
createDateTime: Date;
createdBy: string;
lastChangedDateTime: Date;
lastChangedBy: string;
deletedAt: Date;
logoUrl: string;
autoEndorsement: boolean;
ledgers: Prisma.JsonValue;
ecosystemOrgs: IEcosystemMemberOrgs[];
}
11 changes: 9 additions & 2 deletions apps/ecosystem/src/ecosystem.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { FetchInvitationsPayload } from '../interfaces/invitations.interface';
import { EcosystemMembersPayload } from '../interfaces/ecosystemMembers.interface';
import { GetEndorsementsPayload, ISchemasResponse } from '../interfaces/endorsements.interface';
import { IEcosystemDashboard, RequestCredDeffEndorsement, IEcosystem, IEcosystemInvitation, IEcosystemInvitations, IEditEcosystem, IEndorsementTransaction, IEcosystemList, IEcosystemLeadOrgs, IRequestSchemaEndorsement, IRequestW3CSchemaEndorsement } from '../interfaces/ecosystem.interfaces';
import { IEcosystemDetails } from '@credebl/common/interfaces/ecosystem.interface';
import { IEcosystemDataDeletionResults, IEcosystemDetails } from '@credebl/common/interfaces/ecosystem.interface';
import { schemaRequestType } from '@credebl/enum/enum';
import { user } from '@prisma/client';
// eslint-disable-next-line camelcase

@Controller()
Expand Down Expand Up @@ -235,4 +236,10 @@ export class EcosystemController {
async declineEndorsementRequestByLead(payload: { ecosystemId: string; endorsementId: string }): Promise<object> {
return this.ecosystemService.declineEndorsementRequestByLead(payload.ecosystemId, payload.endorsementId);
}
}

@MessagePattern({ cmd: 'delete-ecosystems-as-member' })
async deleteEcosystemAsMember(payload: { orgId: string, userDetails: user}): Promise<IEcosystemDataDeletionResults> {
const { orgId, userDetails } = payload;
return this.ecosystemService.deleteEcosystemAsMember(orgId, userDetails);
}
}
5 changes: 4 additions & 1 deletion apps/ecosystem/src/ecosystem.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { CommonModule} from '@credebl/common';
import { EcosystemRepository } from './ecosystem.repository';
import { PrismaService } from '@credebl/prisma-service';
import { CacheModule } from '@nestjs/cache-manager';
import { getNatsOptions } from '@credebl/common/nats.config';
import { UserActivityRepository } from 'libs/user-activity/repositories';

@Module({
imports: [
ClientsModule.register([
Expand All @@ -20,6 +23,6 @@ import { CacheModule } from '@nestjs/cache-manager';
CacheModule.register()
],
controllers: [EcosystemController],
providers: [EcosystemService, PrismaService, Logger, EcosystemRepository]
providers: [EcosystemService, UserActivityRepository, PrismaService, Logger, EcosystemRepository]
})
export class EcosystemModule { }
119 changes: 115 additions & 4 deletions apps/ecosystem/src/ecosystem.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { PrismaService } from '@credebl/prisma-service';
import { Prisma, credential_definition, ecosystem, ecosystem_config, ecosystem_invitations, ecosystem_orgs, ecosystem_roles, endorsement_transaction, org_agents, org_roles, organisation, platform_config, schema, user_org_roles } from '@prisma/client';
import { DeploymentModeType, EcosystemInvitationStatus, EcosystemOrgStatus, EcosystemRoles, endorsementTransactionStatus, endorsementTransactionType } from '../enums/ecosystem.enum';
import { updateEcosystemOrgsDto } from '../dtos/update-ecosystemOrgs.dto';
import { CreateEcosystem, IEcosystemInvitation, SaveSchema, SchemaTransactionResponse, saveCredDef } from '../interfaces/ecosystem.interfaces';
import { CreateEcosystem, IEcosystemData, IEcosystemInvitation, IEcosystemOrgs, IEcosystemOrgsData, SaveSchema, SchemaTransactionResponse, saveCredDef } from '../interfaces/ecosystem.interfaces';
import { ResponseMessages } from '@credebl/common/response-messages';
import { NotFoundException } from '@nestjs/common';
import { CommonConstants } from '@credebl/common/common.constant';
import { GetAllSchemaList, ISchemasResponse } from '../interfaces/endorsements.interface';
import { SortValue } from '@credebl/enum/enum';
import { IEcosystemDataDeletionResults } from '@credebl/common/interfaces/ecosystem.interface';
// eslint-disable-next-line camelcase

@Injectable()
Expand Down Expand Up @@ -520,11 +521,12 @@ export class EcosystemRepository {
// eslint-disable-next-line camelcase
async getEcosystemRole(name: string): Promise<ecosystem_roles> {
try {
return this.prisma.ecosystem_roles.findFirst({
const getEcosytemRoles = this.prisma.ecosystem_roles.findFirst({
where: {
name
}
});
return getEcosytemRoles;
} catch (error) {
this.logger.error(`getEcosystemRole: ${JSON.stringify(error)}`);
throw error;
Expand Down Expand Up @@ -740,7 +742,6 @@ export class EcosystemRepository {

}


async getEndorsementsWithPagination(queryObject: object, pageNumber: number, pageSize: number): Promise<object> {
try {
const result = await this.prisma.$transaction([
Expand Down Expand Up @@ -1374,4 +1375,114 @@ export class EcosystemRepository {
throw error;
}
}
}

async getEcosystemsByOrgId(orgId: string): Promise<IEcosystemData[]> {
try {

const ecosystemsDetails = await this.prisma.ecosystem.findMany({
where: {
ecosystemOrgs: {
some: {
orgId
}
}

},
include: {
ecosystemOrgs: {
include: {
ecosystemRole: true
}
}
}
});

return ecosystemsDetails;
} catch (error) {
this.logger.error(`Error in getting ecosystems: ${error.message}`);
throw error;
}
}

async getOrgName(orgId: string): Promise<{
name: string;
}> {
try {
const orgName = await this.prisma.organisation.findUnique({
where: {
id: orgId
},
select: {
name: true
}
});

return orgName;
} catch (error) {
this.logger.error(`Error in getting organization names: ${error.message}`);
throw error;
}
}

async deleteEcosystemInvitations(orgId: string): Promise<Prisma.BatchPayload> {
try {
const deletedEcosystemInvitations = await this.prisma.ecosystem_invitations.deleteMany({
where: {
orgId
}
});

return deletedEcosystemInvitations;
} catch (error) {
this.logger.error(`Error in deleting ecosystem invitations: ${error.message}`);
throw error;
}
}

async deleteEcosystemAsMember(orgId: string): Promise<IEcosystemDataDeletionResults> {
try {
return await this.prisma.$transaction(async (prisma) => {
const deletedEcosystemUsers = await prisma.ecosystem_users.deleteMany({
where: {
ecosystem: {
ecosystemOrgs: {
some: {
orgId
}
}
}
}
});

const deleteEndorsementTransactions = await prisma.endorsement_transaction.deleteMany({
where: {
ecosystemOrgs: {
orgId
}
}
});

const deletedEcosystemOrgs = await prisma.ecosystem_orgs.deleteMany({
where: {
orgId
}
});

const deletedEcosystems = await prisma.ecosystem.deleteMany({
where: {
ecosystemOrgs: {
some: {
orgId
}
}
}
});

return { deletedEcosystemUsers, deleteEndorsementTransactions, deletedEcosystemOrgs, deletedEcosystems };
});
} catch (error) {
this.logger.error(`Error in deleting ecosystems: ${error.message}`);
throw error;
}
}
}
Loading

0 comments on commit 99dc0c6

Please sign in to comment.