From 6ea795e8a1e5fc0e57a56af4a60789bf856deb87 Mon Sep 17 00:00:00 2001 From: Abhijeet <41686026+abhvsn@users.noreply.github.com> Date: Thu, 15 Apr 2021 16:46:46 +0530 Subject: [PATCH] Autoscroll to invited organisation (#3945) * Added organization slug to the invite Url, which is finally used by email template(#2359) * Separate method to create email params. Welcome email url variable updated so as to be consistent with other email templates * Unused enum emailType removed, importing of java.util.* removed as per suggestion --- .../appsmith/server/services/UserService.java | 5 ++ .../server/services/UserServiceImpl.java | 48 +++++++++++++------ .../resources/email/welcomeUserTemplate.html | 2 +- .../server/services/UserServiceTest.java | 38 +++++++++++++++ 4 files changed, 77 insertions(+), 16 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserService.java index 442f236461..b0728b0fc4 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserService.java @@ -1,13 +1,17 @@ package com.appsmith.server.services; import com.appsmith.server.domains.InviteUser; +import com.appsmith.server.domains.Organization; import com.appsmith.server.domains.User; import com.appsmith.server.dtos.InviteUsersDTO; import com.appsmith.server.dtos.ResetUserPasswordDTO; +import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; +import java.util.HashMap; import java.util.List; +import java.util.Map; public interface UserService extends CrudService { @@ -31,4 +35,5 @@ public interface UserService extends CrudService { Mono updateCurrentUser(User updates, ServerWebExchange exchange); + Map getEmailParams(Organization organization, User inviterUser, String inviteUrl, boolean isNewUser); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java index ddb10d81e5..418319ccf5 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java @@ -494,7 +494,7 @@ public class UserServiceImpl extends BaseService i public Mono sendWelcomeEmail(User user, String originHeader) { Map params = new HashMap<>(); params.put("firstName", user.getName()); - params.put("appsmithLink", originHeader); + params.put("inviteUrl", originHeader); return emailSender .sendMail(user.getEmail(), "Welcome to Appsmith", WELCOME_USER_EMAIL_TEMPLATE, params) .thenReturn(user) @@ -536,6 +536,7 @@ public class UserServiceImpl extends BaseService i * 2. User exists : * a. Add user to the organization * b. Add organization to the user + * * @return Publishes the invited users, after being saved with the new organization ID. */ @Override @@ -557,7 +558,7 @@ public class UserServiceImpl extends BaseService i List usernames = new ArrayList<>(); for (String username : originalUsernames) { - usernames.add(username.toLowerCase()); + usernames.add(username.toLowerCase()); } Mono currentUserMono = sessionUserService.getCurrentUser().cache(); @@ -588,22 +589,16 @@ public class UserServiceImpl extends BaseService i Organization organization = tuple.getT2(); User currentUser = tuple.getT3(); - // Email template parameters initialization below. - Map params = new HashMap<>(); - if (!StringUtils.isEmpty(currentUser.getName())) { - params.put("Inviter_First_Name", currentUser.getName()); - } else { - params.put("Inviter_First_Name", currentUser.getEmail()); - } - params.put("inviter_org_name", organization.getName()); - return repository.findByEmail(username) .flatMap(existingUser -> { // The user already existed, just send an email informing that the user has been added // to a new organization log.debug("Going to send email to user {} informing that the user has been added to new organization {}", existingUser.getEmail(), organization.getName()); - params.put("inviteUrl", originHeader); + + // Email template parameters initialization below. + Map params = getEmailParams(organization, currentUser, originHeader, false); + Mono emailMono = emailSender.sendMail(existingUser.getEmail(), "Appsmith: You have been added to a new organization", USER_ADDED_TO_ORGANIZATION_EMAIL_TEMPLATE, params); @@ -611,7 +606,7 @@ public class UserServiceImpl extends BaseService i return emailMono .thenReturn(existingUser); }) - .switchIfEmpty(createNewUserAndSendInviteEmail(username, originHeader, params)); + .switchIfEmpty(createNewUserAndSendInviteEmail(username, originHeader, organization, currentUser)); }) .cache(); @@ -657,7 +652,7 @@ public class UserServiceImpl extends BaseService i ); } - private Mono createNewUserAndSendInviteEmail(String email, String originHeader, Map params) { + private Mono createNewUserAndSendInviteEmail(String email, String originHeader, Organization organization, User inviter) { User newUser = new User(); newUser.setEmail(email.toLowerCase()); @@ -678,7 +673,9 @@ public class UserServiceImpl extends BaseService i URLEncoder.encode(createdUser.getEmail(), StandardCharsets.UTF_8) ); - params.put("inviteUrl", inviteUrl); + // Email template parameters initialization below. + Map params = getEmailParams(organization, inviter, inviteUrl, true); + Mono emailMono = emailSender.sendMail(createdUser.getEmail(), "Invite for Appsmith", INVITE_USER_EMAIL_TEMPLATE, params); // We have sent out the emails. Just send back the saved user. @@ -742,4 +739,25 @@ public class UserServiceImpl extends BaseService i ); } + public Map getEmailParams(Organization organization, User inviter, String inviteUrl, boolean isNewUser) { + Map params = new HashMap<>(); + + if (inviter != null) { + if (!StringUtils.isEmpty(inviter.getName())) { + params.put("Inviter_First_Name", inviter.getName()); + } else { + params.put("Inviter_First_Name", inviter.getEmail()); + } + } + if (organization != null) { + params.put("inviter_org_name", organization.getName()); + } + if (isNewUser) { + params.put("inviteUrl", inviteUrl); + } else { + params.put("inviteUrl", inviteUrl + "/applications#" + organization.getSlug()); + } + return params; + } + } diff --git a/app/server/appsmith-server/src/main/resources/email/welcomeUserTemplate.html b/app/server/appsmith-server/src/main/resources/email/welcomeUserTemplate.html index 4a3be65b33..4ae1d91eaa 100644 --- a/app/server/appsmith-server/src/main/resources/email/welcomeUserTemplate.html +++ b/app/server/appsmith-server/src/main/resources/email/welcomeUserTemplate.html @@ -374,7 +374,7 @@ - Go to Appsmith + Go to Appsmith diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/UserServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/UserServiceTest.java index 34d303cbd5..642ed5cc70 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/UserServiceTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/UserServiceTest.java @@ -34,6 +34,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import static com.appsmith.server.acl.AclPermission.MANAGE_APPLICATIONS; @@ -43,6 +44,7 @@ import static com.appsmith.server.acl.AclPermission.READ_USERS; import static com.appsmith.server.acl.AclPermission.USER_MANAGE_ORGANIZATIONS; import static com.appsmith.server.acl.AclPermission.USER_READ_ORGANIZATIONS; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @Slf4j @@ -79,6 +81,42 @@ public class UserServiceTest { organizationMono = organizationService.getBySlug("spring-test-organization"); } + //Test if email params are updating correctly + @Test + public void checkEmailParamsForExistingUser() { + Organization organization = new Organization(); + organization.setName("UserServiceTest Update Org"); + organization.setSlug("userservicetest-update-org"); + + User inviter = new User(); + inviter.setName("inviterUserToApplication"); + + String inviteUrl = "http://localhost:8080"; + String expectedUrl = inviteUrl + "/applications#userservicetest-update-org"; + + Map params = userService.getEmailParams(organization, inviter, inviteUrl, false); + assertEquals(expectedUrl, params.get("inviteUrl")); + assertEquals("inviterUserToApplication", params.get("Inviter_First_Name")); + assertEquals("UserServiceTest Update Org", params.get("inviter_org_name")); + } + + @Test + public void checkEmailParamsForNewUser() { + Organization organization = new Organization(); + organization.setName("UserServiceTest Update Org"); + organization.setSlug("userservicetest-update-org"); + + User inviter = new User(); + inviter.setName("inviterUserToApplication"); + + String inviteUrl = "http://localhost:8080"; + + Map params = userService.getEmailParams(organization, inviter, inviteUrl, true); + assertEquals(inviteUrl, params.get("inviteUrl")); + assertEquals("inviterUserToApplication", params.get("Inviter_First_Name")); + assertEquals("UserServiceTest Update Org", params.get("inviter_org_name")); + } + //Test the update organization flow. @Test public void updateInvalidUserWithAnything() {