Skip to content

Commit

Permalink
Merge pull request #407 from ita-social-projects/feuture/user-can-cho…
Browse files Browse the repository at this point in the history
…ose-periodicity-of-email-notification

User can choose periodicity of email notification
  • Loading branch information
ToriForH authored Oct 3, 2024
2 parents b820d73 + 108bbec commit c6c93a7
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 284 deletions.
56 changes: 0 additions & 56 deletions core/src/main/java/greencity/controller/EmailController.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,62 +90,6 @@ public ResponseEntity<Object> sendUserViolation(@RequestBody UserViolationMailDt
return ResponseEntity.ok().build();
}

/**
* Sends notification to user on email.
*
* @param notification {@link GeneralEmailMessage} - object with all necessary
* data for sending notification via email.
* @author Yurii Midianyi
*/
@Operation(summary = "Send general email notification")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = HttpStatuses.OK),
@ApiResponse(responseCode = "400", description = HttpStatuses.BAD_REQUEST),
})
@PostMapping("/general/notification")
public ResponseEntity<Object> sendEmailNotification(@RequestBody GeneralEmailMessage notification) {
emailService.sendEmailNotification(notification);
return ResponseEntity.ok().build();
}

/**
* Sends habit assign email notification.
*
* @param message {@link HabitAssignNotificationMessage} - object with all
* necessary data for sending notification via email.
*/
@Operation(summary = "Send habit assign email notification")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = HttpStatuses.OK),
@ApiResponse(responseCode = "400", description = HttpStatuses.BAD_REQUEST),
@ApiResponse(responseCode = "404", description = HttpStatuses.NOT_FOUND)
})
@PostMapping("/habitAssign/notification")
public ResponseEntity<Void> sendHabitAssignNotification(
@RequestBody @Valid HabitAssignNotificationMessage message) {
emailService.sendHabitAssignNotificationEmail(message);
return ResponseEntity.ok().build();
}

/**
* Sends email notification to tagged user in comment.
*
* @param message {@link UserTaggedInCommentMessage} - object with all necessary
* data for sending notification via email.
*/
@Operation(summary = "Send email notification to tagged user")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = HttpStatuses.OK),
@ApiResponse(responseCode = "400", description = HttpStatuses.BAD_REQUEST),
@ApiResponse(responseCode = "404", description = HttpStatuses.NOT_FOUND)
})
@PostMapping("/taggedUserInComment/notification")
public ResponseEntity<Void> sendTaggedUserInCommentNotification(
@RequestBody @Valid UserTaggedInCommentMessage message) {
emailService.sendTaggedUserInCommentNotificationEmail(message);
return ResponseEntity.ok().build();
}

/**
* Sends scheduled email notification to user.
*
Expand Down
54 changes: 0 additions & 54 deletions core/src/test/java/greencity/controller/EmailControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,60 +187,6 @@ void sendUserViolationEmailTest() throws Exception {
verify(emailService).sendUserViolationEmail(userViolationMailDto);
}

@Test
@SneakyThrows
void sendEmailNotificationTest() {
ObjectMapper objectMapper = new ObjectMapper();
GeneralEmailMessage emailMessage = new GeneralEmailMessage("test@example.com", "Test Subject", "Test Message");
String jsonRequest = objectMapper.writeValueAsString(emailMessage);
mockMvc.perform(MockMvcRequestBuilders.post(LINK + "/general/notification")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonRequest))
.andExpect(status().isOk());
}

@Test
@SneakyThrows
void sendHabitAssignNotification() {
ObjectMapper objectMapper = new ObjectMapper();
HabitAssignNotificationMessage message = HabitAssignNotificationMessage.builder()
.language("ua")
.habitAssignId(100L)
.habitName("TEST")
.receiverEmail("test@gmail.com")
.receiverName("TEST")
.senderName("TEST")
.build();
String content = objectMapper.writeValueAsString(message);
mockMvc.perform(MockMvcRequestBuilders.post(LINK + "/habitAssign/notification")
.contentType(MediaType.APPLICATION_JSON)
.content(content))
.andExpect(status().isOk());
}

@Test
@SneakyThrows
void sendUserTaggedInCommentNotification() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
UserTaggedInCommentMessage message = UserTaggedInCommentMessage.builder()
.receiverEmail("receiver@example.com")
.receiverName("receiver")
.elementName("event")
.commentText("test")
.taggerName("tagger")
.commentedElementId(1L)
.language("en")
.baseLink("testLink")
.creationDate(LocalDateTime.now())
.build();
String content = objectMapper.writeValueAsString(message);
mockMvc.perform(MockMvcRequestBuilders.post(LINK + "/taggedUserInComment/notification")
.contentType(MediaType.APPLICATION_JSON)
.content(content))
.andExpect(status().isOk());
}

@Test
@SneakyThrows
void sendUserReceivedScheduledNotification() {
Expand Down
10 changes: 8 additions & 2 deletions core/src/test/java/greencity/controller/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,14 @@ void saveWithEmailPreferencesTest() throws Exception {
"longitude": 20.000000
},
"emailPreferences": [
"LIKES",
"SYSTEM"
{
"emailPreference": "SYSTEM",
"periodicity": "DAILY"
},
{
"emailPreference": "LIKES",
"periodicity": "NEVER"
}
]
}
""";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package greencity.entity;

import greencity.enums.EmailPreference;
import greencity.enums.EmailPreferencePeriodicity;
import jakarta.persistence.Table;
import jakarta.persistence.Id;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -39,4 +40,8 @@ public class UserNotificationPreference {
@Enumerated(EnumType.STRING)
@Column(name = "email_preference")
private EmailPreference emailPreference;

@Enumerated(EnumType.STRING)
@Column
private EmailPreferencePeriodicity periodicity;
}
10 changes: 10 additions & 0 deletions dao/src/main/java/greencity/enums/EmailPreferencePeriodicity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package greencity.enums;

public enum EmailPreferencePeriodicity {
IMMEDIATELY,
TWICE_A_DAY,
DAILY,
WEEKLY,
MONTHLY,
NEVER
}
2 changes: 2 additions & 0 deletions service-api/src/main/java/greencity/dto/CoordinatesDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@EqualsAndHashCode
public class CoordinatesDto {
private Double latitude;
private Double longitude;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.fasterxml.jackson.annotation.JsonBackReference;
import greencity.enums.EmailPreference;
import greencity.enums.EmailPreferencePeriodicity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -13,11 +15,14 @@
@Getter
@Setter
@Builder
@EqualsAndHashCode
public class UserNotificationPreferenceDto {
private Long id;

@JsonBackReference
private UserVO userVO;

private EmailPreference emailPreference;

private EmailPreferencePeriodicity periodicity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import greencity.annotations.ValidName;
import greencity.annotations.ValidSocialNetworkLinks;
import greencity.dto.CoordinatesDto;
import greencity.enums.EmailPreference;
import greencity.validator.BooleanValueDeserializer;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
Expand Down Expand Up @@ -49,5 +48,5 @@ public class UserProfileDtoRequest {

private CoordinatesDto coordinates;

private Set<EmailPreference> emailPreferences;
private Set<UserNotificationPreferenceDto> emailPreferences;
}
31 changes: 0 additions & 31 deletions service-api/src/main/java/greencity/service/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import greencity.dto.user.UserActivationDto;
import greencity.dto.user.UserDeactivationReasonDto;
import greencity.dto.violation.UserViolationMailDto;
import greencity.message.GeneralEmailMessage;
import greencity.message.HabitAssignNotificationMessage;
import greencity.message.ScheduledEmailMessage;
import greencity.message.UserTaggedInCommentMessage;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -127,25 +124,6 @@ void sendRestoreEmail(Long userId, String userFistName, String userEmail, String
*/
void sendSuccessRestorePasswordByEmail(String email, String language, String userName, boolean isUbs);

/**
* Method for sending general email notifications.
*
* @param notification {@link GeneralEmailMessage}
* @author Yurii Midianyi
*/
void sendEmailNotification(GeneralEmailMessage notification);

/**
* Sends an email notification based on the given
* {@link HabitAssignNotificationMessage}.
*
* @param message {@link HabitAssignNotificationMessage} represents the
* information needed for the email notification. This includes
* details such as recipient, subject, and content of the email.
* @author Kizeov Dmytro
*/
void sendHabitAssignNotificationEmail(HabitAssignNotificationMessage message);

/**
* Sends email message to create new password for employee after signUp.
*
Expand All @@ -164,15 +142,6 @@ void sendRestoreEmail(Long userId, String userFistName, String userEmail, String
void sendCreateNewPasswordForEmployee(Long employeeId, String employeeFistName, String employeeEmail, String token,
String language, boolean isUbs);

/**
* Sends an email notification to tagged in comment user
* {@link UserTaggedInCommentMessage}.
*
* @param message {@link UserTaggedInCommentMessage}
* @author Dmytro Dmytruk
*/
void sendTaggedUserInCommentNotificationEmail(UserTaggedInCommentMessage message);

/**
* Sends an email notification user that received scheduled message
* {@link ScheduledEmailMessage}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import greencity.dto.user.UserVO;
import greencity.entity.Language;
import greencity.entity.User;
import greencity.entity.UserNotificationPreference;
import greencity.enums.EmailNotification;
import greencity.enums.EmailPreference;
import greencity.enums.EmailPreferencePeriodicity;
import greencity.enums.Role;
import greencity.enums.UserStatus;
import greencity.exception.exceptions.IdTokenExpiredException;
Expand All @@ -24,7 +27,10 @@
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
Expand Down Expand Up @@ -125,7 +131,7 @@ private SuccessSignInDto handleNewUser(String email, String userName, String pro
}

private User createNewUser(String email, String userName, String profilePicture, String language) {
return User.builder()
User user = User.builder()
.email(email)
.name(userName)
.role(Role.ROLE_USER)
Expand All @@ -138,6 +144,15 @@ private User createNewUser(String email, String userName, String profilePicture,
.rating(DEFAULT_RATING)
.language(Language.builder().id(modelMapper.map(language, Long.class)).build())
.build();
Set<UserNotificationPreference> userNotificationPreferences = Arrays.stream(EmailPreference.values())
.map(emailPreference -> UserNotificationPreference.builder()
.user(user)
.emailPreference(emailPreference)
.periodicity(EmailPreferencePeriodicity.TWICE_A_DAY)
.build())
.collect(Collectors.toSet());
user.setNotificationPreferences(userNotificationPreferences);
return user;
}

private User saveNewUser(User newUser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import greencity.dto.user.UserVO;
import greencity.entity.*;
import greencity.enums.EmailNotification;
import greencity.enums.EmailPreference;
import greencity.enums.EmailPreferencePeriodicity;
import greencity.enums.Role;
import greencity.enums.UserStatus;
import greencity.exception.exceptions.*;
Expand All @@ -25,8 +27,10 @@
import io.jsonwebtoken.ExpiredJwtException;
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -74,6 +78,14 @@ public SuccessSignUpDto signUp(OwnSignUpDto dto, String language) {
user.setOwnSecurity(createOwnSecurity(dto, user));
user.setVerifyEmail(createVerifyEmail(user, jwtTool.generateTokenKey()));
user.setUuid(UUID.randomUUID().toString());
Set<UserNotificationPreference> userNotificationPreferences = Arrays.stream(EmailPreference.values())
.map(emailPreference -> UserNotificationPreference.builder()
.user(user)
.emailPreference(emailPreference)
.periodicity(EmailPreferencePeriodicity.TWICE_A_DAY)
.build())
.collect(Collectors.toSet());
user.setNotificationPreferences(userNotificationPreferences);
try {
User savedUser = userRepo.save(user);
user.setId(savedUser.getId());
Expand Down
Loading

0 comments on commit c6c93a7

Please sign in to comment.