use of uk.gov.pay.adminusers.model.InviteType.SERVICE in project pay-adminusers by alphagov.
the class ServiceInviteCreator method doInvite.
@Transactional
public Invite doInvite(InviteServiceRequest inviteServiceRequest) {
String requestEmail = inviteServiceRequest.getEmail();
Optional<UserEntity> anExistingUser = userDao.findByEmail(requestEmail);
if (anExistingUser.isPresent()) {
UserEntity user = anExistingUser.get();
if (user.isDisabled()) {
sendUserDisabledNotification(requestEmail, user.getExternalId());
} else {
sendUserExistsNotification(requestEmail, user.getExternalId());
}
throw conflictingEmail(requestEmail);
}
List<InviteEntity> exitingInvites = inviteDao.findByEmail(requestEmail);
List<InviteEntity> existingValidServiceInvitesForSameEmail = exitingInvites.stream().filter(inviteEntity -> !inviteEntity.isDisabled() && !inviteEntity.isExpired()).filter(InviteEntity::isServiceType).collect(toUnmodifiableList());
if (!existingValidServiceInvitesForSameEmail.isEmpty()) {
InviteEntity foundInvite = existingValidServiceInvitesForSameEmail.get(0);
return constructInviteAndSendEmail(inviteServiceRequest, foundInvite, inviteEntity -> {
inviteDao.merge(inviteEntity);
return null;
});
}
return roleDao.findByRoleName(inviteServiceRequest.getRoleName()).map(roleEntity -> {
InviteEntity inviteEntity = new InviteEntity(requestEmail, randomUuid(), inviteServiceRequest.getOtpKey(), roleEntity);
inviteEntity.setType(SERVICE);
return constructInviteAndSendEmail(inviteServiceRequest, inviteEntity, inviteToPersist -> {
inviteDao.persist(inviteToPersist);
return null;
});
}).orElseThrow(() -> internalServerError(format("Role [%s] not a valid role for creating a invite service request", inviteServiceRequest.getRoleName())));
}
Aggregations