mirror of
https://github.com/valitydev/org-manager.git
synced 2024-11-06 00:15:23 +00:00
fix after review
This commit is contained in:
parent
f75d7d6f89
commit
795e14369e
@ -4,13 +4,18 @@ import com.rbkmoney.bouncer.context.v1.Entity;
|
||||
import com.rbkmoney.bouncer.context.v1.User;
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.service.dto.MemberDto;
|
||||
import com.rbkmoney.orgmanager.service.dto.MemberWithRoleDto;
|
||||
import com.rbkmoney.swag.organizations.model.Member;
|
||||
import com.rbkmoney.swag.organizations.model.MemberRole;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
@Service
|
||||
@ -38,6 +43,27 @@ public class MemberConverter {
|
||||
.collect(toList()));
|
||||
}
|
||||
|
||||
public List<Member> toDomain(List<MemberWithRoleDto> memberWithRoleDtos) {
|
||||
Map<MemberDto, List<MemberWithRoleDto>> rolesByMember = memberWithRoleDtos.stream()
|
||||
.collect(groupingBy(
|
||||
memberWithRoleDto -> new MemberDto(memberWithRoleDto.getId(), memberWithRoleDto.getEmail())));
|
||||
return rolesByMember.entrySet().stream()
|
||||
.map(this::toMember)
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
private Member toMember(Map.Entry<MemberDto, List<MemberWithRoleDto>> roleWithMember) {
|
||||
Member member = new Member();
|
||||
MemberDto memberDto = roleWithMember.getKey();
|
||||
member.setId(memberDto.getId());
|
||||
member.setUserEmail(memberDto.getEmail());
|
||||
List<MemberRole> roles = roleWithMember.getValue().stream()
|
||||
.map(memberRoleConverter::toDomain)
|
||||
.collect(toList());
|
||||
member.setRoles(roles);
|
||||
return member;
|
||||
}
|
||||
|
||||
public User toThrift(MemberEntity member) {
|
||||
return new User()
|
||||
.setId(member.getId())
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.rbkmoney.orgmanager.converter;
|
||||
|
||||
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.service.dto.MemberWithRoleDto;
|
||||
import com.rbkmoney.swag.organizations.model.MemberRole;
|
||||
import com.rbkmoney.swag.organizations.model.MemberRoleScope;
|
||||
import com.rbkmoney.swag.organizations.model.ResourceScopeId;
|
||||
@ -37,4 +38,17 @@ public class MemberRoleConverter {
|
||||
.roleId(RoleId.fromValue(entity.getRoleId()))
|
||||
.scope(memberRoleScope);
|
||||
}
|
||||
|
||||
public MemberRole toDomain(MemberWithRoleDto memberWithRoleDto) {
|
||||
MemberRoleScope memberRoleScope = Optional.ofNullable(memberWithRoleDto.getScopeId())
|
||||
.map(ResourceScopeId::fromValue)
|
||||
.map(resourceScopeId -> new MemberRoleScope()
|
||||
.id(resourceScopeId)
|
||||
.resourceId(memberWithRoleDto.getResourceId()))
|
||||
.orElse(null);
|
||||
return new MemberRole()
|
||||
.id(memberWithRoleDto.getMemberRoleId())
|
||||
.roleId(RoleId.fromValue(memberWithRoleDto.getRoleId()))
|
||||
.scope(memberRoleScope);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,35 @@
|
||||
package com.rbkmoney.orgmanager.repository;
|
||||
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.service.dto.MemberWithRoleDto;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface MemberRepository extends JpaRepository<MemberEntity, String> {
|
||||
|
||||
|
||||
@Query(value = "SELECT m.id, " +
|
||||
" m.email, " +
|
||||
" mr.id as memberRoleId, " +
|
||||
" mr.organization_id as organizationId, " +
|
||||
" mr.role_id as roleId, " +
|
||||
" mr.scope_id as scopeId, " +
|
||||
" mr.resource_id as resourceId" +
|
||||
" FROM org_manager.member_to_member_role mtmr, " +
|
||||
" org_manager.organization o, " +
|
||||
" org_manager.member_role mr, " +
|
||||
" org_manager.member m " +
|
||||
" WHERE " +
|
||||
" o.id = ?1 " +
|
||||
" AND mr.active = 'true' " +
|
||||
" AND mr.id = mtmr.member_role_id " +
|
||||
" AND mr.organization_id = o.id " +
|
||||
" AND m.id = mtmr.member_id ", nativeQuery = true)
|
||||
List<MemberWithRoleDto> getOrgMemberList(String orgId);
|
||||
|
||||
|
||||
}
|
||||
|
@ -35,4 +35,7 @@ public interface OrganizationRepository extends JpaRepository<OrganizationEntity
|
||||
nativeQuery = true)
|
||||
List<OrganizationEntity> findAllByMember(String userId, String continuationId);
|
||||
|
||||
|
||||
boolean existsById(String id);
|
||||
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
|
||||
import com.rbkmoney.orgmanager.repository.InvitationRepository;
|
||||
import com.rbkmoney.orgmanager.repository.MemberRepository;
|
||||
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
|
||||
import com.rbkmoney.orgmanager.service.dto.MemberWithRoleDto;
|
||||
import com.rbkmoney.swag.organizations.model.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@ -137,23 +138,15 @@ public class OrganizationService {
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public MemberOrgListResult listMembers(String orgId) {
|
||||
OrganizationEntity entity = findById(orgId);
|
||||
List<Member> members = getOrgMembersWithActiveRole(entity);
|
||||
if (!organizationRepository.existsById(orgId)) {
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
List<MemberWithRoleDto> orgMemberList = memberRepository.getOrgMemberList(orgId);
|
||||
List<Member> members = memberConverter.toDomain(orgMemberList);
|
||||
return new MemberOrgListResult()
|
||||
.result(members);
|
||||
}
|
||||
|
||||
private List<Member> getOrgMembersWithActiveRole(OrganizationEntity entity) {
|
||||
return entity.getMembers().stream()
|
||||
.map(memberEntity -> {
|
||||
List<MemberRoleEntity> rolesInOrg = memberEntity.getRoles().stream()
|
||||
.filter(memberRole -> isActiveOrgMemberRole(entity.getId(), memberRole))
|
||||
.collect(toList());
|
||||
return memberConverter.toDomain(memberEntity, rolesInOrg);
|
||||
})
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public OrganizationSearchResult findAllOrganizations(String userId, Integer limit, String continuationId) {
|
||||
if (limit == null || limit == 0) {
|
||||
|
@ -0,0 +1,12 @@
|
||||
package com.rbkmoney.orgmanager.service.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MemberDto {
|
||||
|
||||
private String id;
|
||||
private String email;
|
||||
}
|
@ -4,11 +4,11 @@ import com.rbkmoney.orgmanager.TestObjectFactory;
|
||||
import com.rbkmoney.orgmanager.converter.MemberConverter;
|
||||
import com.rbkmoney.orgmanager.converter.OrganizationConverter;
|
||||
import com.rbkmoney.orgmanager.entity.MemberEntity;
|
||||
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
|
||||
import com.rbkmoney.orgmanager.repository.MemberRepository;
|
||||
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
|
||||
import com.rbkmoney.orgmanager.service.dto.MemberWithRoleDto;
|
||||
import com.rbkmoney.swag.organizations.model.Member;
|
||||
import com.rbkmoney.swag.organizations.model.MemberOrgListResult;
|
||||
import com.rbkmoney.swag.organizations.model.Organization;
|
||||
@ -110,22 +110,21 @@ public class OrganizationServiceTest {
|
||||
@Test
|
||||
void shouldListMembers() {
|
||||
// Given
|
||||
OrganizationEntity organizationEntity = TestObjectFactory.buildOrganization();
|
||||
MemberEntity memberEntity = new MemberEntity();
|
||||
MemberRoleEntity memberRoleEntity = new MemberRoleEntity();
|
||||
memberRoleEntity.setActive(true);
|
||||
memberRoleEntity.setOrganizationId(organizationEntity.getId());
|
||||
memberEntity.setRoles(Set.of(memberRoleEntity));
|
||||
organizationEntity.setMembers(Set.of(memberEntity));
|
||||
String orgId = TestObjectFactory.randomString();
|
||||
Member member = new Member();
|
||||
|
||||
when(organizationRepository.findById(organizationEntity.getId()))
|
||||
.thenReturn(Optional.of(organizationEntity));
|
||||
when(memberConverter.toDomain(memberEntity, List.of(memberRoleEntity)))
|
||||
.thenReturn(member);
|
||||
MemberWithRoleDto memberWithRoleDto = getMemberWithRoleDto();
|
||||
List<MemberWithRoleDto> memberWithRoleList = List.of(memberWithRoleDto);
|
||||
|
||||
when(organizationRepository.existsById(orgId))
|
||||
.thenReturn(true);
|
||||
when(memberRepository.getOrgMemberList(orgId))
|
||||
.thenReturn(memberWithRoleList);
|
||||
when(memberConverter.toDomain(memberWithRoleList))
|
||||
.thenReturn(List.of(member));
|
||||
|
||||
// When
|
||||
MemberOrgListResult response = service.listMembers(organizationEntity.getId());
|
||||
MemberOrgListResult response = service.listMembers(orgId);
|
||||
|
||||
// Then
|
||||
assertThat(response)
|
||||
@ -134,13 +133,52 @@ public class OrganizationServiceTest {
|
||||
.containsExactly(member);
|
||||
}
|
||||
|
||||
private MemberWithRoleDto getMemberWithRoleDto() {
|
||||
return new MemberWithRoleDto() {
|
||||
@Override
|
||||
public String getId() {
|
||||
return TestObjectFactory.randomString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEmail() {
|
||||
return TestObjectFactory.randomString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMemberRoleId() {
|
||||
return TestObjectFactory.randomString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOrganizationId() {
|
||||
return TestObjectFactory.randomString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRoleId() {
|
||||
return TestObjectFactory.randomString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getScopeId() {
|
||||
return TestObjectFactory.randomString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getResourceId() {
|
||||
return TestObjectFactory.randomString();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnNotFoundIfNoOrganizationExistForMembersList() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
// When
|
||||
when(organizationRepository.findById(orgId))
|
||||
.thenReturn(Optional.empty());
|
||||
when(organizationRepository.existsById(orgId))
|
||||
.thenReturn(false);
|
||||
// Then
|
||||
assertThrows(ResourceNotFoundException.class, () -> service.listMembers(orgId));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user