Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validation on pagination #433

Merged
merged 10 commits into from
Jan 14, 2024

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiExtraModels, ApiProperty } from '@nestjs/swagger';
import { IsArray, IsEmail, IsNotEmpty, IsString, ValidateNested } from 'class-validator';
import { IsArray, IsEmail, IsNotEmpty, IsString, IsUUID, ValidateNested } from 'class-validator';
import { Transform, Type } from 'class-transformer';
import { trim } from '@credebl/common/cast.helper';

Expand All @@ -16,6 +16,7 @@ export class SendInvitationDto {
@ApiProperty({ example: [2, 1, 3] })
@IsNotEmpty({ message: 'Please provide valid orgRoleId' })
@IsArray()
@IsUUID('4', { each: true, message: 'Invalid format of orgRoleId' })
orgRoleId: string[];

}
Expand Down
14 changes: 9 additions & 5 deletions apps/api-gateway/src/organization/dtos/update-user-roles.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsArray, IsNotEmpty} from 'class-validator';
import { ArrayNotEmpty, IsArray, IsNotEmpty, IsUUID, MinLength} from 'class-validator';

import { ApiProperty } from '@nestjs/swagger';

Expand All @@ -8,10 +8,14 @@ export class UpdateUserRolesDto {
userId: string;

@ApiProperty({
example: [2, 1, 3]
example: ['2', '1', '3']
})

// @IsNotEmpty()
@IsArray()
@IsNotEmpty({ message: 'orgRoleId is required' })
@ArrayNotEmpty()
@MinLength(0, {each: true})
@IsNotEmpty({each: true})
@IsUUID('4', { each: true, message: 'Invalid format of orgRoleId' })
orgRoleId: string[];

}
}
36 changes: 20 additions & 16 deletions apps/api-gateway/src/organization/organization.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiBearerAuth, ApiForbiddenResponse, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { ApiBearerAuth, ApiExcludeEndpoint, ApiForbiddenResponse, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { CommonService } from '@credebl/common';
import { Controller, Get, Put, Param, UseGuards, UseFilters, Post, Body, Res, HttpStatus, Query, Delete, ParseUUIDPipe, BadRequestException } from '@nestjs/common';
import { OrganizationService } from './organization.service';
Expand All @@ -17,13 +17,11 @@ import { OrgRolesGuard } from '../authz/guards/org-roles.guard';
import { Roles } from '../authz/decorators/roles.decorator';
import { OrgRoles } from 'libs/org-roles/enums';
import { UpdateUserRolesDto } from './dtos/update-user-roles.dto';
import { GetAllOrganizationsDto } from './dtos/get-all-organizations.dto';
import { GetAllSentInvitationsDto } from './dtos/get-all-sent-invitations.dto';
import { UpdateOrganizationDto } from './dtos/update-organization-dto';
import { CustomExceptionFilter } from 'apps/api-gateway/common/exception-handler';
import { IUserRequestInterface } from '../interfaces/IUserRequestInterface';
import { GetAllUsersDto } from '../user/dto/get-all-users.dto';
import { ImageServiceService } from '@credebl/image-service';
import { PaginationDto } from '@credebl/common/dtos/pagination.dto';

@UseFilters(CustomExceptionFilter)
@Controller('orgs')
Expand Down Expand Up @@ -78,9 +76,9 @@ export class OrganizationController {
type: String,
required: false
})
async get(@Query() getAllUsersDto: GetAllOrganizationsDto, @Res() res: Response): Promise<Response> {
async get(@Query() paginationDto: PaginationDto, @Res() res: Response): Promise<Response> {

const users = await this.organizationService.getPublicOrganizations(getAllUsersDto);
const users = await this.organizationService.getPublicOrganizations(paginationDto);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.organisation.success.getOrganizations,
Expand Down Expand Up @@ -194,9 +192,9 @@ export class OrganizationController {
required: false
})
@Roles(OrgRoles.OWNER, OrgRoles.SUPER_ADMIN, OrgRoles.ADMIN, OrgRoles.ISSUER, OrgRoles.VERIFIER, OrgRoles.MEMBER)
async getInvitationsByOrgId(@Param('orgId') orgId: string, @Query() getAllInvitationsDto: GetAllSentInvitationsDto, @Res() res: Response): Promise<Response> {
async getInvitationsByOrgId(@Param('orgId') orgId: string, @Query() paginationDto: PaginationDto, @Res() res: Response): Promise<Response> {

const getInvitationById = await this.organizationService.getInvitationsByOrgId(orgId, getAllInvitationsDto);
const getInvitationById = await this.organizationService.getInvitationsByOrgId(orgId, paginationDto);

const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
Expand Down Expand Up @@ -230,9 +228,9 @@ export class OrganizationController {
type: String,
required: false
})
async getOrganizations(@Query() getAllOrgsDto: GetAllOrganizationsDto, @Res() res: Response, @User() reqUser: user): Promise<Response> {
async getOrganizations(@Query() paginationDto: PaginationDto, @Res() res: Response, @User() reqUser: user): Promise<Response> {

const getOrganizations = await this.organizationService.getOrganizations(getAllOrgsDto, reqUser.id);
const getOrganizations = await this.organizationService.getOrganizations(paginationDto, reqUser.id);

const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
Expand Down Expand Up @@ -290,8 +288,8 @@ export class OrganizationController {
type: String,
required: false
})
async getOrganizationUsers(@User() user: IUserRequestInterface, @Query() getAllUsersDto: GetAllUsersDto, @Param('orgId') orgId: string, @Res() res: Response): Promise<Response> {
const users = await this.organizationService.getOrgUsers(orgId, getAllUsersDto);
async getOrganizationUsers(@User() user: IUserRequestInterface, @Query() paginationDto: PaginationDto, @Param('orgId') orgId: string, @Res() res: Response): Promise<Response> {
const users = await this.organizationService.getOrgUsers(orgId, paginationDto);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.fetchUsers,
Expand Down Expand Up @@ -349,7 +347,7 @@ export class OrganizationController {
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
@ApiOperation({ summary: 'Update user roles', description: 'update user roles' })
async updateUserRoles(@Body() updateUserDto: UpdateUserRolesDto, @Param('orgId') orgId: string, @Param('userId') userId: string, @Res() res: Response): Promise<Response> {
async updateUserRoles(@Body() updateUserDto: UpdateUserRolesDto, @Param('orgId') orgId: string, @Param('userId', new ParseUUIDPipe({exceptionFactory: (): Error => { throw new BadRequestException(ResponseMessages.organisation.error.invalidUserId); }})) userId: string, @Res() res: Response): Promise<Response> {

updateUserDto.orgId = orgId;
updateUserDto.userId = userId;
Expand Down Expand Up @@ -386,12 +384,17 @@ export class OrganizationController {
/**
* @returns Boolean
*/
//Todo
@Delete('/:orgId')
@ApiOperation({ summary: 'Delete Organization', description: 'Delete an organization' })
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
@ApiExcludeEndpoint()
@ApiResponse({ status: HttpStatus.ACCEPTED, description: 'Success', type: ApiResponseDto })
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
async deleteOrganization(@Param('orgId') orgId: number, @Res() res: Response): Promise<Response> {
async deleteOrganization(
@Param('orgId') orgId: string,
@Res() res: Response
): Promise<Response> {

await this.organizationService.deleteOrganization(orgId);

Expand All @@ -402,6 +405,7 @@ export class OrganizationController {
return res.status(HttpStatus.ACCEPTED).json(finalResponse);
}


@Delete('/:orgId/invitations/:invitationId')
@ApiOperation({ summary: 'Delete organization invitation', description: 'Delete organization invitation' })
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
Expand All @@ -410,7 +414,7 @@ export class OrganizationController {
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
async deleteOrganizationInvitation(
@Param('orgId') orgId: string,
@Param('invitationId') invitationId: string,
@Param('invitationId', new ParseUUIDPipe({exceptionFactory: (): Error => { throw new BadRequestException(ResponseMessages.organisation.error.invalidInvitationId); }})) invitationId: string,
@Res() res: Response
): Promise<Response> {
await this.organizationService.deleteOrganizationInvitation(orgId, invitationId);
Expand Down
22 changes: 10 additions & 12 deletions apps/api-gateway/src/organization/organization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import { Injectable } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { BaseService } from 'libs/service/base.service';
import { CreateOrganizationDto } from './dtos/create-organization-dto';
import { GetAllOrganizationsDto } from './dtos/get-all-organizations.dto';
import { GetAllSentInvitationsDto } from './dtos/get-all-sent-invitations.dto';
import { BulkSendInvitationDto } from './dtos/send-invitation.dto';
import { UpdateUserRolesDto } from './dtos/update-user-roles.dto';
import { UpdateOrganizationDto } from './dtos/update-organization-dto';
import { GetAllUsersDto } from '../user/dto/get-all-users.dto';
import { IOrgRoles } from 'libs/org-roles/interfaces/org-roles.interface';
import { organisation } from '@prisma/client';
import { IGetOrgById, IGetOrganization } from 'apps/organization/interfaces/organization.interface';
import { IOrganizationInvitations } from '@credebl/common/interfaces/organizations.interface';
import { IOrgUsers } from 'apps/user/interfaces/user.interface';
import { IOrganizationDashboard } from '@credebl/common/interfaces/organization.interface';
import { PaginationDto } from '@credebl/common/dtos/pagination.dto';

@Injectable()
export class OrganizationService extends BaseService {
Expand Down Expand Up @@ -48,8 +46,8 @@ export class OrganizationService extends BaseService {
* @returns Organizations details
*/

async getOrganizations(getAllOrgsDto: GetAllOrganizationsDto, userId: string): Promise<IGetOrganization> {
const payload = { userId, ...getAllOrgsDto };
async getOrganizations(paginationDto: PaginationDto, userId: string): Promise<IGetOrganization> {
const payload = { userId, ...paginationDto };
const fetchOrgs = await this.sendNatsMessage(this.serviceProxy, 'get-organizations', payload);
return fetchOrgs;
}
Expand All @@ -59,8 +57,8 @@ export class OrganizationService extends BaseService {
* @param
* @returns Public organizations list
*/
async getPublicOrganizations(getAllOrgsDto: GetAllOrganizationsDto): Promise<IGetOrganization> {
const payload = { ...getAllOrgsDto };
async getPublicOrganizations(paginationDto: PaginationDto): Promise<IGetOrganization> {
const payload = { ...paginationDto };
const PublicOrg = this.sendNatsMessage(this.serviceProxy, 'get-public-organizations', payload);
return PublicOrg;
}
Expand Down Expand Up @@ -91,9 +89,9 @@ export class OrganizationService extends BaseService {
*/
async getInvitationsByOrgId(
orgId: string,
getAllInvitationsDto: GetAllSentInvitationsDto
pagination: PaginationDto
): Promise<IOrganizationInvitations> {
const { pageNumber, pageSize, search } = getAllInvitationsDto;
const { pageNumber, pageSize, search } = pagination;
const payload = { orgId, pageNumber, pageSize, search };
return this.sendNatsMessage(this.serviceProxy, 'get-invitations-by-orgId', payload);
}
Expand Down Expand Up @@ -137,9 +135,9 @@ export class OrganizationService extends BaseService {

async getOrgUsers(
orgId: string,
getAllUsersDto: GetAllUsersDto
paginationDto: PaginationDto
): Promise<IOrgUsers> {
const { pageNumber, pageSize, search } = getAllUsersDto;
const { pageNumber, pageSize, search } = paginationDto;
const payload = { orgId, pageNumber, pageSize, search };

return this.sendNatsMessage(this.serviceProxy, 'fetch-organization-user', payload);
Expand All @@ -154,7 +152,7 @@ export class OrganizationService extends BaseService {
}

async deleteOrganization(
orgId: number
orgId: string
): Promise<boolean> {
const payload = { orgId };

Expand Down
25 changes: 4 additions & 21 deletions apps/api-gateway/src/user/dto/get-all-invitations.dto.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import { IsOptional, IsString } from 'class-validator';
import { Transform, Type } from 'class-transformer';
import { toNumber } from '@credebl/common/cast.helper';
import { IsOptional, IsString} from 'class-validator';


import { ApiProperty } from '@nestjs/swagger';
import { Invitation } from '@credebl/enum/enum';
import { PaginationDto } from '@credebl/common/dtos/pagination.dto';

export class GetAllInvitationsDto {
@ApiProperty({ required: false })
@IsOptional()
@Type(() => Number)
@Transform(({ value }) => toNumber(value))
pageNumber = 1;

@ApiProperty({ required: false })
@IsOptional()
@Type(() => String)
search = '';

@ApiProperty({ required: false })
@IsOptional()
@Type(() => Number)
@Transform(({ value }) => toNumber(value))
pageSize = 8;

export class GetAllInvitationsDto extends PaginationDto {
@ApiProperty({ required: false })
@IsOptional()
@IsString()
Expand Down
26 changes: 0 additions & 26 deletions apps/api-gateway/src/user/dto/get-all-users.dto.ts

This file was deleted.

8 changes: 4 additions & 4 deletions apps/api-gateway/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { AcceptRejectInvitationDto } from './dto/accept-reject-invitation.dto';
import { Invitation } from '@credebl/enum/enum';
import { IUserRequestInterface } from './interfaces';
import { GetAllInvitationsDto } from './dto/get-all-invitations.dto';
import { GetAllUsersDto } from './dto/get-all-users.dto';
import { UpdateUserProfileDto } from './dto/update-user-profile.dto';
import { CustomExceptionFilter } from 'apps/api-gateway/common/exception-handler';
import { AddPasskeyDetailsDto } from './dto/add-user.dto';
Expand All @@ -50,6 +49,7 @@ import { OrgRolesGuard } from '../authz/guards/org-roles.guard';
import { OrgRoles } from 'libs/org-roles/enums';
import { CreateUserCertificateDto } from './dto/share-certificate.dto';
import { AwsService } from '@credebl/aws/aws.service';
import { PaginationDto } from '@credebl/common/dtos/pagination.dto';

@UseFilters(CustomExceptionFilter)
@Controller('users')
Expand Down Expand Up @@ -91,10 +91,10 @@ export class UserController {
})
async get(
@User() user: IUserRequestInterface,
@Query() getAllUsersDto: GetAllUsersDto,
@Query() paginationDto: PaginationDto,
@Res() res: Response
): Promise<Response> {
const users = await this.userService.get(getAllUsersDto);
const users = await this.userService.get(paginationDto);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.user.success.fetchUsers,
Expand Down Expand Up @@ -224,7 +224,7 @@ export class UserController {
required: false
})
async invitations(
@Query() getAllInvitationsDto: GetAllInvitationsDto,
@Query() getAllInvitationsDto: GetAllInvitationsDto,
@User() reqUser: user,
@Res() res: Response
): Promise<Response> {
Expand Down
6 changes: 3 additions & 3 deletions apps/api-gateway/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ClientProxy } from '@nestjs/microservices';
import { BaseService } from 'libs/service/base.service';
import { AcceptRejectInvitationDto } from './dto/accept-reject-invitation.dto';
import { GetAllInvitationsDto } from './dto/get-all-invitations.dto';
import { GetAllUsersDto } from './dto/get-all-users.dto';
import { UpdateUserProfileDto } from './dto/update-user-profile.dto';
import { AddPasskeyDetailsDto } from './dto/add-user.dto';
import { UpdatePlatformSettingsDto } from './dto/update-platform-settings.dto';
Expand All @@ -13,6 +12,7 @@ import { IUsersProfile, ICheckUserDetails } from 'apps/user/interfaces/user.inte
import { IUsersActivity } from 'libs/user-activity/interface';
import { IUserInvitations } from '@credebl/common/interfaces/user.interface';
import { user } from '@prisma/client';
import { PaginationDto } from '@credebl/common/dtos/pagination.dto';

@Injectable()
export class UserService extends BaseService {
Expand Down Expand Up @@ -69,9 +69,9 @@ export class UserService extends BaseService {
}

async get(
getAllUsersDto: GetAllUsersDto
paginationDto:PaginationDto
): Promise<object> {
const { pageNumber, pageSize, search } = getAllUsersDto;
const { pageNumber, pageSize, search } = paginationDto;
const payload = { pageNumber, pageSize, search };
return this.sendNatsMessage(this.serviceProxy, 'fetch-users', payload);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/organization/repositories/organization.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class OrganizationRepository {
});
} catch (error) {
this.logger.error(`error: ${JSON.stringify(error)}`);
throw new InternalServerErrorException(error);
throw error;
}
}

Expand Down
Loading