refactoring invitation revoke method

This commit is contained in:
ggmaleva 2021-04-29 13:57:48 +03:00
parent 600de6b12e
commit d59045e6c3
No known key found for this signature in database
GPG Key ID: 0E412B78565B108F
4 changed files with 25 additions and 30 deletions

View File

@ -100,7 +100,10 @@ public class OrgsController implements OrgsApi {
log.info("Revoke invitation: requestId={}, orgId={}, invitationId={}, payload={}",
xRequestID, orgId, invitationId, inlineObject1);
resourceAccessService.checkInvitationRights(orgId, invitationId);
return invitationService.revoke(orgId, invitationId, inlineObject1);
invitationService.revoke(orgId, invitationId, inlineObject1);
return ResponseEntity
.status(HttpStatus.NO_CONTENT)
.build();
}
@Override

View File

@ -27,4 +27,7 @@ public interface InvitationRepository extends JpaRepository<InvitationEntity, St
@Query("select i from InvitationEntity i where i.status = 'Pending'")
Stream<InvitationEntity> findAllPendingStatus();
Optional<InvitationEntity> findByIdAndOrganizationId(String id, String orgId);
}

View File

@ -2,6 +2,7 @@ package com.rbkmoney.orgmanager.service;
import com.rbkmoney.orgmanager.converter.InvitationConverter;
import com.rbkmoney.orgmanager.entity.InvitationEntity;
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
import com.rbkmoney.orgmanager.repository.InvitationRepository;
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
import com.rbkmoney.swag.organizations.model.*;
@ -21,6 +22,7 @@ import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Slf4j
public class InvitationService {
@ -31,6 +33,7 @@ public class InvitationService {
private final MailInviteMessageSender mailInviteMessageSender;
// TODO [a.romanov]: idempotency
@Transactional
public ResponseEntity<Invitation> create(
String orgId,
InvitationRequest invitation,
@ -88,25 +91,15 @@ public class InvitationService {
.result(invitations));
}
public ResponseEntity<Void> revoke(String orgId, String invitationId, InlineObject1 inlineObject) {
Optional<InvitationEntity> entity = invitationRepository.findById(invitationId);
@Transactional
public void revoke(String orgId, String invitationId, InlineObject1 inlineObject) {
InvitationEntity invitation = invitationRepository.findByIdAndOrganizationId(invitationId, orgId)
.orElseThrow(ResourceNotFoundException::new);
invitation.setStatus(inlineObject.getStatus().getValue());
invitation.setRevocationReason(inlineObject.getReason());
invitation.setRevokedAt(LocalDateTime.now());
invitationRepository.save(invitation);
if (entity.isEmpty()) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.build();
}
InvitationEntity updatedEntity = entity.get();
updatedEntity.setStatus(inlineObject.getStatus().getValue());
updatedEntity.setRevocationReason(inlineObject.getReason());
updatedEntity.setRevokedAt(LocalDateTime.now());
invitationRepository.save(updatedEntity);
return ResponseEntity
.status(HttpStatus.NO_CONTENT)
.build();
}
@Transactional

View File

@ -2,6 +2,7 @@ package com.rbkmoney.orgmanager.service;
import com.rbkmoney.orgmanager.converter.InvitationConverter;
import com.rbkmoney.orgmanager.entity.InvitationEntity;
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
import com.rbkmoney.orgmanager.repository.InvitationRepository;
import com.rbkmoney.orgmanager.repository.OrganizationRepository;
import com.rbkmoney.swag.organizations.model.*;
@ -18,6 +19,7 @@ import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@ -152,11 +154,11 @@ public class InvitationServiceTest {
String invitationId = "invitationId";
InvitationEntity entity = new InvitationEntity();
when(invitationRepository.findById(invitationId))
when(invitationRepository.findByIdAndOrganizationId(invitationId, orgId))
.thenReturn(Optional.of(entity));
// When
ResponseEntity<Void> response = service.revoke(orgId, invitationId, new InlineObject1()
service.revoke(orgId, invitationId, new InlineObject1()
.reason("reason")
.status(InlineObject1.StatusEnum.REVOKED));
@ -167,8 +169,6 @@ public class InvitationServiceTest {
.isEqualTo("reason");
verify(invitationRepository, times(1))
.save(entity);
assertThat(response.getStatusCode())
.isEqualTo(HttpStatus.NO_CONTENT);
}
@Test
@ -177,14 +177,10 @@ public class InvitationServiceTest {
String orgId = "orgId";
String invitationId = "invitationId";
when(invitationRepository.findById(invitationId))
when(invitationRepository.findByIdAndOrganizationId(invitationId, orgId))
.thenReturn(Optional.empty());
// When
ResponseEntity<Void> response = service.revoke(orgId, invitationId, new InlineObject1());
// Then
assertThat(response.getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND);
// When Then
assertThrows(ResourceNotFoundException.class, () -> service.revoke(orgId, invitationId, new InlineObject1()));
}
}