Skip to content

Commit

Permalink
feat: get ecosystem invitations
Browse files Browse the repository at this point in the history
Signed-off-by: bhavanakarwade <[email protected]>
  • Loading branch information
bhavanakarwade authored and KulkarniShashank committed Sep 10, 2024
1 parent df1ccbe commit 2e9e15e
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Transform, Type } from 'class-transformer';
import { toNumber } from '@credebl/common/cast.helper';
import { ApiProperty } from '@nestjs/swagger';
import { IsOptional, IsString } from 'class-validator';
import { Invitation } from '@credebl/enum/enum';

export class GetAllSentEcosystemInvitationsDto {
@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 = 10;

@ApiProperty({ required: false })
@IsOptional()
@IsString()
status = Invitation.PENDING;
}
49 changes: 46 additions & 3 deletions apps/api-gateway/src/ecosystem/ecosystem.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiBearerAuth, ApiForbiddenResponse, ApiOperation, ApiResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { Controller, UseFilters, Put, Param, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiForbiddenResponse, ApiOperation, ApiQuery, ApiResponse, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { Controller, UseFilters, Put, Param, UseGuards, Query, BadRequestException } from '@nestjs/common';
import { EcosystemService } from './ecosystem.service';
import { Post, Get } from '@nestjs/common';
import { Body } from '@nestjs/common';
Expand All @@ -15,7 +15,11 @@ import { ResponseMessages } from '@credebl/common/response-messages';
import { CustomExceptionFilter } from 'apps/api-gateway/common/exception-handler';
import { EditEcosystemDto } from './dtos/edit-ecosystem-dto';
import { AuthGuard } from '@nestjs/passport';

import { GetAllSentEcosystemInvitationsDto } from './dtos/get-all-sent-ecosystemInvitations-dto';
import { User } from '../authz/decorators/user.decorator';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { user } from '@prisma/client';
import { Invitation } from '@credebl/enum/enum';

@UseFilters(CustomExceptionFilter)
@Controller('ecosystem')
Expand Down Expand Up @@ -43,6 +47,45 @@ export class EcosystemController {
return res.status(HttpStatus.OK).json(finalResponse);
}

@Get('/users/invitations')
@ApiOperation({ summary: 'Get an ecosystem invitations', description: 'Get an ecosystem invitations' })
@ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto })
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiQuery({
name: 'pageNumber',
type: Number,
required: false
})
@ApiQuery({
name: 'pageSize',
type: Number,
required: false
})
@ApiQuery({
name: 'search',
type: String,
required: false
})
@ApiQuery({
name: 'status',
type: String,
required: false
})
async getEcosystemInvitations(@Query() getAllInvitationsDto: GetAllSentEcosystemInvitationsDto, @User() user: user, @Res() res: Response): Promise<Response> {
if (!Object.values(Invitation).includes(getAllInvitationsDto.status)) {
throw new BadRequestException(ResponseMessages.ecosystem.error.invalidInvitationStatus);
}
const getEcosystemInvitation = await this.ecosystemService.getEcosystemInvitations(getAllInvitationsDto, user.email, getAllInvitationsDto.status);
const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.ecosystem.success.getInvitation,
data: getEcosystemInvitation.response
};
return res.status(HttpStatus.OK).json(finalResponse);

}

@Post('/')
@ApiOperation({ summary: 'Create a new ecosystem', description: 'Create an ecosystem' })
@ApiResponse({ status: 201, description: 'Success', type: ApiResponseDto })
Expand Down
3 changes: 2 additions & 1 deletion apps/api-gateway/src/ecosystem/ecosystem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Inject } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { BaseService } from 'libs/service/base.service';
import { GetAllSentEcosystemInvitationsDto } from './dtos/get-all-sent-ecosystemInvitations-dto';


@Injectable()
Expand Down Expand Up @@ -39,4 +40,4 @@ export class EcosystemService extends BaseService {
return this.sendNats(this.serviceProxy, 'get-all-ecosystem', '');
}

}
}
2 changes: 1 addition & 1 deletion apps/ecosystem/src/ecosystem.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ export class EcosystemController {
return this.ecosystemService.getAllEcosystem();
}

}
}
51 changes: 50 additions & 1 deletion apps/ecosystem/src/ecosystem.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Logger } from '@nestjs/common';
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
import { PrismaService } from '@credebl/prisma-service';
import { ecosystem } from '@prisma/client';
import {EcosystemOrgStatus, EcosystemRoles} from '../enums/ecosystem.enum';
Expand Down Expand Up @@ -112,5 +112,54 @@ export class EcosystemRepository {
}
}

async getEcosystemInvitationsPagination(queryObject: object, status: string, pageNumber: number, pageSize: number): Promise<object> {
try {
const result = await this.prisma.$transaction([
this.prisma.ecosystem_invitations.findMany({
where: {
...queryObject,
status
},
include: {
ecosystem: true
},
take: pageSize,
skip: (pageNumber - 1) * pageSize,
orderBy: {
createDateTime: 'desc'
}
}),
this.prisma.ecosystem_invitations.count({
where: {
...queryObject
}
})
]);

const [invitations, totalCount] = result;
const totalPages = Math.ceil(totalCount / pageSize);

return { totalPages, invitations };
} catch (error) {
this.logger.error(`error: ${JSON.stringify(error)}`);
throw new InternalServerErrorException(error);
}
}

async getEcosystemInvitations(userEmail: string, status: string, pageNumber: number, pageSize: number, search = ''): Promise<object> {
try {
const query = {
AND: [
{ email: userEmail },
{ status: { contains: search, mode: 'insensitive' } }
]
};

return this.getEcosystemInvitationsPagination(query, status, pageNumber, pageSize);
} catch (error) {
this.logger.error(`error: ${JSON.stringify(error)}`);
throw new InternalServerErrorException(error);
}
}

}
9 changes: 6 additions & 3 deletions apps/ecosystem/src/ecosystem.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// eslint-disable-next-line camelcase
import { Injectable, NotFoundException } from '@nestjs/common';
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { EcosystemRepository } from './ecosystem.repository';
import { ResponseMessages } from '@credebl/common/response-messages';
import { RpcException } from '@nestjs/microservices';

@Injectable()
export class EcosystemService {
constructor(
private readonly ecosystemRepository: EcosystemRepository
private readonly ecosystemRepository: EcosystemRepository,
private readonly logger: Logger

) { }

/**
Expand Down Expand Up @@ -54,4 +57,4 @@ export class EcosystemService {
}
return getAllEcosystemDetails;
}
}
}
8 changes: 5 additions & 3 deletions libs/common/src/response-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,14 @@ export const ResponseMessages = {
success: {
create: 'Ecosystem created successfully',
update: 'Ecosystem updated successfully',
fetch: 'Ecosystem fetched successfully'
fetch: 'Ecosystem fetched successfully',
getInvitation: 'Ecosystem invitations fetched successfully'
},
error: {
notCreated: 'Error while creating ecosystem',
update: 'Error while updating ecosystem'
update: 'Error while updating ecosystem',
invalidInvitationStatus: 'Invalid invitation status'
}

},
}
};

0 comments on commit 2e9e15e

Please sign in to comment.