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

com.firstdata.clovergo.domain.usecase.AuthenticateEmployee Maven / Gradle / Ivy

package com.firstdata.clovergo.domain.usecase;


import com.firstdata.clovergo.domain.model.Application;
import com.firstdata.clovergo.domain.model.EmployeeMerchant;
import com.firstdata.clovergo.domain.model.UseCaseConstants;
import com.firstdata.clovergo.domain.repository.AuthenticationRepository;

import java.util.Date;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;


public class AuthenticateEmployee extends UseCase {

    @Inject
    public AuthenticateEmployee(AuthenticationRepository authenticationRepository) {
        super(authenticationRepository);
    }

    public Observable getObservable(final String pin, final boolean isKeyed, final String location, final String pushNotificationId) {
        repository.log("authenticate employee");
        repository.evictFromCache(UseCaseConstants.EMPLOYEE_MERCHANT);
        repository.evictFromCache(UseCaseConstants.ORDER);
        return repository.getAllActivatedEmployees().filter(employeeSignIn -> employeeSignIn.getEmployee().isActive() && employeeSignIn.getEmployee().isPinSet()).flatMap(employeeSignIn -> {
            Application application = repository.retrieveFromCache(UseCaseConstants.APPLICATION);
            repository.storeInCache(UseCaseConstants.EMPLOYEE_MERCHANT, employeeSignIn);
            return repository.login(employeeSignIn.getEmployee().getId(), employeeSignIn.getMerchant().getId(), employeeSignIn.getEmployee().getEmail(), pin, isKeyed, location, pushNotificationId, application != null ? application.getTokenType() : "fixed");
        }).doOnNext(employeeLogin -> {
            EmployeeMerchant employeeMerchant = repository.retrieveFromCache(UseCaseConstants.EMPLOYEE_MERCHANT);

            employeeMerchant.getEmployee().setPin(null);
            employeeMerchant.getEmployee().setSessionToken(employeeLogin.getEmployee().getSessionToken());
            employeeMerchant.getEmployee().setEula(employeeLogin.getEmployee().getEula());
            employeeLogin.getMerchant().setName(employeeMerchant.getMerchant().getName());
            employeeMerchant.setMerchant(employeeLogin.getMerchant());
            employeeMerchant.setRole(employeeLogin.getRole());
            employeeMerchant.getEmployee().setLoggedIn(true);
            employeeMerchant.getEmployee().setPinSet(true);
            employeeLogin.setNeedsSetup(employeeMerchant.isNeedsSetup());
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy