mirror of
https://github.com/valitydev/org-manager.git
synced 2024-11-06 00:15:23 +00:00
JD-749: handle null status in InvitationService.list(), fix date conversion for Accepted and Revoked statuses. (#55)
* JD-749: handle null status in InvitationService.list(), fix date conversion for Accepted and Revoked statuses.
This commit is contained in:
parent
2b6e48f696
commit
ee3b229a04
@ -63,7 +63,7 @@ public class InvitationConverter {
|
||||
break;
|
||||
case ACCEPTED:
|
||||
invitation = new InvitationAccepted()
|
||||
.acceptedAt(OffsetDateTime.from(entity.getAcceptedAt()))
|
||||
.acceptedAt(entity.getAcceptedAt().atOffset(ZoneOffset.UTC))
|
||||
.member(new InvitationAcceptedAllOfMember().id(entity.getAcceptedMemberId()));
|
||||
break;
|
||||
case EXPIRED:
|
||||
@ -71,7 +71,7 @@ public class InvitationConverter {
|
||||
break;
|
||||
case REVOKED:
|
||||
invitation = new InvitationRevoked()
|
||||
.revokedAt(OffsetDateTime.from(entity.getRevokedAt()))
|
||||
.revokedAt(entity.getRevokedAt().atOffset(ZoneOffset.UTC))
|
||||
.reason(entity.getRevocationReason());
|
||||
break;
|
||||
default:
|
||||
|
@ -19,6 +19,8 @@ public interface InvitationRepository extends JpaRepository<InvitationEntity, St
|
||||
|
||||
List<InvitationEntity> findByOrganizationIdAndStatus(String organizationId, String status);
|
||||
|
||||
List<InvitationEntity> findByOrganizationId(String organizationId);
|
||||
|
||||
Optional<InvitationEntity> findByAcceptToken(String token);
|
||||
|
||||
@QueryHints(value = {
|
||||
|
@ -72,14 +72,12 @@ public class InvitationService {
|
||||
.build();
|
||||
}
|
||||
|
||||
List<InvitationEntity> entities = invitationRepository.findByOrganizationIdAndStatus(orgId, status.getValue());
|
||||
if (status == InvitationStatusName.PENDING) {
|
||||
// Additional check if invitation already expired
|
||||
entities = entities.stream().filter(invitationEntity -> !invitationEntity.isExpired())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
List<InvitationEntity> entities = status != null
|
||||
? invitationRepository.findByOrganizationIdAndStatus(orgId, status.getValue())
|
||||
: invitationRepository.findByOrganizationId(orgId);
|
||||
|
||||
List<Invitation> invitations = entities.stream()
|
||||
.filter(invite -> !isExpiredPendingInvitation(invite))
|
||||
.map(invitationConverter::toDomain)
|
||||
.collect(toList());
|
||||
|
||||
@ -129,4 +127,10 @@ public class InvitationService {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isExpiredPendingInvitation(InvitationEntity invitationEntity) {
|
||||
return invitationEntity.getStatus() != null
|
||||
&& invitationEntity.getStatus().equalsIgnoreCase(InvitationStatusName.PENDING.getValue())
|
||||
&& invitationEntity.isExpired();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package com.rbkmoney.orgmanager.service;
|
||||
|
||||
import com.rbkmoney.orgmanager.TestObjectFactory;
|
||||
import com.rbkmoney.orgmanager.entity.InvitationEntity;
|
||||
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
|
||||
import com.rbkmoney.orgmanager.repository.AbstractRepositoryTest;
|
||||
import com.rbkmoney.orgmanager.util.TestData;
|
||||
import com.rbkmoney.swag.organizations.model.Invitation;
|
||||
import com.rbkmoney.swag.organizations.model.InvitationListResult;
|
||||
import com.rbkmoney.swag.organizations.model.InvitationStatusName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -12,8 +14,11 @@ import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class InvitationServiceIntegrationTest extends AbstractRepositoryTest {
|
||||
|
||||
@ -52,4 +57,45 @@ public class InvitationServiceIntegrationTest extends AbstractRepositoryTest {
|
||||
assertEquals(1, responseEntity.getBody().getResult().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkInviteWithEmptyStatusRequest() {
|
||||
OrganizationEntity organizationEntity = TestData.buildOrganization(ORGANIZATION_ID, MEMBER_ID);
|
||||
organizationRepository.save(organizationEntity);
|
||||
|
||||
final InvitationEntity invite =
|
||||
TestData.buildInvitation(ORGANIZATION_ID, INVITATION_ID + "_1", LocalDateTime.now().plusDays(1));
|
||||
final InvitationEntity expiredInvite =
|
||||
TestData.buildInvitation(ORGANIZATION_ID, INVITATION_ID + "_2", LocalDateTime.now().minusDays(1));
|
||||
final InvitationEntity acceptedInvite =
|
||||
TestData.buildInvitation(
|
||||
ORGANIZATION_ID,
|
||||
INVITATION_ID + "_3",
|
||||
LocalDateTime.now().plusDays(1),
|
||||
InvitationStatusName.ACCEPTED
|
||||
);
|
||||
acceptedInvite.setAcceptedAt(LocalDateTime.now().plusHours(2));
|
||||
acceptedInvite.setAcceptedMemberId(TestObjectFactory.randomString());
|
||||
final InvitationEntity revokedInvite =
|
||||
TestData.buildInvitation(
|
||||
ORGANIZATION_ID,
|
||||
INVITATION_ID + "_4",
|
||||
LocalDateTime.now().plusDays(1),
|
||||
InvitationStatusName.REVOKED
|
||||
);
|
||||
revokedInvite.setRevokedAt(LocalDateTime.now().plusHours(2));
|
||||
revokedInvite.setRevocationReason(TestObjectFactory.randomString());
|
||||
invitationRepository.saveAll(List.of(invite, expiredInvite, acceptedInvite, revokedInvite));
|
||||
|
||||
ResponseEntity<InvitationListResult> responseEntity =
|
||||
invitationService.list(ORGANIZATION_ID, null);
|
||||
Map<String, Invitation> invites = responseEntity.getBody().getResult().stream()
|
||||
.collect(Collectors.toMap(
|
||||
Invitation::getId,
|
||||
invitation -> invitation
|
||||
));
|
||||
assertEquals(3, invites.size());
|
||||
assertNotNull(invites.get(INVITATION_ID + "_1"));
|
||||
assertNotNull(invites.get(INVITATION_ID + "_3"));
|
||||
assertNotNull(invites.get(INVITATION_ID + "_4"));
|
||||
}
|
||||
}
|
||||
|
@ -30,8 +30,10 @@ import static org.mockito.Mockito.*;
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class InvitationServiceTest {
|
||||
|
||||
@Mock private InvitationConverter invitationConverter;
|
||||
@Mock private InvitationRepository invitationRepository;
|
||||
@Mock
|
||||
private InvitationConverter invitationConverter;
|
||||
@Mock
|
||||
private InvitationRepository invitationRepository;
|
||||
@Mock
|
||||
private OrganizationRepository organizationRepository;
|
||||
@Mock
|
||||
@ -132,6 +134,33 @@ public class InvitationServiceTest {
|
||||
.containsExactly(invitation);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindByOrganizationId() {
|
||||
// Given
|
||||
String orgId = "orgId";
|
||||
|
||||
InvitationEntity entity = new InvitationEntity();
|
||||
Invitation invitation = new Invitation();
|
||||
|
||||
when(organizationRepository.existsById(orgId))
|
||||
.thenReturn(true);
|
||||
when(invitationRepository.findByOrganizationId(orgId))
|
||||
.thenReturn(List.of(entity));
|
||||
when(invitationConverter.toDomain(entity))
|
||||
.thenReturn(invitation);
|
||||
|
||||
// When
|
||||
ResponseEntity<InvitationListResult> response = service.list(orgId, null);
|
||||
|
||||
// Then
|
||||
assertThat(response.getStatusCode())
|
||||
.isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getBody())
|
||||
.isNotNull();
|
||||
assertThat(response.getBody().getResult())
|
||||
.containsExactly(invitation);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnNotFoundIfOrganizationDoesNotExist() {
|
||||
// Given
|
||||
|
@ -43,6 +43,15 @@ public class TestData {
|
||||
String organizationId,
|
||||
String invitationId,
|
||||
LocalDateTime expiresAt
|
||||
) {
|
||||
return buildInvitation(organizationId, invitationId, expiresAt, InvitationStatusName.PENDING);
|
||||
}
|
||||
|
||||
public static InvitationEntity buildInvitation(
|
||||
String organizationId,
|
||||
String invitationId,
|
||||
LocalDateTime expiresAt,
|
||||
InvitationStatusName statusName
|
||||
) {
|
||||
return InvitationEntity.builder()
|
||||
.id(invitationId)
|
||||
@ -52,7 +61,7 @@ public class TestData {
|
||||
.inviteeContactEmail("contactEmail")
|
||||
.inviteeContactType(InviteeContact.TypeEnum.EMAIL.getValue())
|
||||
.organizationId(organizationId)
|
||||
.status("Pending")
|
||||
.status(statusName.getValue())
|
||||
.inviteeRoles(Set.of(
|
||||
MemberRoleEntity.builder()
|
||||
.id("role1")
|
||||
|
Loading…
Reference in New Issue
Block a user