Merge pull request #33 from rbkmoney/ft/JD-251

deactivate role while remove role and fix get member list query
This commit is contained in:
Gregory 2021-04-16 17:10:29 +03:00 committed by GitHub
commit e8ca700bd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 16 deletions

View File

@ -1,9 +1,6 @@
package com.rbkmoney.orgmanager.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import javax.persistence.Entity;
import javax.persistence.Id;
@ -17,9 +14,11 @@ import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "member_role")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class MemberRoleEntity implements Serializable {
@Id
@EqualsAndHashCode.Include
private String id;
private String organizationId;
private String roleId;

View File

@ -20,14 +20,15 @@ public interface MemberRepository extends JpaRepository<MemberEntity, String> {
" 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_to_organization mto, " +
" org_manager.member_role mr, " +
" org_manager.member m " +
" WHERE " +
" o.id = ?1 " +
" mto.organization_id = ?1 " +
" AND mto .member_id = m.id " +
" AND mr.active = 'true' " +
" AND mr.id = mtmr.member_role_id " +
" AND mr.organization_id = o.id " +
" AND mr.id = mtmr.member_role_id " +
" AND mr.organization_id = mto.organization_id " +
" AND m.id = mtmr.member_id ", nativeQuery = true)
List<MemberWithRoleDto> getOrgMemberList(String orgId);

View File

@ -1,10 +1,13 @@
package com.rbkmoney.orgmanager.service;
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
import com.rbkmoney.swag.organizations.model.MemberRole;
public interface MemberRoleService {
MemberRole findById(String id);
MemberRoleEntity findEntityById(String id);
}

View File

@ -1,6 +1,7 @@
package com.rbkmoney.orgmanager.service;
import com.rbkmoney.orgmanager.converter.MemberRoleConverter;
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
import com.rbkmoney.orgmanager.repository.MemberRoleRepository;
import com.rbkmoney.swag.organizations.model.MemberRole;
@ -23,4 +24,11 @@ public class MemberRoleServiceImpl implements MemberRoleService {
.orElseThrow(ResourceNotFoundException::new);
}
@Override
@Transactional(readOnly = true)
public MemberRoleEntity findEntityById(String id) {
return repository.findById(id)
.orElseThrow(ResourceNotFoundException::new);
}
}

View File

@ -41,6 +41,7 @@ public class OrganizationService {
private final MemberRoleConverter memberRoleConverter;
private final MemberRepository memberRepository;
private final InvitationRepository invitationRepository;
private final MemberRoleService memberRoleService;
// TODO [a.romanov]: idempotency
public ResponseEntity<Organization> create(
@ -115,13 +116,13 @@ public class OrganizationService {
public void expelOrgMember(String orgId, String userId) {
OrganizationEntity organization = findById(orgId);
MemberEntity member = getMember(userId, organization);
deactivateOrgMemberRole(orgId, member);
deactivateOrgMemberRoles(orgId, member);
member.getRoles()
.removeIf(memberRoleEntity -> memberRoleEntity.getOrganizationId().equals(orgId));
organization.getMembers().remove(member);
}
private void deactivateOrgMemberRole(String orgId, MemberEntity member) {
private void deactivateOrgMemberRoles(String orgId, MemberEntity member) {
member.getRoles()
.stream()
.filter(memberRoleEntity -> memberRoleEntity.getOrganizationId().equals(orgId))
@ -132,8 +133,9 @@ public class OrganizationService {
public void removeMemberRole(String orgId, String userId, String memberRoleId) {
OrganizationEntity organization = findById(orgId);
MemberEntity member = getMember(userId, organization);
member.getRoles()
.removeIf(memberRoleEntity -> memberRoleEntity.getId().equals(memberRoleId));
MemberRoleEntity roleToRemove = memberRoleService.findEntityById(memberRoleId);
roleToRemove.setActive(Boolean.FALSE);
member.getRoles().remove(roleToRemove);
}
@Transactional(readOnly = true)

View File

@ -145,7 +145,9 @@ public class OrganizationServiceIntegrationTest extends AbstractRepositoryTest {
void shouldGetListOrgMembers() {
MemberEntity member1 = TestObjectFactory.testMemberEntity(TestObjectFactory.randomString());
MemberEntity member2 = TestObjectFactory.testMemberEntity(TestObjectFactory.randomString());
MemberEntity anotherMember = TestObjectFactory.testMemberEntity(TestObjectFactory.randomString());
OrganizationEntity organization = TestObjectFactory.buildOrganization(Set.of(member1, member2));
OrganizationEntity anotherOrganization = TestObjectFactory.buildOrganization(Set.of(anotherMember));
MemberRoleEntity activeMember1RoleInOrg = buildMemberRole(RoleId.ACCOUNTANT, organization.getId());
activeMember1RoleInOrg.setActive(Boolean.TRUE);
MemberRoleEntity savedMember1Role = memberRoleRepository.save(activeMember1RoleInOrg);
@ -157,12 +159,16 @@ public class OrganizationServiceIntegrationTest extends AbstractRepositoryTest {
nonActiveMember2RoleInOrg.setActive(Boolean.FALSE);
MemberRoleEntity savedNonActiveMember2Role = memberRoleRepository.save(nonActiveMember2RoleInOrg);
member2.setRoles(Set.of(savedActiveMember2Role, savedNonActiveMember2Role));
memberRepository.saveAll(Set.of(member1, member2));
OrganizationEntity savedOrganization = organizationRepository.save(organization);
MemberRoleEntity activeAnotherMemberRoleInOrg = buildMemberRole(RoleId.ACCOUNTANT, anotherOrganization.getId());
activeAnotherMemberRoleInOrg.setActive(Boolean.TRUE);
MemberRoleEntity savedAnotherRole = memberRoleRepository.save(activeAnotherMemberRoleInOrg);
anotherMember.setRoles(Set.of(savedAnotherRole));
memberRepository.saveAll(Set.of(member1, member2, anotherMember));
organizationRepository.saveAll(Set.of(organization, anotherOrganization));
MemberOrgListResult memberOrgListResult = organizationService.listMembers(savedOrganization.getId());
MemberOrgListResult memberOrgListResult = organizationService.listMembers(organization.getId());
assertEquals(savedOrganization.getMembers().size(), memberOrgListResult.getResult().size());
assertEquals(organization.getMembers().size(), memberOrgListResult.getResult().size());
List<String> roles = memberOrgListResult.getResult().stream()
.map(Member::getRoles)
@ -173,8 +179,25 @@ public class OrganizationServiceIntegrationTest extends AbstractRepositoryTest {
assertThat(roles, hasItem(activeMember1RoleInOrg.getId()));
assertThat(roles, hasItem(activeMember2RoleInOrg.getId()));
assertThat(roles, not(hasItem(nonActiveMember2RoleInOrg.getId())));
assertThat(roles, not(hasItem(activeAnotherMemberRoleInOrg.getId())));
}
@Test
@Transactional
void removeMemberRoleTest() {
MemberEntity memberEntity = TestObjectFactory.testMemberEntity(TestObjectFactory.randomString());
OrganizationEntity organization = TestObjectFactory.buildOrganization(memberEntity);
MemberRoleEntity savedMemberRole =
memberRoleRepository.save(TestObjectFactory.buildMemberRole(RoleId.ACCOUNTANT, organization.getId()));
memberEntity.setRoles(Set.of(savedMemberRole));
MemberEntity savedMember = memberRepository.save(memberEntity);
OrganizationEntity savedOrganization = organizationRepository.save(organization);
organizationService.removeMemberRole(savedOrganization.getId(), savedMember.getId(), savedMemberRole.getId());
assertThat(memberRepository.findById(savedMember.getId()).get().getRoles(), not(hasItem(savedMemberRole)));
assertFalse(memberRoleRepository.findById(savedMemberRole.getId()).get().isActive());
}