Commit c6b5aab1 authored by Raigo Ukkivi's avatar Raigo Ukkivi
Browse files

TEIS-343. Correct handling of deleted users, groups and users that do not belong to any group.

parent 5bc0887c
......@@ -41,10 +41,12 @@ class OfficialsGroupGwListenerTest extends OfficialsAppTestBase {
private static final RequestMetaDTO REQUEST_META_DTO = RequestMetaDTO.builder().userId(UUID.randomUUID().toString()).privileges(of("TI_MANAGE_OFFICIALS_GROUPS")).userType(UserType.OFFICIAL_USER).requestId(UUID.randomUUID().toString()).build();
private static final UUID ADMIN_GROUP_ID = UUID.fromString("68b5b265-9427-457f-8f16-0bd5cf608714");
private static final UUID DELETED_GROUP_ID = UUID.fromString("c6bbd0d0-a614-408e-b22b-c60eed1a0dc9");
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");
private static final UUID DELETED_USER_ID = UUID.fromString("2d2aa827-d92a-4f77-af3c-a9537ba31d7b");
@Autowired
OfficialsGroupGwListener groupListener;
......@@ -322,6 +324,12 @@ class OfficialsGroupGwListenerTest extends OfficialsAppTestBase {
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(UUID.randomUUID().toString(), TEST_USER1_ID.toString()));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(DELETED_GROUP_ID.toString(), TEST_USER1_ID.toString()));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
response = groupListener.deleteOfficialsGroupMember(composeDeleteMemberRequest(ADMIN_GROUP_ID.toString(), DELETED_USER_ID.toString()));
assertThat(response.getError().getHttpResponse()).isEqualTo(HttpStatus.NOT_FOUND);
}
@NotNull
......
......@@ -100,7 +100,7 @@ public class OfficialsGroupEmtakService {
}
if (StringUtils.isNotBlank(request.getPayload().getOfficialsGroupId())) {
final UUID groupId = UUID.fromString(request.getPayload().getOfficialsGroupId());
groupService.findGroupById(groupId);
groupService.findCurrentGroupById(groupId);
}
if (StringUtils.isNotBlank(request.getPayload().getEmtak())) {
final String emtakClassifierCode = composeEmtakCode(request.getPayload().getEmtak());
......@@ -136,7 +136,7 @@ public class OfficialsGroupEmtakService {
@Transactional
public List<OfficialsGroupEmtakEntity> createGroupEmtakCodes(CreateOfficialsGroupEmtakCodesRequest request) {
UUID groupId = UUID.fromString(request.getPayload().getEmtakCodes().getOfficialsGroupId());
final OfficialsGroupEntity officialsGroup = groupService.findGroupById(groupId);
final OfficialsGroupEntity officialsGroup = groupService.findCurrentGroupById(groupId);
List<OfficialsGroupEmtakEntity> groupEmtakCodes = new ArrayList<>();
for (String emtakCode : request.getPayload().getEmtakCodes().getEmtaks()) {
final String emtakClassifierCode = composeEmtakCode(emtakCode);
......
......@@ -141,25 +141,21 @@ public class OfficialsGroupService {
@Transactional
public void delete(UUID groupId, RequestMetaDTO requestMetaDTO) {
OfficialsGroupEntity group = findGroupById(groupId);
if (group.getObjectStatus() == CURRENT) {
group.setObjectStatus(ObjectStatus.DELETED);
repository.save(group);
userService.removeDeletedGroupFromUsers(groupId, requestMetaDTO);
officialsGroupNotifyService.notifyUpdate(mapper.toDomainDto(group), requestMetaDTO);
}
OfficialsGroupEntity group = findCurrentGroupById(groupId);
group.setObjectStatus(ObjectStatus.DELETED);
repository.save(group);
userService.removeDeletedGroupFromUsers(groupId, requestMetaDTO);
officialsGroupNotifyService.notifyUpdate(mapper.toDomainDto(group), requestMetaDTO);
}
public OfficialsGroupEntity findGroupById(UUID id) {
return repository.findById(id).orElseThrow(() -> new TeisResourceNotFoundException(OFFICIALS_GROUP_NOT_FOUND_MESSAGE, List.of(id)));
public OfficialsGroupEntity findCurrentGroupById(UUID id) {
return repository.findByIdAndObjectStatus(id, CURRENT)
.orElseThrow(() -> new TeisResourceNotFoundException(OFFICIALS_GROUP_NOT_FOUND_MESSAGE, List.of(id)));
}
@Transactional
public OfficialsGroupEntity updateGroup(OfficialsGroupDto dto, RequestMetaDTO requestMetaDTO) {
OfficialsGroupEntity group = findGroupById(dto.getId());
if (group.getObjectStatus() != CURRENT) {
throw new TeisResourceNotFoundException(OFFICIALS_GROUP_NOT_FOUND_MESSAGE, List.of(dto.getId()));
}
OfficialsGroupEntity group = findCurrentGroupById(dto.getId());
group.setName(dto.getName());
group.setOfficialsGroupManagerId(dto.getOfficialsGroupManager() == null ? null : dto.getOfficialsGroupManager().getId());
OfficialsGroupEntity officialsGroupEntity = repository.save(group);
......@@ -173,7 +169,7 @@ public class OfficialsGroupService {
@Transactional
public List<OfficialUserDto> addMembersToGroup(String groupId, List<String> memberIds, RequestMetaDTO requestMetaDTO) {
final OfficialsGroupEntity group = findGroupById(UUID.fromString(StringUtils.defaultString(groupId)));
final OfficialsGroupEntity group = findCurrentGroupById(UUID.fromString(StringUtils.defaultString(groupId)));
validateGroupMembers(memberIds);
List<OfficialUserDto> groupMembers = new ArrayList<>();
for (String memberId : memberIds) {
......@@ -187,9 +183,9 @@ public class OfficialsGroupService {
@Transactional
public void deleteMemberFromGroup(String groupId, String memberId, RequestMetaDTO requestMetaDTO) {
final OfficialsGroupEntity group = findGroupById(UUID.fromString(StringUtils.defaultString(groupId)));
final OfficialUserEntity officialUser = userService.findUserById(UUID.fromString(StringUtils.defaultString(memberId)));
if (group.getId().equals(officialUser.getOfficialsGroup().getId())) {
final OfficialsGroupEntity group = findCurrentGroupById(UUID.fromString(StringUtils.defaultString(groupId)));
final OfficialUserEntity officialUser = userService.findCurrentUserById(UUID.fromString(StringUtils.defaultString(memberId)));
if (officialUser.getOfficialsGroup() != null && officialUser.getOfficialsGroup().getId().equals(group.getId())) {
userService.updateUserGroupData(officialUser.getId(), null, requestMetaDTO);
}
}
......@@ -207,7 +203,7 @@ public class OfficialsGroupService {
}
memberIds.forEach(memberId -> {
UUID userId = UUID.fromString(StringUtils.defaultString(memberId));
userService.findUserById(userId);
userService.findCurrentUserById(userId);
});
}
......
......@@ -95,6 +95,11 @@ public class OfficialService {
return repository.findById(id).orElseThrow(() -> new TeisResourceNotFoundException("Official user not found.", List.of(id)));
}
public OfficialUserEntity findCurrentUserById(UUID id) {
return repository.findByIdAndObjectStatus(id, ObjectStatus.CURRENT)
.orElseThrow(() -> new TeisResourceNotFoundException("Official user not found.", List.of(id)));
}
List<OfficialUserEntity> findUsers(String idCode, UUID groupId, String objectStatus) {
BooleanBuilder where = new BooleanBuilder();
QOfficialUserEntity officialUserEntity = QOfficialUserEntity.officialUserEntity;
......
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