Skip to content

Commit

Permalink
Merge pull request #764 from rocket-admin/backend_refactoring
Browse files Browse the repository at this point in the history
Remove external invitation in group
  • Loading branch information
Artuomka authored Jul 31, 2024
2 parents c2cdc30 + d81db4c commit 641c072
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 102 deletions.
27 changes: 12 additions & 15 deletions backend/src/entities/email/send-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,6 @@ export async function sendEmailChanged(email: string): Promise<SMTPTransport.Sen
return await sendEmailToUser(letterContent);
}

export async function sendInvitationToGroup(
email: string,
verificationString: string,
): Promise<SMTPTransport.SentMessageInfo> {
const emailFrom = getProcessVariable('EMAIL_FROM') || Constants.AUTOADMIN_SUPPORT_MAIL;
const letterContent: IMessage = {
from: emailFrom,
to: email,
subject: Constants.EMAIL.GROUP_INVITE.GROUP_INVITE_SUBJECT_DATA,
text: Constants.EMAIL.GROUP_INVITE.GROUP_INVITE_TEXT_DATA(verificationString),
html: Constants.EMAIL.GROUP_INVITE.GROUP_INVITE_HTML_DATA(verificationString),
};
return await sendEmailToUser(letterContent);
}

export async function sendReminderToUser(email: string): Promise<SMTPTransport.SentMessageInfo> {
const emailFrom = getProcessVariable('EMAIL_FROM') || Constants.AUTOADMIN_SUPPORT_MAIL;
const letterContent: IMessage = {
Expand Down Expand Up @@ -159,3 +144,15 @@ export async function send2faEnabledInCompanyToUser(
};
return await sendEmailToUser(letterContent);
}

export async function sendInvitedInNewGroup(email: string, groupTitle: string) {
const emailFrom = getProcessVariable('EMAIL_FROM') || Constants.AUTOADMIN_SUPPORT_MAIL;
const letterContent: IMessage = {
from: emailFrom,
to: email,
subject: Constants.EMAIL.GROUP_INVITE.GROUP_INVITE_SUBJECT_DATA,
text: Constants.EMAIL.GROUP_INVITE.GROUP_INVITE_TEXT_DATA(groupTitle),
html: Constants.EMAIL.GROUP_INVITE.GROUP_INVITE_HTML_DATA(groupTitle),
};
return await sendEmailToUser(letterContent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { AddedUserInGroupDs } from '../application/data-sctructures/added-user-i
import { IAddUserInGroup } from './use-cases.interfaces.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { SaasCompanyGatewayService } from '../../../microservices/gateways/saas-gateway.ts/saas-company-gateway.service.js';
import { sendEmailConfirmation, sendInvitationToGroup } from '../../email/send-email.js';
import { UserInvitationEntity } from '../../user/user-invitation/user-invitation.entity.js';
import { sendInvitedInNewGroup } from '../../email/send-email.js';
import { buildFoundGroupResponseDto } from '../utils/biuld-found-group-response.dto.js';
import { slackPostMessage } from '../../../helpers/index.js';
import { isSaaS } from '../../../helpers/app/is-saas.js';
Expand All @@ -26,11 +25,12 @@ export class AddUserInGroupUseCase
}

protected async implementation(inputData: AddUserInGroupWithSaaSDs): Promise<AddedUserInGroupDs> {
const { email, groupId, inviterId } = inputData;
const { email, groupId } = inputData;
const foundGroup = await this._dbContext.groupRepository.findGroupByIdWithConnectionAndUsers(groupId);

const foundConnection =
await this._dbContext.connectionRepository.getConnectionByGroupIdWithCompanyAndUsersInCompany(groupId);

if (!foundConnection) {
throw new HttpException(
{
Expand Down Expand Up @@ -76,86 +76,29 @@ export class AddUserInGroupUseCase
const saasFoundCompany = await this.saasCompanyGatewayService.getCompanyInfo(foundConnection.company.id);
const saasFoundUserInCompany = saasFoundCompany?.users.find((u) => u.email === email);

if (foundUser && !saasFoundUserInCompany) {
await slackPostMessage('probable desynchronization of users (adding a user to a group)');
if (foundUser && !saasFoundUserInCompany && saasFoundCompany.users.length) {
await slackPostMessage(`probable desynchronization of users (adding a user to a group, user not found is saas, but found in core)
user: ${email}, company: ${foundConnection.company.id}`);
}
}

if (foundUser && foundUser.isActive) {
const userAlreadyAdded = !!foundGroup.users.find((u) => u.id === foundUser.id);
if (userAlreadyAdded) {
throw new HttpException(
{
message: Messages.USER_ALREADY_ADDED,
},
HttpStatus.BAD_REQUEST,
);
}

foundGroup.users.push(foundUser);
const savedGroup = await this._dbContext.groupRepository.saveNewOrUpdatedGroup(foundGroup);
return {
group: buildFoundGroupResponseDto(savedGroup),
message: Messages.USER_ADDED_IN_GROUP(foundUser.email),
external_invite: false,
};
}

if (foundUser && !foundUser.isActive) {
const userAlreadyAdded = !!foundGroup.users.find((u) => u.id === foundUser.id);
let savedInvitation: UserInvitationEntity;
if (userAlreadyAdded) {
savedInvitation = await this._dbContext.userInvitationRepository.createOrUpdateInvitationEntity(
foundUser,
inviterId,
);
} else {
savedInvitation = await this._dbContext.userInvitationRepository.createOrUpdateInvitationEntity(
foundUser,
null,
);
foundGroup.users.push(foundUser);
}
const savedGroup = await this._dbContext.groupRepository.saveNewOrUpdatedGroup(foundGroup);

const newEmailVerification =
await this._dbContext.emailVerificationRepository.createOrUpdateEmailVerification(foundUser);
const confiramtionMailResult = await sendEmailConfirmation(
foundUser.email,
newEmailVerification.verification_string,
const userAlreadyAdded = !!foundGroup.users.find((u) => u.id === foundUser.id);
if (userAlreadyAdded) {
throw new HttpException(
{
message: Messages.USER_ALREADY_ADDED,
},
HttpStatus.BAD_REQUEST,
);

if (confiramtionMailResult?.rejected.includes(foundUser.email)) {
throw new HttpException(
{
message: Messages.FAILED_TO_SEND_CONFIRMATION_EMAIL(foundUser.email),
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
const invitationMailResult = await sendInvitationToGroup(foundUser.email, savedInvitation.verification_string);

if (invitationMailResult?.rejected.includes(foundUser.email)) {
throw new HttpException(
{
message: Messages.FAILED_TO_SEND_INVITATION_EMAIL(foundUser.email),
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
if (userAlreadyAdded) {
throw new HttpException(
{
message: Messages.USER_ALREADY_ADDED_BUT_NOT_ACTIVE,
},
HttpStatus.BAD_REQUEST,
);
}
return {
group: buildFoundGroupResponseDto(savedGroup),
message: Messages.USER_ADDED_IN_GROUP(foundUser.email),
external_invite: false,
};
}

foundGroup.users.push(foundUser);
const savedGroup = await this._dbContext.groupRepository.saveNewOrUpdatedGroup(foundGroup);
await sendInvitedInNewGroup(foundUser.email, foundGroup.title);
return {
group: buildFoundGroupResponseDto(savedGroup),
message: Messages.USER_ADDED_IN_GROUP(foundUser.email),
external_invite: false,
};
}
}
12 changes: 4 additions & 8 deletions backend/src/helpers/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,17 @@ export const Constants = {
},
GROUP_INVITE: {
GROUP_INVITE_SUBJECT_DATA: 'You were invited to a group in the Rocketadmin project',
GROUP_INVITE_TEXT_DATA: function (verificationString) {
return `You have been added to a group in the Rocketadmin project.
Please follow the link and accept the invitation:
${Constants.APP_DOMAIN_ADDRESS}/external/group/user/verify/${verificationString}/`;
GROUP_INVITE_TEXT_DATA: function (groupTitle: string) {
return `You have been added to a "${groupTitle}" group in the Rocketadmin project.`;
},
GROUP_INVITE_HTML_DATA: function (verificationString) {
GROUP_INVITE_HTML_DATA: function (groupTitle: string) {
return `
<body>
<p>
Hi!
</p>
<p>
You have been added to a group in the Rocketadmin project.
Please follow the link and accept the invitation:
<a href="${Constants.APP_DOMAIN_ADDRESS}/external/group/user/verify/${verificationString}"></a>
You have been added to a "${groupTitle}" group in the Rocketadmin project.
</p>
<p>
Thanks.
Expand Down

0 comments on commit 641c072

Please sign in to comment.