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

#5920 notofication for brought by himself orders #1245

Merged
merged 3 commits into from
Oct 24, 2023
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 @@ -18,6 +18,7 @@ public class NotificationParameter {

@ManyToOne
@JoinColumn(name = "notification_id", nullable = false)
@ToString.Exclude
private UserNotification userNotification;

@Column(nullable = false, name = "key", length = 50)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ public interface NotificationService {
*/
void notifyUnpaidOrder(Order order);

/**
* Notifies the customer that the order status has been changed to "Brought by
* himself".
*
* @param order The order {@link Order} which status was changed.
* @author Maksym Lenets
*/
public void notifySelfPickupOrder(Order order);

/**
* Method that returns page with notifications for user by UUID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import greencity.dto.order.RequestToChangeOrdersDataDto;
import greencity.dto.table.ColumnWidthDto;
import greencity.dto.table.TableParamsDto;
import greencity.entity.user.employee.Employee;

import java.util.List;

Expand Down Expand Up @@ -48,12 +49,12 @@ ChangeOrderResponseDTO chooseOrdersDataSwitcher(String email,
/**
* Method changing order's status.
*
* @param value of {@link String}
* @param ordersId of {@link List}
* @param employeeId of {@link Long}
* @param value of {@link String}
* @param ordersId of {@link List}
* @param employee of {@link Employee}
* @author Liubomyr Pater
*/
List<Long> orderStatusForDevelopStage(List<Long> ordersId, String value, Long employeeId);
List<Long> orderStatusForDevelopStage(List<Long> ordersId, String value, Employee employee);

/**
* Method changing order's date of export.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ public void notifyUnpaidOrder(Order order) {
}
}

/**
* {@inheritDoc}
*/
@Override
public void notifySelfPickupOrder(Order order) {
Set<NotificationParameter> parameters = Set.of(NotificationParameter.builder()
.key(ORDER_NUMBER_KEY)
.value(order.getId().toString())
.build());
fillAndSendNotification(parameters, order, NotificationType.ORDER_STATUS_CHANGED);
}

private Double getAmountToPay(Order order) {
long bonusesInCoins = order.getPointsToUse() == null ? 0L : order.getPointsToUse() * 100L;
long certificatesInCoins = order.getCertificates() == null ? 0L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public ChangeOrderResponseDTO chooseOrdersDataSwitcher(String email,
.orElseThrow(() -> new EntityNotFoundException(EMPLOYEE_NOT_FOUND));
switch (columnName) {
case ORDER_STATUS:
return createReturnForSwitchChangeOrder(orderStatusForDevelopStage(ordersId, value, employee.getId()));
return createReturnForSwitchChangeOrder(orderStatusForDevelopStage(ordersId, value, employee));
case DATE_OF_EXPORT:
return createReturnForSwitchChangeOrder(dateOfExportForDevelopStage(ordersId, value, employee.getId()));
case TIME_OF_EXPORT:
Expand Down Expand Up @@ -454,22 +454,24 @@ private List<OptionForColumnDTO> includeItemsWithoutResponsiblePerson(String nam

/* methods for changing order */
@Override
public synchronized List<Long> orderStatusForDevelopStage(List<Long> ordersId, String value, Long employeeId) {
public synchronized List<Long> orderStatusForDevelopStage(List<Long> ordersId, String updatedStatusValue,
Employee employee) {
List<Long> unresolvedGoals = new ArrayList<>();
for (Long orderId : ordersId) {
try {
Order existedOrder = orderRepository.findById(orderId)
.orElseThrow(() -> new EntityNotFoundException(ORDER_WITH_CURRENT_ID_DOES_NOT_EXIST));
if (isOrderBlockedByAnotherEmployee(existedOrder, employeeId)) {
if (isOrderBlockedByAnotherEmployee(existedOrder, employee.getId())) {
throw new IllegalArgumentException(ORDER_IS_BLOCKED + existedOrder.getBlockedByEmployee().getId());
}
if (existedOrder.getOrderStatus().checkPossibleStatus(value)) {
existedOrder.setOrderStatus(OrderStatus.valueOf(value));
if (existedOrder.getOrderStatus().checkPossibleStatus(updatedStatusValue)) {
existedOrder.setOrderStatus(OrderStatus.valueOf(updatedStatusValue));
removePickUpDetailsAndResponsibleEmployees(existedOrder);
} else {
throw new BadRequestException(
"Such desired status isn't applicable with current status!");
}

if (existedOrder.getOrderStatus() == OrderStatus.CANCELED
&& (existedOrder.getPointsToUse() != 0 || !existedOrder.getCertificates().isEmpty())) {
notificationService.notifyBonusesFromCanceledOrder(existedOrder);
Expand All @@ -478,6 +480,12 @@ public synchronized List<Long> orderStatusForDevelopStage(List<Long> ordersId, S
existedOrder.setBlocked(false);
existedOrder.setBlockedByEmployee(null);
orderRepository.save(existedOrder);

if (OrderStatus.BROUGHT_IT_HIMSELF == OrderStatus.valueOf(updatedStatusValue)) {
eventService.save(OrderHistory.ORDER_BROUGHT_IT_HIMSELF,
employee.getFirstName() + " " + employee.getLastName(), existedOrder);
notificationService.notifySelfPickupOrder(existedOrder);
}
} catch (Exception e) {
unresolvedGoals.add(orderId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ public OrderDetailStatusDto updateOrderDetailStatus(Order order, OrderDetailStat
eventService.saveEvent(OrderHistory.ORDER_DONE, email, order);
} else if (order.getOrderStatus() == OrderStatus.BROUGHT_IT_HIMSELF) {
eventService.saveEvent(OrderHistory.ORDER_BROUGHT_IT_HIMSELF, email, order);
notificationService.notifySelfPickupOrder(order);
} else if (order.getOrderStatus() == OrderStatus.ON_THE_ROUTE) {
eventService.saveEvent(OrderHistory.ORDER_ON_THE_ROUTE, email, order);
}
Expand Down
1 change: 1 addition & 0 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ public static ChangeOfPoints getChangeOfPoints() {
public static Order getOrder() {
return Order.builder()
.id(1L)
.orderDate(LocalDateTime.of(2023, 10, 20, 14, 58))
.payment(Lists.newArrayList(Payment.builder()
.id(1L)
.paymentId("1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,30 @@ void createNotificationDtoTitleEnLanguageTest() {
assertEquals(TEST_NOTIFICATION_TEMPLATE.getTitleEng(), result.getTitle());
}

@Test
void notifySelfPickupOrderTest() {
User user = getUser();
Long orderId = 2L;
Order order = Order.builder()
.id(orderId)
.user(user)
.build();
UserNotification notification = new UserNotification();
notification.setNotificationType(NotificationType.ORDER_STATUS_CHANGED);
notification.setUser(user);
notification.setOrder(order);
Set<NotificationParameter> parameters = Set.of(NotificationParameter.builder()
.key("orderNumber")
.value(orderId.toString())
.userNotification(notification)
.build());

when(userNotificationRepository.save(any())).thenReturn(notification);
when(notificationParameterRepository.saveAll(any())).thenReturn(new ArrayList<>(parameters));

notificationService.notifySelfPickupOrder(order);

verify(userNotificationRepository).save(notification);
verify(notificationParameterRepository).saveAll(parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ void orderStatusForDevelopStage(String oldStatus, String newStatus) {

when(orderRepository.findById(1L)).thenReturn(Optional.of(order));

ordersAdminsPageService.orderStatusForDevelopStage(List.of(1L), newStatus, 1L);
ordersAdminsPageService.orderStatusForDevelopStage(List.of(1L), newStatus, ModelUtils.getEmployee());

assertNotNull(order.getDateOfExport());
assertNotNull(order.getDeliverFrom());
Expand All @@ -535,6 +535,28 @@ void orderStatusForDevelopStage(String oldStatus, String newStatus) {
verify(orderRepository).save(order);
}

@Test
void orderStatusForDevelopStageChangeStatusToBroughtItHimselfTest() {
String newStatus = "BROUGHT_IT_HIMSELF";
Order saved = ModelUtils.getOrder();
saved.setOrderStatus(OrderStatus.FORMED);

Order expected = ModelUtils.getOrder();
expected.setOrderStatus(OrderStatus.BROUGHT_IT_HIMSELF);
expected.setDateOfExport(null);
expected.setDeliverFrom(null);
expected.setDeliverTo(null);
expected.setReceivingStation(null);

when(orderRepository.findById(1L)).thenReturn(Optional.of(saved));

ordersAdminsPageService.orderStatusForDevelopStage(List.of(1L), newStatus, ModelUtils.getEmployee());

verify(eventService).save(eq(OrderHistory.ORDER_BROUGHT_IT_HIMSELF), anyString(), any(Order.class));
verify(notificationService).notifySelfPickupOrder(expected);
verify(orderRepository).save(expected);
}

@ParameterizedTest
@CsvSource({
"CONFIRMED, FORMED",
Expand All @@ -556,7 +578,7 @@ void orderStatusForDevelopStageErasedPickUpDetailsAndResponsibleEmployees(String

when(orderRepository.findById(1L)).thenReturn(Optional.of(order));

ordersAdminsPageService.orderStatusForDevelopStage(List.of(1L), newStatus, 1L);
ordersAdminsPageService.orderStatusForDevelopStage(List.of(1L), newStatus, ModelUtils.getEmployee());

verify(orderRepository).save(order);
}
Expand All @@ -565,23 +587,22 @@ void orderStatusForDevelopStageErasedPickUpDetailsAndResponsibleEmployees(String
void orderStatusForDevelopStageEntityNotFoundException() {
when(orderRepository.findById(1L)).thenReturn(Optional.empty());
assertEquals(List.of(1L),
ordersAdminsPageService.orderStatusForDevelopStage(List.of(1L), "", 1L));
ordersAdminsPageService.orderStatusForDevelopStage(List.of(1L), "", ModelUtils.getEmployee()));
}

@Test
void orderStatusForDevelopStageBadOrderStatusRequestException() {
when(orderRepository.findById(1L))
.thenReturn(Optional.of(ModelUtils.getOrder().setOrderStatus(OrderStatus.FORMED)));
assertEquals(List.of(1L),
ordersAdminsPageService.orderStatusForDevelopStage(List.of(1L), "DONE", 1L));
ordersAdminsPageService.orderStatusForDevelopStage(List.of(1L), "DONE", ModelUtils.getEmployee()));
}

@Test
void orderStatusForDevelopStageBlockedByAnotherEmployeeThrowExceptionTest() {
var orderId = 1L;
var ordersId = List.of(orderId);
var newValue = "CONFIRMED";
var employeeId = 3L;
var anotherEmployeeId = 4L;

var order = Order.builder()
Expand All @@ -593,7 +614,7 @@ void orderStatusForDevelopStageBlockedByAnotherEmployeeThrowExceptionTest() {

when(orderRepository.findById(orderId)).thenReturn(Optional.of(order));

var result = ordersAdminsPageService.orderStatusForDevelopStage(ordersId, newValue, employeeId);
var result = ordersAdminsPageService.orderStatusForDevelopStage(ordersId, newValue, ModelUtils.getEmployee());

verify(orderRepository).findById(orderId);
verify(orderRepository, never()).save(any(Order.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,29 @@ void updateOrderDetailStatusSecond() {
verify(orderRepository, times(2)).save(any());
}

@Test
void updateOrderDetailStatusToBroughtItHimselfTest() {
String email = "[email protected]";
Order saved = getOrder();
saved.setOrderStatus(OrderStatus.FORMED);
Order updated = getOrder();
updated.setOrderStatus(OrderStatus.BROUGHT_IT_HIMSELF);

when(paymentRepository.findAllByOrderId(saved.getId())).thenReturn(saved.getPayment());

OrderDetailStatusRequestDto detailStatusDto = OrderDetailStatusRequestDto.builder()
.orderStatus("BROUGHT_IT_HIMSELF")
.build();

OrderDetailStatusDto result = ubsManagementService.updateOrderDetailStatus(saved, detailStatusDto, email);

verify(eventService).saveEvent(OrderHistory.ORDER_BROUGHT_IT_HIMSELF, email, updated);
verify(notificationService).notifySelfPickupOrder(updated);
verify(orderRepository).save(updated);

assertEquals(OrderStatus.BROUGHT_IT_HIMSELF.name(), result.getOrderStatus());
}

@Test
void getAllEmployeesByPosition() {
Order order = getOrder();
Expand Down