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

com.commercetools.sunrise.myaccount.CustomerFinderBySession Maven / Gradle / Ivy

There is a newer version: 1.0.0-M10
Show newest version
package com.commercetools.sunrise.myaccount;

import com.commercetools.sunrise.hooks.RequestHookContext;
import com.commercetools.sunrise.hooks.events.CustomerLoadedHook;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.customers.Customer;
import io.sphere.sdk.customers.queries.CustomerByIdGet;
import play.libs.concurrent.HttpExecution;

import javax.inject.Inject;
import java.util.Optional;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.completedFuture;

public final class CustomerFinderBySession implements CustomerFinder {

    @Inject
    private SphereClient sphereClient;
    @Inject
    private CustomerInSession customerInSession;
    @Inject
    private RequestHookContext hookContext;

    @Override
    public CompletionStage> findCustomer(final Void unused) {
        final CompletionStage> customerStage = fetchCustomer();
        customerStage.thenAcceptAsync(customerOpt ->
                        customerOpt.ifPresent(customer -> CustomerLoadedHook.runHook(hookContext, customer)), HttpExecution.defaultContext());
        return customerStage;
    }

    private CompletionStage> fetchCustomer() {
        return customerInSession.findCustomerId()
                .map(this::fetchCustomerById)
                .orElseGet(() -> completedFuture(Optional.empty()));
    }

    private CompletionStage> fetchCustomerById(final String customerId) {
        final CustomerByIdGet query = CustomerByIdGet.of(customerId);
        return sphereClient.execute(query).thenApply(Optional::ofNullable);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy