controller
=================================
[ApiMethod]
[Route("BirthdayLoanAnniversaryAlerts")]
public async Task<ScheduleBatchResponse> BirthdayLoanAnniversaryAlerts()
{
try
{
await birthdayLoanAnniversaryAlertsOrchestrator.ProcessBatch();
return new ScheduleBatchResponse
{
Status = true,
Message = "Success"
};
}
catch (Exception ex)
{
logger.LogError($"[ERROR] BirthdayLoanAnniversaryAlerts - Message : {ex.Message}", ex);
return new ScheduleBatchResponse
{
Status = false,
Message = "Failed",
Error = ex
};
}
}
Service
==================================================
public async Task ProcessBatch()
{
try
{
var batchId = await marketingWatchesRepository.InsertBatchDetails(
DateTimeOffset.Now,
BatchStatus.Started,
BatchType.BirthdayLoanAnniversaryAlertsV2).ConfigureAwait(false);
logger.LogInformation($"BirthdayLoanAnniversaryV2 Process - BatchId : {batchId} Started Birthday Loan Anniversary Alert process.", batchId);
var userIds = await marketingProfileService.GetAllAcceptedTermsOfUseUsers().ConfigureAwait(false);
logger.LogInformation($"BirthdayLoanAnniversaryV2 Process - marketingProfileService.GetAllAcceptedTermsOfUseUsers. UserIds Count : {userIds.Count}", batchId);
var hasAcceptedTerms = userIds.Contains(16798);
performanceLogger.Start(Processes.GetBirthdayLoanAnniversaryLeads, $"Request - [ BatchId : {batchId} ]");
var leads = await clientConnectService.GetBirthdayLoanAnniversaryLeadsAsync().ConfigureAwait(false);
var testLeads = leads.BirthdayLeads.Where(l => l.BrokerContactId.Equals("66486") && l.BrokerAccountId.Equals("38821")).ToList();
var testLeadsCount = testLeads.Count;
await performanceLogger.End(Processes.GetBirthdayLoanAnniversaryLeads, $"Response - [ BirthdayLeads : {leads?.BirthdayLeads?.Count}. LoanAnniversaryLeads : {leads?.LoanAnniversaryLeads?.Count} ]").ConfigureAwait(false);
performanceLogger.Start(Processes.GetBirthdayLoanAnniversaryAlerts, $"Request - [ BatchId : {batchId} ]");
var (birthdayAlerts, loanAnniversaryAlerts) = await GetBirthdayLoanAnniversaryAlertsAsDictionaryAsync().ConfigureAwait(false);
logger.LogInformation($"BirthdayLoanAnniversaryV2 Process - GetBirthdayLoanAnniversaryAlertsAsDictionaryAsync. Birthday Alerts Count : {birthdayAlerts?.Count}, Loan Anniversary Alerts Count : {loanAnniversaryAlerts?.Count}");
await performanceLogger.End(Processes.GetBirthdayLoanAnniversaryAlerts, $"Response - [ BirthdayAlerts : {birthdayAlerts?.Count}. LoanAnniversaryAlerts : {loanAnniversaryAlerts?.Count} ]").ConfigureAwait(false);
(birthdayAlerts, loanAnniversaryAlerts) = await VerifyDataAsync(leads, birthdayAlerts, loanAnniversaryAlerts).ConfigureAwait(false);
logger.LogInformation(
$"BirthdayLoanAnniversaryV2 Process - Retrieved data for Birthday : {leads.BirthdayLeads?.Count} (Existing: {birthdayAlerts?.Count}) - Loan Anniversary : {leads.LoanAnniversaryLeads?.Count} (Existing: {loanAnniversaryAlerts?.Count})",
batchId);
await ProcessBirthdayAlerts(leads.BirthdayLeads, userIds, birthdayAlerts).ConfigureAwait(false);
await ProcessLoanAnniversary(leads.LoanAnniversaryLeads, userIds, loanAnniversaryAlerts).ConfigureAwait(false);
await clientConnectService.UpdateNewStatusesAsync().ConfigureAwait(false);
marketingWatchesRepository.UpdateBatchDetails(
batchId,
DateTimeOffset.Now,
BatchStatus.Completed);
logger.LogInformation($"BirthdayLoanAnniversaryV2 - BatchId : {batchId} Completed Birthday Loan Anniversary Alert process.", batchId);
}
catch (Exception ex)
{
logger.LogError($"[ERROR] BirthdayLoanAnniversaryV2 - ProcessBatch : {ex.Message}", ex);
throw ex;
}
}
private async Task<(IDictionary<string, Alert>, IDictionary<string, Alert>)> VerifyDataAsync(BirthdayLoanAnniversaryLead leads, IDictionary<string, Alert> birthdayAlerts, IDictionary<string, Alert> loanAnniversaryAlerts)
{
logger.LogInformation($"BirthdayLoanAnniversaryV2 - VerifyDataAsync. " +
$"Birthday Leads Count : {leads.BirthdayLeads?.Count()}. " +
$"Loan Anniversary Leads Count : {leads.BirthdayLeads?.Count()}. " +
$"Birthday Alerts Count : {birthdayAlerts?.Count()}. " +
$"Loan Anniversary Alerts Count : {loanAnniversaryAlerts?.Count()}.");
if (birthdayAlerts?.Count > 0 || loanAnniversaryAlerts?.Count > 0)
{
return (birthdayAlerts, loanAnniversaryAlerts);
}
try
{
leads.BirthdayLeads = leads.BirthdayLeads?.Where(x => IsToday(x.BorrowerBirthDate))
.ToList();
leads.LoanAnniversaryLeads = leads.LoanAnniversaryLeads?.Where(x => IsToday(x.LoanClosingDate))
.ToList();
var (todayBirthdayAlerts, todayLoanAnniversaryAlerts) = await GetBirthdayLoanAnniversaryAlertsAsDictionaryAsync(false).ConfigureAwait(false);
return (todayBirthdayAlerts, todayLoanAnniversaryAlerts);
}
catch (Exception ex)
{
logger.LogError($"BirthdayLoanAnniversaryV2 - VerifyDataAsync failed. Message : {ex.Message}", ex);
throw ex;
}
}
private async Task ProcessBirthdayAlerts(
IReadOnlyCollection<BirthdayLead> birthdayLeads,
IList<int> usersAcceptedTermsAndConditions,
IDictionary<string, Alert> alerts)
{
logger.LogInformation("[START] BirthdayLoanAnniversaryV2 - ProcessBirthdayAlerts");
if (!alerts?.Any() ?? true)
{
logger.LogInformation("BirthdayLoanAnniversaryV2 - ProcessBirthdayAlerts - There were no alerts to process.");
return;
}
var alertsBatch = 0;
var alertsSent = 0;
var distinctContactEntityIds = new List<Tuple<int, int>>();
foreach (var birthdayLead in birthdayLeads)
{
if (int.TryParse(birthdayLead.BrokerContactId, out var contactId) && int.TryParse(birthdayLead.BrokerAccountId, out var entityId))
{
distinctContactEntityIds.Add(Tuple.Create(contactId, entityId));
}
}
logger.LogInformation($"BirthdayLoanAnniversaryV2 - ProcessBirthdayAlerts - Birthday Leads Count : {birthdayLeads.Count}");
var userPreferences = await marketingProfileService.GetUserPreferences(distinctContactEntityIds).ConfigureAwait(false);
var userProfiles = await GetUserProfilesAsync(distinctContactEntityIds).ConfigureAwait(false);
var contactEmailTemplates = await marketingProfileService.GetEmailTemplates(distinctContactEntityIds).ConfigureAwait(false);
var batchedBirthdayItems = birthdayLeads.Split();
performanceLogger.Start(Processes.SaveAlerts, $"Birthday Alerts Count : {birthdayLeads.Count}");
//more time taken process this should be optimise using threading
foreach (var batchAlertBatch in batchedBirthdayItems)
{
try
{
var borrowerCrmIds = GetBorrowerCrmIds(batchAlertBatch);
var triggerTypePreferences = await preferenceService.GetTriggerTypePreferencesForWatches(borrowerCrmIds, new List<string>()).ConfigureAwait(false);
logger.LogInformation($"BirthdayLoanAnniversaryV2 - ProcessBirthdayAlerts - Batch {alertsBatch}");
var birthdayAlerts = await GetBirthdayAlert(
batchAlertBatch,
triggerTypePreferences?.Borrowers,
userPreferences,
contactEmailTemplates,
usersAcceptedTermsAndConditions,
alerts,
userProfiles).ConfigureAwait(false);
var validBirthdayAlerts = birthdayAlerts.Where(n => n.AlertType != null && n.BorrowerId != null);
var birthdayAlertsWithActiveUserProfile = await FilterOutBirthdayLoanAnniversaryAlertsWithInactiveProfile(validBirthdayAlerts).ConfigureAwait(false);
logger.LogInformation($"BirthdayLoanAnniversaryV2 - ProcessBirthdayAlerts - Batch {alertsBatch} - Call clientConnectService.SaveAlertsAsync. Count : {birthdayAlertsWithActiveUserProfile.ToList().Count}");
await clientConnectService.SaveAlertsAsync(birthdayAlertsWithActiveUserProfile).ConfigureAwait(false);
alertsSent += birthdayAlertsWithActiveUserProfile.ToList().Count;
alertsBatch += 1;
logger.LogInformation($"BirthdayLoanAnniversaryV2 - ProcessBirthdayAlerts - Completed Batch {alertsBatch} - Alerts Sent : {alertsSent}");
}
catch (Exception ex)
{
logger.LogError($"[ERROR] BirthdayLoanAnniversaryV2 - ProcessBirthdayAlerts: {ex.Message}", ex);
}
}
await performanceLogger.End(Processes.SaveAlerts, "End Birthday Alerts").ConfigureAwait(false);
logger.LogInformation("[DONE] BirthdayLoanAnniversaryV2 - ProcessBirthdayAlerts");
}
No comments:
Post a Comment