fix after review

This commit is contained in:
ggmaleva 2021-04-12 22:37:38 +03:00
parent f75d7d6f89
commit 795e14369e
No known key found for this signature in database
GPG Key ID: 0E412B78565B108F
7 changed files with 140 additions and 28 deletions

View File

@ -4,13 +4,18 @@ import com.rbkmoney.bouncer.context.v1.Entity;
import com.rbkmoney.bouncer.context.v1.User; import com.rbkmoney.bouncer.context.v1.User;
import com.rbkmoney.orgmanager.entity.MemberEntity; import com.rbkmoney.orgmanager.entity.MemberEntity;
import com.rbkmoney.orgmanager.entity.MemberRoleEntity; 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.Member;
import com.rbkmoney.swag.organizations.model.MemberRole;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
@Service @Service
@ -38,6 +43,27 @@ public class MemberConverter {
.collect(toList())); .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) { public User toThrift(MemberEntity member) {
return new User() return new User()
.setId(member.getId()) .setId(member.getId())

View File

@ -1,6 +1,7 @@
package com.rbkmoney.orgmanager.converter; package com.rbkmoney.orgmanager.converter;
import com.rbkmoney.orgmanager.entity.MemberRoleEntity; 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.MemberRole;
import com.rbkmoney.swag.organizations.model.MemberRoleScope; import com.rbkmoney.swag.organizations.model.MemberRoleScope;
import com.rbkmoney.swag.organizations.model.ResourceScopeId; import com.rbkmoney.swag.organizations.model.ResourceScopeId;
@ -37,4 +38,17 @@ public class MemberRoleConverter {
.roleId(RoleId.fromValue(entity.getRoleId())) .roleId(RoleId.fromValue(entity.getRoleId()))
.scope(memberRoleScope); .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);
}
} }

View File

@ -1,9 +1,35 @@
package com.rbkmoney.orgmanager.repository; package com.rbkmoney.orgmanager.repository;
import com.rbkmoney.orgmanager.entity.MemberEntity; 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.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @Repository
public interface MemberRepository extends JpaRepository<MemberEntity, String> { 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);
} }

View File

@ -35,4 +35,7 @@ public interface OrganizationRepository extends JpaRepository<OrganizationEntity
nativeQuery = true) nativeQuery = true)
List<OrganizationEntity> findAllByMember(String userId, String continuationId); List<OrganizationEntity> findAllByMember(String userId, String continuationId);
boolean existsById(String id);
} }

View File

@ -12,6 +12,7 @@ import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
import com.rbkmoney.orgmanager.repository.InvitationRepository; import com.rbkmoney.orgmanager.repository.InvitationRepository;
import com.rbkmoney.orgmanager.repository.MemberRepository; import com.rbkmoney.orgmanager.repository.MemberRepository;
import com.rbkmoney.orgmanager.repository.OrganizationRepository; import com.rbkmoney.orgmanager.repository.OrganizationRepository;
import com.rbkmoney.orgmanager.service.dto.MemberWithRoleDto;
import com.rbkmoney.swag.organizations.model.*; import com.rbkmoney.swag.organizations.model.*;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -137,23 +138,15 @@ public class OrganizationService {
@Transactional(readOnly = true) @Transactional(readOnly = true)
public MemberOrgListResult listMembers(String orgId) { public MemberOrgListResult listMembers(String orgId) {
OrganizationEntity entity = findById(orgId); if (!organizationRepository.existsById(orgId)) {
List<Member> members = getOrgMembersWithActiveRole(entity); throw new ResourceNotFoundException();
}
List<MemberWithRoleDto> orgMemberList = memberRepository.getOrgMemberList(orgId);
List<Member> members = memberConverter.toDomain(orgMemberList);
return new MemberOrgListResult() return new MemberOrgListResult()
.result(members); .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) @Transactional(readOnly = true)
public OrganizationSearchResult findAllOrganizations(String userId, Integer limit, String continuationId) { public OrganizationSearchResult findAllOrganizations(String userId, Integer limit, String continuationId) {
if (limit == null || limit == 0) { if (limit == null || limit == 0) {

View File

@ -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;
}

View File

@ -4,11 +4,11 @@ import com.rbkmoney.orgmanager.TestObjectFactory;
import com.rbkmoney.orgmanager.converter.MemberConverter; import com.rbkmoney.orgmanager.converter.MemberConverter;
import com.rbkmoney.orgmanager.converter.OrganizationConverter; import com.rbkmoney.orgmanager.converter.OrganizationConverter;
import com.rbkmoney.orgmanager.entity.MemberEntity; import com.rbkmoney.orgmanager.entity.MemberEntity;
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
import com.rbkmoney.orgmanager.entity.OrganizationEntity; import com.rbkmoney.orgmanager.entity.OrganizationEntity;
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException; import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
import com.rbkmoney.orgmanager.repository.MemberRepository; import com.rbkmoney.orgmanager.repository.MemberRepository;
import com.rbkmoney.orgmanager.repository.OrganizationRepository; 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.Member;
import com.rbkmoney.swag.organizations.model.MemberOrgListResult; import com.rbkmoney.swag.organizations.model.MemberOrgListResult;
import com.rbkmoney.swag.organizations.model.Organization; import com.rbkmoney.swag.organizations.model.Organization;
@ -110,22 +110,21 @@ public class OrganizationServiceTest {
@Test @Test
void shouldListMembers() { void shouldListMembers() {
// Given // Given
OrganizationEntity organizationEntity = TestObjectFactory.buildOrganization(); String orgId = TestObjectFactory.randomString();
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));
Member member = new Member(); Member member = new Member();
when(organizationRepository.findById(organizationEntity.getId())) MemberWithRoleDto memberWithRoleDto = getMemberWithRoleDto();
.thenReturn(Optional.of(organizationEntity)); List<MemberWithRoleDto> memberWithRoleList = List.of(memberWithRoleDto);
when(memberConverter.toDomain(memberEntity, List.of(memberRoleEntity)))
.thenReturn(member); when(organizationRepository.existsById(orgId))
.thenReturn(true);
when(memberRepository.getOrgMemberList(orgId))
.thenReturn(memberWithRoleList);
when(memberConverter.toDomain(memberWithRoleList))
.thenReturn(List.of(member));
// When // When
MemberOrgListResult response = service.listMembers(organizationEntity.getId()); MemberOrgListResult response = service.listMembers(orgId);
// Then // Then
assertThat(response) assertThat(response)
@ -134,13 +133,52 @@ public class OrganizationServiceTest {
.containsExactly(member); .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 @Test
void shouldReturnNotFoundIfNoOrganizationExistForMembersList() { void shouldReturnNotFoundIfNoOrganizationExistForMembersList() {
// Given // Given
String orgId = "orgId"; String orgId = "orgId";
// When // When
when(organizationRepository.findById(orgId)) when(organizationRepository.existsById(orgId))
.thenReturn(Optional.empty()); .thenReturn(false);
// Then // Then
assertThrows(ResourceNotFoundException.class, () -> service.listMembers(orgId)); assertThrows(ResourceNotFoundException.class, () -> service.listMembers(orgId));
} }