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("muleSoftAPIDomain", true); public static string dealerDetailApiUrl = ConfigurationHelper.GetSetting("muleSoftDealerDetail", true); public static string bookingApiUrl = ConfigurationHelper.GetSetting("muleSoftBookingAPILeadSquareDomain", true); public static string masterSchemaId = ConfigurationHelper.GetSetting("masterSchemaId", true); public static string masterDealerDetailSchemaId = ConfigurationHelper.GetSetting("masterDealerDetailSchemaId", true); public static string muleSoftStateWisePrice = ConfigurationHelper.GetSetting("muleSoftStateWisePrice", true); public static string muleSoftBrandWiseModel = ConfigurationHelper.GetSetting("muleSoftBrandWiseModel", true); public static string muleSoftPricemasterSchemaId = ConfigurationHelper.GetSetting("muleSoftStateWisePricemasterSchemaId", true); public static string muleSoftModelmasterSchemaId = ConfigurationHelper.GetSetting("muleSoftBrandWiseModelmasterSchemaId", true); public static string muleSoftStateMasterSchemaId = ConfigurationHelper.GetSetting("muleSoftStateMasterSchemaId", true); public static string muleSoftStateMasterSchemaApi = ConfigurationHelper.GetSetting("muleSoftStateMasterSchemaApi", true); public static string muleSoftCityMasterSchemaId = ConfigurationHelper.GetSetting("muleSoftCityMasterSchemaId", true); public static string muleSoftCityMasterSchemaApi = ConfigurationHelper.GetSetting("muleSoftCityMasterSchemaApi", true); public static string muleSoftModelMasterSchemaItemId = ConfigurationHelper.GetSetting("muleSoftModelMasterSchemaItemId", true); public static string muleSoftModelMasterSchemaItemApi = ConfigurationHelper.GetSetting("muleSoftModelMasterSchemaItemApi", true); #endregion #region Caching public static string CacheGetDealersDetail = ConfigurationHelper.GetSetting("MuleSoftCacheGetDealersDetail", true); private int _cacheGetDealersDetail = !string.IsNullOrEmpty(CacheGetDealersDetail) ? int.Parse(CacheGetDealersDetail) : 10; public static string CacheGetPriceOnState = ConfigurationHelper.GetSetting("MuleSoftCacheGetPriceOnState", true); private int _cacheGetPriceOnState = !string.IsNullOrEmpty(CacheGetPriceOnState) ? int.Parse(CacheGetPriceOnState) : 10; public static string CacheGetModelOnBrand = ConfigurationHelper.GetSetting("MuleSoftCacheGetModelOnBrand", true); private int _cacheGetModelOnBrand = !string.IsNullOrEmpty(CacheGetModelOnBrand) ? int.Parse(CacheGetModelOnBrand) : 10; public static string CacheGetActiveState = ConfigurationHelper.GetSetting("MuleSoftCacheGetActiveState", true); private int _cacheGetActiveState = !string.IsNullOrEmpty(CacheGetActiveState) ? int.Parse(CacheGetActiveState) : 10; public static string CacheGetActiveCity = ConfigurationHelper.GetSetting("MuleSoftCacheGetActiveCity", true); private int _cacheGetActiveCity = !string.IsNullOrEmpty(CacheGetActiveCity) ? int.Parse(CacheGetActiveCity) : 10; public static string CacheModelDetails = ConfigurationHelper.GetSetting("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 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); } }