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

refactor: send ecosystem invitation link #177

Merged
merged 2 commits into from
Oct 18, 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
2 changes: 1 addition & 1 deletion apps/api-gateway/src/ecosystem/ecosystem.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
36 changes: 31 additions & 5 deletions apps/ecosystem/src/ecosystem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { CreateEcosystem, CredDefMessage, RequestCredDeffEndorsement, RequestSch
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()
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -293,7 +294,8 @@ export class EcosystemService {
*/
async sendInviteEmailTemplate(
email: string,
ecosystemName: string
ecosystemName: string,
isUserExist: boolean
): Promise<boolean> {
const platformConfigData = await this.prisma.platform_config.findMany();

Expand All @@ -303,14 +305,38 @@ 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);

return isEmailSent;
}

async checkUserExistInPlatform(email: string): Promise<boolean> {
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
Expand Down
11 changes: 8 additions & 3 deletions apps/ecosystem/templates/EcosystemInviteTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<!DOCTYPE html>
Expand Down
2 changes: 1 addition & 1 deletion libs/common/src/send-grid-helper-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const sendEmail = async (EmailDto: EmailDto): Promise<boolean> => {
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;
Expand Down