Skip to content

Commit

Permalink
7834 greencityubs add parameter to freeze tablecolumnwidthforemployee3 (
Browse files Browse the repository at this point in the history
#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 <lazarenkodmytro.work@gmail.com>
  • Loading branch information
Warded120 and LazarenkoDmytro authored Dec 16, 2024
1 parent 84a08a3 commit 7130a9d
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/java/greencity/controller/AdminUbsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -250,4 +252,22 @@ public ResponseEntity<UserViolationsWithUserName> getAllViolationsByUser(
public ResponseEntity<List<RegionInfoDto>> 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<HttpStatus> saveIsFreeze(@CurrentUserUuid String uuid,
@RequestParam boolean value) {
bigOrderTableServiceView.changeIsFreezeStatus(uuid, value);
return ResponseEntity.status(HttpStatus.OK).body(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
<include file="db/changelog/logs/ch-add-column-isDeleted-to-UserNotifications-Kasarab.xml"/>
<include file="/db/changelog/logs/2024-11-07-rename-positions-Kizerov.xml"/>
<include file="db/changelog/logs/2024-11-08-rename-position-kizerov.xml"/>
<include file="db/changelog/logs/2024-11-28-change-add-column-isTableFreeze-to-column_width_for_employee-Warded120.xml"/>
<include file="db/changelog/logs/2024-12-09-update-big-order-table-view-Kizerov.xml"/>
<include file="db/changelog/logs/2024-12-09-update-colum_width_for_employee-Kizerov.xml"/>
<include file="db/changelog/logs/2024-12-14-update-custom_table_view-Kizerov.xml"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<changeSet id="change-add-column-isTableFreeze-to-column_width_for_employee-Warded120" author="Warded120">
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="column_width_for_employee" columnName="is_table_freeze"/>
</not>
</preConditions>
<addColumn tableName="column_width_for_employee">
<column name="is_table_freeze" type="boolean">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ public class ColumnWidthDto {
private Integer totalOrderSum;
private Integer totalPayment;
private Integer violationsAmount;
private Boolean isTableFreeze;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<BigOrderTableDTO> getOrders(OrderPage orderPage, OrderSearchCriteria searchCriteria, String email) {
Expand All @@ -50,6 +57,15 @@ public Page<BigOrderTableDTO> 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 {
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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> employee = Optional.of(ModelUtils.getEmployee());
Optional<Employee> employee = Optional.of(getEmployee());
List<Long> tariffsInfoIds = new ArrayList<>();
when(employeeRepository.findByEmail("test@gmail.com")).thenReturn(employee);
UserVO userVO = new UserVO().setLanguageVO(new LanguageVO(null, "eng"));
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down

0 comments on commit 7130a9d

Please sign in to comment.