Skip to content

Commit

Permalink
feat(invitation_list_by_query): Add the possibility to filter invitat…
Browse files Browse the repository at this point in the history
…ion list by string or exact email (#1531)
  • Loading branch information
NyraSama authored Jul 3, 2024
1 parent 99a8831 commit 2d505a1
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,15 @@ public List<UserEntityWithID> findUserByIdRange(int idStart, int idEnd) {
}

@Override
public Page<UserEntityWithID> findAllUsers(Pageable pageable, Integer customerId, Boolean excludeCaptains, Integer excludeTeamId) {
public Page<UserEntityWithID> findAllUsers(
Pageable pageable,
Integer customerId,
Boolean excludeCaptains,
Integer excludeTeamId,
String q,
String email) {
// Call the getUserPage method to get a Page of UserModels, sorted by last name.
Page<UserModel> userPage = getUserPage(pageable, customerId, excludeCaptains, excludeTeamId);
Page<UserModel> userPage = getUserPage(pageable, customerId, excludeCaptains, excludeTeamId, q, email);

return userPage.map(user -> new UserEntityWithID(
user.getUserId(),
Expand All @@ -125,7 +131,13 @@ public Page<UserEntityWithID> findAllUsers(Pageable pageable, Integer customerId
}

@Override
public Page<UserModel> getUserPage(Pageable pageable, Integer customerId, Boolean excludeCaptains, Integer excludeTeamId) {
public Page<UserModel> getUserPage(
Pageable pageable,
Integer customerId,
Boolean excludeCaptains,
Integer excludeTeamId,
String q,
String email) {
// Create a new PageRequest object. This is a concrete implementation of the Pageable interface, which is used to add pagination information to database queries.
// The parameters to the of() method are:
// - pageable.getPageNumber(): This gets the number of the page that we want to retrieve. Page numbers are zero-based, so the first page is page 0.
Expand All @@ -139,14 +151,14 @@ public Page<UserModel> getUserPage(Pageable pageable, Integer customerId, Boolea
// The findAll method takes the Pageable object as a parameter, which includes the page number, page size, and sorting details.
if (customerId != null) {
if (excludeCaptains != null && excludeCaptains) {
return userJpaRepository.findAllEnabledUsersByCustomerExcludingCaptains(sortedByName, customerId);
return userJpaRepository.findAllEnabledUsersByCustomerExcludingCaptains(sortedByName, customerId, q, email);
} else if (excludeTeamId != null) {
return userJpaRepository.findAllEnabledUsersByCustomerExcludingTeam(sortedByName, customerId, excludeTeamId);
return userJpaRepository.findAllEnabledUsersByCustomerExcludingTeam(sortedByName, customerId, excludeTeamId, q, email);
} else {
return userJpaRepository.findAllEnabledUsersByCustomer(sortedByName, customerId);
return userJpaRepository.findAllEnabledUsersByCustomer(sortedByName, customerId, q, email);
}
} else {
return userJpaRepository.findAllEnabledUsers(sortedByName);
return userJpaRepository.findAllEnabledUsers(sortedByName, q, email);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,35 @@ public interface UserJpaRepository extends JpaRepository<UserModel, Integer> {
@Query("""
SELECT u
FROM UserModel u
WHERE u.enabled = true
""")
Page<UserModel> findAllEnabledUsers(Pageable pageable);
WHERE u.enabled = true and
(
(:q is null\s
or LOWER(u.firstName) like LOWER(CONCAT('%', :q, '%'))\s
or LOWER(u.lastName) like LOWER(CONCAT('%', :q, '%'))\s
or LOWER(u.email) like LOWER(CONCAT('%', :q, '%')))\s
and
(:email is null or LOWER(u.email) = LOWER(:email))
)
\s""")
Page<UserModel> findAllEnabledUsers(Pageable pageable, String q, String email);

@Query("""
SELECT u
FROM UserModel u
JOIN TeammateModel tm ON u.userId = tm.userId
JOIN TeamModel t ON tm.teamId = t.id
WHERE u.enabled = true AND t.customerId = :customerId
and
(
(:q is null\s
or LOWER(u.firstName) like LOWER(CONCAT('%', :q, '%'))\s
or LOWER(u.lastName) like LOWER(CONCAT('%', :q, '%'))\s
or LOWER(u.email) like LOWER(CONCAT('%', :q, '%')))\s
and
(:email is null or LOWER(u.email) = LOWER(:email))
)
""")
Page<UserModel> findAllEnabledUsersByCustomer(Pageable pageable, Integer customerId);
Page<UserModel> findAllEnabledUsersByCustomer(Pageable pageable, Integer customerId, String q, String email);

@Query("""
SELECT u
Expand All @@ -33,17 +50,35 @@ public interface UserJpaRepository extends JpaRepository<UserModel, Integer> {
LEFT JOIN CaptainModel c ON u.userId = c.userId
JOIN TeamModel t ON tm.teamId = t.id
WHERE u.enabled = true AND t.customerId = :customerId AND c.userId IS NULL
and
(
(:q is null\s
or LOWER(u.firstName) like LOWER(CONCAT('%', :q, '%'))\s
or LOWER(u.lastName) like LOWER(CONCAT('%', :q, '%'))\s
or LOWER(u.email) like LOWER(CONCAT('%', :q, '%')))\s
and
(:email is null or LOWER(u.email) = LOWER(:email))
)
""")
Page<UserModel> findAllEnabledUsersByCustomerExcludingCaptains(Pageable pageable, Integer customerId);
Page<UserModel> findAllEnabledUsersByCustomerExcludingCaptains(Pageable pageable, Integer customerId, String q, String email);

@Query("""
SELECT u
FROM UserModel u
JOIN TeammateModel tm ON u.userId = tm.userId
JOIN TeamModel t ON tm.teamId = t.id
WHERE u.enabled = true AND t.customerId = :customerId AND t.id != :excludeTeamId
and
(
(:q is null\s
or LOWER(u.firstName) like LOWER(CONCAT('%', :q, '%'))\s
or LOWER(u.lastName) like LOWER(CONCAT('%', :q, '%'))\s
or LOWER(u.email) like LOWER(CONCAT('%', :q, '%')))\s
and
(:email is null or LOWER(u.email) = LOWER(:email))
)
""")
Page<UserModel> findAllEnabledUsersByCustomerExcludingTeam(Pageable pageable, Integer customerId, Integer excludeTeamId);
Page<UserModel> findAllEnabledUsersByCustomerExcludingTeam(Pageable pageable, Integer customerId, Integer excludeTeamId, String q, String email);

Optional<UserModel> findByLogin(String login);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public interface UserService {
List<UserEntityWithID> findUserByIdRange(int idStart, int idEnd);


Page<UserEntityWithID> findAllUsers(Pageable pageable, Integer customerId, Boolean excludeCaptains, Integer excludeTeamId);
Page<UserEntityWithID> findAllUsers(Pageable pageable, Integer customerId, Boolean excludeCaptains, Integer excludeTeamId, String q, String email);

Page<UserModel> getUserPage(Pageable pageable, Integer customerId, Boolean excludeCaptains, Integer excludeTeamId);
Page<UserModel> getUserPage(Pageable pageable, Integer customerId, Boolean excludeCaptains, Integer excludeTeamId, String q, String email);

UserEntity deleteById(int id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
Expand Down Expand Up @@ -49,7 +47,9 @@ public ResponseEntity<UserResponseEntity> findAllUsers(
@RequestParam(defaultValue = "10") int size,
@RequestParam(required = false) Integer customerId,
@RequestParam(required = false) Boolean excludeCaptains,
@RequestParam(required = false) Integer excludeTeamId) {
@RequestParam(required = false) Integer excludeTeamId,
@RequestParam(required = false) String q,
@RequestParam(required = false) String email) {

// Create a Pageable object with the provided page number and size. This is used
// to add pagination information to the database query.
Expand All @@ -58,7 +58,7 @@ public ResponseEntity<UserResponseEntity> findAllUsers(
// Call the userService to get a Page of UserEntityWithID. The Page includes the
// data for the requested page, as well as pagination information.
Page<UserEntityWithID> userPage = userService.findAllUsers(pageable, customerId, excludeCaptains,
excludeTeamId);
excludeTeamId, q, email);

// Create a map to hold the response data.
// The getContent() method is used to get the list of UserEntityWithID from the Page.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ void getUserPage_should_call_findAllEnabledUsersByCustomerExcludingCaptains() {
Boolean excludeCaptains = true;
Integer excludeTeamId = null;

when(userJpaRepository.findAllEnabledUsersByCustomerExcludingCaptains(any(), any())).thenReturn(new PageImpl<>(List.of(user1)));
when(userJpaRepository.findAllEnabledUsersByCustomerExcludingCaptains(any(), any(), any(), any())).thenReturn(new PageImpl<>(List.of(user1)));

var expectedResult = new PageImpl<>(List.of(user1));


// WHEN
var result = userService.getUserPage(pageable, customerId, excludeCaptains, excludeTeamId);
var result = userService.getUserPage(pageable, customerId, excludeCaptains, excludeTeamId, null, null);

// THEN
verify(userJpaRepository).findAllEnabledUsersByCustomerExcludingCaptains(any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomer(any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingTeam(any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsers(any());
verify(userJpaRepository).findAllEnabledUsersByCustomerExcludingCaptains(any(), any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomer(any(), any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingTeam(any(), any(), any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsers(any(), any(), any());

assertEquals(expectedResult, result);

Expand All @@ -65,18 +65,18 @@ void getUserPage_should_call_findAllEnabledUsersByCustomerExcludingTeam() {
Boolean excludeCaptains = null;
Integer excludeTeamId = 1;

when(userJpaRepository.findAllEnabledUsersByCustomerExcludingTeam(any(), any(), any())).thenReturn(new PageImpl<>(List.of(user1)));
when(userJpaRepository.findAllEnabledUsersByCustomerExcludingTeam(any(), any(), any(), any(), any())).thenReturn(new PageImpl<>(List.of(user1)));

var expectedResult = new PageImpl<>(List.of(user1));

// WHEN
var result = userService.getUserPage(pageable, customerId, excludeCaptains, excludeTeamId);
var result = userService.getUserPage(pageable, customerId, excludeCaptains, excludeTeamId, null, null);

// THEN
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingCaptains(any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomer(any(), any());
verify(userJpaRepository).findAllEnabledUsersByCustomerExcludingTeam(any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsers(any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingCaptains(any(), any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomer(any(), any(), any(), any());
verify(userJpaRepository).findAllEnabledUsersByCustomerExcludingTeam(any(), any(), any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsers(any(), any(), any());

assertEquals(expectedResult, result);
}
Expand All @@ -91,18 +91,18 @@ void getUserPage_should_call_findAllEnabledUsersByCustomer() {
Boolean excludeCaptains = null;
Integer excludeTeamId = null;

when(userJpaRepository.findAllEnabledUsersByCustomer(any(), any())).thenReturn(new PageImpl<>(List.of(user1)));
when(userJpaRepository.findAllEnabledUsersByCustomer(any(), any(), any(), any())).thenReturn(new PageImpl<>(List.of(user1)));

var expectedResult = new PageImpl<>(List.of(user1));

// WHEN
var result = userService.getUserPage(pageable, customerId, excludeCaptains, excludeTeamId);
var result = userService.getUserPage(pageable, customerId, excludeCaptains, excludeTeamId, null, null);

// THEN
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingCaptains(any(), any());
verify(userJpaRepository).findAllEnabledUsersByCustomer(any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingTeam(any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsers(any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingCaptains(any(), any(), any(), any());
verify(userJpaRepository).findAllEnabledUsersByCustomer(any(), any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingTeam(any(), any(), any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsers(any(), any(), any());

assertEquals(expectedResult, result);
}
Expand All @@ -117,18 +117,18 @@ void getUserPage_should_call_findAllEnabledUsers() {
Boolean excludeCaptains = null;
Integer excludeTeamId = null;

when(userJpaRepository.findAllEnabledUsers(any())).thenReturn(new PageImpl<>(List.of(user1)));
when(userJpaRepository.findAllEnabledUsers(any(), any(), any())).thenReturn(new PageImpl<>(List.of(user1)));

var expectedResult = new PageImpl<>(List.of(user1));

// WHEN
var result = userService.getUserPage(pageable, customerId, excludeCaptains, excludeTeamId);
var result = userService.getUserPage(pageable, customerId, excludeCaptains, excludeTeamId, null, null);

// THEN
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingCaptains(any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomer(any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingTeam(any(), any(), any());
verify(userJpaRepository).findAllEnabledUsers(any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingCaptains(any(), any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomer(any(), any(), any(), any());
verify(userJpaRepository, never()).findAllEnabledUsersByCustomerExcludingTeam(any(), any(), any(), any(), any());
verify(userJpaRepository).findAllEnabledUsers(any(), any(), any());

assertEquals(expectedResult, result);
}
Expand Down

0 comments on commit 2d505a1

Please sign in to comment.