Skip to content

Commit

Permalink
Merge pull request #543 from credebl/endorsement-schema-exist
Browse files Browse the repository at this point in the history
fix: check the schema if exist or not in endorsement
  • Loading branch information
pranalidhanavade authored Feb 26, 2024
2 parents c3d5fc8 + 3d0e590 commit a3fc7b1
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 100 deletions.
55 changes: 42 additions & 13 deletions apps/ecosystem/src/ecosystem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ import { GetAllSchemaList, GetEndorsementsPayload } from '../interfaces/endorsem
import { CommonConstants } from '@credebl/common/common.constant';
// eslint-disable-next-line camelcase
import {
// eslint-disable-next-line camelcase
// eslint-disable-next-line camelcase
credential_definition,
// eslint-disable-next-line camelcase
// eslint-disable-next-line camelcase
endorsement_transaction,
// eslint-disable-next-line camelcase
// eslint-disable-next-line camelcase
org_agents,
// eslint-disable-next-line camelcase
// eslint-disable-next-line camelcase
platform_config,
schema,
user
Expand Down Expand Up @@ -679,9 +679,15 @@ export class EcosystemService {
ecosystemId: string
): Promise<IEndorsementTransaction> {
try {
const getEcosystemLeadDetails = await this.ecosystemRepository.getEcosystemLeadDetails(ecosystemId);

const { name, version } = requestSchemaPayload;
const alreadySchemaExist = await this._schemaExist(version, name);
this.logger.log(`alreadySchemaExist ::: ${JSON.stringify(alreadySchemaExist)}`);

if (alreadySchemaExist) {
throw new BadRequestException(ResponseMessages.ecosystem.error.schemaAlreadyExist);
}

const getEcosystemLeadDetails = await this.ecosystemRepository.getEcosystemLeadDetails(ecosystemId);

if (0 === name.length) {
throw new BadRequestException(ResponseMessages.schema.error.nameNotEmpty);
Expand Down Expand Up @@ -711,7 +717,8 @@ export class EcosystemService {
this.ecosystemRepository.getEcosystemOrgDetailsbyId(orgId, ecosystemId)
]);

const existSchema = schemaRequestExist?.filter(
const existSchema =
schemaRequestExist?.filter(
(schema) => schema.status === endorsementTransactionStatus.REQUESTED ||
schema.status === endorsementTransactionStatus.SIGNED ||
schema.status === endorsementTransactionStatus.SUBMITED
Expand Down Expand Up @@ -803,6 +810,26 @@ export class EcosystemService {
}
}

async _schemaExist(version: string, schemaName: string): Promise<string> {
const pattern = { cmd: 'schema-exist' };
const payload = { version, schemaName };

try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const message = await this.ecosystemServiceProxy.send<any>(pattern, payload).toPromise();
return message;
} catch (error) {
this.logger.error(`catch: ${JSON.stringify(error)}`);
throw new HttpException(
{
status: error.status,
error: error.message
},
error.status
);
}
}

async requestCredDeffEndorsement(
requestCredDefPayload: RequestCredDeffEndorsement,
orgId: string,
Expand All @@ -824,11 +851,13 @@ export class EcosystemService {
this.ecosystemRepository.getAgentDetails(getEcosystemLeadDetails.orgId),
this.ecosystemRepository.getEcosystemOrgDetailsbyId(orgId, ecosystemId)
]);

const existsCredDef = credDefRequestExist?.filter(tag => tag.status === endorsementTransactionStatus.REQUESTED ||
tag.status === endorsementTransactionStatus.SIGNED ||
tag.status === endorsementTransactionStatus.SUBMITED
) ?? [];

const existsCredDef =
credDefRequestExist?.filter(
(tag) => tag.status === endorsementTransactionStatus.REQUESTED ||
tag.status === endorsementTransactionStatus.SIGNED ||
tag.status === endorsementTransactionStatus.SUBMITED
) ?? [];

if (0 < existsCredDef.length) {
throw new ConflictException(ResponseMessages.ecosystem.error.credDefAlreadyExist);
Expand Down Expand Up @@ -878,7 +907,7 @@ export class EcosystemService {
// To return selective response
await this.removeEndorsementTransactionFields(storeTransaction);

await new Promise(resolve => setTimeout(resolve, 5000));
await new Promise((resolve) => setTimeout(resolve, 5000));
return storeTransaction;
} else {
const orgAgentType = await this.ecosystemRepository.getOrgAgentType(ecosystemMemberDetails.orgAgentTypeId);
Expand Down
5 changes: 5 additions & 0 deletions apps/ledger/src/schema/interfaces/schema-payload.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ export interface ISchemaCredDeffSearchInterface {
user: IUserRequestInterface,
}

export interface ISchemaExist {
schemaName: string;
version: string;
}

96 changes: 55 additions & 41 deletions apps/ledger/src/schema/repositories/schema.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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 { ISchema, ISchemaSearchCriteria } from '../interfaces/schema-payload.interface';
import { ISchema, ISchemaExist, ISchemaSearchCriteria } from '../interfaces/schema-payload.interface';
import { ResponseMessages } from '@credebl/common/response-messages';
import { AgentDetails, ISchemasWithCount } from '../interfaces/schema.interface';
import { SortValue } from '@credebl/enum/enum';
Expand All @@ -12,23 +12,18 @@ import { ICredDefWithCount } from '@credebl/common/interfaces/schema.interface';
export class SchemaRepository {
private readonly logger = new Logger('SchemaRepository');

constructor(
private prisma: PrismaService
) { }
constructor(private prisma: PrismaService) {}
async saveSchema(schemaResult: ISchema): Promise<schema> {
try {
if (schemaResult.schema.schemaName) {
const schema = await this.schemaExists(
schemaResult.schema.schemaName,
schemaResult.schema.schemaVersion
);
const schema = await this.schemaExists(schemaResult.schema.schemaName, schemaResult.schema.schemaVersion);

const schemaLength = 0;
if (schema.length !== schemaLength) {
throw new ConflictException(
ResponseMessages.schema.error.exists,
{ cause: new Error(), description: ResponseMessages.errorMessages.conflict }
);
if (schema.length !== schemaLength) {
throw new ConflictException(ResponseMessages.schema.error.exists, {
cause: new Error(),
description: ResponseMessages.errorMessages.conflict
});
}
const saveResult = await this.prisma.schema.create({
data: {
Expand Down Expand Up @@ -96,7 +91,7 @@ export class SchemaRepository {
issuerId: true
},
orderBy: {
[payload.sortField]: SortValue.ASC === payload.sortBy ? 'asc' : 'desc'
[payload.sortField]: SortValue.ASC === payload.sortBy ? 'asc' : 'desc'
},
take: Number(payload.pageSize),
skip: (payload.pageNumber - 1) * payload.pageSize
Expand All @@ -111,11 +106,10 @@ export class SchemaRepository {
return { schemasCount, schemasResult };
} catch (error) {
this.logger.error(`Error in getting schemas: ${error}`);
throw new InternalServerErrorException(
ResponseMessages.schema.error.failedFetchSchema,
{ cause: new Error(), description: error.message }
);

throw new InternalServerErrorException(ResponseMessages.schema.error.failedFetchSchema, {
cause: new Error(),
description: error.message
});
}
}

Expand All @@ -138,11 +132,13 @@ export class SchemaRepository {
}
}

async getAgentType(orgId: string): Promise<organisation & {
org_agents: (org_agents & {
org_agent_type: org_agents_type;
})[];
}> {
async getAgentType(orgId: string): Promise<
organisation & {
org_agents: (org_agents & {
org_agent_type: org_agents_type;
})[];
}
> {
try {
const agentDetails = await this.prisma.organisation.findUnique({
where: {
Expand All @@ -164,16 +160,12 @@ export class SchemaRepository {
}

async getSchemasCredDeffList(payload: ISchemaSearchCriteria): Promise<ICredDefWithCount> {
const { orgId, schemaId } = payload;

const {orgId, schemaId} = payload;

try {
const credDefResult = await this.prisma.credential_definition.findMany({
where: {
AND: [
{ orgId },
{ schemaLedgerId: schemaId }
]
AND: [{ orgId }, { schemaLedgerId: schemaId }]
},
select: {
tag: true,
Expand All @@ -190,10 +182,7 @@ export class SchemaRepository {
});
const credDefCount = await this.prisma.credential_definition.count({
where: {
AND: [
{ orgId },
{ schemaLedgerId: schemaId }
]
AND: [{ orgId }, { schemaLedgerId: schemaId }]
}
});
return { credDefResult, credDefCount };
Expand All @@ -202,7 +191,7 @@ export class SchemaRepository {
throw error;
}
}

async getAllSchemaDetails(payload: ISchemaSearchCriteria): Promise<{
schemasCount: number;
schemasResult: {
Expand All @@ -216,7 +205,7 @@ export class SchemaRepository {
issuerId: string;
orgId: string;
}[];
}> {
}> {
try {
const schemasResult = await this.prisma.schema.findMany({
where: {
Expand All @@ -240,7 +229,7 @@ export class SchemaRepository {
issuerId: true
},
orderBy: {
[payload.sortField]: 'desc' === payload.sortBy ? 'desc' : 'asc'
[payload.sortField]: 'desc' === payload.sortBy ? 'desc' : 'asc'
},
take: Number(payload.pageSize),
skip: (payload.pageNumber - 1) * payload.pageSize
Expand All @@ -265,7 +254,6 @@ export class SchemaRepository {
schemaLedgerId: schemaId
}
});

} catch (error) {
this.logger.error(`Error in getting get schema by schema ledger id: ${error}`);
throw error;
Expand All @@ -274,7 +262,6 @@ export class SchemaRepository {

async getOrgAgentType(orgAgentId: string): Promise<string> {
try {

const { agent } = await this.prisma.org_agents_type.findFirst({
where: {
id: orgAgentId
Expand All @@ -295,10 +282,37 @@ export class SchemaRepository {
indyNamespace: LedgerName
}
});

} catch (error) {
this.logger.error(`Error in getting get schema by schema ledger id: ${error}`);
throw error;
}
}
}

async schemaExist(payload: ISchemaExist): Promise<{
id: string;
createDateTime: Date;
createdBy: string;
lastChangedDateTime: Date;
lastChangedBy: string;
name: string;
version: string;
attributes: string;
schemaLedgerId: string;
publisherDid: string;
issuerId: string;
orgId: string;
ledgerId: string;
}> {
try {
return this.prisma.schema.findFirstOrThrow({
where: {
name: payload.schemaName,
version: payload.version
}
});
} catch (error) {
this.logger.error(`Error in getting get schema by name and version: ${error}`);
throw error;
}
}
}
Loading

0 comments on commit a3fc7b1

Please sign in to comment.