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/schema.service.ts b/apps/ledger/src/schema/schema.service.ts index 3607d1a85..c31a0ac19 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,49 @@ 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) + ]); + + if (!getSchemaDetails) { + throw new NotFoundException(ResponseMessages.schema.error.notFound); + } + 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) {