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 emails #408

Merged
merged 24 commits into from
Oct 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a8083d2
Refactor change place status email.
IliaWithHat Sep 30, 2024
38b3e03
Refactor create-employee-password-page.html email.
IliaWithHat Sep 30, 2024
3e0229b
Refactor habit-assign-friend-request.html email.
IliaWithHat Sep 30, 2024
50948a9
Removed unused new-event-comment-email-page.html
IliaWithHat Oct 2, 2024
e27c665
Removed unused user-received-comment-email-page.html and user-receive…
IliaWithHat Oct 2, 2024
6e171fb
Refactor new-places-report-email-page.html
IliaWithHat Oct 2, 2024
bb1a256
Refactor restore-email-page.html
IliaWithHat Oct 2, 2024
e052fda
Refactor scheduled-notification-email-page.html
IliaWithHat Oct 2, 2024
1e19397
Refactor success-restored-password-page.html
IliaWithHat Oct 2, 2024
b8b43df
Refactor user-activation-page.html
IliaWithHat Oct 2, 2024
aee69d8
Refactor user-approval-email-page.html
IliaWithHat Oct 2, 2024
2f2b663
Remove dumb code.
IliaWithHat Oct 2, 2024
e0cf3b3
Remove unnecessary validation.
IliaWithHat Oct 2, 2024
3a6095a
Merge refactor-email in dev.
IliaWithHat Oct 3, 2024
3d01afc
Fix.
IliaWithHat Oct 3, 2024
7c1ce9f
Fix.
IliaWithHat Oct 3, 2024
479f9f2
Merge refactor-emails in dev.
IliaWithHat Oct 3, 2024
48385f3
Refactored user-reasons-of-deactivation-page.html
IliaWithHat Oct 3, 2024
a446255
Refactored user-violation-mail.html
IliaWithHat Oct 3, 2024
c2186e4
Refactored verify-email-page.html
IliaWithHat Oct 3, 2024
9af2739
Cleanup.
IliaWithHat Oct 3, 2024
9b33483
Fix test.
IliaWithHat Oct 3, 2024
4125af7
Changed logo for Pick Up City and fixing sincerely text.
IliaWithHat Oct 4, 2024
dfe26a6
Removed duplication.
IliaWithHat Oct 7, 2024
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
Prev Previous commit
Next Next commit
Remove unnecessary validation.
  • Loading branch information
IliaWithHat committed Oct 2, 2024
commit e0cf3b3bf03d976e939e89a577446e72d9cfbf4b
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@
/**
* Provides the interface to manage sending emails to {@code User}.
*/

public interface EmailService {
/**
* Method for sending notification to users who subscribed for updates about

This file was deleted.

This file was deleted.

24 changes: 1 addition & 23 deletions service/src/main/java/greencity/service/EmailServiceImpl.java
Original file line number Diff line number Diff line change
@@ -7,15 +7,13 @@
import greencity.dto.user.UserActivationDto;
import greencity.dto.user.UserDeactivationReasonDto;
import greencity.dto.violation.UserViolationMailDto;
import greencity.exception.exceptions.LanguageNotSupportedException;
import greencity.message.ChangePlaceStatusDto;
import greencity.message.GeneralEmailMessage;
import greencity.message.HabitAssignNotificationMessage;
import greencity.message.ScheduledEmailMessage;
import greencity.message.SendReportEmailMessage;
import greencity.message.UserTaggedInCommentMessage;
import greencity.validator.EmailAddressValidator;
import greencity.validator.LanguageValidationUtils;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import java.util.HashMap;
@@ -139,7 +137,6 @@ public void sendVerificationEmail(Long id, String name, String email, String tok
model.put(EmailConstants.CLIENT_LINK, baseLink);
model.put(EmailConstants.USER_NAME, name);
model.put(EmailConstants.VERIFY_ADDRESS, baseLink + "?token=" + token + PARAM_USER_ID + id);
validateLanguage(language);
model.put(EmailConstants.IS_UBS, isUbs);
model.put(EmailConstants.LANGUAGE, language);
String template = createEmailTemplate(model, EmailConstants.VERIFY_EMAIL_PAGE);
@@ -178,17 +175,6 @@ public void sendRestoreEmail(Long userId, String userName, String userEmail, Str
getLocale(language)), template);
}

/**
* This method validates language.
*
* @param language language which will be used for sending letter.
*/
private void validateLanguage(String language) {
if (!LanguageValidationUtils.isValid(language)) {
throw new LanguageNotSupportedException("Invalid language");
}
}

private String createEmailTemplate(Map<String, Object> vars, String templateName) {
log.info(LogMessage.IN_CREATE_TEMPLATE_NAME, null, templateName);
Context context = new Context();
@@ -228,7 +214,6 @@ public void sendReasonOfDeactivation(UserDeactivationReasonDto userDeactivationD
model.put(EmailConstants.CLIENT_LINK, clientLink);
model.put(EmailConstants.USER_NAME, userDeactivationDto.getName());
model.put(EmailConstants.REASON, userDeactivationDto.getDeactivationReason());
validateLanguage(userDeactivationDto.getLang());
String template = createEmailTemplate(model, EmailConstants.REASONS_OF_DEACTIVATION_PAGE);
sendEmail(userDeactivationDto.getEmail(), EmailConstants.DEACTIVATION, template);
}
@@ -238,7 +223,6 @@ public void sendMessageOfActivation(UserActivationDto userActivationDto) {
Map<String, Object> model = new HashMap<>();
model.put(EmailConstants.CLIENT_LINK, clientLink);
model.put(EmailConstants.USER_NAME, userActivationDto.getName());
validateLanguage(userActivationDto.getLang());
model.put(EmailConstants.LANGUAGE, userActivationDto.getLang());
String template = createEmailTemplate(model, EmailConstants.ACTIVATION_PAGE);
sendEmail(userActivationDto.getEmail(), messageSource.getMessage(EmailConstants.ACTIVATION, null,
@@ -251,7 +235,6 @@ public void sendUserViolationEmail(UserViolationMailDto dto) {
model.put(EmailConstants.CLIENT_LINK, clientLink);
model.put(EmailConstants.USER_NAME, dto.getName());
model.put(EmailConstants.DESCRIPTION, dto.getViolationDescription());
validateLanguage(dto.getLanguage());
model.put(EmailConstants.LANGUAGE, dto.getLanguage());
String template = createEmailTemplate(model, EmailConstants.USER_VIOLATION_PAGE);
sendEmail(dto.getEmail(), EmailConstants.VIOLATION_EMAIL, template);
@@ -262,7 +245,6 @@ public void sendSuccessRestorePasswordByEmail(String email, String language, Str
Map<String, Object> model = new HashMap<>();
model.put(EmailConstants.CLIENT_LINK, clientLink + "/#" + (isUbs ? "/ubs" : "/greenCity"));
model.put(EmailConstants.USER_NAME, userName);
validateLanguage(language);
model.put(EmailConstants.LANGUAGE, language);
model.put(EmailConstants.IS_UBS, isUbs);
String template = createEmailTemplate(model, EmailConstants.SUCCESS_RESTORED_PASSWORD_PAGE);
@@ -295,7 +277,6 @@ private static Locale getLocale(String language) {
public void sendHabitAssignNotificationEmail(HabitAssignNotificationMessage message) {
Map<String, Object> model = new HashMap<>();
String language = message.getLanguage();
validateLanguage(language);
model.put(EmailConstants.CLIENT_LINK, clientLink);
model.put(EmailConstants.USER_NAME, message.getReceiverName());
model.put(EmailConstants.VERIFY_ADDRESS, serverLink + "/habit/assign/confirm/" + message.getHabitAssignId());
@@ -329,7 +310,6 @@ public void sendCreateNewPasswordForEmployee(Long employeeId, String employeeFis
public void sendTaggedUserInCommentNotificationEmail(UserTaggedInCommentMessage message) {
Map<String, Object> model = new HashMap<>();
String language = message.getLanguage();
validateLanguage(language);
model.put(EmailConstants.CLIENT_LINK, message.getBaseLink());
model.put(EmailConstants.USER_NAME, message.getReceiverName());
model.put(EmailConstants.AUTHOR_NAME, message.getTaggerName());
@@ -346,7 +326,6 @@ public void sendTaggedUserInCommentNotificationEmail(UserTaggedInCommentMessage
public void sendScheduledNotificationEmail(ScheduledEmailMessage message) {
Map<String, Object> model = new HashMap<>();
String language = message.getLanguage();
validateLanguage(language);
model.put(EmailConstants.CLIENT_LINK, message.getBaseLink());
model.put(EmailConstants.USER_NAME, message.getUsername());
model.put(EmailConstants.LANGUAGE, language);
@@ -366,9 +345,8 @@ private Map<String, Object> buildModelMapForPasswordRestore(Long userId, String
model.put(EmailConstants.USER_NAME, name);
model.put(EmailConstants.RESTORE_PASS, clientLink + "/#" + (isUbs ? "/ubs" : "") + "/auth/restore?"
+ "token=" + token + PARAM_USER_ID + userId);
validateLanguage(language);
model.put(EmailConstants.IS_UBS, isUbs);
model.put(EmailConstants.LANGUAGE, language);
return model;
}
}
}
23 changes: 10 additions & 13 deletions service/src/test/java/greencity/service/EmailServiceImplTest.java
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@
import greencity.dto.user.UserActivationDto;
import greencity.dto.user.UserDeactivationReasonDto;
import greencity.dto.violation.UserViolationMailDto;
import greencity.enums.EmailNotification;
import greencity.enums.PlaceStatus;
import greencity.exception.exceptions.LanguageNotSupportedException;
import greencity.exception.exceptions.WrongEmailException;
import greencity.message.ChangePlaceStatusDto;
import greencity.message.GeneralEmailMessage;
@@ -75,7 +75,10 @@ void sendChangePlaceStatusEmailTest() {
.authorEmail("useremail@gmail.com")
.placeName("test place name")
.authorFirstName("test author first name")
.authorLanguage("en")
.build();
when(messageSource.getMessage(EmailConstants.CHANGE_PLACE_STATUS, null, getLocale(changePlaceStatusDto.getAuthorLanguage())))
.thenReturn("Change place status");
service.sendChangePlaceStatusEmail(changePlaceStatusDto);
verify(javaMailSender).createMimeMessage();
}
@@ -97,6 +100,7 @@ void sendAddedNewPlacesReportEmailTest() {
.build()
)
))
.emailNotification(EmailNotification.WEEKLY)
.build();
service.sendAddedNewPlacesReportEmail(sendReportEmailMessage);
verify(javaMailSender).createMimeMessage();
@@ -128,7 +132,7 @@ void sendVerificationEmail(Long id, String name, String email, String token, Str

@Test
void sendVerificationEmailLanguageNotFoundException() {
assertThrows(LanguageNotSupportedException.class,
assertThrows(IllegalStateException.class,
() -> service.sendVerificationEmail(1L, "Test", "test@gmail.com", "token", "enuaru", false));
}

@@ -150,7 +154,7 @@ void sendRestoreEmail(Long id, String name, String email, String token, String l

@Test
void sendRestoreEmailLanguageNotFoundException() {
assertThrows(LanguageNotSupportedException.class,
assertThrows(IllegalStateException.class,
() -> service.sendRestoreEmail(1L, "Test", "test@gmail.com", "token", "enuaru", false));
}

@@ -185,6 +189,8 @@ void sendMessageOfActivation() {
.email("test@ukr.net")
.name("test")
.build();
when(messageSource.getMessage(EmailConstants.ACTIVATION, null, getLocale(test1.getLang())))
.thenReturn("Activation");
service.sendMessageOfActivation(test1);
verify(javaMailSender).createMimeMessage();
}
@@ -196,15 +202,6 @@ void sendUserViolationEmailTest() {
verify(javaMailSender).createMimeMessage();
}

@Test
void sendUserViolationEmailWithEmptyLanguageTest() {
UserViolationMailDto dto = ModelUtils.getUserViolationMailDto();
dto.setLanguage("");
assertThrows(LanguageNotSupportedException.class, () -> service.sendUserViolationEmail(dto));
dto.setLanguage(null);
assertThrows(LanguageNotSupportedException.class, () -> service.sendUserViolationEmail(dto));
}

@Test
void sendSuccessRestorePasswordByEmailTest() {
String email = "test@gmail.com";
@@ -222,7 +219,7 @@ void sendSuccessRestorePasswordByEmailTest() {
void sendUserViolationEmailWithUnsupportedLanguageTest() {
UserViolationMailDto dto = ModelUtils.getUserViolationMailDto();
dto.setLanguage("de");
assertThrows(LanguageNotSupportedException.class, () -> service.sendUserViolationEmail(dto));
assertThrows(IllegalStateException.class, () -> service.sendUserViolationEmail(dto));
}

@Test