Skip to content

Commit 203ba58

Browse files
feat: archive schema for deleted wallet (#1078)
* feat: archive schemas Signed-off-by: bhavanakarwade <[email protected]> * refactor: update function name Signed-off-by: bhavanakarwade <[email protected]> --------- Signed-off-by: bhavanakarwade <[email protected]>
1 parent 575de0f commit 203ba58

File tree

10 files changed

+94
-43
lines changed

10 files changed

+94
-43
lines changed

apps/agent-service/src/agent-service.service.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,11 @@ export class AgentServiceService {
17221722
{ records: deleteOrgAgent ? 1 : 0, tableName: 'org_agents' }
17231723
];
17241724

1725+
const did = orgAgent?.orgDid;
1726+
1727+
//archive schemas
1728+
await this._updateIsSchemaArchivedFlag(did);
1729+
17251730
const logDeletionActivity = async (records, tableName): Promise<void> => {
17261731
if (records) {
17271732
const txnMetadata = {
@@ -1745,6 +1750,30 @@ export class AgentServiceService {
17451750
}
17461751
}
17471752

1753+
async _updateIsSchemaArchivedFlag(did: string): Promise<{
1754+
count: number
1755+
}> {
1756+
const pattern = { cmd: 'archive-schemas' };
1757+
1758+
const payload = {
1759+
did
1760+
};
1761+
const updatedSchemaInfo = await this.agentServiceProxy
1762+
.send(pattern, payload)
1763+
.toPromise()
1764+
.catch((error) => {
1765+
this.logger.error(`catch: ${JSON.stringify(error)}`);
1766+
throw new HttpException(
1767+
{
1768+
status: error.status,
1769+
error: error.message
1770+
},
1771+
error.status
1772+
);
1773+
});
1774+
return updatedSchemaInfo;
1775+
}
1776+
17481777
async receiveInvitationUrl(receiveInvitationUrl: IReceiveInvitationUrl, url: string, orgId: string): Promise<string> {
17491778
try {
17501779
const getApiKey = await this.getOrgAgentApiKey(orgId);

apps/ledger/src/credential-definition/credential-definition.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,18 @@ export class CredentialDefinitionService extends BaseService {
341341

342342
return schemaResponse;
343343
}
344-
const credDefSchemaList: CredDefSchema[] =
344+
const credDefSchemaList =
345345
await this.credentialDefinitionRepository.getAllCredDefsByOrgIdForBulk(
346346
payload
347347
);
348+
348349
if (!credDefSchemaList) {
349350
throw new NotFoundException(ResponseMessages.credentialDefinition.error.NotFound);
350351
}
351-
return credDefSchemaList;
352+
353+
const activeCredDefSchemaList = credDefSchemaList.filter((item) => !item?.['isSchemaArchived']);
354+
355+
return activeCredDefSchemaList;
352356
} catch (error) {
353357
this.logger.error(
354358
`get Cred-Defs and schema List By OrgId for bulk operations: ${JSON.stringify(error)}`

apps/ledger/src/credential-definition/repositories/credential-definition.repository.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ export class CredentialDefinitionRepository {
243243
version: true,
244244
schemaLedgerId: true,
245245
orgId: true,
246-
attributes: true
246+
attributes: true,
247+
isSchemaArchived: true
247248
}
248249
});
249250

@@ -254,11 +255,13 @@ export class CredentialDefinitionRepository {
254255
if (matchingSchema) {
255256
return {
256257
credentialDefinitionId: credDef.credentialDefinitionId,
258+
credentialDefinition: credDef.tag,
257259
schemaCredDefName: `${matchingSchema.name}:${matchingSchema.version}-${credDef.tag}`,
258260
schemaName: matchingSchema.name,
259261
schemaVersion: matchingSchema.version,
260262
schemaAttributes: matchingSchema.attributes,
261-
credentialDefinition: credDef.tag
263+
schemaLedgerId: matchingSchema.schemaLedgerId,
264+
isSchemaArchived: matchingSchema.isSchemaArchived
262265
};
263266
}
264267
return null;
@@ -277,6 +280,7 @@ export class CredentialDefinitionRepository {
277280
return await this.prisma.schema.findMany({
278281
where: {
279282
orgId,
283+
isSchemaArchived: false,
280284
type: schemaType
281285
},
282286
select: {

apps/ledger/src/schema/repositories/schema.repository.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable camelcase */
22
import { ConflictException, Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
33
import { PrismaService } from '@credebl/prisma-service';
4-
import { ledgers, org_agents, org_agents_type, organisation, schema } from '@prisma/client';
4+
import { ledgers, org_agents, org_agents_type, organisation, Prisma, schema } from '@prisma/client';
55
import { ISchema, ISchemaExist, ISchemaSearchCriteria, ISaveSchema } from '../interfaces/schema-payload.interface';
66
import { ResponseMessages } from '@credebl/common/response-messages';
77
import { AgentDetails, ISchemasWithCount } from '../interfaces/schema.interface';
@@ -36,7 +36,8 @@ export class SchemaRepository {
3636
publisherDid: schemaResult.issuerId.split(':')[4] || schemaResult.issuerId,
3737
orgId: schemaResult.orgId,
3838
ledgerId: schemaResult.ledgerId,
39-
type: schemaResult.type
39+
type: schemaResult.type,
40+
isSchemaArchived: false
4041
}
4142
});
4243
return saveResult;
@@ -77,6 +78,7 @@ export class SchemaRepository {
7778
return this.prisma.schema.findMany({
7879
where: {
7980
type: SchemaType.INDY,
81+
isSchemaArchived: false,
8082
name: {
8183
contains: schemaName,
8284
mode: 'insensitive'
@@ -98,6 +100,7 @@ export class SchemaRepository {
98100
const schemasResult = await this.prisma.schema.findMany({
99101
where: {
100102
organisation: { id: orgId },
103+
isSchemaArchived: false,
101104
OR: [
102105
{ name: { contains: payload.searchByText, mode: 'insensitive' } },
103106
{ version: { contains: payload.searchByText, mode: 'insensitive' } },
@@ -176,6 +179,24 @@ export class SchemaRepository {
176179
}
177180
}
178181

182+
async archiveSchemasByDid(did: string): Promise<Prisma.BatchPayload> {
183+
try {
184+
const schemasResult = await this.prisma.schema.updateMany({
185+
where: {
186+
issuerId: did
187+
},
188+
data: {
189+
isSchemaArchived: true
190+
}
191+
});
192+
193+
return schemasResult;
194+
} catch (error) {
195+
this.logger.error(`Error in archive schemas: ${error}`);
196+
throw error;
197+
}
198+
}
199+
179200
async getAgentDetailsByOrgId(orgId: string): Promise<AgentDetails> {
180201
try {
181202
const schemasResult = await this.prisma.org_agents.findFirst({
@@ -275,6 +296,7 @@ export class SchemaRepository {
275296
schemaResult = await this.prisma.schema.findMany({
276297
where: {
277298
ledgerId,
299+
isSchemaArchived: false,
278300
type: schemaType,
279301
OR: [
280302
{ name: { contains: searchByText, mode: 'insensitive' } },
@@ -289,6 +311,7 @@ export class SchemaRepository {
289311
version: true,
290312
attributes: true,
291313
schemaLedgerId: true,
314+
isSchemaArchived: true,
292315
createdBy: true,
293316
publisherDid: true,
294317
orgId: true, // This field can be null
@@ -307,6 +330,7 @@ export class SchemaRepository {
307330
schemaResult = await this.prisma.schema.findMany({
308331
where: {
309332
ledgerId,
333+
isSchemaArchived: false,
310334
type: schemaType
311335
},
312336
select: {
@@ -315,6 +339,7 @@ export class SchemaRepository {
315339
version: true,
316340
attributes: true,
317341
schemaLedgerId: true,
342+
isSchemaArchived: true,
318343
createdBy: true,
319344
publisherDid: true,
320345
orgId: true, // This field can be null

apps/ledger/src/schema/schema.controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
ISchemaSearchPayload,
99
SaveSchemaPayload
1010
} from './interfaces/schema-payload.interface';
11-
import { schema } from '@prisma/client';
11+
import { Prisma, schema } from '@prisma/client';
1212
import {
1313
ICredDefWithPagination,
1414
ISchemaData,
@@ -75,6 +75,11 @@ export class SchemaController {
7575
return this.schemaService.schemaExist(payload);
7676
}
7777

78+
@MessagePattern({ cmd: 'archive-schemas' })
79+
async archiveSchemas(payload: {did: string}): Promise<Prisma.BatchPayload> {
80+
return this.schemaService.archiveSchemas(payload.did);
81+
}
82+
7883
@MessagePattern({ cmd: 'store-schema-record' })
7984
async saveSchemaRecord(payload: SaveSchemaPayload): Promise<schema> {
8085
return this.schemaService.storeSchemaDetails(payload.schemaDetails);

apps/ledger/src/schema/schema.service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { ClientProxy, RpcException } from '@nestjs/microservices';
1010
import { BaseService } from 'libs/service/base.service';
1111
import { SchemaRepository } from './repositories/schema.repository';
12-
import { schema } from '@prisma/client';
12+
import { Prisma, schema } from '@prisma/client';
1313
import { ISaveSchema, ISchema, ISchemaCredDeffSearchInterface, ISchemaExist, ISchemaSearchCriteria, W3CCreateSchema } from './interfaces/schema-payload.interface';
1414
import { ResponseMessages } from '@credebl/common/response-messages';
1515
import { ICreateSchema, ICreateW3CSchema, IGenericSchema, IUserRequestInterface } from './interfaces/schema.interface';
@@ -54,6 +54,7 @@ export class SchemaService extends BaseService {
5454
schema.schemaName,
5555
schema.schemaVersion
5656
);
57+
5758
if (0 !== schemaExists.length) {
5859
this.logger.error(ResponseMessages.schema.error.exists);
5960
throw new ConflictException(
@@ -886,6 +887,15 @@ export class SchemaService extends BaseService {
886887
}
887888
}
888889

890+
async archiveSchemas(did: string): Promise<Prisma.BatchPayload> {
891+
try {
892+
const schemaDetails = await this.schemaRepository.archiveSchemasByDid(did);
893+
return schemaDetails;
894+
} catch (error) {
895+
this.logger.error(`Error in archive schemas: ${error}`);
896+
throw new RpcException(error.response ? error.response : error);
897+
}
898+
}
889899

890900
async storeSchemaDetails(schemaDetails: ISaveSchema): Promise<schema> {
891901
try {

apps/organization/repositories/organization.repository.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,12 @@ export class OrganizationRepository {
500500
);
501501

502502
const schemasCount = await this.prisma.schema.count({
503-
...query
503+
where: {
504+
orgId,
505+
isSchemaArchived: false
506+
}
504507
});
505-
508+
506509
const credentialsCount = await this.prisma.credentials.count({
507510
...query
508511
});

apps/organization/src/organization.service.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { CreateOrganizationDto } from '../dtos/create-organization.dto';
2828
import { BulkSendInvitationDto } from '../dtos/send-invitation.dto';
2929
import { UpdateInvitationDto } from '../dtos/update-invitation.dt';
3030
import { DidMethod, Invitation, Ledgers, PrismaTables, transition } from '@credebl/enum/enum';
31-
import { IGetOrgById, IGetOrganization, IUpdateOrganization, IOrgAgent, IClientCredentials, ICreateConnectionUrl, IOrgRole, IDidList, IPrimaryDidDetails, IEcosystemOrgStatus, IOrgDetails } from '../interfaces/organization.interface';
31+
import { IGetOrgById, IGetOrganization, IUpdateOrganization, IClientCredentials, ICreateConnectionUrl, IOrgRole, IDidList, IPrimaryDidDetails, IEcosystemOrgStatus, IOrgDetails } from '../interfaces/organization.interface';
3232
import { UserActivityService } from '@credebl/user-activity';
3333
import { ClientRegistrationService } from '@credebl/client-registration/client-registration.service';
3434
import { map } from 'rxjs/operators';
@@ -1659,38 +1659,6 @@ export class OrganizationService {
16591659
return isEmailSent;
16601660
}
16611661

1662-
async _deleteWallet(payload: IOrgAgent): Promise<{
1663-
response;
1664-
}> {
1665-
try {
1666-
const pattern = {
1667-
cmd: 'delete-wallet'
1668-
};
1669-
1670-
return this.organizationServiceProxy
1671-
.send<string>(pattern, payload)
1672-
.pipe(
1673-
map((response) => ({
1674-
response
1675-
}))
1676-
)
1677-
.toPromise()
1678-
.catch((error) => {
1679-
this.logger.error(`catch: ${JSON.stringify(error)}`);
1680-
throw new HttpException(
1681-
{
1682-
status: error.statusCode,
1683-
error: error.message
1684-
},
1685-
error.error
1686-
);
1687-
});
1688-
} catch (error) {
1689-
this.logger.error(`[_deleteWallet] - error in delete wallet : ${JSON.stringify(error)}`);
1690-
throw error;
1691-
}
1692-
}
1693-
16941662
async getUserKeycloakIdByEmail(userEmails: string[]): Promise<{
16951663
response;
16961664
}> {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "schema" ADD COLUMN "isSchemaArchived" BOOLEAN NOT NULL DEFAULT false;

libs/prisma-service/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ model schema {
267267
ledgers ledgers? @relation(fields: [ledgerId], references: [id])
268268
ledgerId String? @db.Uuid
269269
type String? @db.VarChar
270+
isSchemaArchived Boolean @default(false)
270271
credential_definition credential_definition[]
271272
}
272273

0 commit comments

Comments
 (0)