From 7130a9db678e46dbc28ac929b9c66a99d6efc5df Mon Sep 17 00:00:00 2001 From: Hrenevych Ivan <121890324+Warded120@users.noreply.github.com> Date: Mon, 16 Dec 2024 12:09:16 +0200 Subject: [PATCH] 7834 greencityubs add parameter to freeze tablecolumnwidthforemployee3 (#1553) * created entity field isTableFreeze and created endpoint to work with it * added tests for changeIsFreezeStatus * added more tests for coverage * removed wildcard imports * throwing exception instead of returning from service method * added new tests and refactored old ones * formatted files * resolved issues after merging dev --------- Co-authored-by: LazarenkoDmytro --- .../configuration/SecurityConfig.java | 1 + .../controller/AdminUbsController.java | 20 ++++ .../table/TableColumnWidthForEmployee.java | 2 + .../db/changelog/db.changelog-master.xml | 1 + ...to-column_width_for_employee-Warded120.xml | 20 ++++ .../java/greencity/constant/ErrorMessage.java | 4 + .../greencity/dto/table/ColumnWidthDto.java | 1 + .../ubs/manager/BigOrderTableServiceView.java | 10 ++ .../manager/BigOrderTableViewServiceImpl.java | 41 ++++++++- .../src/test/java/greencity/ModelUtils.java | 12 +++ .../manager/BigOrderTableServiceImplTest.java | 92 ++++++++++++++++++- 11 files changed, 198 insertions(+), 6 deletions(-) create mode 100644 dao/src/main/resources/db/changelog/logs/2024-11-28-change-add-column-isTableFreeze-to-column_width_for_employee-Warded120.xml diff --git a/core/src/main/java/greencity/configuration/SecurityConfig.java b/core/src/main/java/greencity/configuration/SecurityConfig.java index 6fbf2f49a9..9ed2077d4b 100644 --- a/core/src/main/java/greencity/configuration/SecurityConfig.java +++ b/core/src/main/java/greencity/configuration/SecurityConfig.java @@ -174,6 +174,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti UBS_MANAG_LINK + "/unblockOrders", UBS_MANAG_LINK + "/save-reason/{id}", UBS_MANAG_LINK + "/orderTableColumnsWidth", + UBS_MANAG_LINK + "/saveOrderTableColumnsWidthIsFreeze", ADMIN_EMPL_LINK + "/**", ADMIN_LINK + "/notification/update-template/{id}", ADMIN_LINK + "/notification/change-template-status/{id}", diff --git a/core/src/main/java/greencity/controller/AdminUbsController.java b/core/src/main/java/greencity/controller/AdminUbsController.java index 6c87f76e5a..91fefcaf9a 100644 --- a/core/src/main/java/greencity/controller/AdminUbsController.java +++ b/core/src/main/java/greencity/controller/AdminUbsController.java @@ -16,6 +16,7 @@ import greencity.service.ubs.OrdersForUserService; import greencity.service.ubs.ValuesForUserTableService; import greencity.service.ubs.ViolationService; +import greencity.service.ubs.manager.BigOrderTableServiceView; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.ArraySchema; @@ -42,6 +43,7 @@ public class AdminUbsController { private final ValuesForUserTableService valuesForUserTable; private final OrdersForUserService ordersForUserService; private final ViolationService violationService; + private final BigOrderTableServiceView bigOrderTableServiceView; /** * Controller for obtaining all users that made at least one order. @@ -250,4 +252,22 @@ public ResponseEntity getAllViolationsByUser( public ResponseEntity> getAllInformationForLocations() { return ResponseEntity.ok(ordersAdminsPageService.getAllLocationsInfo()); } + + @Operation(summary = "sets a isTableFreeze value for tableColumnWidthForEmployee entity") + @ApiResponses(value = { + @ApiResponse( + responseCode = "200", + description = "isTableFreeze is successfully updated", + content = @Content(array = @ArraySchema(schema = @Schema(implementation = Long.class)))), + @ApiResponse( + responseCode = "404", + description = HttpStatuses.NOT_FOUND, + content = @Content) + }) + @PutMapping("/saveOrderTableColumnsWidthIsFreeze") + public ResponseEntity saveIsFreeze(@CurrentUserUuid String uuid, + @RequestParam boolean value) { + bigOrderTableServiceView.changeIsFreezeStatus(uuid, value); + return ResponseEntity.status(HttpStatus.OK).body(HttpStatus.OK); + } } diff --git a/dao/src/main/java/greencity/entity/table/TableColumnWidthForEmployee.java b/dao/src/main/java/greencity/entity/table/TableColumnWidthForEmployee.java index 7c09169b81..9ad1e5b36a 100644 --- a/dao/src/main/java/greencity/entity/table/TableColumnWidthForEmployee.java +++ b/dao/src/main/java/greencity/entity/table/TableColumnWidthForEmployee.java @@ -93,6 +93,8 @@ public class TableColumnWidthForEmployee { private Integer totalPayment = DEFAULT_WIDTH; @Column private Integer violationsAmount = DEFAULT_WIDTH; + @Column + private boolean isTableFreeze = Boolean.FALSE; /** * Constructor with default width for all columns. diff --git a/dao/src/main/resources/db/changelog/db.changelog-master.xml b/dao/src/main/resources/db/changelog/db.changelog-master.xml index 7b8786375e..d12baf2d49 100644 --- a/dao/src/main/resources/db/changelog/db.changelog-master.xml +++ b/dao/src/main/resources/db/changelog/db.changelog-master.xml @@ -255,6 +255,7 @@ + diff --git a/dao/src/main/resources/db/changelog/logs/2024-11-28-change-add-column-isTableFreeze-to-column_width_for_employee-Warded120.xml b/dao/src/main/resources/db/changelog/logs/2024-11-28-change-add-column-isTableFreeze-to-column_width_for_employee-Warded120.xml new file mode 100644 index 0000000000..b0b6089360 --- /dev/null +++ b/dao/src/main/resources/db/changelog/logs/2024-11-28-change-add-column-isTableFreeze-to-column_width_for_employee-Warded120.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/service-api/src/main/java/greencity/constant/ErrorMessage.java b/service-api/src/main/java/greencity/constant/ErrorMessage.java index 216f07d156..4e622af7f9 100644 --- a/service-api/src/main/java/greencity/constant/ErrorMessage.java +++ b/service-api/src/main/java/greencity/constant/ErrorMessage.java @@ -179,5 +179,9 @@ public class ErrorMessage { public static final String TARIFF_FOR_BAGS_AT_LOCATION_NOT_EXIST = "Could not find tariff information for bags with IDs %s at location with id: %d"; public static final String COULD_NOT_RETRIEVE_USER_LANGUAGE = "Could not retrieve user language"; + public static final String CANNOT_CHANGE_ORDER_TABLE_VIEW = + "You can't change order table view when table is frozen"; + public static final String TABLE_COLUMN_WIDTH_BY_EMPLOYEE_ID_NOT_FOUND = + "Table column width by employee id not found"; public static final String ORDER_STATUS_INVALID = "Order with ID %d cannot be updated due to its current status."; } diff --git a/service-api/src/main/java/greencity/dto/table/ColumnWidthDto.java b/service-api/src/main/java/greencity/dto/table/ColumnWidthDto.java index 82f129a071..fc14411444 100644 --- a/service-api/src/main/java/greencity/dto/table/ColumnWidthDto.java +++ b/service-api/src/main/java/greencity/dto/table/ColumnWidthDto.java @@ -46,4 +46,5 @@ public class ColumnWidthDto { private Integer totalOrderSum; private Integer totalPayment; private Integer violationsAmount; + private Boolean isTableFreeze; } diff --git a/service-api/src/main/java/greencity/service/ubs/manager/BigOrderTableServiceView.java b/service-api/src/main/java/greencity/service/ubs/manager/BigOrderTableServiceView.java index 1a7d82186e..78ed2fa052 100644 --- a/service-api/src/main/java/greencity/service/ubs/manager/BigOrderTableServiceView.java +++ b/service-api/src/main/java/greencity/service/ubs/manager/BigOrderTableServiceView.java @@ -2,6 +2,7 @@ import greencity.dto.order.BigOrderTableDTO; import greencity.dto.table.CustomTableViewDto; +import greencity.entity.table.TableColumnWidthForEmployee; import greencity.filters.OrderPage; import greencity.filters.OrderSearchCriteria; import org.springframework.data.domain.Page; @@ -30,4 +31,13 @@ public interface BigOrderTableServiceView { * @author Sikhovskiy Rostyslav */ CustomTableViewDto getCustomTableParameters(String uuid); + + /** + * Method sets isTableFreeze field in TableColumnWidthForEmployee. + * + * @return {@link TableColumnWidthForEmployee} that has been saved + * + * @author Hrenevych Ivan + */ + TableColumnWidthForEmployee changeIsFreezeStatus(String uuid, Boolean value); } diff --git a/service/src/main/java/greencity/service/ubs/manager/BigOrderTableViewServiceImpl.java b/service/src/main/java/greencity/service/ubs/manager/BigOrderTableViewServiceImpl.java index 2aff506b8c..0ba0ce43f6 100644 --- a/service/src/main/java/greencity/service/ubs/manager/BigOrderTableViewServiceImpl.java +++ b/service/src/main/java/greencity/service/ubs/manager/BigOrderTableViewServiceImpl.java @@ -5,7 +5,14 @@ import greencity.client.UserRemoteClient; import greencity.constant.ErrorMessage; import greencity.dto.user.UserVO; +import greencity.entity.table.TableColumnWidthForEmployee; +import greencity.entity.user.employee.Employee; +import greencity.exceptions.BadRequestException; import greencity.exceptions.user.UserNotFoundException; +import greencity.repository.BigOrderTableRepository; +import greencity.repository.CustomTableViewRepo; +import greencity.repository.EmployeeRepository; +import greencity.repository.TableColumnWidthForEmployeeRepository; import jakarta.persistence.EntityNotFoundException; import org.modelmapper.ModelMapper; import org.springframework.cache.annotation.Cacheable; @@ -17,12 +24,12 @@ import greencity.entity.parameters.CustomTableView; import greencity.filters.OrderPage; import greencity.filters.OrderSearchCriteria; -import greencity.repository.BigOrderTableRepository; -import greencity.repository.CustomTableViewRepo; -import greencity.repository.EmployeeRepository; -import greencity.repository.UserRepository; import lombok.AllArgsConstructor; +import static greencity.constant.ErrorMessage.CANNOT_CHANGE_ORDER_TABLE_VIEW; +import static greencity.constant.ErrorMessage.EMPLOYEE_WITH_UUID_NOT_FOUND; import static greencity.constant.ErrorMessage.EMPLOYEE_NOT_FOUND; +import static greencity.constant.ErrorMessage.TABLE_COLUMN_WIDTH_BY_EMPLOYEE_ID_NOT_FOUND; +import static java.util.Objects.nonNull; @Service @AllArgsConstructor @@ -31,8 +38,8 @@ public class BigOrderTableViewServiceImpl implements BigOrderTableServiceView { private final CustomTableViewRepo customTableViewRepo; private final ModelMapper modelMapper; private final EmployeeRepository employeeRepository; - private final UserRepository userRepository; private final UserRemoteClient userRemoteClient; + private final TableColumnWidthForEmployeeRepository tableColumnWidthForEmployeeRepository; @Override public Page getOrders(OrderPage orderPage, OrderSearchCriteria searchCriteria, String email) { @@ -50,6 +57,15 @@ public Page getOrders(OrderPage orderPage, OrderSearchCriteria @Override public void changeOrderTableView(String uuid, String titles) { + Employee employeeByUuid = employeeRepository.findByUuid(uuid).orElse(null); + if (nonNull(employeeByUuid)) { + TableColumnWidthForEmployee tableByEmployeeId = tableColumnWidthForEmployeeRepository + .findByEmployeeId(employeeByUuid.getId()).orElse(null); + if (nonNull(tableByEmployeeId) && tableByEmployeeId.isTableFreeze()) { + throw new BadRequestException(CANNOT_CHANGE_ORDER_TABLE_VIEW); + } + } + if (Boolean.TRUE.equals(customTableViewRepo.existsByUuid(uuid))) { customTableViewRepo.update(uuid, titles); } else { @@ -73,6 +89,21 @@ public CustomTableViewDto getCustomTableParameters(String uuid) { } } + @Override + public TableColumnWidthForEmployee changeIsFreezeStatus(String uuid, Boolean value) { + Employee employeeByUuid = employeeRepository.findByUuid(uuid).orElse(null); + if (nonNull(employeeByUuid)) { + TableColumnWidthForEmployee tableByEmployeeId = tableColumnWidthForEmployeeRepository + .findByEmployeeId(employeeByUuid.getId()).orElse(null); + if (nonNull(tableByEmployeeId)) { + tableByEmployeeId.setTableFreeze(value); + return tableColumnWidthForEmployeeRepository.save(tableByEmployeeId); + } + throw new EntityNotFoundException(TABLE_COLUMN_WIDTH_BY_EMPLOYEE_ID_NOT_FOUND); + } + throw new EntityNotFoundException(EMPLOYEE_WITH_UUID_NOT_FOUND); + } + private CustomTableViewDto castTableViewToDto(String titles) { return CustomTableViewDto.builder() .titles(titles) diff --git a/service/src/test/java/greencity/ModelUtils.java b/service/src/test/java/greencity/ModelUtils.java index 3db2103cd6..7b77b197f4 100644 --- a/service/src/test/java/greencity/ModelUtils.java +++ b/service/src/test/java/greencity/ModelUtils.java @@ -5300,6 +5300,18 @@ public static TableColumnWidthForEmployee getTestTableColumnWidth() { .amountDue(60) .mixedWaste120L(150) .city(200) + .isTableFreeze(false) + .build(); + } + + public static TableColumnWidthForEmployee getTestTableColumnWidthWithIsTableFreezeTrue() { + return TableColumnWidthForEmployee.builder() + .employee(getEmployee()) + .address(50) + .amountDue(60) + .mixedWaste120L(150) + .city(200) + .isTableFreeze(true) .build(); } diff --git a/service/src/test/java/greencity/service/ubs/manager/BigOrderTableServiceImplTest.java b/service/src/test/java/greencity/service/ubs/manager/BigOrderTableServiceImplTest.java index 8e0af60050..8e266f0eb7 100644 --- a/service/src/test/java/greencity/service/ubs/manager/BigOrderTableServiceImplTest.java +++ b/service/src/test/java/greencity/service/ubs/manager/BigOrderTableServiceImplTest.java @@ -5,13 +5,17 @@ import greencity.dto.language.LanguageVO; import greencity.dto.user.UserVO; import greencity.entity.parameters.CustomTableView; +import greencity.entity.table.TableColumnWidthForEmployee; import greencity.entity.user.employee.Employee; +import greencity.exceptions.BadRequestException; import greencity.filters.DateFilter; import greencity.filters.OrderPage; import greencity.filters.OrderSearchCriteria; import greencity.repository.BigOrderTableRepository; import greencity.repository.CustomTableViewRepo; import greencity.repository.EmployeeRepository; +import greencity.repository.TableColumnWidthForEmployeeRepository; +import jakarta.persistence.EntityNotFoundException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -22,8 +26,12 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import static greencity.ModelUtils.getEmployee; +import static greencity.ModelUtils.getTestTableColumnWidth; +import static greencity.ModelUtils.getTestTableColumnWidthWithIsTableFreezeTrue; import static org.mockito.Mockito.when; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.times; @ExtendWith(MockitoExtension.class) class BigOrderTableServiceImplTest { @@ -37,12 +45,14 @@ class BigOrderTableServiceImplTest { private EmployeeRepository employeeRepository; @Mock private UserRemoteClient userRemoteClient; + @Mock + TableColumnWidthForEmployeeRepository tableColumnWidthForEmployeeRepository; @Test void getOrders() { var orderPage = getOrderPage(); var orderSearchCriteria = getOrderSearchCriteria(); - Optional employee = Optional.of(ModelUtils.getEmployee()); + Optional employee = Optional.of(getEmployee()); List tariffsInfoIds = new ArrayList<>(); when(employeeRepository.findByEmail("test@gmail.com")).thenReturn(employee); UserVO userVO = new UserVO().setLanguageVO(new LanguageVO(null, "eng")); @@ -79,6 +89,36 @@ void changeOrderTableView2() { verify(customTableViewRepo).existsByUuid(uuid); } + @Test + void changeOrderTableViewForEmployeeTableWhenIsTableFreezeTrue() { + String uuid = "uuid1"; + + when(employeeRepository.findByUuid(uuid)).thenReturn(Optional.ofNullable(getEmployee())); + when(tableColumnWidthForEmployeeRepository.findByEmployeeId(getEmployee().getId())) + .thenReturn(Optional.ofNullable(getTestTableColumnWidthWithIsTableFreezeTrue())); + + Assertions.assertThrows(BadRequestException.class, + () -> bigOrderTableService.changeOrderTableView(uuid, "titles1,titles2"), + "should throw BadRequestException"); + + verify(employeeRepository).findByUuid(uuid); + verify(tableColumnWidthForEmployeeRepository).findByEmployeeId(getEmployee().getId()); + } + + @Test + void changeOrderTableViewForEmployeeTableWhenIsTableFreezeFalse() { + String uuid = "Test"; + + when(employeeRepository.findByUuid(uuid)).thenReturn(Optional.ofNullable(getEmployee())); + when(tableColumnWidthForEmployeeRepository.findByEmployeeId(getEmployee().getId())) + .thenReturn(Optional.ofNullable(getTestTableColumnWidth())); + + bigOrderTableService.changeOrderTableView(uuid, "titles1,titles2"); + + verify(employeeRepository).findByUuid(uuid); + verify(tableColumnWidthForEmployeeRepository).findByEmployeeId(getEmployee().getId()); + } + @Test void getCustomTableParametersForExistUuid() { CustomTableView customTableView = ModelUtils.getCustomTableView(); @@ -101,6 +141,56 @@ void getCustomTableParametersForNon_ExistUuid() { Assertions.assertNotNull(customTableView); } + @Test + void changeIsFreezeStatusTest() { + when(employeeRepository.findByUuid("Test")).thenReturn(Optional.ofNullable(getEmployee())); + when(tableColumnWidthForEmployeeRepository.findByEmployeeId(getEmployee().getId())) + .thenReturn(Optional.ofNullable(getTestTableColumnWidth())); + when(tableColumnWidthForEmployeeRepository.save(getTestTableColumnWidthWithIsTableFreezeTrue())) + .thenReturn(getTestTableColumnWidthWithIsTableFreezeTrue()); + + TableColumnWidthForEmployee byUuid1 = bigOrderTableService.changeIsFreezeStatus("Test", true); + + verify(employeeRepository).findByUuid("Test"); + verify(tableColumnWidthForEmployeeRepository).findByEmployeeId(getEmployee().getId()); + verify(tableColumnWidthForEmployeeRepository).save(getTestTableColumnWidthWithIsTableFreezeTrue()); + + Assertions.assertTrue(byUuid1.isTableFreeze(), "Should be true"); + } + + @Test + void changeIsFreezeStatusForNon_ExistUuidTest() { + String nonExistUuid = "Non_Exist"; + when(employeeRepository.findByUuid(nonExistUuid)).thenReturn(Optional.empty()); + + Assertions.assertThrows( + EntityNotFoundException.class, + () -> bigOrderTableService.changeIsFreezeStatus(nonExistUuid, true), + "Should throw EntityNotFoundException"); + + verify(employeeRepository).findByUuid(nonExistUuid); + verify(tableColumnWidthForEmployeeRepository, times(0)).findByEmployeeId(getEmployee().getId()); + verify(tableColumnWidthForEmployeeRepository, times(0)).save(getTestTableColumnWidthWithIsTableFreezeTrue()); + + } + + @Test + void changeIsFreezeStatusForNon_ExistEmployeeTableTest() { + String uuid = "Test"; + when(employeeRepository.findByUuid(uuid)).thenReturn(Optional.ofNullable(getEmployee())); + when(tableColumnWidthForEmployeeRepository.findByEmployeeId(getEmployee().getId())) + .thenReturn(Optional.empty()); + + Assertions.assertThrows( + EntityNotFoundException.class, + () -> bigOrderTableService.changeIsFreezeStatus(uuid, true), + "Should throw EntityNotFoundException"); + + verify(employeeRepository).findByUuid(uuid); + verify(tableColumnWidthForEmployeeRepository, times(1)).findByEmployeeId(getEmployee().getId()); + + } + private OrderPage getOrderPage() { return new OrderPage().setPageNumber(1); }