From 3c6fb147f30a66c68f89e385226bb06298f68cce Mon Sep 17 00:00:00 2001 From: bhavanakarwade Date: Tue, 25 Jun 2024 13:44:40 +0530 Subject: [PATCH 1/2] feat: get w3c schema details by schema id Signed-off-by: bhavanakarwade --- .../src/schema/schema.controller.ts | 3 +- .../schema/repositories/schema.repository.ts | 2 +- apps/ledger/src/schema/schema.service.ts | 60 +++++++++++-------- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/apps/api-gateway/src/schema/schema.controller.ts b/apps/api-gateway/src/schema/schema.controller.ts index d3ec7187f..4d5bcf75e 100644 --- a/apps/api-gateway/src/schema/schema.controller.ts +++ b/apps/api-gateway/src/schema/schema.controller.ts @@ -20,6 +20,7 @@ import { OrgRolesGuard } from '../authz/guards/org-roles.guard'; import { GenericSchemaDTO } from '../dtos/create-schema.dto'; import { CustomExceptionFilter } from 'apps/api-gateway/common/exception-handler'; import { CredDefSortFields, SortFields } from '@credebl/enum/enum'; +import { TrimStringParamPipe } from '@credebl/common/cast.helper'; @UseFilters(CustomExceptionFilter) @Controller('orgs') @@ -43,7 +44,7 @@ export class SchemaController { async getSchemaById( @Res() res: Response, @Param('orgId', new ParseUUIDPipe({exceptionFactory: (): Error => { throw new BadRequestException(ResponseMessages.organisation.error.invalidOrgId); }})) orgId: string, - @Param('schemaId') schemaId: string + @Param('schemaId', TrimStringParamPipe) schemaId: string ): Promise { if (!schemaId) { diff --git a/apps/ledger/src/schema/repositories/schema.repository.ts b/apps/ledger/src/schema/repositories/schema.repository.ts index b582acc8a..26ab1f3a2 100644 --- a/apps/ledger/src/schema/repositories/schema.repository.ts +++ b/apps/ledger/src/schema/repositories/schema.repository.ts @@ -248,7 +248,7 @@ export class SchemaRepository { async getSchemaBySchemaId(schemaId: string): Promise { try { - return this.prisma.schema.findFirst({ + return this.prisma.schema.findFirstOrThrow({ where: { schemaLedgerId: schemaId } diff --git a/apps/ledger/src/schema/schema.service.ts b/apps/ledger/src/schema/schema.service.ts index 3607d1a85..c6d793f3d 100644 --- a/apps/ledger/src/schema/schema.service.ts +++ b/apps/ledger/src/schema/schema.service.ts @@ -5,7 +5,8 @@ import { Inject, ConflictException, Injectable, - NotAcceptableException, NotFoundException + NotAcceptableException, NotFoundException, + ForbiddenException } from '@nestjs/common'; import { ClientProxy, RpcException } from '@nestjs/microservices'; import { BaseService } from 'libs/service/base.service'; @@ -667,34 +668,45 @@ export class SchemaService extends BaseService { async getSchemaById(schemaId: string, orgId: string): Promise { try { - const { agentEndPoint } = await this.schemaRepository.getAgentDetailsByOrgId(orgId); - const getAgentDetails = await this.schemaRepository.getAgentType(orgId); + const [{agentEndPoint}, getAgentDetails, getSchemaDetails] = await Promise.all([ + this.schemaRepository.getAgentDetailsByOrgId(orgId), + this.schemaRepository.getAgentType(orgId), + this.schemaRepository.getSchemaBySchemaId(schemaId) + ]); + const orgAgentType = await this.schemaRepository.getOrgAgentType(getAgentDetails.org_agents[0].orgAgentTypeId); + if (getSchemaDetails?.orgId !== orgId) { + throw new ForbiddenException(ResponseMessages.organisation.error.orgNotMatch); + } let schemaResponse; - if (OrgAgentType.DEDICATED === orgAgentType) { - const getSchemaPayload = { - schemaId, - orgId, - agentEndPoint, - agentType: OrgAgentType.DEDICATED - }; - schemaResponse = await this._getSchemaById(getSchemaPayload); - } else if (OrgAgentType.SHARED === orgAgentType) { - const { tenantId } = await this.schemaRepository.getAgentDetailsByOrgId(orgId); - const getSchemaPayload = { - tenantId, - method: 'getSchemaById', - payload: { schemaId }, - agentType: OrgAgentType.SHARED, - agentEndPoint, - orgId - }; - schemaResponse = await this._getSchemaById(getSchemaPayload); + if (getSchemaDetails?.type === SchemaType.INDY) { + if (OrgAgentType.DEDICATED === orgAgentType) { + const getSchemaPayload = { + schemaId, + orgId, + agentEndPoint, + agentType: OrgAgentType.DEDICATED + }; + schemaResponse = await this._getSchemaById(getSchemaPayload); + } else if (OrgAgentType.SHARED === orgAgentType) { + const { tenantId } = await this.schemaRepository.getAgentDetailsByOrgId(orgId); + const getSchemaPayload = { + tenantId, + method: 'getSchemaById', + payload: { schemaId }, + agentType: OrgAgentType.SHARED, + agentEndPoint, + orgId + }; + schemaResponse = await this._getSchemaById(getSchemaPayload); + } + return schemaResponse.response; + } else if (getSchemaDetails?.type === SchemaType.W3C_Schema) { + return getSchemaDetails; } - return schemaResponse.response; - + } catch (error) { this.logger.error(`Error in getting schema by id: ${error}`); if (error && error?.status && error?.status?.message && error?.status?.message?.error) { From f0cb8e184f045cf84763e9c84431a27bde083117 Mon Sep 17 00:00:00 2001 From: bhavanakarwade Date: Tue, 25 Jun 2024 16:45:20 +0530 Subject: [PATCH 2/2] refactor: applied validations Signed-off-by: bhavanakarwade --- apps/ledger/src/schema/repositories/schema.repository.ts | 2 +- apps/ledger/src/schema/schema.service.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/ledger/src/schema/repositories/schema.repository.ts b/apps/ledger/src/schema/repositories/schema.repository.ts index 26ab1f3a2..b582acc8a 100644 --- a/apps/ledger/src/schema/repositories/schema.repository.ts +++ b/apps/ledger/src/schema/repositories/schema.repository.ts @@ -248,7 +248,7 @@ export class SchemaRepository { async getSchemaBySchemaId(schemaId: string): Promise { try { - return this.prisma.schema.findFirstOrThrow({ + return this.prisma.schema.findFirst({ where: { schemaLedgerId: schemaId } diff --git a/apps/ledger/src/schema/schema.service.ts b/apps/ledger/src/schema/schema.service.ts index c6d793f3d..c31a0ac19 100644 --- a/apps/ledger/src/schema/schema.service.ts +++ b/apps/ledger/src/schema/schema.service.ts @@ -673,6 +673,10 @@ export class SchemaService extends BaseService { this.schemaRepository.getAgentType(orgId), this.schemaRepository.getSchemaBySchemaId(schemaId) ]); + + if (!getSchemaDetails) { + throw new NotFoundException(ResponseMessages.schema.error.notFound); + } const orgAgentType = await this.schemaRepository.getOrgAgentType(getAgentDetails.org_agents[0].orgAgentTypeId);