private void ParseCallUploadedCalls(IEnumerable <DataRow> rows, List <CallUploadLog> failedCustomerList, List <CallUploadLog> successCustomerList, long callUploadId) { var callUploadLogs = rows.Select(row => _callUploadHelper.GetUploadLog(row, callUploadId)); if (callUploadLogs.IsNullOrEmpty()) { _logger.Info("No Record Found For Parsing in file"); return; } SaveFailedCustomers(failedCustomerList, callUploadLogs); callUploadLogs = callUploadLogs.Where(x => x.IsSuccessfull); if (callUploadLogs.IsNullOrEmpty()) { _logger.Info("No Successfull Customer Found For Parsing in file"); return; } var customerIds = callUploadLogs.Select(x => x.CustomerId).Distinct(); var outboundCustomerIds = callUploadLogs.Where(x => !x.IsDirectMail).Select(x => x.CustomerId).ToArray(); IEnumerable <Customer> customers = null; try { customers = _customerRepository.GetCustomers(customerIds.ToArray()); } catch (Exception ex) { _logger.Error(" Exception : " + ex.Message + " \n Stack Trace: " + ex.StackTrace); } if (customers != null && customers.Any()) { var healthPlans = _corporateAccountRepository.GetAllHealthPlan(); var events = ((IUniqueItemRepository <Event>)_eventRepository).GetByIds(callUploadLogs.Where(x => x.EventId > 0).Select(x => x.EventId).ToArray()); var callQueue = _callQueueRepository.GetCallQueueByCategory(HealthPlanCallQueueCategory.CallRound); var callQueueCustomers = _callQueueCustomerRepository.GetCallQueueCustomersBuCustomerIds(outboundCustomerIds, callQueue.Id).ToList(); var prospectCustomers = _prospectCustomerRepository.GetProspectCustomersByCustomerIds(outboundCustomerIds); var calls = _callCenterCallRepository.GetCallDetails(outboundCustomerIds); var emailIds = callUploadLogs.Select(x => x.Email).Distinct().ToList(); var userNames = callUploadLogs.Where(x => !string.IsNullOrEmpty(x.UserName)).Select(x => x.UserName).Distinct().ToList(); var organizationRoleUserEmail = _organizationRoleUserRepository.GetNameIdPairofOrgRoleIdByEmail(emailIds, (long)Roles.CallCenterRep); var organizationRoleUserUserName = _organizationRoleUserRepository.GetNameIdPairofOrgRoleIdByUserNames(userNames, (long)Roles.CallCenterRep); var directMailTypes = _directMailTypeRepository.GetAll(); foreach (var callUploadLog in callUploadLogs) { try { var customer = customers.SingleOrDefault(x => x.CustomerId == callUploadLog.CustomerId); if (customer == null) { callUploadLog.IsSuccessfull = false; callUploadLog.ErrorMessage = "Customer ID is not valid."; _callUploadLogRepository.SaveCallUploadLog(callUploadLog); failedCustomerList.Add(callUploadLog); continue; } if (string.IsNullOrEmpty(customer.Tag)) { callUploadLog.IsSuccessfull = false; callUploadLog.ErrorMessage = "Customer does not belong to any HealthPlan."; _callUploadLogRepository.SaveCallUploadLog(callUploadLog); failedCustomerList.Add(callUploadLog); continue; } var healthPlan = healthPlans.SingleOrDefault(x => x.Tag == customer.Tag); if (healthPlan == null) { callUploadLog.IsSuccessfull = false; callUploadLog.ErrorMessage = "HealthPlan " + customer.Tag + " not found."; _callUploadLogRepository.SaveCallUploadLog(callUploadLog); failedCustomerList.Add(callUploadLog); continue; } var orgRoleId = _callUploadHelper.GetOrganizationRoleId(callUploadLog, organizationRoleUserEmail, organizationRoleUserUserName); if (orgRoleId <= 0) { callUploadLog.IsSuccessfull = false; callUploadLog.ErrorMessage = "Created By Information does not found."; _callUploadLogRepository.SaveCallUploadLog(callUploadLog); failedCustomerList.Add(callUploadLog); continue; } long?directMailTypeId = null; if (callUploadLog.IsDirectMail) { var campaign = _campaignRepository.GetCampaignByName(callUploadLog.CampaignName); if (campaign == null) { callUploadLog.IsSuccessfull = false; callUploadLog.ErrorMessage = " Please provide a valid campaign name."; _callUploadLogRepository.SaveCallUploadLog(callUploadLog); failedCustomerList.Add(callUploadLog); continue; } if (!string.IsNullOrEmpty(callUploadLog.DirectMailType)) { var directMailType = directMailTypes.SingleOrDefault(s => s.Name.ToLower() == callUploadLog.DirectMailType.ToLower()); if (directMailType == null) { callUploadLog.IsSuccessfull = false; callUploadLog.ErrorMessage = " Please provide a valid Direct Mail Type."; _callUploadLogRepository.SaveCallUploadLog(callUploadLog); failedCustomerList.Add(callUploadLog); continue; } directMailTypeId = directMailType.Id; } var isInvalidAddress = false; if (callUploadLog.IsInvalidAddress != null) { isInvalidAddress = callUploadLog.IsInvalidAddress.ToLower().Trim() == "yes" ? true : false; } var directMail = new DirectMail { CustomerId = callUploadLog.CustomerId, MailDate = callUploadLog.OutreachDateTime.Value.Date, CallUploadId = callUploadLog.CallUploadId, CampaignId = campaign.Id, DirectMailTypeId = directMailTypeId, Mailedby = orgRoleId, IsInvalidAddress = isInvalidAddress, Notes = isInvalidAddress ? "Direct Mail returned because of invalid address." : string.Empty, }; _directMailRepository.Save(directMail); } else { Event theEventData = null; if (callUploadLog.EventId > 0) { theEventData = events.SingleOrDefault(x => x.Id == callUploadLog.EventId); if (theEventData == null) { callUploadLog.IsSuccessfull = false; callUploadLog.ErrorMessage = "Event Id provide is not valid."; _callUploadLogRepository.SaveCallUploadLog(callUploadLog); failedCustomerList.Add(callUploadLog); continue; } } if (callUploadLog.EventId > 0) { var isValidEventForAccount = _eventRepository.ValidateEventForAccount(callUploadLog.EventId, healthPlan.Id); if (!isValidEventForAccount) { callUploadLog.IsSuccessfull = false; callUploadLog.ErrorMessage = "Event Id provide is not valid for customer HealthPlan."; _callUploadLogRepository.SaveCallUploadLog(callUploadLog); failedCustomerList.Add(callUploadLog); continue; } } var prospectCustomer = prospectCustomers.FirstOrDefault(x => x.CustomerId == customer.CustomerId); if (prospectCustomer == null && customer != null) { prospectCustomer = _prospectCustomerFactory.CreateProspectCustomerFromCustomer(customer, false); prospectCustomer = ((IUniqueItemRepository <ProspectCustomer>)_prospectCustomerRepository).Save(prospectCustomer); } var callQueueCustomer = SaveCallQueueCustomer(callQueue.Id, callUploadLog, healthPlan.Id, callQueueCustomers); var call = SaveCall(callUploadLog, theEventData != null ? theEventData.Id : 0, orgRoleId, healthPlan.Id); if (prospectCustomer != null) { UpdateContactedInfo(prospectCustomer.Id, call.Id, orgRoleId); } SaveCallQueueCustomerCall(callQueueCustomer, call); SaveCallNotes(callUploadLog, call.Id, orgRoleId); var customerCalls = calls.Where(x => x.CalledCustomerId == callUploadLog.CustomerId); bool isRemovedFromCallQueue; var isRuleApplied = _callUploadRuleEngine.ApplyRuleEngine(callUploadLog, customerCalls, prospectCustomer, orgRoleId, callQueueCustomer.Id, callQueueCustomer.CallQueueId, out isRemovedFromCallQueue, _logger); if (isRemovedFromCallQueue) { callQueueCustomer.Status = CallQueueStatus.Removed; _callQueueCustomerRepository.Save(callQueueCustomer); } callUploadLog.IsRuleApplied = isRuleApplied; } successCustomerList.Add(callUploadLog); _callUploadLogRepository.SaveCallUploadLog(callUploadLog); } catch (Exception ex) { callUploadLog.IsSuccessfull = false; callUploadLog.ErrorMessage = "Message: " + ex.Message + "\n stack Trace: " + ex.StackTrace; failedCustomerList.Add(callUploadLog); _logger.Error("Message: " + ex.Message + "\n stack Trace: " + ex.StackTrace); } } } else { foreach (var callUploadLog in callUploadLogs) { callUploadLog.IsSuccessfull = false; callUploadLog.ErrorMessage = "Please Provide a valid CustomerId"; failedCustomerList.Add(callUploadLog); } } SaveFailedCustomers(failedCustomerList, callUploadLogs); }
public void PollForApplyRule() { try { var timeOfDay = DateTime.Now; _logger.Info("Entering In Apply Rules On Locked Customers PollingAgent"); if (_isDevEnvironment || timeOfDay.TimeOfDay > new TimeSpan(4, 0, 0)) { var callUploadLogs = _callUploadLogRepository.GetCustomerForApplyRule(OutReachType); if (callUploadLogs.IsNullOrEmpty()) { _logger.Info("No Record Found"); return; } var customerIds = callUploadLogs.Select(x => x.CustomerId); var callcenterCalls = _callCenterCallRepository.GetCallDetails(customerIds); var prospectCustomers = _prospectCustomerRepository.GetProspectCustomersByCustomerIds(customerIds.ToArray()); var emailIds = callUploadLogs.Select(x => x.Email).Distinct().ToList(); var userNames = callUploadLogs.Select(x => x.UserName).Distinct().ToList(); var organizationRoleUserEmail = _organizationRoleUserRepository.GetNameIdPairofOrgRoleIdByEmail(emailIds, (long)Roles.CallCenterRep); var organizationRoleUserUserName = _organizationRoleUserRepository.GetNameIdPairofOrgRoleIdByUserNames(userNames, (long)Roles.CallCenterRep); var callQueue = _callQueueRepository.GetCallQueueByCategory(HealthPlanCallQueueCategory.CallRound); var callQueueCustomers = _callQueueCustomerRepository.GetCallQueueCustomersBuCustomerIds(customerIds, callQueue.Id).ToList(); foreach (var uploadLog in callUploadLogs) { try { var customerCalls = callcenterCalls.Where(x => x.CalledCustomerId == uploadLog.CustomerId); var prospectCustomer = prospectCustomers.Single(x => x.CustomerId == uploadLog.CustomerId); var orgRoleId = _callUploadHelper.GetOrganizationRoleId(uploadLog, organizationRoleUserEmail, organizationRoleUserUserName); var callQueueCustomer = callQueueCustomers.FirstOrDefault(x => x.CustomerId.HasValue && x.CustomerId.Value == uploadLog.CustomerId); bool isRemovedFromCallQueue; var isRuleApplied = _callUploadRuleEngine.ApplyRuleEngine(uploadLog, customerCalls, prospectCustomer, orgRoleId, callQueueCustomer.Id, callQueueCustomer.CallQueueId, out isRemovedFromCallQueue, _logger); if (isRuleApplied) { if (isRemovedFromCallQueue) { callQueueCustomer.Status = CallQueueStatus.Removed; _callQueueCustomerRepository.Save(callQueueCustomer); } uploadLog.IsRuleApplied = isRuleApplied; _callUploadLogRepository.SaveCallUploadLog(uploadLog); } } catch (Exception exception) { _logger.Error("Error on Apply Rule on Locked Customer For CallUploadLogId: " + uploadLog.Id + " Exception: " + exception + "\n Stack Trace:" + exception.StackTrace); } } } else { _logger.Info(string.Format("Apply Rule on Locked Customer can not be called as time of day is {0}", timeOfDay.TimeOfDay)); } } catch (Exception exception) { _logger.Error("Error on Apply Rule on Locked Customer Exception: " + exception + "\n Stack Trace:" + exception.StackTrace); } }