Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

OmnisendCustomerService.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Newtonsoft.Json;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Plugin.Misc.Omnisend.DTO;
using Nop.Plugin.Misc.Omnisend.DTO.Events;
using Nop.Services.Common;

namespace Nop.Plugin.Misc.Omnisend.Services;

/// 
/// Represents the helper class for customer
/// 
public class OmnisendCustomerService
{
    #region Fields

    private readonly IActionContextAccessor _actionContextAccessor;
    private readonly IAddressService _addressService;
    private readonly IGenericAttributeService _genericAttributeService;
    private readonly IUrlHelperFactory _urlHelperFactory;
    private readonly IWebHelper _webHelper;
    private readonly OmnisendHttpClient _httpClient;

    #endregion

    #region Ctor

    public OmnisendCustomerService(IActionContextAccessor actionContextAccessor,
        IAddressService addressService,
        IGenericAttributeService genericAttributeService,
        IUrlHelperFactory urlHelperFactory,
        IWebHelper webHelper,
        OmnisendHttpClient httpClient)
    {
        _actionContextAccessor = actionContextAccessor;
        _addressService = addressService;
        _genericAttributeService = genericAttributeService;
        _urlHelperFactory = urlHelperFactory;
        _webHelper = webHelper;
        _httpClient = httpClient;
    }

    #endregion

    #region Methods

    /// 
    /// Gets the cart identifier for customer
    /// 
    /// Customer
    public async Task GetCartIdAsync(Customer customer)
    {
        var cartId = await _genericAttributeService.GetAttributeAsync(customer,
            OmnisendDefaults.StoredCustomerShoppingCartIdAttribute);

        cartId = string.IsNullOrEmpty(cartId)
            ? await _genericAttributeService.GetAttributeAsync(customer,
                OmnisendDefaults.CurrentCustomerShoppingCartIdAttribute)
            : cartId;

        if (!string.IsNullOrEmpty(cartId))
            return cartId;

        cartId = Guid.NewGuid().ToString();

        await _genericAttributeService.SaveAttributeAsync(customer,
            OmnisendDefaults.CurrentCustomerShoppingCartIdAttribute, cartId);

        return cartId;
    }

    /// 
    /// Gets the customer email address
    /// 
    /// Customer
    /// billing address identifier
    public async Task GetEmailAsync(Customer customer, int? billingAddressId = null)
    {
        var email = !string.IsNullOrEmpty(customer.Email)
            ? customer.Email
            : await _genericAttributeService.GetAttributeAsync(customer,
                OmnisendDefaults.CustomerEmailAttribute);

        return !string.IsNullOrEmpty(email)
            ? email
            : (await _addressService.GetAddressByIdAsync((billingAddressId ?? customer.BillingAddressId) ?? 0))
            ?.Email;
    }

    /// 
    /// Create customer event
    /// 
    /// Customer
    /// Event properties
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains customer event or null if customer email is not determinate
    /// 
    public async Task CreateCustomerEventAsync(Customer customer, IEventProperty properties)
    {
        var email = await GetEmailAsync(customer);

        if (string.IsNullOrEmpty(email))
            return null;

        var customerEvent = new CustomerEvents { Email = email, Properties = properties };

        return customerEvent;
    }

    /// 
    /// Gets the contact identifier
    /// 
    /// Email to determinate customer
    public async Task GetContactIdAsync(string email)
    {
        var url = $"{OmnisendDefaults.ContactsApiUrl}?email={email}&limit=1";

        var res = await _httpClient.PerformRequestAsync(url, httpMethod: HttpMethod.Get);

        if (string.IsNullOrEmpty(res))
            return null;

        var contact = JsonConvert
            .DeserializeAnonymousType(res, new { contacts = new[] { new { contactID = string.Empty } } })?.contacts
            .FirstOrDefault();

        return contact?.contactID;
    }

    /// 
    /// Store the cart identifier
    /// 
    /// Customer
    public async Task StoreCartIdAsync(Customer customer)
    {
        var cartId = await _genericAttributeService.GetAttributeAsync(customer,
            OmnisendDefaults.StoredCustomerShoppingCartIdAttribute);

        if (string.IsNullOrEmpty(cartId))
            await _genericAttributeService.SaveAttributeAsync(customer,
                OmnisendDefaults.StoredCustomerShoppingCartIdAttribute, await GetCartIdAsync(customer));
    }

    /// 
    /// Delete the current shopping cart identifier for customer
    /// 
    /// Customer
    public async Task DeleteCurrentCustomerShoppingCartIdAsync(Customer customer)
    {
        await _genericAttributeService.SaveAttributeAsync(customer,
            OmnisendDefaults.CurrentCustomerShoppingCartIdAttribute, null);
    }

    /// 
    /// Specifies whether to send the delete shopping cart event
    /// 
    /// Customer
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the True if we need to sand delete events
    public async Task IsNeedToSendDeleteShoppingCartEventAsync(Customer customer)
    {
        return string.IsNullOrEmpty(await _genericAttributeService.GetAttributeAsync(customer,
            OmnisendDefaults.StoredCustomerShoppingCartIdAttribute));
    }

    /// 
    /// Delete the stored shopping cart identifier for customer
    /// 
    /// Customer
    public async Task DeleteStoredCustomerShoppingCartIdAsync(Customer customer)
    {
        await _genericAttributeService.SaveAttributeAsync(customer,
            OmnisendDefaults.StoredCustomerShoppingCartIdAttribute, null);
    }

    /// 
    /// Gets the abandoned checkout url
    /// 
    /// Cart identifier
    /// The abandoned checkout url
    public string GetAbandonedCheckoutUrl(string cartId)
    {
        if (_actionContextAccessor.ActionContext == null)
            return null;

        return _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext)
            .RouteUrl(OmnisendDefaults.AbandonedCheckoutRouteName, new { cartId }, _webHelper.GetCurrentRequestProtocol());
    }

    #endregion
}