All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.foodtechlab.lib.auth.service.domain.credential.usecases.UpdateCredentialUseCase Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
package ru.foodtechlab.lib.auth.service.domain.credential.usecases;

import com.rcore.commons.utils.PhoneNumberFormatter;
import com.rcore.commons.utils.PhoneNumberValidator;
import com.rcore.domain.commons.usecase.AbstractUpdateUseCase;
import com.rcore.domain.commons.usecase.UseCase;
import com.rcore.domain.commons.usecase.model.IdInputValues;
import com.rcore.domain.commons.usecase.model.SingletonEntityOutputValues;
import com.rcore.domain.commons.validators.ValidationDomain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import ru.foodtechlab.lib.auth.service.domain.Domain;
import ru.foodtechlab.lib.auth.service.domain.confirmationCode.entity.ConfirmationCodeDestinationType;
import ru.foodtechlab.lib.auth.service.domain.confirmationCode.entity.ConfirmationCodeEntity;
import ru.foodtechlab.lib.auth.service.domain.credential.entity.CredentialEntity;
import ru.foodtechlab.lib.auth.service.domain.credential.entity.Email;
import ru.foodtechlab.lib.auth.service.domain.credential.entity.PhoneNumber;
import ru.foodtechlab.lib.auth.service.domain.credential.exceptions.CredentialNotFoundException;
import ru.foodtechlab.lib.auth.service.domain.credential.exceptions.CredentialWithPhoneAlreadyExistException;
import ru.foodtechlab.lib.auth.service.domain.credential.exceptions.CredentialWithUsernameAlreadyExistException;
import ru.foodtechlab.lib.auth.service.domain.credential.port.CredentialRepository;
import ru.foodtechlab.lib.auth.service.domain.credential.usecases.inputValues.BasicCredentialInputValues;
import ru.foodtechlab.lib.auth.service.domain.role.entity.RoleEntity;
import ru.foodtechlab.lib.auth.service.domain.role.exception.RoleNotFoundException;
import ru.foodtechlab.lib.auth.service.domain.role.usecases.FindRoleByIdUseCase;
import ru.foodtechlab.lib.auth.service.domain.role.usecases.FindRoleByCodeUseCase;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Обновление учетных данных
 */
public class UpdateCredentialUseCase extends AbstractUpdateUseCase {

    private final FindRoleByIdUseCase findRoleById;
    private final FindRoleByCodeUseCase findRoleByCode;
    private final FindCredentialByPhoneNumberUseCase findCredentialByPhoneNumber;
    private final FindCredentialByUsernameUseCase findCredentialByUsername;
    private final FindCredentialByEmailUseCase findCredentialByEmail;

    public UpdateCredentialUseCase(CredentialRepository repository, FindRoleByIdUseCase findRoleById, FindRoleByCodeUseCase findRoleByCode, FindCredentialByPhoneNumberUseCase findCredentialByPhoneNumber, FindCredentialByUsernameUseCase findCredentialByUsername, FindCredentialByEmailUseCase findCredentialByEmail) {
        super(repository);
        this.findRoleById = findRoleById;
        this.findRoleByCode = findRoleByCode;
        this.findCredentialByPhoneNumber = findCredentialByPhoneNumber;
        this.findCredentialByUsername = findCredentialByUsername;
        this.findCredentialByEmail = findCredentialByEmail;
    }

    @Override
    public SingletonEntityOutputValues execute(InputValues inputValues) {

        var credential = repository.findById(inputValues.getId())
                .orElseThrow(CredentialNotFoundException::new);

        List roles = new ArrayList<>();

        //Проверка ролей

        if (inputValues.getRoles() != null)
            for (CreateCredentialUseCase.InputValues.Role role : inputValues.getRoles()) {
                if (role.getRoleId() != null)
                    roles.add(findRoleById.execute(IdInputValues.of(role.getRoleId()))
                            .getEntity()
                            .orElseThrow(RoleNotFoundException::new));
                else if (role.getCode() != null)
                    roles.add(findRoleByCode.execute(FindRoleByCodeUseCase.InputValues.of(role.getCode()))
                            .getEntity()
                            .orElseThrow(RoleNotFoundException::new));
            }

        validate(credential, inputValues);

        credential.setEmail(inputValues.getEmail());
        credential.setPhoneNumber(inputValues.getPhoneNumber());
        credential.setRoles(roles
                .stream()
                .map(role -> new CredentialEntity.Role(
                        role,
                        findInputRole(inputValues.getRoles(), role)
                                .map(CreateCredentialUseCase.InputValues.Role::getIsBlocked)
                                .orElse(false)))
                .collect(Collectors.toList()));
        credential.setBlocked(inputValues.getIsBlocked());
        credential.setUsername(inputValues.getUsername());

        return SingletonEntityOutputValues.of(repository.save(credential));
    }

    private void validate(CredentialEntity credentialEntity, UpdateCredentialUseCase.InputValues inputValues) {

        if (inputValues.getPhoneNumber() != null) {
            var credentialWithPhone = findCredentialByPhoneNumber.execute(FindCredentialByPhoneNumberUseCase.InputValues.of(inputValues.getPhoneNumber().getValue()))
                    .getValue();
            if (credentialWithPhone.isPresent() && !credentialEntity.getId().equals(credentialWithPhone.get().getId()))
                throw new CredentialWithPhoneAlreadyExistException();
        }

        if (inputValues.getUsername() != null) {
            var credentialWithUsername = findCredentialByUsername.execute(FindCredentialByUsernameUseCase.InputValues.of(inputValues.getUsername()))
                    .getValue();
            if (credentialWithUsername.isPresent() && !credentialEntity.getId().equals(credentialWithUsername.get().getId()))
                throw new CredentialWithUsernameAlreadyExistException();
        }

        if (inputValues.getEmail() != null) {
            var credentialWithEmail = findCredentialByEmail.execute(FindCredentialByEmailUseCase.InputValues.of(inputValues.getEmail().getValue()))
                    .getValue();
            if (credentialWithEmail.isPresent() && !credentialEntity.getId().equals(credentialWithEmail.get().getId()))
                throw new CredentialWithUsernameAlreadyExistException();
        }
    }

    private Optional findInputRole(List inputRoles, RoleEntity roleEntity) {
        return inputRoles
                .stream()
                .filter(inputRole -> {
                    if (inputRole.getCode()!= null && inputRole.getCode().equals(roleEntity.getCode()))
                        return true;
                    if (inputRole.getRoleId() != null && inputRole.getRoleId().equals(roleEntity.getId()))
                        return true;

                    return false;
                })
                .findFirst();
    }

    @ValidationDomain(domainName = Domain.CREDENTIAL)
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @Data
    public static class InputValues extends BasicCredentialInputValues {
        private String id;
        @NotBlank
        protected String username;
        protected String isoTwoLetterCountryCode;
        protected Email email;
        @NotNull
        protected List roles;
        @NotNull
        protected Boolean isBlocked;
        protected ConfirmationCodeDestinationType confirmationCodeDestinationType;
        protected String personalConfirmationCode;
        protected ConfirmationCodeEntity.Type confirmationCodeType;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy