isolate invitation repository in invitation service

This commit is contained in:
ggmaleva 2021-04-29 15:37:10 +03:00
parent d59045e6c3
commit b65181314f
No known key found for this signature in database
GPG Key ID: 0E412B78565B108F
2 changed files with 14 additions and 14 deletions

View File

@ -1,5 +1,6 @@
package com.rbkmoney.orgmanager.service;
import com.rbkmoney.orgmanager.controller.error.InviteExpiredException;
import com.rbkmoney.orgmanager.converter.InvitationConverter;
import com.rbkmoney.orgmanager.entity.InvitationEntity;
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
@ -112,4 +113,13 @@ public class InvitationService {
});
}
public InvitationEntity getByToken(String token) {
InvitationEntity invitationEntity = invitationRepository.findByAcceptToken(token)
.orElseThrow(ResourceNotFoundException::new);
if (invitationEntity.isExpired()) {
throw new InviteExpiredException(invitationEntity.getExpiresAt().toString());
}
return invitationEntity;
}
}

View File

@ -1,6 +1,5 @@
package com.rbkmoney.orgmanager.service;
import com.rbkmoney.orgmanager.controller.error.InviteExpiredException;
import com.rbkmoney.orgmanager.converter.MemberConverter;
import com.rbkmoney.orgmanager.converter.MemberRoleConverter;
import com.rbkmoney.orgmanager.converter.OrganizationConverter;
@ -9,7 +8,6 @@ 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.InvitationRepository;
import com.rbkmoney.orgmanager.repository.MemberRepository;
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
import com.rbkmoney.orgmanager.service.dto.MemberWithRoleDto;
@ -41,7 +39,7 @@ public class OrganizationService {
private final MemberConverter memberConverter;
private final MemberRoleConverter memberRoleConverter;
private final MemberRepository memberRepository;
private final InvitationRepository invitationRepository;
private final InvitationService invitationService;
private final MemberRoleService memberRoleService;
// TODO [a.romanov]: idempotency
@ -234,7 +232,7 @@ public class OrganizationService {
@Transactional
public OrganizationMembership joinOrganization(String token, String userId, String userEmail) {
InvitationEntity invitationEntity = getInvitationByToken(token);
InvitationEntity invitationEntity = invitationService.getByToken(token);
OrganizationEntity organizationEntity = findById(invitationEntity.getOrganizationId());
MemberEntity memberEntity = findOrCreateMember(userId, userEmail);
memberEntity.getRoles().addAll(invitationEntity.getInviteeRoles());
@ -247,15 +245,6 @@ public class OrganizationService {
return organizationMembership;
}
private InvitationEntity getInvitationByToken(String token) {
InvitationEntity invitationEntity = invitationRepository.findByAcceptToken(token)
.orElseThrow(ResourceNotFoundException::new);
if (invitationEntity.isExpired()) {
throw new InviteExpiredException(invitationEntity.getExpiresAt().toString());
}
return invitationEntity;
}
private MemberEntity findOrCreateMember(String userId, String userEmail) {
return memberRepository.findById(userId)
.orElseGet(() -> {
@ -273,8 +262,9 @@ public class OrganizationService {
invitationEntity.setStatus(InvitationStatusName.ACCEPTED.getValue());
}
@Transactional(readOnly = true)
public String getOrgIdByInvitationToken(String token) {
InvitationEntity invitationEntity = getInvitationByToken(token);
InvitationEntity invitationEntity = invitationService.getByToken(token);
OrganizationEntity organizationEntity = findById(invitationEntity.getOrganizationId());
return organizationEntity.getId();
}