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.
 
 
 

190 lines
9.8 KiB

using DealerSelection.Api.CommonUtil;
using DealerSelection.Api.Infrastructure.AssignDealer;
using 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;
public class AssignDealerApi : IAssignDealerApi
{
#region MuleSoft
public static string baseApiUrl = ConfigurationHelper.GetSetting<string>("muleSoftAPIDomain", true);
public static string dealerDetailApiUrl = ConfigurationHelper.GetSetting<string>("muleSoftDealerDetail", true);
public static string bookingApiUrl = ConfigurationHelper.GetSetting<string>("muleSoftBookingAPILeadSquareDomain", true);
public static string masterSchemaId = ConfigurationHelper.GetSetting<string>("masterSchemaId", true);
public static string masterDealerDetailSchemaId = ConfigurationHelper.GetSetting<string>("masterDealerDetailSchemaId", true);
public static string muleSoftStateWisePrice = ConfigurationHelper.GetSetting<string>("muleSoftStateWisePrice", true);
public static string muleSoftBrandWiseModel = ConfigurationHelper.GetSetting<string>("muleSoftBrandWiseModel", true);
public static string muleSoftPricemasterSchemaId = ConfigurationHelper.GetSetting<string>("muleSoftStateWisePricemasterSchemaId", true);
public static string muleSoftModelmasterSchemaId = ConfigurationHelper.GetSetting<string>("muleSoftBrandWiseModelmasterSchemaId", true);
public static string muleSoftStateMasterSchemaId = ConfigurationHelper.GetSetting<string>("muleSoftStateMasterSchemaId", true);
public static string muleSoftStateMasterSchemaApi = ConfigurationHelper.GetSetting<string>("muleSoftStateMasterSchemaApi", true);
public static string muleSoftCityMasterSchemaId = ConfigurationHelper.GetSetting<string>("muleSoftCityMasterSchemaId", true);
public static string muleSoftCityMasterSchemaApi = ConfigurationHelper.GetSetting<string>("muleSoftCityMasterSchemaApi", true);
public static string muleSoftModelMasterSchemaItemId = ConfigurationHelper.GetSetting<string>("muleSoftModelMasterSchemaItemId", true);
public static string muleSoftModelMasterSchemaItemApi = ConfigurationHelper.GetSetting<string>("muleSoftModelMasterSchemaItemApi", true);
#endregion
#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 IAssignDealerRepository Repository { get; }
private DealerSelection.Api.Infrastructure.CustomerDetail.IRepository CustomerDetailRepository { get; }
private IMulesoftApi MulesoftApi { get; }
private IHttpClientHandler ClientHandler { get; }
private readonly IMemoryCache _memoryCache;
public AssignDealerApi(IAssignDealerRepository repository, IMulesoftApi mulesoftApi, DealerSelection.Api.Infrastructure.CustomerDetail.IRepository customerRepo,
IHttpClientHandler clientHandler, IMemoryCache memoryCache, ILogger<MulesoftApi> logger)
{
Repository = repository;
MulesoftApi = mulesoftApi;
CustomerDetailRepository = customerRepo;
ClientHandler = clientHandler;
_memoryCache = memoryCache;
_logger = logger;
}
public async Task GetCustomerDataForJob(int buId, int recordId)
{
try
{
MulesoftCustomerInfoDto customerInfo= await CustomerDetailRepository.GetCustomerData( buId, recordId);
await AssignDealers(customerInfo);
}
catch (Exception ex)
{
_logger.LogError("Api:- GetDealers:- " + ex.StackTrace.ToString());
}
}
private async Task AssignDealers(MulesoftCustomerInfoDto customerInfo)
{
try
{
MulesoftResponse dealerResponse = await MulesoftApi.GetDealers(customerInfo.BuId, customerInfo.BuName, 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)
{
//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";
try
{
var responseApi = await MulesoftApi.CallLSQBookingApi(customerInfo);
if (responseApi != null && responseApi.IsRequestSuccessfull)
await UpdateDealerDetail(customerInfo, DealerSelectionJobStatus.Complete);
else
await UpdateDealerDetail(customerInfo, DealerSelectionJobStatus.ReadyForJob);
}
catch (Exception ex)
{
await UpdateDealerDetail(customerInfo, DealerSelectionJobStatus.ReadyForJob);
_logger.LogError("Error in LSQ " + ex.StackTrace.ToString());
}
}
}
else
{
//Send to Webengage
CustomCfg cfg = CustomCfg.GetCustomCfg(customerInfo.BuId);
WebEngageEventData eventData = new WebEngageEventData(customerInfo.MobileNumber);
WebEngageEvent events = new WebEngageEvent(customerInfo.MobileNumber, cfg.WebengageEventName, eventData);
await WebEngageEventsAPICall(events, cfg);
}
}
}
}
catch (Exception ex)
{
_logger.LogError("BU:- " + customerInfo.BuId + "Api:- GetDealers:- " + ex.StackTrace.ToString());
}
}
private async Task WebEngageEventsAPICall(WebEngageEvent message, CustomCfg cfg)
{
_logger.LogInformation("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("WebEngageEventsAPICall End: " + content);
}
catch (Exception ex)
{
_logger.LogError("PendingStatusUpdateApi:- WebEngageEventsAPICall:- " + ex.Message.ToString());
}
}
private async Task UpdateDealerDetail(MulesoftCustomerInfoDto customerInfo, DealerSelectionJobStatus jobStatus)
{
CustomerDealerInfoRequestDto dto = new CustomerDealerInfoRequestDto
{
BuId = customerInfo.BuId,
DealerName = jobStatus == DealerSelectionJobStatus.Complete ? customerInfo.DealerName : "",
DealerCode = jobStatus == DealerSelectionJobStatus.Complete ? customerInfo.DealerCode : "",
DealerSelectionJobStatus = jobStatus == DealerSelectionJobStatus.Complete ? DealerSelectionJobStatus.Complete : DealerSelectionJobStatus.ReadyForJob,
RecordId = customerInfo.RecordId
};
await CustomerDetailRepository.UpdateDealerDetail(dto);
}
}