diff --git a/apps/api-gateway/src/ecosystem/dtos/request-schema.dto.ts b/apps/api-gateway/src/ecosystem/dtos/request-schema.dto.ts index 641a16168..f39e29f0c 100644 --- a/apps/api-gateway/src/ecosystem/dtos/request-schema.dto.ts +++ b/apps/api-gateway/src/ecosystem/dtos/request-schema.dto.ts @@ -1,6 +1,9 @@ import { ApiExtraModels, ApiProperty } from '@nestjs/swagger'; -import { Type } from 'class-transformer'; +import { Transform, Type } from 'class-transformer'; import { IsArray, IsBoolean, IsNotEmpty, IsOptional, IsString, ValidateNested } from 'class-validator'; +import { trim } from '@credebl/common/cast.helper'; + + @ApiExtraModels() class AttributeValue { @@ -20,11 +23,16 @@ class AttributeValue { export class RequestSchemaDto { + @ApiProperty() + @Transform(({ value }) => trim(value)) + @IsNotEmpty({ message: 'Schema name is required' }) @IsString({ message: 'name must be in string format.' }) name: string; @ApiProperty() + @Transform(({ value }) => trim(value)) + @IsNotEmpty({ message: 'Schema version is required' }) @IsString({ message: 'version must be in string format.' }) version: string; diff --git a/apps/api-gateway/src/ecosystem/ecosystem.controller.ts b/apps/api-gateway/src/ecosystem/ecosystem.controller.ts index 9a8a6715b..fb477e265 100644 --- a/apps/api-gateway/src/ecosystem/ecosystem.controller.ts +++ b/apps/api-gateway/src/ecosystem/ecosystem.controller.ts @@ -282,6 +282,25 @@ export class EcosystemController { return res.status(HttpStatus.OK).json(finalResponse); } + @Post('/:ecosystemId/:orgId/transaction/schema') + @ApiOperation({ summary: 'Request new schema', description: 'Request new schema' }) + @ApiResponse({ status: 201, description: 'Success', type: ApiResponseDto }) + @UseGuards(AuthGuard('jwt'), EcosystemRolesGuard, OrgRolesGuard) + @ApiBearerAuth() + @EcosystemsRoles(EcosystemRoles.ECOSYSTEM_MEMBER, EcosystemRoles.ECOSYSTEM_LEAD, EcosystemRoles.ECOSYSTEM_OWNER) + @Roles(OrgRoles.OWNER, OrgRoles.ADMIN, OrgRoles.ISSUER) + async requestSchemaTransaction(@Body() requestSchemaPayload: RequestSchemaDto, @Param('orgId') orgId: string, @Param('ecosystemId') ecosystemId: string, @Res() res: Response, @User() user: user): Promise { + requestSchemaPayload.userId = user.id; + + await this.ecosystemService.schemaEndorsementRequest(requestSchemaPayload, orgId, ecosystemId); + const finalResponse: IResponseType = { + statusCode: HttpStatus.CREATED, + message: ResponseMessages.ecosystem.success.schemaRequest + }; + return res.status(HttpStatus.CREATED).json(finalResponse); + } + + /** * * @param createOrgDto @@ -309,23 +328,6 @@ export class EcosystemController { return res.status(HttpStatus.CREATED).json(finalResponse); } - @Post('/:ecosystemId/:orgId/transaction/schema') - @ApiOperation({ summary: 'Request new schema', description: 'Request new schema' }) - @ApiResponse({ status: 201, description: 'Success', type: ApiResponseDto }) - @UseGuards(AuthGuard('jwt'), EcosystemRolesGuard, OrgRolesGuard) - @ApiBearerAuth() - @EcosystemsRoles(EcosystemRoles.ECOSYSTEM_MEMBER) - @Roles(OrgRoles.OWNER, OrgRoles.ADMIN, OrgRoles.ISSUER) - async requestSchemaTransaction(@Body() requestSchemaPayload: RequestSchemaDto, @Param('orgId') orgId: string, @Param('ecosystemId') ecosystemId: string, @Res() res: Response, @User() user: user): Promise { - requestSchemaPayload.userId = user.id; - await this.ecosystemService.schemaEndorsementRequest(requestSchemaPayload, orgId, ecosystemId); - const finalResponse: IResponseType = { - statusCode: HttpStatus.CREATED, - message: ResponseMessages.ecosystem.success.schemaRequest - }; - return res.status(HttpStatus.CREATED).json(finalResponse); - } - @Post('/:ecosystemId/:orgId/transaction/cred-def') @ApiOperation({ summary: 'Request new credential-definition', description: 'Request new credential-definition' }) diff --git a/apps/ecosystem/src/ecosystem.service.ts b/apps/ecosystem/src/ecosystem.service.ts index 5e213796f..247ea5124 100644 --- a/apps/ecosystem/src/ecosystem.service.ts +++ b/apps/ecosystem/src/ecosystem.service.ts @@ -1,6 +1,6 @@ /* eslint-disable prefer-destructuring */ // eslint-disable-next-line camelcase -import { ConflictException, ForbiddenException, HttpException, Inject, Injectable, InternalServerErrorException, Logger, NotFoundException } from '@nestjs/common'; +import { BadRequestException, ConflictException, ForbiddenException, HttpException, Inject, Injectable, InternalServerErrorException, Logger, NotAcceptableException, NotFoundException } from '@nestjs/common'; import { EcosystemRepository } from './ecosystem.repository'; import { ResponseMessages } from '@credebl/common/response-messages'; import { BulkSendInvitationDto } from '../dtos/send-invitation.dto'; @@ -424,6 +424,28 @@ export class EcosystemService { try { const getEcosystemLeadDetails = await this.ecosystemRepository.getEcosystemLeadDetails(ecosystemId); + const {name, version} = requestSchemaPayload; + + if (0 === name.length) { + throw new BadRequestException(ResponseMessages.schema.error.nameNotEmpty); + } + + if (0 === version.length) { + throw new BadRequestException(ResponseMessages.schema.error.versionNotEmpty); + } + + const schemaVersionIndexOf = -1; + + if ( + isNaN(parseFloat(version)) || + version.toString().indexOf('.') === + schemaVersionIndexOf + ) { + throw new NotAcceptableException( + ResponseMessages.schema.error.invalidVersion + ); + } + const [schemaRequestExist, ecosystemMemberDetails, platformConfig, ecosystemLeadAgentDetails, getEcosystemOrgDetailsByOrgId] = await Promise.all([ this.ecosystemRepository.findRecordsByNameAndVersion(requestSchemaPayload?.name, requestSchemaPayload?.version), this.ecosystemRepository.getAgentDetails(orgId), diff --git a/libs/common/src/response-messages/index.ts b/libs/common/src/response-messages/index.ts index 8e29f1e89..98045a561 100644 --- a/libs/common/src/response-messages/index.ts +++ b/libs/common/src/response-messages/index.ts @@ -103,6 +103,8 @@ export const ResponseMessages = { }, error: { invalidSchemaId: 'Invalid schema Id provided.', + nameNotEmpty: 'Schema name is required', + versionNotEmpty: 'Schema version is required', invalidVersion: 'Invalid schema version provided.', insufficientAttributes: 'Please provide at least one attribute.', invalidAttributes: 'Please provide unique attributes',