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

TEIS-365. If user is in any other group as manager remove it form there.

parent aa3ab951
......@@ -84,7 +84,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;
}
......@@ -160,7 +160,7 @@ 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;
......@@ -169,6 +169,19 @@ public class OfficialsGroupService {
@Transactional
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();
}
......@@ -178,17 +191,14 @@ public class OfficialsGroupService {
.message("Member id is mandatory.")
.build());
}
List<OfficialUserDto> groupMembers = new ArrayList<>();
for (String memberId : memberIds) {
UUID officialUserId = UUID.fromString(memberId);
final OfficialUserEntity officialUserEntity = userService.updateUserGroupData(officialUserId, group.getId(), requestMetaDTO);
groupMembers.add(userService.composeDtoFromEntity(officialUserEntity));
}
return groupMembers;
memberIds.forEach(memberId -> {
UUID userId = UUID.fromString(memberId);
userService.findUserById(userId);
});
}
private void removeManagerFromOtherGroups(OfficialsGroupEntity officialsGroupEntity) {
repository.findByOfficialsGroupManagerId(officialsGroupEntity.getOfficialsGroupManagerId())
private void removeManagerFromOtherGroups(OfficialsGroupEntity officialsGroupEntity, UUID managerId) {
repository.findByOfficialsGroupManagerId(managerId)
.stream()
.filter(group -> !group.getId().equals(officialsGroupEntity.getId()))
.forEach(group -> {
......
......@@ -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