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: archive schema for deleted wallet #1078

Merged
merged 3 commits into from
Nov 20, 2024
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
29 changes: 29 additions & 0 deletions apps/agent-service/src/agent-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,11 @@ export class AgentServiceService {
{ records: deleteOrgAgent ? 1 : 0, tableName: 'org_agents' }
];

const did = orgAgent?.orgDid;

//archive schemas
await this._updateIsSchemaArchivedFlag(did);

const logDeletionActivity = async (records, tableName): Promise<void> => {
if (records) {
const txnMetadata = {
Expand All @@ -1745,6 +1750,30 @@ export class AgentServiceService {
}
}

async _updateIsSchemaArchivedFlag(did: string): Promise<{
count: number
}> {
const pattern = { cmd: 'archive-schemas' };

const payload = {
did
};
const updatedSchemaInfo = await this.agentServiceProxy
.send(pattern, payload)
.toPromise()
.catch((error) => {
this.logger.error(`catch: ${JSON.stringify(error)}`);
throw new HttpException(
{
status: error.status,
error: error.message
},
error.status
);
});
return updatedSchemaInfo;
}

async receiveInvitationUrl(receiveInvitationUrl: IReceiveInvitationUrl, url: string, orgId: string): Promise<string> {
try {
const getApiKey = await this.getOrgAgentApiKey(orgId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,18 @@ export class CredentialDefinitionService extends BaseService {

return schemaResponse;
}
const credDefSchemaList: CredDefSchema[] =
const credDefSchemaList =
await this.credentialDefinitionRepository.getAllCredDefsByOrgIdForBulk(
payload
);

if (!credDefSchemaList) {
throw new NotFoundException(ResponseMessages.credentialDefinition.error.NotFound);
}
return credDefSchemaList;

const activeCredDefSchemaList = credDefSchemaList.filter((item) => !item?.['isSchemaArchived']);

return activeCredDefSchemaList;
} catch (error) {
this.logger.error(
`get Cred-Defs and schema List By OrgId for bulk operations: ${JSON.stringify(error)}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ export class CredentialDefinitionRepository {
version: true,
schemaLedgerId: true,
orgId: true,
attributes: true
attributes: true,
isSchemaArchived: true
}
});

Expand All @@ -254,11 +255,13 @@ export class CredentialDefinitionRepository {
if (matchingSchema) {
return {
credentialDefinitionId: credDef.credentialDefinitionId,
credentialDefinition: credDef.tag,
schemaCredDefName: `${matchingSchema.name}:${matchingSchema.version}-${credDef.tag}`,
schemaName: matchingSchema.name,
schemaVersion: matchingSchema.version,
schemaAttributes: matchingSchema.attributes,
credentialDefinition: credDef.tag
schemaLedgerId: matchingSchema.schemaLedgerId,
isSchemaArchived: matchingSchema.isSchemaArchived
};
}
return null;
Expand All @@ -277,6 +280,7 @@ export class CredentialDefinitionRepository {
return await this.prisma.schema.findMany({
where: {
orgId,
isSchemaArchived: false,
type: schemaType
},
select: {
Expand Down
29 changes: 27 additions & 2 deletions apps/ledger/src/schema/repositories/schema.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable camelcase */
import { ConflictException, Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
import { PrismaService } from '@credebl/prisma-service';
import { ledgers, org_agents, org_agents_type, organisation, schema } from '@prisma/client';
import { ledgers, org_agents, org_agents_type, organisation, Prisma, schema } from '@prisma/client';
import { ISchema, ISchemaExist, ISchemaSearchCriteria, ISaveSchema } from '../interfaces/schema-payload.interface';
import { ResponseMessages } from '@credebl/common/response-messages';
import { AgentDetails, ISchemasWithCount } from '../interfaces/schema.interface';
Expand Down Expand Up @@ -36,7 +36,8 @@ export class SchemaRepository {
publisherDid: schemaResult.issuerId.split(':')[4] || schemaResult.issuerId,
orgId: schemaResult.orgId,
ledgerId: schemaResult.ledgerId,
type: schemaResult.type
type: schemaResult.type,
isSchemaArchived: false
}
});
return saveResult;
Expand Down Expand Up @@ -77,6 +78,7 @@ export class SchemaRepository {
return this.prisma.schema.findMany({
where: {
type: SchemaType.INDY,
isSchemaArchived: false,
name: {
contains: schemaName,
mode: 'insensitive'
Expand All @@ -98,6 +100,7 @@ export class SchemaRepository {
const schemasResult = await this.prisma.schema.findMany({
where: {
organisation: { id: orgId },
isSchemaArchived: false,
OR: [
{ name: { contains: payload.searchByText, mode: 'insensitive' } },
{ version: { contains: payload.searchByText, mode: 'insensitive' } },
Expand Down Expand Up @@ -176,6 +179,24 @@ export class SchemaRepository {
}
}

async archiveSchemasByDid(did: string): Promise<Prisma.BatchPayload> {
try {
const schemasResult = await this.prisma.schema.updateMany({
where: {
issuerId: did
},
data: {
isSchemaArchived: true
}
});

return schemasResult;
} catch (error) {
this.logger.error(`Error in archive schemas: ${error}`);
throw error;
}
}

async getAgentDetailsByOrgId(orgId: string): Promise<AgentDetails> {
try {
const schemasResult = await this.prisma.org_agents.findFirst({
Expand Down Expand Up @@ -275,6 +296,7 @@ export class SchemaRepository {
schemaResult = await this.prisma.schema.findMany({
where: {
ledgerId,
isSchemaArchived: false,
type: schemaType,
OR: [
{ name: { contains: searchByText, mode: 'insensitive' } },
Expand All @@ -289,6 +311,7 @@ export class SchemaRepository {
version: true,
attributes: true,
schemaLedgerId: true,
isSchemaArchived: true,
createdBy: true,
publisherDid: true,
orgId: true, // This field can be null
Expand All @@ -307,6 +330,7 @@ export class SchemaRepository {
schemaResult = await this.prisma.schema.findMany({
where: {
ledgerId,
isSchemaArchived: false,
type: schemaType
},
select: {
Expand All @@ -315,6 +339,7 @@ export class SchemaRepository {
version: true,
attributes: true,
schemaLedgerId: true,
isSchemaArchived: true,
createdBy: true,
publisherDid: true,
orgId: true, // This field can be null
Expand Down
7 changes: 6 additions & 1 deletion apps/ledger/src/schema/schema.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ISchemaSearchPayload,
SaveSchemaPayload
} from './interfaces/schema-payload.interface';
import { schema } from '@prisma/client';
import { Prisma, schema } from '@prisma/client';
import {
ICredDefWithPagination,
ISchemaData,
Expand Down Expand Up @@ -75,6 +75,11 @@ export class SchemaController {
return this.schemaService.schemaExist(payload);
}

@MessagePattern({ cmd: 'archive-schemas' })
async archiveSchemas(payload: {did: string}): Promise<Prisma.BatchPayload> {
return this.schemaService.archiveSchemas(payload.did);
}

@MessagePattern({ cmd: 'store-schema-record' })
async saveSchemaRecord(payload: SaveSchemaPayload): Promise<schema> {
return this.schemaService.storeSchemaDetails(payload.schemaDetails);
Expand Down
12 changes: 11 additions & 1 deletion apps/ledger/src/schema/schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { ClientProxy, RpcException } from '@nestjs/microservices';
import { BaseService } from 'libs/service/base.service';
import { SchemaRepository } from './repositories/schema.repository';
import { schema } from '@prisma/client';
import { Prisma, schema } from '@prisma/client';
import { ISaveSchema, ISchema, ISchemaCredDeffSearchInterface, ISchemaExist, ISchemaSearchCriteria, W3CCreateSchema } from './interfaces/schema-payload.interface';
import { ResponseMessages } from '@credebl/common/response-messages';
import { ICreateSchema, ICreateW3CSchema, IGenericSchema, IUserRequestInterface } from './interfaces/schema.interface';
Expand Down Expand Up @@ -54,6 +54,7 @@ export class SchemaService extends BaseService {
schema.schemaName,
schema.schemaVersion
);

if (0 !== schemaExists.length) {
this.logger.error(ResponseMessages.schema.error.exists);
throw new ConflictException(
Expand Down Expand Up @@ -886,6 +887,15 @@ export class SchemaService extends BaseService {
}
}

async archiveSchemas(did: string): Promise<Prisma.BatchPayload> {
try {
const schemaDetails = await this.schemaRepository.archiveSchemasByDid(did);
return schemaDetails;
} catch (error) {
this.logger.error(`Error in archive schemas: ${error}`);
throw new RpcException(error.response ? error.response : error);
}
}

async storeSchemaDetails(schemaDetails: ISaveSchema): Promise<schema> {
try {
Expand Down
7 changes: 5 additions & 2 deletions apps/organization/repositories/organization.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,12 @@ export class OrganizationRepository {
);

const schemasCount = await this.prisma.schema.count({
...query
where: {
orgId,
isSchemaArchived: false
}
});

const credentialsCount = await this.prisma.credentials.count({
...query
});
Expand Down
34 changes: 1 addition & 33 deletions apps/organization/src/organization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { CreateOrganizationDto } from '../dtos/create-organization.dto';
import { BulkSendInvitationDto } from '../dtos/send-invitation.dto';
import { UpdateInvitationDto } from '../dtos/update-invitation.dt';
import { DidMethod, Invitation, Ledgers, PrismaTables, transition } from '@credebl/enum/enum';
import { IGetOrgById, IGetOrganization, IUpdateOrganization, IOrgAgent, IClientCredentials, ICreateConnectionUrl, IOrgRole, IDidList, IPrimaryDidDetails, IEcosystemOrgStatus, IOrgDetails } from '../interfaces/organization.interface';
import { IGetOrgById, IGetOrganization, IUpdateOrganization, IClientCredentials, ICreateConnectionUrl, IOrgRole, IDidList, IPrimaryDidDetails, IEcosystemOrgStatus, IOrgDetails } from '../interfaces/organization.interface';
import { UserActivityService } from '@credebl/user-activity';
import { ClientRegistrationService } from '@credebl/client-registration/client-registration.service';
import { map } from 'rxjs/operators';
Expand Down Expand Up @@ -1659,38 +1659,6 @@ export class OrganizationService {
return isEmailSent;
}

async _deleteWallet(payload: IOrgAgent): Promise<{
response;
}> {
try {
const pattern = {
cmd: 'delete-wallet'
};

return this.organizationServiceProxy
.send<string>(pattern, payload)
.pipe(
map((response) => ({
response
}))
)
.toPromise()
.catch((error) => {
this.logger.error(`catch: ${JSON.stringify(error)}`);
throw new HttpException(
{
status: error.statusCode,
error: error.message
},
error.error
);
});
} catch (error) {
this.logger.error(`[_deleteWallet] - error in delete wallet : ${JSON.stringify(error)}`);
throw error;
}
}

async getUserKeycloakIdByEmail(userEmails: string[]): Promise<{
response;
}> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "schema" ADD COLUMN "isSchemaArchived" BOOLEAN NOT NULL DEFAULT false;
1 change: 1 addition & 0 deletions libs/prisma-service/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ model schema {
ledgers ledgers? @relation(fields: [ledgerId], references: [id])
ledgerId String? @db.Uuid
type String? @db.VarChar
isSchemaArchived Boolean @default(false)
credential_definition credential_definition[]
}

Expand Down