Search in sources :

Example 1 with SERVICE

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())));
}
Also used : Logger(org.slf4j.Logger) RandomIdGenerator.randomUuid(uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomUuid) LoggerFactory(org.slf4j.LoggerFactory) TelephoneNumberUtility(uk.gov.pay.adminusers.utils.telephonenumber.TelephoneNumberUtility) AdminUsersExceptions.internalServerError(uk.gov.pay.adminusers.service.AdminUsersExceptions.internalServerError) AdminUsersExceptions.conflictingEmail(uk.gov.pay.adminusers.service.AdminUsersExceptions.conflictingEmail) InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) Function(java.util.function.Function) Invite(uk.gov.pay.adminusers.model.Invite) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) String.format(java.lang.String.format) Transactional(com.google.inject.persist.Transactional) UserDao(uk.gov.pay.adminusers.persistence.dao.UserDao) Collectors.toUnmodifiableList(java.util.stream.Collectors.toUnmodifiableList) LinksConfig(uk.gov.pay.adminusers.app.config.LinksConfig) InviteServiceRequest(uk.gov.pay.adminusers.model.InviteServiceRequest) Inject(javax.inject.Inject) SERVICE(uk.gov.pay.adminusers.model.InviteType.SERVICE) List(java.util.List) InviteDao(uk.gov.pay.adminusers.persistence.dao.InviteDao) Optional(java.util.Optional) RoleDao(uk.gov.pay.adminusers.persistence.dao.RoleDao) InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Transactional(com.google.inject.persist.Transactional)

Aggregations

Transactional (com.google.inject.persist.Transactional)1 String.format (java.lang.String.format)1 List (java.util.List)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Collectors.toUnmodifiableList (java.util.stream.Collectors.toUnmodifiableList)1 Inject (javax.inject.Inject)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 LinksConfig (uk.gov.pay.adminusers.app.config.LinksConfig)1 RandomIdGenerator.randomUuid (uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomUuid)1 Invite (uk.gov.pay.adminusers.model.Invite)1 InviteServiceRequest (uk.gov.pay.adminusers.model.InviteServiceRequest)1 SERVICE (uk.gov.pay.adminusers.model.InviteType.SERVICE)1 InviteDao (uk.gov.pay.adminusers.persistence.dao.InviteDao)1 RoleDao (uk.gov.pay.adminusers.persistence.dao.RoleDao)1 UserDao (uk.gov.pay.adminusers.persistence.dao.UserDao)1 InviteEntity (uk.gov.pay.adminusers.persistence.entity.InviteEntity)1 UserEntity (uk.gov.pay.adminusers.persistence.entity.UserEntity)1 AdminUsersExceptions.conflictingEmail (uk.gov.pay.adminusers.service.AdminUsersExceptions.conflictingEmail)1