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 #7558

Merged
merged 9 commits into from
Oct 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.util.Set;

public interface UserNotificationPreferenceRepo extends JpaRepository<UserNotificationPreference, Long> {
void deleteAllByUserId(Long id);

Set<UserNotificationPreference> findAllByUserId(Long id);

boolean existsByUserIdAndEmailPreferenceAndPeriodicity(Long id, EmailPreference emailPreference,
Expand Down
10 changes: 4 additions & 6 deletions service-api/src/main/java/greencity/client/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import greencity.dto.user.UserVO;
import greencity.enums.EmailPreference;
import greencity.enums.Role;
import greencity.message.ChangePlaceStatusDto;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import greencity.message.ScheduledEmailMessage;
import greencity.message.SendChangePlaceStatusEmailMessage;
import greencity.message.SendHabitNotification;
import greencity.message.SendReportEmailMessage;
import jakarta.servlet.http.Cookie;
Expand Down Expand Up @@ -457,18 +457,16 @@ public void scheduleDeleteDeactivatedUsers() {
}

/**
* send SendChangePlaceStatusEmailMessage to GreenCityUser.
* Send ChangePlaceStatusDto to GreenCityUser.
*
* @param changePlaceStatusEmailMessage with information for sending email
* during status update for {@link PlaceVO}
* when PlaceStatus.PROPOSED.
* @author Taras Kavkalo
*/
public void changePlaceStatus(SendChangePlaceStatusEmailMessage changePlaceStatusEmailMessage) {
public void changePlaceStatus(ChangePlaceStatusDto changePlaceStatusEmailMessage) {
HttpHeaders headers = setHeader();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<SendChangePlaceStatusEmailMessage> entity =
new HttpEntity<>(changePlaceStatusEmailMessage, headers);
HttpEntity<ChangePlaceStatusDto> entity = new HttpEntity<>(changePlaceStatusEmailMessage, headers);
restTemplate.exchange(greenCityUserServerAddress
+ RestTemplateLinks.CHANGE_PLACE_STATUS, HttpMethod.POST, entity, Object.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package greencity.message;

import java.io.Serializable;
import greencity.enums.PlaceStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
import lombok.Data;
import lombok.NoArgsConstructor;

@Getter
@ToString
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public final class SendChangePlaceStatusEmailMessage implements Serializable, EmailMessage {
public final class ChangePlaceStatusDto implements EmailMessage {
private String authorFirstName;
private String authorLanguage;
private String placeName;
private String placeStatus;
private PlaceStatus placeStatus;
private String authorEmail;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package greencity.message;

public interface EmailMessage {
public String getEmail();
String getEmail();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import greencity.dto.category.CategoryDto;
import greencity.dto.place.PlaceNotificationDto;
import greencity.dto.user.PlaceAuthorDto;
import java.io.Serializable;
import greencity.dto.user.SubscriberDto;
import greencity.enums.EmailNotification;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
import lombok.Data;
import lombok.NoArgsConstructor;

@Getter
@ToString
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SendReportEmailMessage implements Serializable {
private List<PlaceAuthorDto> subscribers;
public class SendReportEmailMessage {
private List<SubscriberDto> subscribers;
private Map<CategoryDto, List<PlaceNotificationDto>> categoriesDtoWithPlacesDtoMap;
private String emailNotification;
private EmailNotification emailNotification;
}
23 changes: 8 additions & 15 deletions service-api/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
import greencity.dto.shoppinglistitem.CustomShoppingListItemResponseDto;
import greencity.dto.tag.TagUaEnDto;
import greencity.dto.user.EcoNewsAuthorDto;
import greencity.dto.user.PlaceAuthorDto;
import greencity.dto.user.SubscriberDto;
import greencity.dto.user.UserShoppingListItemResponseDto;
import greencity.dto.user.UserVO;
import greencity.dto.verifyemail.VerifyEmailVO;
import greencity.enums.EmailNotification;
import greencity.enums.PlaceStatus;
import greencity.enums.Role;
import greencity.enums.ShoppingListItemStatus;
import greencity.message.ChangePlaceStatusDto;
import greencity.message.ScheduledEmailMessage;
import greencity.message.SendChangePlaceStatusEmailMessage;
import greencity.message.SendReportEmailMessage;
import greencity.message.SendHabitNotification;
import java.util.UUID;
Expand Down Expand Up @@ -55,17 +56,9 @@ public static PlaceNotificationDto getPlaceNotificationDto() {
.build();
}

private static PlaceAuthorDto getPlaceAuthorDto() {
return PlaceAuthorDto.builder()
.id(1L)
.email("test@gmail.com")
.name("taras")
.build();
}

public static SendChangePlaceStatusEmailMessage getSendChangePlaceStatusEmailMessage() {
return SendChangePlaceStatusEmailMessage.builder()
.placeStatus("status")
public static ChangePlaceStatusDto getSendChangePlaceStatusEmailMessage() {
return ChangePlaceStatusDto.builder()
.placeStatus(PlaceStatus.APPROVED)
.authorEmail("test@gmail.com")
.placeName("placeName")
.authorFirstName("taras")
Expand Down Expand Up @@ -95,10 +88,10 @@ public static CategoryDto getCategoryDto() {

public static SendReportEmailMessage getSendReportEmailMessage() {
return SendReportEmailMessage.builder()
.emailNotification("notification")
.emailNotification(EmailNotification.WEEKLY)
.categoriesDtoWithPlacesDtoMap(Collections.singletonMap(
getCategoryDto(), Collections.singletonList(getPlaceNotificationDto())))
.subscribers(Collections.singletonList(getPlaceAuthorDto()))
.subscribers(getSubscribers())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import greencity.dto.achievement.UserVOAchievement;
import greencity.enums.EmailNotification;
import greencity.enums.Role;
import greencity.message.ChangePlaceStatusDto;
import greencity.message.ScheduledEmailMessage;
import greencity.message.SendChangePlaceStatusEmailMessage;
import greencity.message.SendHabitNotification;
import greencity.message.SendReportEmailMessage;
import java.util.Collections;
Expand Down Expand Up @@ -413,11 +413,11 @@ void sendReport() {

@Test
void changePlaceStatus() {
SendChangePlaceStatusEmailMessage message = ModelUtils.getSendChangePlaceStatusEmailMessage();
ChangePlaceStatusDto message = ModelUtils.getSendChangePlaceStatusEmailMessage();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set(AUTHORIZATION, ACCESS_TOKEN);
HttpEntity<SendChangePlaceStatusEmailMessage> entity = new HttpEntity<>(message, httpHeaders);
HttpEntity<ChangePlaceStatusDto> entity = new HttpEntity<>(message, httpHeaders);

when(jwtTool.createAccessToken(anyString(), any(Role.class))).thenReturn(TOKEN);
when(restTemplate.exchange(GREEN_CITY_USER_ADDRESS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import greencity.dto.notification.EmailNotificationDto;
import greencity.dto.place.PlaceNotificationDto;
import greencity.dto.place.PlaceVO;
import greencity.dto.user.PlaceAuthorDto;
import greencity.dto.user.SubscriberDto;
import greencity.entity.Language;
import greencity.entity.Notification;
import greencity.entity.Place;
Expand All @@ -23,28 +23,26 @@
import greencity.repository.PlaceRepo;
import greencity.repository.UserNotificationPreferenceRepo;
import greencity.repository.UserRepo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

@Slf4j
@Service
Expand All @@ -66,16 +64,15 @@ public class NotificationServiceImpl implements NotificationService {
public void sendImmediatelyReport(PlaceVO newPlace) {
log.info(LogMessage.IN_SEND_IMMEDIATELY_REPORT, newPlace.getName());
EmailNotification emailNotification = EmailNotification.IMMEDIATELY;
List<PlaceAuthorDto> subscribers = getSubscribers(emailNotification);
List<SubscriberDto> subscribers = getSubscribers(emailNotification);

Map<CategoryDto, List<PlaceNotificationDto>> categoriesDtoWithPlacesDtoMap = new HashMap<>();
CategoryDto map = modelMapper.map(newPlace.getCategory(), CategoryDto.class);
List<PlaceNotificationDto> placeDtoList =
Collections.singletonList(modelMapper.map(newPlace, PlaceNotificationDto.class));
List<PlaceNotificationDto> placeDtoList = List.of(modelMapper.map(newPlace, PlaceNotificationDto.class));
categoriesDtoWithPlacesDtoMap.put(map, placeDtoList);

restClient.sendReport(new SendReportEmailMessage(subscribers,
categoriesDtoWithPlacesDtoMap, emailNotification.toString()));
restClient.sendReport(new SendReportEmailMessage(subscribers, categoriesDtoWithPlacesDtoMap,
emailNotification));
}

/**
Expand Down Expand Up @@ -286,8 +283,6 @@ private boolean isTimeToSendScheduleNotification(Long userId, EmailPreference em

/**
* {@inheritDoc}
*
* @author Viktoriia Herchanivska
*/
@Override
public void sendEmailNotification(EmailNotificationDto notificationDto) {
Expand Down Expand Up @@ -338,7 +333,7 @@ public void sendEmailNotification(EmailNotificationDto notificationDto) {

private void sendReport(EmailNotification emailNotification, LocalDateTime startDate) {
log.info(LogMessage.IN_SEND_REPORT, emailNotification);
List<PlaceAuthorDto> subscribers = getSubscribers(emailNotification);
List<SubscriberDto> subscribers = getSubscribers(emailNotification);
Map<CategoryDto, List<PlaceNotificationDto>> categoriesDtoWithPlacesDtoMap = new HashMap<>();
LocalDateTime endDate = LocalDateTime.now(ZONE_ID);
if (!subscribers.isEmpty()) {
Expand All @@ -347,16 +342,16 @@ private void sendReport(EmailNotification emailNotification, LocalDateTime start
categoriesDtoWithPlacesDtoMap = getCategoriesDtoWithPlacesDtoMap(places);
}
if (!categoriesDtoWithPlacesDtoMap.isEmpty()) {
restClient.sendReport(
new SendReportEmailMessage(subscribers, categoriesDtoWithPlacesDtoMap, emailNotification.toString()));
restClient.sendReport(new SendReportEmailMessage(subscribers, categoriesDtoWithPlacesDtoMap,
emailNotification));
}
}

private List<PlaceAuthorDto> getSubscribers(EmailNotification emailNotification) {
private List<SubscriberDto> getSubscribers(EmailNotification emailNotification) {
log.info(LogMessage.IN_GET_SUBSCRIBERS, emailNotification);
return restClient.findAllByEmailNotification(emailNotification).stream()
.map(o -> modelMapper.map(o, PlaceAuthorDto.class))
.collect(Collectors.toList());
.map(o -> modelMapper.map(o, SubscriberDto.class))
.toList();
}

private Map<CategoryDto, List<PlaceNotificationDto>> getCategoriesDtoWithPlacesDtoMap(List<Place> places) {
Expand Down Expand Up @@ -422,4 +417,4 @@ private ScheduledEmailMessage createScheduledEmailMessage(Notification notificat
private String createBaseLink(Notification notification) {
return clientAddress + "/#/profile/" + notification.getTargetUser().getId() + "/notifications";
}
}
}
18 changes: 9 additions & 9 deletions service/src/main/java/greencity/service/PlaceServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
import greencity.exception.exceptions.NotFoundException;
import greencity.exception.exceptions.PlaceStatusException;
import greencity.exception.exceptions.UserBlockedException;
import greencity.message.SendChangePlaceStatusEmailMessage;
import greencity.message.ChangePlaceStatusDto;
import greencity.repository.CategoryRepo;
import greencity.repository.PlaceRepo;
import greencity.repository.UserRepo;
Expand Down Expand Up @@ -313,8 +313,6 @@ public PageableDto<AdminPlaceDto> findAll(Pageable pageable, Principal principal

/**
* {@inheritDoc}
*
* @author Nazar Vladyka
*/
@Override
public UpdatePlaceStatusDto updateStatus(Long id, PlaceStatus status) {
Expand All @@ -328,8 +326,11 @@ public UpdatePlaceStatusDto updateStatus(Long id, PlaceStatus status) {
notificationService.sendImmediatelyReport(modelMapper.map(updatable, PlaceVO.class));
}
if (oldStatus.equals(PlaceStatus.PROPOSED)) {
restClient.changePlaceStatus(new SendChangePlaceStatusEmailMessage(updatable.getAuthor().getName(),
updatable.getName(), updatable.getStatus().toString().toLowerCase(),
restClient.changePlaceStatus(new ChangePlaceStatusDto(
updatable.getAuthor().getName(),
updatable.getAuthor().getLanguage().getCode(),
updatable.getName(),
updatable.getStatus(),
updatable.getAuthor().getEmail()));
}
return modelMapper.map(placeRepo.save(updatable), UpdatePlaceStatusDto.class);
Expand Down Expand Up @@ -608,10 +609,9 @@ public PlaceResponse addPlaceFromUi(AddPlaceDto dto, String email, MultipartFile
private void mapMultipartFilesToPhotos(MultipartFile[] images, Place place, User user) {
if (images != null && images.length > 0 && images[0] != null) {
List<Photo> placePhotos = new ArrayList<>();
for (int i = 0; i < images.length; i++) {
if (images[i] != null) {
placePhotos
.add(Photo.builder().place(place).name(fileService.upload(images[i])).user(user).build());
for (MultipartFile image : images) {
if (image != null) {
placePhotos.add(Photo.builder().place(place).name(fileService.upload(image)).user(user).build());
}
}
place.setPhotos(placePhotos);
Expand Down
Loading
Loading