You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

163 lines
8.2 KiB

using DealerSelection.Api.CommonUtil;
using DealerSelection.Api.Infrastructure.AssignDealer;
using CustomerDetailRepo = DealerSelection.Api.Infrastructure.CustomerDetail;
using DealerSelection.Api.Infrastructure.Mulesoft;
using DealerSelection.Api.Interface;
using DealerSelection.Api.Models;
using DealerSelection.Common.Configuration;
using DealerSelection.Common.Interfaces.HttpClient;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Asn1.Tsp;
using RestSharp;
using System;
using DealerSelection.Api.Models.Enum;
using JasperFx.CodeGeneration.Frames;
public class AssignDealerApi : IAssignDealerApi
{
#region Caching
public static string CacheGetDealersDetail = ConfigurationHelper.GetSetting<string>("MuleSoftCacheGetDealersDetail", true);
private int _cacheGetDealersDetail = !string.IsNullOrEmpty(CacheGetDealersDetail) ? int.Parse(CacheGetDealersDetail) : 10;
public static string CacheGetPriceOnState = ConfigurationHelper.GetSetting<string>("MuleSoftCacheGetPriceOnState", true);
private int _cacheGetPriceOnState = !string.IsNullOrEmpty(CacheGetPriceOnState) ? int.Parse(CacheGetPriceOnState) : 10;
public static string CacheGetModelOnBrand = ConfigurationHelper.GetSetting<string>("MuleSoftCacheGetModelOnBrand", true);
private int _cacheGetModelOnBrand = !string.IsNullOrEmpty(CacheGetModelOnBrand) ? int.Parse(CacheGetModelOnBrand) : 10;
public static string CacheGetActiveState = ConfigurationHelper.GetSetting<string>("MuleSoftCacheGetActiveState", true);
private int _cacheGetActiveState = !string.IsNullOrEmpty(CacheGetActiveState) ? int.Parse(CacheGetActiveState) : 10;
public static string CacheGetActiveCity = ConfigurationHelper.GetSetting<string>("MuleSoftCacheGetActiveCity", true);
private int _cacheGetActiveCity = !string.IsNullOrEmpty(CacheGetActiveCity) ? int.Parse(CacheGetActiveCity) : 10;
public static string CacheModelDetails = ConfigurationHelper.GetSetting<string>("MuleSoftCacheModelDetails", true);
private int _cacheModelDetails = !string.IsNullOrEmpty(CacheModelDetails) ? int.Parse(CacheModelDetails) : 10;
#endregion
private readonly ILogger _logger;
private DealerSelection.Api.Infrastructure.AssignDealer.IRepository Repository { get; }
private CustomerDetailRepo.IRepository CustomerDetailRepository { get; }
private IMulesoftApi MulesoftApi { get; }
private IHttpClientHandler ClientHandler { get; }
private readonly IMemoryCache _memoryCache;
public AssignDealerApi(DealerSelection.Api.Infrastructure.AssignDealer.IRepository repository, IMulesoftApi mulesoftApi, CustomerDetailRepo.IRepository customerRepo,
IHttpClientHandler clientHandler, IMemoryCache memoryCache, ILogger<AssignDealerApi> logger)
{
Repository = repository;
MulesoftApi = mulesoftApi;
CustomerDetailRepository = customerRepo;
ClientHandler = clientHandler;
_memoryCache = memoryCache;
_logger = logger;
}
public async Task GetCustomerDataForJob(int buId, int recordId)
{
try
{
_logger.LogInformation($"DS GetCustomerDataForJob Api called for:- BU:- {buId} and Record Id {recordId}");
await CustomerDetailRepository.UpdateDealerSelectionJobStatus(buId, recordId, DealerSelectionJobStatus.Picked); // Record is picked by Job for Dealer Seclection
MulesoftCustomerInfoDto customerInfo = await CustomerDetailRepository.GetCustomerData(buId, recordId);
await AssignDealers(customerInfo);
}
catch (Exception ex)
{
_logger.LogError($"DS Api:- GetCustomerDataForJob:- " + ex.StackTrace.ToString());
}
}
private async Task AssignDealers(MulesoftCustomerInfoDto customerInfo)
{
_logger.LogInformation($"DS Api:-AssignDealers Started BuId:- { customerInfo.BuId} RecordId: {customerInfo.RecordId}" );
try
{
CustomCfg cfg = CustomCfg.GetCustomCfg(customerInfo.BuId);
MulesoftResponse dealerResponse = await MulesoftApi.GetDealers(customerInfo.BuId, cfg.BuCode, customerInfo.CustomerLat, customerInfo.CustomerLong);
if (dealerResponse != null && dealerResponse.IsRequestSuccessfull)
{
JObject obj = JObject.Parse(dealerResponse.Result);
if (obj != null && obj["Data"] != null && obj["Data"].Count() > 0)
{
if (obj["Data"].Count() == 1)
{
_logger.LogInformation($"DS Api:- BU:- {customerInfo.BuId} RecordId: {customerInfo.RecordId} Api:- AssignDealers with 1 Dealer");
//Save dealer Detail and Send to LSQ
JToken jArrResultData = (JToken)obj["Data"][0];
if (jArrResultData != null)
{
customerInfo.DealerCode = (JValue)jArrResultData["Code"] != null ? Convert.ToString(((JValue)jArrResultData["Code"]).Value) : "0";
customerInfo.DealerName = (JValue)jArrResultData["DisplayName"] != null ? Convert.ToString(((JValue)jArrResultData["DisplayName"]).Value) : "0";
customerInfo.DealerSelectedMode = DealerSelectedMode.Auto.ToString();
await MulesoftApi.InsertLSQData(customerInfo);
}
}
else if (obj["Data"].Count() > 1)
{
_logger.LogInformation($"DS Api:- BU:- {customerInfo.BuId} RecordId: {customerInfo.RecordId} Api:- Inititate Bot more then 1 Dealer");
// Send Message to Webengage to Initiate BOT
WebEngageEventData eventData = new WebEngageEventData(customerInfo.BookingId, "SUCCESS", customerInfo.AmountPaid, "", "N",
customerInfo.CustomerName, customerInfo.MobileNumber, customerInfo.ModelName, customerInfo.ModelCode, customerInfo.CustomerLat, "", "",customerInfo.CompletionDate.ToString());
WebEngageEvent events = new WebEngageEvent(customerInfo.MobileNumber, cfg.WebengageEventName, eventData);
await WebEngageEventsAPICall(events, cfg, customerInfo.RecordId);
}
}
}
}
catch (Exception ex)
{
_logger.LogError($"DS Api:- BU:- {customerInfo.BuId} Api:- AssignDealers:- {ex.StackTrace.ToString()}");
}
}
private async Task WebEngageEventsAPICall(WebEngageEvent message, CustomCfg cfg, int recordId)
{
_logger.LogInformation($"DS Api:-WebEngageEventsAPICall Started: " + JsonConvert.SerializeObject(message));
try
{
var json = JsonConvert.SerializeObject(message);
var baseApiUrl = Convert.ToString(cfg.WebengageApiHost + cfg.WebEngageLicenseCode + "/events");
var options = new RestClientOptions(baseApiUrl)
{
MaxTimeout = -1,
};
var client = new RestClient();
var request = new RestRequest(baseApiUrl, Method.Post);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + cfg.WebEngageAuthToken);
request.AddStringBody(json, DataFormat.Json);
RestResponse response = await client.ExecuteAsync(request);
var content = response.Content;
_logger.LogInformation($"DS Api:-WebEngageEventsAPICall End: " + content);
if (content != null)
{
JObject obj = JObject.Parse(content);
if (obj != null && obj["response"] != null)
{
JToken jArrResultData = (JToken)obj["response"]["status"];
if (jArrResultData != null && Convert.ToString(((JValue)jArrResultData).Value) == "queued")
await CustomerDetailRepository.UpdateDealerSelectionJobStatus(cfg.BuId, recordId, DealerSelectionJobStatus.SentToBot);
}
}
}
catch (Exception ex)
{
_logger.LogError($"DS Api:-PendingStatusUpdateApi:- WebEngageEventsAPICall:- " + ex.Message.ToString());
}
}
}