From 6e167d5ea2a382d44ca3ad602569dd34fe4383b2 Mon Sep 17 00:00:00 2001 From: bhavanakarwade Date: Wed, 18 Oct 2023 15:46:32 +0530 Subject: [PATCH] refactor: send ecosystem invitations link Signed-off-by: bhavanakarwade --- .../src/ecosystem/ecosystem.controller.ts | 2 +- apps/ecosystem/src/ecosystem.service.ts | 36 ++++++++++++++++--- .../templates/EcosystemInviteTemplate.ts | 11 ++++-- libs/common/src/send-grid-helper-file.ts | 2 +- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/apps/api-gateway/src/ecosystem/ecosystem.controller.ts b/apps/api-gateway/src/ecosystem/ecosystem.controller.ts index c6e037a0d..d9a8bf1a0 100644 --- a/apps/api-gateway/src/ecosystem/ecosystem.controller.ts +++ b/apps/api-gateway/src/ecosystem/ecosystem.controller.ts @@ -209,7 +209,7 @@ export class EcosystemController { */ @Get('/:ecosystemId/:orgId/members') @Roles(OrgRoles.OWNER, OrgRoles.ADMIN) - @EcosystemsRoles(EcosystemRoles.ECOSYSTEM_OWNER, EcosystemRoles.ECOSYSTEM_LEAD) + @EcosystemsRoles(EcosystemRoles.ECOSYSTEM_OWNER, EcosystemRoles.ECOSYSTEM_LEAD, EcosystemRoles.ECOSYSTEM_MEMBER) @ApiBearerAuth() @UseGuards(AuthGuard('jwt'), EcosystemRolesGuard, OrgRolesGuard) @ApiResponse({ status: 200, description: 'Success', type: ApiResponseDto }) diff --git a/apps/ecosystem/src/ecosystem.service.ts b/apps/ecosystem/src/ecosystem.service.ts index 503685d61..18f75e7c3 100644 --- a/apps/ecosystem/src/ecosystem.service.ts +++ b/apps/ecosystem/src/ecosystem.service.ts @@ -17,7 +17,7 @@ import { CreateEcosystem, CredDefMessage, SaveSchema, SchemaMessage, SignedTrans import { GetEndorsementsPayload } from '../interfaces/endorsements.interface'; import { CommonConstants } from '@credebl/common/common.constant'; // eslint-disable-next-line camelcase -import { credential_definition, org_agents, platform_config, schema } from '@prisma/client'; +import { credential_definition, org_agents, platform_config, schema, user } from '@prisma/client'; @Injectable() @@ -158,13 +158,14 @@ export class EcosystemService { for (const invitation of invitations) { const { email } = invitation; + const isUserExist = await this.checkUserExistInPlatform(email); + const isInvitationExist = await this.checkInvitationExist(email, ecosystemId); if (!isInvitationExist) { await this.ecosystemRepository.createSendInvitation(email, ecosystemId, userId); - try { - await this.sendInviteEmailTemplate(email, ecosystemDetails.name); + await this.sendInviteEmailTemplate(email, ecosystemDetails.name, isUserExist); } catch (error) { throw new InternalServerErrorException(ResponseMessages.user.error.emailSend); } @@ -293,7 +294,8 @@ export class EcosystemService { */ async sendInviteEmailTemplate( email: string, - ecosystemName: string + ecosystemName: string, + isUserExist: boolean ): Promise { const platformConfigData = await this.prisma.platform_config.findMany(); @@ -303,7 +305,7 @@ export class EcosystemService { emailData.emailTo = email; emailData.emailSubject = `${process.env.PLATFORM_NAME} Platform: Invitation`; - emailData.emailHtml = await urlEmailTemplate.sendInviteEmailTemplate(email, ecosystemName); + emailData.emailHtml = await urlEmailTemplate.sendInviteEmailTemplate(email, ecosystemName, isUserExist); //Email is sent to user for the verification through emailData const isEmailSent = await sendEmail(emailData); @@ -311,6 +313,30 @@ export class EcosystemService { return isEmailSent; } + async checkUserExistInPlatform(email: string): Promise { + const pattern = { cmd: 'get-user-by-mail' }; + const payload = { email }; + + const userData: user = await this.ecosystemServiceProxy + .send(pattern, payload) + .toPromise() + .catch((error) => { + this.logger.error(`catch: ${JSON.stringify(error)}`); + throw new HttpException( + { + status: error.status, + error: error.message + }, + error.status + ); + }); + + if (userData && userData.isEmailVerified) { + return true; + } + return false; + } + /** * * @param RequestSchemaEndorsement diff --git a/apps/ecosystem/templates/EcosystemInviteTemplate.ts b/apps/ecosystem/templates/EcosystemInviteTemplate.ts index 2f872654a..1486b1db4 100644 --- a/apps/ecosystem/templates/EcosystemInviteTemplate.ts +++ b/apps/ecosystem/templates/EcosystemInviteTemplate.ts @@ -2,12 +2,17 @@ export class EcosystemInviteTemplate { public sendInviteEmailTemplate( email: string, - ecosystemName: string + ecosystemName: string, + isUserExist = false ): string { - const validUrl = `${process.env.FRONT_END_URL}/authentication/sign-in`; + const validUrl = isUserExist ? `${process.env.FRONT_END_URL}/authentication/sign-in` : `${process.env.FRONT_END_URL}/authentication/sign-up`; + + const message = isUserExist + ? `You have already registered on platform, you can access the application. + Please log in and accept the ecosystem “INVITATION” and participate in the ecosystem` + : `You have to register on the platform and then you can access the application. Accept the ecosystem “INVITATION” and participate in the ecosystem`; - const message = `You have been invited to join the ecosystem so please log in and accept the ecosystem “INVITATION” and participate in the ecosystem`; const year: number = new Date().getFullYear(); return ` diff --git a/libs/common/src/send-grid-helper-file.ts b/libs/common/src/send-grid-helper-file.ts index a98bd518b..623d118e1 100644 --- a/libs/common/src/send-grid-helper-file.ts +++ b/libs/common/src/send-grid-helper-file.ts @@ -18,7 +18,7 @@ export const sendEmail = async (EmailDto: EmailDto): Promise => { html: EmailDto.emailHtml, attachments: EmailDto.emailAttachments }; - return await sendgrid.send(msg).then(() => true).catch(() => false) + return await sendgrid.send(msg).then(() => true).catch(() => false); } catch (error) { return false;