use of uk.gov.pay.adminusers.model.InviteCompleteResponse in project pay-adminusers by alphagov.
the class UserInviteCompleter method complete.
@Override
@Transactional
public Optional<InviteCompleteResponse> complete(String inviteCode) {
return inviteDao.findByCode(inviteCode).map(inviteEntity -> {
if (inviteEntity.isExpired() || inviteEntity.isDisabled()) {
throw inviteLockedException(inviteEntity.getCode());
}
return userDao.findByEmail(inviteEntity.getEmail()).map(userEntity -> {
if (inviteEntity.getService() != null && inviteEntity.isUserType()) {
ServiceRoleEntity serviceRole = new ServiceRoleEntity(inviteEntity.getService(), inviteEntity.getRole());
userEntity.addServiceRole(serviceRole);
userDao.merge(userEntity);
inviteEntity.setDisabled(true);
inviteDao.merge(inviteEntity);
InviteCompleteResponse response = new InviteCompleteResponse(inviteEntity.toInvite());
response.setUserExternalId(userEntity.getExternalId());
response.setServiceExternalId(inviteEntity.getService().getExternalId());
return Optional.of(response);
} else {
throw internalServerError(format("Attempting to complete user subscription to a service for a non existent service. invite-code = %s", inviteEntity.getCode()));
}
}).orElseGet(() -> {
throw internalServerError(format("Attempting to complete user subscription to a service for a non existent user. invite-code = %s", inviteEntity.getCode()));
});
}).orElseGet(Optional::empty);
}
use of uk.gov.pay.adminusers.model.InviteCompleteResponse in project pay-adminusers by alphagov.
the class ServiceInviteCompleter method complete.
/**
* Completes a service invite.
* ie. it creates and persists a user from an invite or/and subscribe a user to an existing service
* and if it is a service invite also creates a default service.
* It then disables the invite.
*/
@Override
@Transactional
public Optional<InviteCompleteResponse> complete(String inviteCode) {
return inviteDao.findByCode(inviteCode).map(inviteEntity -> {
if (inviteEntity.isExpired() || inviteEntity.isDisabled()) {
throw inviteLockedException(inviteEntity.getCode());
}
if (userDao.findByEmail(inviteEntity.getEmail()).isPresent()) {
throw conflictingEmail(inviteEntity.getEmail());
}
if (inviteEntity.isServiceType()) {
UserEntity userEntity = inviteEntity.mapToUserEntity();
ServiceEntity serviceEntity = ServiceEntity.from(Service.from());
if (!data.getGatewayAccountIds().isEmpty()) {
serviceEntity.addGatewayAccountIds(data.getGatewayAccountIds().toArray(new String[0]));
}
serviceDao.persist(serviceEntity);
ServiceRoleEntity serviceRoleEntity = new ServiceRoleEntity(serviceEntity, inviteEntity.getRole());
userEntity.addServiceRole(serviceRoleEntity);
userDao.merge(userEntity);
inviteEntity.setService(serviceEntity);
inviteEntity.setDisabled(true);
inviteDao.merge(inviteEntity);
Invite invite = linksBuilder.addUserLink(userEntity.toUser(), inviteEntity.toInvite());
InviteCompleteResponse response = new InviteCompleteResponse(invite);
response.setServiceExternalId(serviceEntity.getExternalId());
response.setUserExternalId(userEntity.getExternalId());
return response;
} else {
throw internalServerError(format("Attempting to complete a service invite for a non service invite of type. invite-code = %s", inviteEntity.getCode()));
}
});
}
use of uk.gov.pay.adminusers.model.InviteCompleteResponse in project pay-adminusers by alphagov.
the class ServiceInviteCompleterTest method shouldCreateServiceAndUser_withGatewayAccounts_whenPassedValidServiceInviteCode.
@Test
public void shouldCreateServiceAndUser_withGatewayAccounts_whenPassedValidServiceInviteCode() {
ServiceEntity service = new ServiceEntity();
service.setId(serviceId);
InviteEntity anInvite = createInvite();
anInvite.setType(InviteType.SERVICE);
when(mockUserDao.findByEmail(email)).thenReturn(Optional.empty());
when(mockInviteDao.findByCode(inviteCode)).thenReturn(Optional.of(anInvite));
InviteCompleteRequest data = new InviteCompleteRequest();
data.setGatewayAccountIds(asList("1", "2"));
InviteCompleteResponse inviteResponse = serviceInviteCompleter.withData(data).complete(anInvite.getCode()).get();
verify(mockServiceDao).persist(expectedService.capture());
verify(mockUserDao).merge(expectedInvitedUser.capture());
verify(mockInviteDao).merge(expectedInvite.capture());
ServiceEntity serviceEntity = expectedService.getValue();
assertThat(serviceEntity.getGatewayAccountIds().stream().map(GatewayAccountIdEntity::getGatewayAccountId).collect(toUnmodifiableList()), hasItems("2", "1"));
assertThat(serviceEntity.getServiceNames().get(SupportedLanguage.ENGLISH).getName(), is(Service.DEFAULT_NAME_VALUE));
assertThat(serviceEntity.isRedirectToServiceImmediatelyOnTerminalState(), is(false));
assertThat(serviceEntity.isCollectBillingAddress(), is(true));
assertThat(serviceEntity.getDefaultBillingAddressCountry(), is("GB"));
assertThat(inviteResponse.getInvite().isDisabled(), is(true));
assertThat(inviteResponse.getInvite().getLinks().size(), is(1));
assertThat(inviteResponse.getInvite().getLinks().get(0).getRel(), is(Link.Rel.USER));
assertThat(inviteResponse.getInvite().getLinks().get(0).getHref(), matchesPattern("^" + baseUrl + "/v1/api/users/[0-9a-z]{32}$"));
}
use of uk.gov.pay.adminusers.model.InviteCompleteResponse in project pay-adminusers by alphagov.
the class ServiceInviteCompleterTest method shouldCreateServiceAndUser_withoutGatewayAccounts_whenPassedValidServiceInviteCode.
@Test
public void shouldCreateServiceAndUser_withoutGatewayAccounts_whenPassedValidServiceInviteCode() {
ServiceEntity service = new ServiceEntity();
service.addOrUpdateServiceName(ServiceNameEntity.from(SupportedLanguage.ENGLISH, Service.DEFAULT_NAME_VALUE));
service.setId(serviceId);
InviteEntity anInvite = createInvite();
anInvite.setType(InviteType.SERVICE);
when(mockUserDao.findByEmail(email)).thenReturn(Optional.empty());
when(mockInviteDao.findByCode(inviteCode)).thenReturn(Optional.of(anInvite));
InviteCompleteResponse inviteResponse = serviceInviteCompleter.withData(new InviteCompleteRequest()).complete(anInvite.getCode()).get();
verify(mockServiceDao).persist(expectedService.capture());
verify(mockUserDao).merge(expectedInvitedUser.capture());
verify(mockInviteDao).merge(expectedInvite.capture());
assertThat(expectedService.getValue().getGatewayAccountIds().isEmpty(), is(true));
ServiceEntity serviceEntity = expectedService.getValue();
assertThat(serviceEntity.getGatewayAccountIds().isEmpty(), is(true));
assertThat(serviceEntity.getServiceNames().get(SupportedLanguage.ENGLISH).getName(), is(Service.DEFAULT_NAME_VALUE));
assertThat(serviceEntity.isRedirectToServiceImmediatelyOnTerminalState(), is(false));
assertThat(serviceEntity.isCollectBillingAddress(), is(true));
assertThat(serviceEntity.getDefaultBillingAddressCountry(), is("GB"));
assertThat(inviteResponse.getInvite().isDisabled(), is(true));
assertThat(inviteResponse.getInvite().getLinks().size(), is(1));
assertThat(inviteResponse.getInvite().getLinks().get(0).getRel(), is(Link.Rel.USER));
assertThat(inviteResponse.getInvite().getLinks().get(0).getHref(), matchesPattern("^" + baseUrl + "/v1/api/users/[0-9a-z]{32}$"));
}
use of uk.gov.pay.adminusers.model.InviteCompleteResponse in project pay-adminusers by alphagov.
the class UserInviteCompleterTest method shouldSuccess_whenSubscribingAServiceToAnExistingUser_forValidInvite.
@Test
public void shouldSuccess_whenSubscribingAServiceToAnExistingUser_forValidInvite() {
ServiceEntity service = new ServiceEntity();
service.setId(serviceId);
service.setExternalId(serviceExternalId);
InviteEntity anInvite = createInvite();
anInvite.setType(InviteType.USER);
anInvite.setService(service);
UserEntity user = UserEntity.from(aUser(anInvite.getEmail()));
when(mockInviteDao.findByCode(inviteCode)).thenReturn(Optional.of(anInvite));
when(mockUserDao.findByEmail(email)).thenReturn(Optional.of(user));
Optional<InviteCompleteResponse> completedInvite = userInviteCompleter.complete(inviteCode);
ArgumentCaptor<UserEntity> persistedUser = ArgumentCaptor.forClass(UserEntity.class);
verify(mockUserDao).merge(persistedUser.capture());
assertThat(completedInvite.isPresent(), is(true));
assertThat(completedInvite.get().getInvite().isDisabled(), is(true));
assertThat(persistedUser.getValue().getServicesRole(service.getExternalId()).isPresent(), is(true));
}
Aggregations