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

feat: fetch ecosystem sent invitations #123

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Transform, Type } from 'class-transformer';
import { toNumber } from '@credebl/common/cast.helper';

import { ApiProperty } from '@nestjs/swagger';
import { IsOptional } from 'class-validator';

export class GetAllEcosystemInvitationsDto {
@ApiProperty({ required: false, default: 1 })
@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;

}
39 changes: 39 additions & 0 deletions apps/api-gateway/src/ecosystem/ecosystem.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { User } from '../authz/decorators/user.decorator';
import { BulkEcosystemInvitationDto } from './dtos/send-invitation.dto';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { user } from '@prisma/client';
import { GetAllEcosystemInvitationsDto } from './dtos/get-all-sent-invitations.dto';


@UseFilters(CustomExceptionFilter)
Expand Down Expand Up @@ -133,6 +134,43 @@ export class EcosystemController {

}

@Get('/:ecosystemId/invitations')
@ApiOperation({ summary: 'Get all sent invitations', description: 'Get all sent 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
})
async getInvitationsByEcosystemId(
@Param('ecosystemId') ecosystemId: string,
@Query() getAllInvitationsDto: GetAllEcosystemInvitationsDto,
@User() user: user,
@Res() res: Response): Promise<Response> {

const getInvitationById = await this.ecosystemService.getInvitationsByEcosystemId(ecosystemId, getAllInvitationsDto, String(user.id));

const finalResponse: IResponseType = {
statusCode: HttpStatus.OK,
message: ResponseMessages.organisation.success.getInvitation,
data: getInvitationById.response
};
return res.status(HttpStatus.OK).json(finalResponse);

}


@Put('/:ecosystemId/')
@ApiOperation({ summary: 'Edit ecosystem', description: 'Edit existing ecosystem' })
Expand All @@ -147,4 +185,5 @@ export class EcosystemController {
};
return res.status(HttpStatus.CREATED).json(finalResponse);
}

}
13 changes: 12 additions & 1 deletion apps/api-gateway/src/ecosystem/ecosystem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ 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';
import { BulkEcosystemInvitationDto } from './dtos/send-invitation.dto';
import { GetAllEcosystemInvitationsDto } from './dtos/get-all-sent-invitations.dto';
import { GetAllSentEcosystemInvitationsDto } from './dtos/get-all-sent-ecosystemInvitations-dto';


@Injectable()
Expand Down Expand Up @@ -52,6 +53,16 @@ export class EcosystemService extends BaseService {
const payload = { bulkInvitationDto, userId };
return this.sendNats(this.serviceProxy, 'send-ecosystem-invitation', payload);
}

async getInvitationsByEcosystemId(
ecosystemId: string,
getAllInvitationsDto: GetAllEcosystemInvitationsDto,
userId: string
): Promise<{ response: object }> {
const { pageNumber, pageSize, search } = getAllInvitationsDto;
const payload = { ecosystemId, pageNumber, pageSize, search, userId };
return this.sendNats(this.serviceProxy, 'get-sent-invitations-ecosystemId', payload);
}


/**
Expand Down
9 changes: 9 additions & 0 deletions apps/ecosystem/interfaces/invitations.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


export interface FetchInvitationsPayload {
ecosystemId: string;
userId: string,
pageNumber: number;
pageSize: number;
search: string
}
11 changes: 11 additions & 0 deletions apps/ecosystem/src/ecosystem.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MessagePattern } from '@nestjs/microservices';
import { EcosystemService } from './ecosystem.service';
import { Body } from '@nestjs/common';
import { BulkSendInvitationDto } from '../dtos/send-invitation.dto';
import { FetchInvitationsPayload } from '../interfaces/invitations.interface';

@Controller()
export class EcosystemController {
Expand Down Expand Up @@ -69,5 +70,15 @@ export class EcosystemController {
): Promise<string> {
return this.ecosystemService.createInvitation(payload.bulkInvitationDto, payload.userId);
}


@MessagePattern({ cmd: 'get-sent-invitations-ecosystemId' })
async getInvitationsByOrgId(
@Body() payload: FetchInvitationsPayload
): Promise<object> {
return this.ecosystemService.getInvitationsByEcosystemId(
payload
);
}

}
91 changes: 55 additions & 36 deletions apps/ecosystem/src/ecosystem.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,41 +113,6 @@ 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);
}
}

/**
*
* @param ecosystemId
Expand Down Expand Up @@ -190,7 +155,6 @@ export class EcosystemRepository {
}
}


/**
*
* @param email
Expand Down Expand Up @@ -219,5 +183,60 @@ export class EcosystemRepository {
throw new InternalServerErrorException(error);
}
}

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

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


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

// eslint-disable-next-line prefer-destructuring
const invitations = result[0];
// eslint-disable-next-line prefer-destructuring
const totalCount = result[1];
const totalPages = Math.ceil(totalCount / pageSize);

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

}
19 changes: 17 additions & 2 deletions apps/ecosystem/src/ecosystem.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// eslint-disable-next-line camelcase
import { Injectable, InternalServerErrorException, Logger, NotFoundException } from '@nestjs/common';
import { EcosystemRepository } from './ecosystem.repository';
import { ResponseMessages } from '@credebl/common/response-messages';
import { Injectable, InternalServerErrorException, Logger, NotFoundException } from '@nestjs/common';
import { BulkSendInvitationDto } from '../dtos/send-invitation.dto';
import { RpcException } from '@nestjs/microservices';
import { PrismaService } from '@credebl/prisma-service';
import { EcosystemInviteTemplate } from '../templates/EcosystemInviteTemplate';
import { EmailDto } from '@credebl/common/dtos/email.dto';
import { sendEmail } from '@credebl/common/send-grid-helper-file';
import { FetchInvitationsPayload } from '../interfaces/invitations.interface';

@Injectable()
export class EcosystemService {
Expand Down Expand Up @@ -64,6 +65,7 @@ export class EcosystemService {
return getAllEcosystemDetails;
}


/**
* Description: get an ecosystem invitation
* @returns Get sent ecosystem invitation details
Expand All @@ -80,7 +82,7 @@ export class EcosystemService {
]
};

return await this.ecosystemRepository.getEcosystemInvitationsPagination(query, status, pageNumber, pageSize);
return await this.ecosystemRepository.getEcosystemInvitationsPagination(query, pageNumber, pageSize);
} catch (error) {
this.logger.error(`In error getEcosystemInvitations: ${JSON.stringify(error)}`);
throw new InternalServerErrorException(error);
Expand Down Expand Up @@ -176,4 +178,17 @@ export class EcosystemService {
return isEmailSent;
}

async getInvitationsByEcosystemId(
payload: FetchInvitationsPayload
): Promise<object> {
try {

const { ecosystemId, userId, pageNumber, pageSize, search} = payload;
const ecosystemInvitations = await this.ecosystemRepository.getInvitationsByEcosystemId(ecosystemId, pageNumber, pageSize, userId, search);
Copy link
Contributor

@tipusinghaw tipusinghaw Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of destructuring the object in the service file we should destruct in the repository

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

return ecosystemInvitations;
} catch (error) {
this.logger.error(`In getInvitationsByEcosystemId : ${JSON.stringify(error)}`);
throw new RpcException(error.response ? error.response : error);
}
}
}