Commit 874df276 authored by Raigo Ukkivi's avatar Raigo Ukkivi
Browse files

Merge branch 'TEIS-365' into 'develop'

Resolve TEIS-365

Closes TEIS-365

See merge request teis/officials-service!23
parents 65c31bd0 91a34cd3
package ee.sm.ti.teis.officials.group.listener;
import ee.sm.ti.teis.OfficialsAppTestBase;
import ee.sm.ti.teis.officegateway.official.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.AddOfficialsGroupMembersResponse;
import ee.sm.ti.teis.officegateway.official.dto.OfficialUserDto;
import ee.sm.ti.teis.officegateway.official.dto.OfficialsGroupDto;
import ee.sm.ti.teis.officegateway.official.request.CreateOfficialsGroupRequest;
......@@ -23,13 +25,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import javax.transaction.Transactional;
import java.util.List;
import java.util.UUID;
import static ee.sm.ti.teis.types.enums.ObjectStatus.CURRENT;
import static ee.sm.ti.teis.types.enums.ObjectStatus.DELETED;
import static java.util.Set.of;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpStatus.NOT_FOUND;
@SuppressWarnings("squid:S00100")
@Transactional
......@@ -39,6 +41,8 @@ class OfficialsGroupGwListenerTest extends OfficialsAppTestBase {
private static final UUID ADMIN_GROUP_ID = UUID.fromString("68b5b265-9427-457f-8f16-0bd5cf608714");
private static final UUID ADMIN_USER_ID = UUID.fromString("b5d59f3b-57af-4971-8110-51019e5e827d");
private static final UUID TEST_GROUP_ID = UUID.fromString("751f8528-64a5-4e0e-862a-549356014861");
private static final UUID TEST_USER1_ID = UUID.fromString("06fe678a-90a8-49a7-8362-1041d244ef90");
private static final UUID TEST_USER2_ID = UUID.fromString("f0cafdc3-ff43-4c73-8319-93f07a7d5a3c");
@Autowired
OfficialsGroupGwListener groupListener;
......@@ -124,8 +128,7 @@ class OfficialsGroupGwListenerTest extends OfficialsAppTestBase {
OfficialsGroupResponse response = groupListener.findOfficialsGroup(request);
assertThat(response.getError().getCode()).isEqualTo("404");
assertThat(response.getError().getHttpResponse()).isEqualTo(NOT_FOUND);
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
}
@Test
......@@ -255,6 +258,30 @@ class OfficialsGroupGwListenerTest extends OfficialsAppTestBase {
assertThat(groupRepo.findById(ADMIN_GROUP_ID).get().getOfficialsGroupManagerId()).isNull();
}
@Test
void addOfficialsGroupMembers_test() {
assertThat(usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup().getId()).isNotEqualTo(TEST_GROUP_ID);
assertThat(usersRepo.findById(TEST_USER2_ID).get().getOfficialsGroup().getId()).isNotEqualTo(TEST_GROUP_ID);
groupListener.addOfficialsGroupMembers(composeAddMemberRequest(TEST_GROUP_ID.toString(), List.of(TEST_USER1_ID.toString(), TEST_USER2_ID.toString())));
assertThat(usersRepo.findById(TEST_USER1_ID).get().getOfficialsGroup().getId()).isEqualTo(TEST_GROUP_ID);
assertThat(usersRepo.findById(TEST_USER2_ID).get().getOfficialsGroup().getId()).isEqualTo(TEST_GROUP_ID);
}
@Test
void addOfficialsGroupMembers_validates_members() {
AddOfficialsGroupMembersResponse response = groupListener
.addOfficialsGroupMembers(composeAddMemberRequest(TEST_GROUP_ID.toString(), null));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(response.getError().getViolations().get(0).getFieldName()).isEqualTo("memberIds");
response = groupListener.addOfficialsGroupMembers(composeAddMemberRequest(TEST_GROUP_ID.toString(), List.of(UUID.randomUUID().toString())));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
}
@NotNull
private OfficialsGroupsRequest composeOfficialsGroupsRequest(String status) {
OfficialsGroupsRequest request = new OfficialsGroupsRequest();
......@@ -262,6 +289,13 @@ class OfficialsGroupGwListenerTest extends OfficialsAppTestBase {
return request;
}
@NotNull
private AddOfficialsGroupMembersRequest composeAddMemberRequest(String groupId, List<String> members) {
AddOfficialsGroupMembersRequest request = new AddOfficialsGroupMembersRequest();
request.setPayload(new AddOfficialsGroupMembersRequest.Parameters(groupId, members), REQUEST_META_DTO);
return request;
}
@NotNull
private CreateOfficialsGroupRequest composeCreateOfficialsGroupRequest(String name, UUID groupManagerId) {
CreateOfficialsGroupRequest request = new CreateOfficialsGroupRequest();
......
......@@ -13,6 +13,7 @@ import ee.sm.ti.teis.domainrequest.DomainCreateDTO;
import ee.sm.ti.teis.domainrequest.DomainDeleteDTO;
import ee.sm.ti.teis.domainrequest.DomainQueryDTO;
import ee.sm.ti.teis.domainrequest.DomainRequestDTO;
import ee.sm.ti.teis.officegateway.official.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.request.*;
import org.springframework.context.annotation.Configuration;
......@@ -34,6 +35,7 @@ public class QueueConfig implements QueueList {
public static final String GW_CREATE_OFFICIALS_GROUP_EMTAK_CODES_QUEUE = "officials-service.gw.createGroupEmtakCodesRequest";
public static final String GW_DELETE_OFFICIALS_GROUP_EMTAK_QUEUE = "officials-service.gw.deleteGroupEmtakRequest";
public static final String GW_OFFICES_QUEUE = "officials-service.gw.findOfficesRequest";
public static final String GW_ADD_OFFICIALS_GROUP_MEMBERS_QUEUE = "officials-service.gw.addGroupMembersRequest";
public static final String MS_OFFICIAL_USER_QUEUE = "officials-service.ms.findUserRequest";
public static final String MS_OFFICIAL_USERS_QUEUE = "officials-service.ms.findUsersRequest";
......@@ -64,6 +66,7 @@ public class QueueConfig implements QueueList {
addGwQueue(queues, "gwCreateGroupEmtakCodesQueue", CreateOfficialsGroupEmtakCodesRequest.ROUTING_KEY, GW_CREATE_OFFICIALS_GROUP_EMTAK_CODES_QUEUE);
addGwQueue(queues, "gwDeleteGroupEmtakQueue", DeleteOfficialsGroupEmtakRequest.ROUTING_KEY, GW_DELETE_OFFICIALS_GROUP_EMTAK_QUEUE);
addGwQueue(queues, "gwFindOfficesQueue", OfficesRequest.ROUTING_KEY, GW_OFFICES_QUEUE);
addGwQueue(queues, "gwAddGroupMembersQueue", AddOfficialsGroupMembersRequest.ROUTING_KEY, GW_ADD_OFFICIALS_GROUP_MEMBERS_QUEUE);
addMsQueue(queues, "msFindUserQueue", new OfficialUserDomainRequest().routingKey(), MS_OFFICIAL_USER_QUEUE);
addMsQueue(queues, "msFindUsersQueue", new OfficialUsersDomainRequest().routingKey(), MS_OFFICIAL_USERS_QUEUE);
......
package ee.sm.ti.teis.officials.group.listener;
import ee.sm.ti.teis.officegateway.official.AddOfficialsGroupMembersRequest;
import ee.sm.ti.teis.officegateway.official.AddOfficialsGroupMembersResponse;
import ee.sm.ti.teis.officegateway.official.dto.OfficialUserDto;
import ee.sm.ti.teis.officegateway.official.dto.OfficialsGroupDto;
import ee.sm.ti.teis.officegateway.official.request.CreateOfficialsGroupRequest;
import ee.sm.ti.teis.officegateway.official.request.DeleteOfficialsGroupRequest;
......@@ -82,4 +85,14 @@ public class OfficialsGroupGwListener {
return response;
}
@RabbitListener(queues = {GW_ADD_OFFICIALS_GROUP_MEMBERS_QUEUE})
@PreAuthorize("@accessController.hasOfficialPrivilege({T(ee.sm.ti.teis.types.enums.RolePrivilegeCode).TI_MANAGE_OFFICIALS_GROUPS})")
public AddOfficialsGroupMembersResponse addOfficialsGroupMembers(AddOfficialsGroupMembersRequest request) {
final List<OfficialUserDto> groupMembers = service.addMembersToGroup(
request.getPayload().getGroupId(), request.getPayload().getMemberIds(), request.getRequestMetaDTO());
AddOfficialsGroupMembersResponse response = new AddOfficialsGroupMembersResponse();
response.setPayload(groupMembers, request.getRequestMetaDTO());
return response;
}
}
......@@ -5,11 +5,13 @@ import ee.sm.ti.teis.errors.ConstraintViolationDto;
import ee.sm.ti.teis.exceptions.TeisConstraintViolationException;
import ee.sm.ti.teis.exceptions.TeisIllegalArgumentException;
import ee.sm.ti.teis.exceptions.TeisResourceNotFoundException;
import ee.sm.ti.teis.officegateway.official.dto.OfficialUserDto;
import ee.sm.ti.teis.officegateway.official.dto.OfficialsGroupDto;
import ee.sm.ti.teis.officials.group.OfficialsGroupEntity;
import ee.sm.ti.teis.officials.group.OfficialsGroupMapper;
import ee.sm.ti.teis.officials.group.OfficialsGroupRepository;
import ee.sm.ti.teis.officials.official.OfficialService;
import ee.sm.ti.teis.officials.official.OfficialUserEntity;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import ee.sm.ti.teis.types.enums.ObjectStatus;
import lombok.extern.slf4j.Slf4j;
......@@ -18,6 +20,8 @@ import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
......@@ -85,7 +89,7 @@ public class OfficialsGroupService {
officialsGroupNotifyService.notifyCreate(mapper.toDomainDto(officialsGroupEntity), requestMetaDTO);
if (officialsGroupEntity.getOfficialsGroupManagerId() != null) {
userService.updateUserGroupData(officialsGroupEntity.getOfficialsGroupManagerId(), officialsGroupEntity.getId(), requestMetaDTO);
removeManagerFromOtherGroups(officialsGroupEntity);
removeManagerFromOtherGroups(officialsGroupEntity, officialsGroupEntity.getOfficialsGroupManagerId());
}
return officialsGroupEntity;
}
......@@ -161,14 +165,45 @@ public class OfficialsGroupService {
OfficialsGroupEntity officialsGroupEntity = repository.save(group);
if (dto.getOfficialsGroupManager() != null && dto.getOfficialsGroupManager().getId() != null) {
userService.updateUserGroupData(dto.getOfficialsGroupManager().getId(), officialsGroupEntity.getId(), requestMetaDTO);
removeManagerFromOtherGroups(officialsGroupEntity);
removeManagerFromOtherGroups(officialsGroupEntity, dto.getOfficialsGroupManager().getId());
}
officialsGroupNotifyService.notifyUpdate(mapper.toDomainDto(officialsGroupEntity), requestMetaDTO);
return officialsGroupEntity;
}
private void removeManagerFromOtherGroups(OfficialsGroupEntity officialsGroupEntity) {
repository.findByOfficialsGroupManagerId(officialsGroupEntity.getOfficialsGroupManagerId())
@Transactional
public List<OfficialUserDto> addMembersToGroup(String groupId, List<String> memberIds, RequestMetaDTO requestMetaDTO) {
final OfficialsGroupEntity group = findGroupById(UUID.fromString(groupId));
validateGroupMembers(memberIds);
List<OfficialUserDto> groupMembers = new ArrayList<>();
for (String memberId : memberIds) {
UUID officialUserId = UUID.fromString(memberId);
final OfficialUserEntity officialUserEntity = userService.updateUserGroupData(officialUserId, group.getId(), requestMetaDTO);
removeManagerFromOtherGroups(group, officialUserId);
groupMembers.add(userService.composeDtoFromEntity(officialUserEntity));
}
return groupMembers;
}
// lets do full validation before performing user group updates, as updates are sending notifications to other micro services
private void validateGroupMembers(List<String> memberIds) {
if (memberIds == null) {
memberIds = Collections.emptyList();
}
if (memberIds.isEmpty()) {
throw new TeisConstraintViolationException(ConstraintViolationDto.builder()
.fieldName("memberIds")
.message("Member id is mandatory.")
.build());
}
memberIds.forEach(memberId -> {
UUID userId = UUID.fromString(memberId);
userService.findUserById(userId);
});
}
private void removeManagerFromOtherGroups(OfficialsGroupEntity officialsGroupEntity, UUID managerId) {
repository.findByOfficialsGroupManagerId(managerId)
.stream()
.filter(group -> !group.getId().equals(officialsGroupEntity.getId()))
.forEach(group -> {
......
......@@ -68,7 +68,7 @@ public class OfficialService {
return newEntity;
}
OfficialUserDto composeDtoFromEntity(OfficialUserEntity entity) {
public OfficialUserDto composeDtoFromEntity(OfficialUserEntity entity) {
final OfficialUserDto officialUserDto = mapper.toDto(entity);
UUID groupManagerId = entity.getOfficialsGroup() != null ? entity.getOfficialsGroup().getOfficialsGroupManagerId() : null;
if (groupManagerId != null) {
......@@ -91,7 +91,7 @@ public class OfficialService {
return mapper.toDomainDto(entity);
}
OfficialUserEntity findUserById(UUID id) {
public OfficialUserEntity findUserById(UUID id) {
return repository.findById(id).orElseThrow(() -> new TeisResourceNotFoundException("Official user not found.", List.of(id)));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment