Skip to content

Commit

Permalink
Merge pull request #6380 from ita-social-projects/5874-email-notifica…
Browse files Browse the repository at this point in the history
…tion-about-event-creation

Event creation notification implementation
  • Loading branch information
HelenSotnik authored Sep 22, 2023
2 parents 697ce48 + 4078ea4 commit a8b5e0e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 3 deletions.
15 changes: 15 additions & 0 deletions service-api/src/main/java/greencity/client/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.servlet.http.HttpServletRequest;

import greencity.dto.eventcomment.EventCommentForSendEmailDto;
import greencity.message.SendEventCreationNotification;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -551,4 +552,18 @@ private String getTokenFromCookies(Cookie[] cookies) {
.map(Cookie::getValue).orElse(null);
return token == null ? null : "Bearer " + token;
}

/**
* Method to send email notification about event creation to GreenCityUser.
*
* @param notification {@link SendEventCreationNotification} has message for
* sending email to user about event creation status.
* @author Olena Sotnik.
*/
public void sendEventCreationNotification(SendEventCreationNotification notification) {
HttpEntity<SendEventCreationNotification> entity = new HttpEntity<>(notification, new HttpHeaders());
restTemplate.exchange(greenCityUserServerAddress
+ RestTemplateLinks.SEND_EVENT_CREATION_NOTIFICATION, HttpMethod.POST, entity, Object.class)
.getBody();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class RestTemplateLinks {
public static final String SEND_REPORT = "/email/sendReport";
public static final String CHANGE_PLACE_STATUS = "/email/changePlaceStatus";
public static final String SEND_HABIT_NOTIFICATION = "/email/sendHabitNotification";
public static final String SEND_EVENT_CREATION_NOTIFICATION = "/email/sendEventNotification";
public static final String USER = "/user";
public static final String USER_FIND_ALL = "user/findAll";
public static final String FRIENDS = "/friends";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package greencity.message;

import java.io.Serializable;

import lombok.Getter;
import lombok.ToString;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;

/**
* Message, that is used for sending notification about event creation status on
* user email.
*/
@Getter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SendEventCreationNotification implements Serializable {
private String email;
private String messageBody;
}
13 changes: 11 additions & 2 deletions service-api/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import greencity.dto.verifyemail.VerifyEmailVO;
import greencity.enums.Role;
import greencity.enums.ShoppingListItemStatus;
import greencity.message.AddEcoNewsMessage;
import greencity.message.SendChangePlaceStatusEmailMessage;
import greencity.message.SendHabitNotification;
import greencity.message.SendEventCreationNotification;
import greencity.message.SendReportEmailMessage;
import greencity.message.SendHabitNotification;
import greencity.message.AddEcoNewsMessage;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Arrays;
Expand Down Expand Up @@ -211,4 +213,11 @@ public static AddCustomHabitDtoResponse getAddCustomHabitDtoResponse() {
.tagIds(Set.of(20L))
.build();
}

public static SendEventCreationNotification getSendEventCreationNotification() {
return SendEventCreationNotification.builder()
.email("[email protected]")
.messageBody("You have successfully created event")
.build();
}
}
14 changes: 14 additions & 0 deletions service-api/src/test/java/greencity/client/RestClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import greencity.dto.eventcomment.EventCommentForSendEmailDto;
import greencity.enums.EmailNotification;
import greencity.message.SendChangePlaceStatusEmailMessage;
import greencity.message.SendEventCreationNotification;
import greencity.message.SendHabitNotification;
import greencity.message.SendReportEmailMessage;

Expand Down Expand Up @@ -561,4 +562,17 @@ void findAllUsersCities() {
.thenReturn(ResponseEntity.status(HttpStatus.OK).body(expected));
assertEquals(expected, restClient.findAllUsersCities());
}

@Test
void sendEventCreationNotificationTest() {
SendEventCreationNotification notification = ModelUtils.getSendEventCreationNotification();
HttpEntity<SendEventCreationNotification> entity = new HttpEntity<>(notification, new HttpHeaders());
when(restTemplate.exchange(greenCityUserServerAddress
+ RestTemplateLinks.SEND_EVENT_CREATION_NOTIFICATION, HttpMethod.POST, entity, Object.class))
.thenReturn(ResponseEntity.ok(Object));
restClient.sendEventCreationNotification(notification);

verify(restTemplate).exchange(greenCityUserServerAddress
+ RestTemplateLinks.SEND_EVENT_CREATION_NOTIFICATION, HttpMethod.POST, entity, Object.class);
}
}
18 changes: 17 additions & 1 deletion service/src/main/java/greencity/service/EventServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import greencity.exception.exceptions.BadRequestException;
import greencity.exception.exceptions.NotFoundException;
import greencity.exception.exceptions.UserHasNoPermissionToAccessException;
import greencity.message.SendEventCreationNotification;
import greencity.repository.EventRepo;
import greencity.repository.UserRepo;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -102,7 +103,7 @@ public EventDto save(AddEventDtoRequest addEventDtoRequest, String email,
}.getType()));

Event savedEvent = eventRepo.save(toSave);

sendEmailNotification(savedEvent.getTitle(), organizer.getFirstName(), organizer.getEmail());
return buildEventDto(savedEvent, organizer.getId());
}

Expand Down Expand Up @@ -780,4 +781,19 @@ private EventDto buildEventDto(Event event, Long userId) {
private EventDto buildEventDto(Event event) {
return modelMapper.map(event, EventDto.class);
}

/**
* {@inheritDoc}
*
* @author Olena Sotnik.
*/
public void sendEmailNotification(String eventTitle, String userName, String email) {
String message = "Dear, " + userName + "!"
+ "\nYou have successfully created an event: " + eventTitle;
SendEventCreationNotification notification = SendEventCreationNotification.builder()
.email(email)
.messageBody(message)
.build();
restClient.sendEventCreationNotification(notification);
}
}

0 comments on commit a8b5e0e

Please sign in to comment.