Skip to content

Commit

Permalink
[UBS] - Exception occurs when we try to update order page admin info …
Browse files Browse the repository at this point in the history
…#6128 (#1213)

* update order

* update order and save reason

* formatter

* tests

* formatting

* code refactoring

* code smells

* suggested changes

* required fields

* changed required fields for updating order

* desc

* formatter

* tests

* formatter

* tests

* desc
  • Loading branch information
ABbondar authored Aug 14, 2023
1 parent aa9ecbf commit ac2d69b
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,6 @@ public ResponseEntity<HttpStatus> updateEcoStoreIdToOrder(
* @param updateOrderPageAdminDto {@link UpdateOrderPageAdminDto}.
* @param language {@link String}.
* @param principal {@link Principal}.
* @param description {@link String}.
* @param images {@link MultipartFile}.
*
* @author Bahlay Yuriy.
* @author Anton Bondar.
Expand All @@ -909,11 +907,9 @@ public ResponseEntity<HttpStatus> updateEcoStoreIdToOrder(
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity<HttpStatus> updatePageAdminInfo(@PathVariable(name = "id") Long orderId,
@Valid @RequestPart UpdateOrderPageAdminDto updateOrderPageAdminDto,
@RequestParam String language,
@ApiIgnore Principal principal, String description,
@RequestPart(required = false) @Nullable MultipartFile[] images) {
@RequestParam String language, @ApiIgnore Principal principal) {
ubsManagementService.updateOrderAdminPageInfoAndSaveReason(orderId, updateOrderPageAdminDto, language,
principal.getName(), description, images);
principal.getName());
return ResponseEntity.status(HttpStatus.CREATED).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,6 @@ void updatePageAdminInfoTest() throws Exception {
mockMvc.perform(
builder.file(jsonFile)
.param("language", "en")
.param("description", "false")
.principal(principal)
.contentType(MediaType.MULTIPART_FORM_DATA))
.andExpect(status().isCreated());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public final class ErrorMessage {
public static final String INVALID_URL = "Invalid URL: ";
public static final String LOCATION_CAN_NOT_BE_DELETED =
"Such location cannot be deleted as it is linked to the tariff";
public static final String REASON_NOT_TAKING_BAG = "Please fill in the reason of not taking bag";

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package greencity.dto.order;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.web.multipart.MultipartFile;

@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class NotTakenOutReasonDto {
private String description;
private MultipartFile[] images;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import greencity.dto.customer.UbsCustomersDtoUpdate;
import greencity.dto.employee.UpdateResponsibleEmployeeDto;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

Expand All @@ -22,4 +27,5 @@ public class UpdateOrderPageAdminDto {
private List<UpdateResponsibleEmployeeDto> updateResponsibleEmployeeDto;
private Double writeOffStationSum;
private Double ubsCourierSum;
private NotTakenOutReasonDto notTakenOutReasonDto;
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,18 @@ ManualPaymentResponseDto updateManualPayment(Long paymentId, ManualPaymentReques
void updateEcoNumberForOrder(EcoNumberDto ecoNumberDto, Order order, String email);

/**
* This is method which is updates admin page info for order and save reason.
* This is method which is updates admin page info for order and save reason if
* needed.
*
* @param orderId {@link Long}.
* @param updateOrderPageAdminDto {@link UpdateOrderPageAdminDto}.
* @param language {@link String}.
* @param email {@link String}.
* @param description {@link String}.
* @param images {@link MultipartFile}.
*
* @author Anton Bondar.
*/
void updateOrderAdminPageInfoAndSaveReason(Long orderId, UpdateOrderPageAdminDto updateOrderPageAdminDto,
String language,
String email, String description, MultipartFile[] images);
String language, String email);

/**
* This is method which is updates admin page info for order.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
import static greencity.constant.ErrorMessage.ORDER_CAN_NOT_BE_UPDATED;
import static greencity.constant.ErrorMessage.ORDER_WITH_CURRENT_ID_DOES_NOT_EXIST;
import static greencity.constant.ErrorMessage.PAYMENT_NOT_FOUND;
import static greencity.constant.ErrorMessage.REASON_NOT_TAKING_BAG;
import static greencity.constant.ErrorMessage.RECEIVING_STATION_NOT_FOUND;
import static greencity.constant.ErrorMessage.RECEIVING_STATION_NOT_FOUND_BY_ID;
import static greencity.constant.ErrorMessage.USER_HAS_NO_OVERPAYMENT;
Expand Down Expand Up @@ -1675,21 +1676,18 @@ private void updateOrderPageFields(UpdateOrderPageAdminDto updateOrderPageDto, O
* @param updateOrderPageAdminDto {@link UpdateOrderPageAdminDto}.
* @param language {@link String}.
* @param email {@link String}.
* @param description {@link String}.
* @param images {@link MultipartFile}.
*
* @author Anton Bondar.
*/
@Override
@Transactional
public void updateOrderAdminPageInfoAndSaveReason(Long orderId, UpdateOrderPageAdminDto updateOrderPageAdminDto,
String language,
String email, String description, MultipartFile[] images) {
String language, String email) {
Order order = orderRepository.findById(orderId)
.orElseThrow(() -> new NotFoundException(ORDER_WITH_CURRENT_ID_DOES_NOT_EXIST + orderId));

checkOrderStatusAndSaveReason(order, updateOrderPageAdminDto);
updateOrderAdminPageInfo(updateOrderPageAdminDto, order, language, email);
saveReason(order, description, images);
}

/**
Expand Down Expand Up @@ -1719,6 +1717,20 @@ && nonNull(updateOrderPageDto.getGeneralOrderInfo())) {
}
}

private void checkOrderStatusAndSaveReason(Order order, UpdateOrderPageAdminDto updateOrderPageAdminDto) {
var status = updateOrderPageAdminDto.getGeneralOrderInfo().getOrderStatus();
var desc = updateOrderPageAdminDto.getNotTakenOutReasonDto().getDescription();
var images = updateOrderPageAdminDto.getNotTakenOutReasonDto().getImages();

if (status.equalsIgnoreCase(OrderStatus.NOT_TAKEN_OUT.name()) && (desc == null || desc.isEmpty())) {
throw new BadRequestException(REASON_NOT_TAKING_BAG);
}
if (images == null) {
images = new MultipartFile[0];
}
saveReason(order, desc, images);
}

private void setUbsCourierSumAndWriteOffStationSum(Order order, Double writeOffStationSum, Double ubsCourierSum) {
if (writeOffStationSum != null) {
order.setWriteOffStationSum(convertBillsIntoCoins(writeOffStationSum));
Expand Down
5 changes: 5 additions & 0 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import greencity.dto.order.ExportDetailsDtoUpdate;
import greencity.dto.order.GroupedOrderDto;
import greencity.dto.order.NotTakenOrderReasonDto;
import greencity.dto.order.NotTakenOutReasonDto;
import greencity.dto.order.OrderAddressDtoRequest;
import greencity.dto.order.OrderAddressDtoResponse;
import greencity.dto.order.OrderAddressExportDetailsDtoUpdate;
Expand Down Expand Up @@ -3274,6 +3275,10 @@ public static UpdateOrderPageAdminDto updateOrderPageAdminDto() {
.positionId(2L)
.employeeId(2L)
.build()))
.notTakenOutReasonDto(
NotTakenOutReasonDto.builder()
.description("")
.build())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2891,9 +2891,115 @@ void saveOrderIdForRefundThrowsNotFoundExceptionTest() {
@Test
void updateOrderAdminPageInfoAndSaveReasonTest() {
var dto = updateOrderPageAdminDto();
MockMultipartFile[] multipartFiles = new MockMultipartFile[0];
Order order = getOrder();

TariffsInfo tariffsInfo = getTariffsInfo();
order.setOrderDate(LocalDateTime.now()).setTariffsInfo(tariffsInfo);

Employee employee = getEmployee();

when(orderRepository.findById(1L)).thenReturn(Optional.of(order));
when(employeeRepository.findByEmail("[email protected]")).thenReturn(Optional.of(employee));
when(tariffsInfoRepository.findTariffsInfoByIdForEmployee(1L, 1L))
.thenReturn(Optional.of(tariffsInfo));
when(paymentRepository.findAllByOrderId(1L)).thenReturn(List.of(getPayment()));

when(orderAddressRepository.findById(1L)).thenReturn(Optional.of(getOrderAddress()));
when(receivingStationRepository.findAll()).thenReturn(List.of(getReceivingStation()));

var receivingStation = getReceivingStation();

when(receivingStationRepository.findById(1L)).thenReturn(Optional.of(receivingStation));
when(orderRepository.getOrderDetails(1L)).thenReturn(Optional.ofNullable(getOrdersStatusFormedDto()));

ubsManagementService.updateOrderAdminPageInfoAndSaveReason(1L, dto, "en", "[email protected]");

verify(orderRepository).findById(1L);
verify(employeeRepository).findByEmail("[email protected]");
verify(tariffsInfoRepository).findTariffsInfoByIdForEmployee(1L, 1L);
verify(paymentRepository).findAllByOrderId(1L);

verify(orderAddressRepository).findById(1L);
verify(receivingStationRepository).findAll();

verify(receivingStationRepository).findById(1L);
verify(orderRepository).getOrderDetails(1L);
}

@Test
void updateOrderAdminPageInfoAndSaveReasonThrowsBadRequestExceptionWithEmptyDescFieldTest() {
var dto = updateOrderPageAdminDto();
Order order = getOrder();
dto.getGeneralOrderInfo().setOrderStatus(OrderStatus.NOT_TAKEN_OUT.name());
TariffsInfo tariffsInfo = getTariffsInfo();
order.setOrderDate(LocalDateTime.now()).setTariffsInfo(tariffsInfo);

when(orderRepository.findById(1L)).thenReturn(Optional.of(order));
assertThrows(BadRequestException.class,
() -> ubsManagementService.updateOrderAdminPageInfoAndSaveReason(1L, dto, "en", "[email protected]"));
verify(orderRepository).findById(1L);
}

@Test
void updateOrderAdminPageInfoAndSaveReasonThrowsBadRequestExceptionWithNullDescFieldTest() {
var dto = updateOrderPageAdminDto();
Order order = getOrder();
dto.getGeneralOrderInfo().setOrderStatus(OrderStatus.NOT_TAKEN_OUT.name());
dto.getNotTakenOutReasonDto().setDescription(null);
TariffsInfo tariffsInfo = getTariffsInfo();
order.setOrderDate(LocalDateTime.now()).setTariffsInfo(tariffsInfo);

when(orderRepository.findById(1L)).thenReturn(Optional.of(order));
assertThrows(BadRequestException.class,
() -> ubsManagementService.updateOrderAdminPageInfoAndSaveReason(1L, dto, "en", "[email protected]"));
verify(orderRepository).findById(1L);
}

@Test
void updateOrderAdminPageInfoAndSaveReasonWithImagesTest() {
var dto = updateOrderPageAdminDto();
Order order = getOrder();
dto.getNotTakenOutReasonDto().setImages(new MultipartFile[2]);

TariffsInfo tariffsInfo = getTariffsInfo();
order.setOrderDate(LocalDateTime.now()).setTariffsInfo(tariffsInfo);

Employee employee = getEmployee();

when(orderRepository.findById(1L)).thenReturn(Optional.of(order));
when(employeeRepository.findByEmail("[email protected]")).thenReturn(Optional.of(employee));
when(tariffsInfoRepository.findTariffsInfoByIdForEmployee(1L, 1L))
.thenReturn(Optional.of(tariffsInfo));
when(paymentRepository.findAllByOrderId(1L)).thenReturn(List.of(getPayment()));

when(orderAddressRepository.findById(1L)).thenReturn(Optional.of(getOrderAddress()));
when(receivingStationRepository.findAll()).thenReturn(List.of(getReceivingStation()));

var receivingStation = getReceivingStation();

when(receivingStationRepository.findById(1L)).thenReturn(Optional.of(receivingStation));
when(orderRepository.getOrderDetails(1L)).thenReturn(Optional.ofNullable(getOrdersStatusFormedDto()));

ubsManagementService.updateOrderAdminPageInfoAndSaveReason(1L, dto, "en", "[email protected]");

verify(orderRepository).findById(1L);
verify(employeeRepository).findByEmail("[email protected]");
verify(tariffsInfoRepository).findTariffsInfoByIdForEmployee(1L, 1L);
verify(paymentRepository).findAllByOrderId(1L);

verify(orderAddressRepository).findById(1L);
verify(receivingStationRepository).findAll();

verify(receivingStationRepository).findById(1L);
verify(orderRepository).getOrderDetails(1L);
}

@Test
void updateOrderAdminPageInfoAndSaveReasonWithReasonDescTest() {
var dto = updateOrderPageAdminDto();
Order order = getOrder();
dto.getGeneralOrderInfo().setOrderStatus(OrderStatus.NOT_TAKEN_OUT.name());
dto.getNotTakenOutReasonDto().setDescription("desc");

TariffsInfo tariffsInfo = getTariffsInfo();
order.setOrderDate(LocalDateTime.now()).setTariffsInfo(tariffsInfo);
Expand All @@ -2914,8 +3020,7 @@ void updateOrderAdminPageInfoAndSaveReasonTest() {
when(receivingStationRepository.findById(1L)).thenReturn(Optional.of(receivingStation));
when(orderRepository.getOrderDetails(1L)).thenReturn(Optional.ofNullable(getOrdersStatusFormedDto()));

ubsManagementService.updateOrderAdminPageInfoAndSaveReason(1L, dto, "en", "[email protected]", "desc",
multipartFiles);
ubsManagementService.updateOrderAdminPageInfoAndSaveReason(1L, dto, "en", "[email protected]");

verify(orderRepository).findById(1L);
verify(employeeRepository).findByEmail("[email protected]");
Expand Down

0 comments on commit ac2d69b

Please sign in to comment.