Skip to content

Commit

Permalink
Merge pull request #333 from credebl/329-major-critical-bugs
Browse files Browse the repository at this point in the history
fix: Validations in endorsement requests.
  • Loading branch information
KulkarniShashank authored Dec 6, 2023
2 parents 1049b6a + 6f714d4 commit 170a2fe
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
10 changes: 9 additions & 1 deletion apps/api-gateway/src/ecosystem/dtos/request-schema.dto.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;

Expand Down
36 changes: 19 additions & 17 deletions apps/api-gateway/src/ecosystem/ecosystem.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> {
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
Expand Down Expand Up @@ -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<Response> {
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' })
Expand Down
24 changes: 23 additions & 1 deletion apps/ecosystem/src/ecosystem.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions libs/common/src/response-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 170a2fe

Please sign in to comment.