private async Task <bool> NotifyExternalSystem <TMessage>(IEvent evnt, TMessage message, string activityType) where TMessage : class { return(await _paperpusherClient.SendMessage(evnt.AggregateId, message, activityType)); }
private async Task ProcessReminders(StringBuilder logMessages) { try { _emailNotificationReminderService.ReminderUpdateToSending(); List <EmailNotificationReminder> sentReminders = new List <EmailNotificationReminder>(); Dictionary <EmailNotificationReminder, EmailReminderStatus> updateReminders = new Dictionary <EmailNotificationReminder, EmailReminderStatus>(); var remindingEmails = await _emailNotificationReminderService.GetEmailNotificationReminderByStatus(EmailReminderStatus.Reminding, 10); List <CostStageRevisionStatus> invalidStatuses = new List <CostStageRevisionStatus>(); invalidStatuses.Add(CostStageRevisionStatus.PendingBrandApproval); invalidStatuses.Add(CostStageRevisionStatus.PendingTechnicalApproval); List <CostStageRevisionStatus> approvedStatuses = new List <CostStageRevisionStatus>(); approvedStatuses.Add(CostStageRevisionStatus.Approved); foreach (var reminder in remindingEmails) { logMessages.AppendLine($"START: sending reminder for cost: {reminder.CostId}"); //Get the cost var cost = await _efContext.Cost .AsNoTracking() .Include(c => c.LatestCostStageRevision) .ThenInclude(csr => csr.CostStage) .Include(c => c.Project) .ThenInclude(p => p.Brand) .Include(c => c.Owner) .ThenInclude(o => o.Agency) .ThenInclude(a => a.Country) .FirstAsync(c => c.Id == reminder.CostId); var users = new CostNotificationUsers { CostOwner = cost.Owner }; if (approvedStatuses.Contains(cost.LatestCostStageRevision.Status)) { updateReminders.Add(reminder, EmailReminderStatus.Reminded); } else if (invalidStatuses.Contains(cost.LatestCostStageRevision.Status)) { //Build var notificationMessages = await _emailNotificationBuilder.BuildCostReminderNotification(users, cost, cost.LatestCostStageRevision, DateTime.UtcNow); //Send bool sent = false; foreach (var notification in notificationMessages) { sent = await _paperpusherClient.SendMessage(notification); if (!sent) { logMessages.AppendLine($"[Error] Failed to send notification for cost: {reminder.CostId}"); } } //Update if (sent) { sentReminders.Add(reminder); logMessages.AppendLine($"FINISHED: Sent reminder for cost: {reminder.CostId}"); } } else { updateReminders.Add(reminder, EmailReminderStatus.Cancelled); } } _emailNotificationReminderService.ReminderUpdateToSent(sentReminders); _emailNotificationReminderService.ReminderUpdateToSent(updateReminders); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public async Task <bool> CostHasBeenSubmitted(Guid costId, Guid userId) { if (costId == Guid.Empty) { return(false); } if (userId == Guid.Empty) { return(false); } var cost = await _efContext.Cost .Include(c => c.LatestCostStageRevision) .ThenInclude(csr => csr.CostStage) .ThenInclude(cs => cs.CostStageRevisions) .Include(c => c.LatestCostStageRevision) .Include(c => c.CostStages) .ThenInclude(cs => cs.CostStageRevisions) .ThenInclude(csr => csr.Approvals) .ThenInclude(a => a.ApprovalMembers) .ThenInclude(am => am.CostUser) .Include(c => c.Project) .ThenInclude(p => p.Brand) .IncludeCostOwner() .FirstOrDefaultAsync(c => c.Id == costId); var latestRevision = cost.LatestCostStageRevision; var costOwner = cost.Owner; var insuranceUsers = await _costUserService.GetInsuranceUsers(costOwner.Agency); var financeManagementUsers = await _costUserService.GetFinanceManagementUsers(cost.UserGroups, Constants.BudgetRegion.NorthAmerica); var costUsers = new CostNotificationUsers { CostOwner = cost.Owner, InsuranceUsers = insuranceUsers, FinanceManagementUsers = financeManagementUsers }; var notificationMessages = await _emailNotificationBuilder.BuildCostSubmittedNotification(costUsers, cost, latestRevision, DateTime.UtcNow); var notifications = notificationMessages.ToList(); //Upon Submit the next stage might be PendingTechnicalApproval or PendingBrandApproval or Approved for FA. var nextStatus = cost.Status; switch (nextStatus) { case CostStageRevisionStatus.Approved: // The Cost is auto approved when the budget does not change between cost stages. await BuildAutoApprovedNotification(costUsers, cost, latestRevision, notifications); break; case CostStageRevisionStatus.PendingBrandApproval: var pendingBrandNotifications = await _emailNotificationBuilder.BuildPendingBrandApprovalNotification(costUsers, cost, latestRevision, DateTime.UtcNow); notifications.AddRange(pendingBrandNotifications); //Enqueue reminder for the brand approvers. await _reminderService.CreateNew(cost.Id, DateTime.UtcNow.AddDays(1)); break; case CostStageRevisionStatus.PendingTechnicalApproval: var pendingTechnicalNotifications = await _emailNotificationBuilder.BuildPendingTechnicalApprovalNotification(costUsers, cost, latestRevision, DateTime.UtcNow); notifications.AddRange(pendingTechnicalNotifications); break; //ADC-2698 comment this line of code to take off the feature from release 1.9.4 // //Enqueue reminder for the technical approvers. // await _reminderService.CreateNew(cost.Id, DateTime.UtcNow.AddDays(2)); // break; } //Notify previous approvers if they've been removed if (cost.HasPreviousRevision()) { var previousRevision = cost.GetPreviousRevision(); var previousRevisionId = previousRevision.Id; //Get the approvers for the previous revision previousRevision.Approvals = await _approvalService.GetApprovalsByCostStageRevisionId(previousRevisionId); var csrAnalyser = new CostStageRevisionAnalyser(); IEnumerable <ApprovalMember> removedApprovers = csrAnalyser.GetRemovedApprovers(previousRevision, latestRevision); var removedApproverNotifications = await _emailNotificationBuilder.BuildPreviousApproverNotification(costUsers, removedApprovers, cost, previousRevision, DateTime.UtcNow); notifications.AddRange(removedApproverNotifications); } bool sent = false; foreach (var message in notifications) { sent = await _paperPusherClient.SendMessage(message); } return(sent); }
public async void Execute() { Stopwatch sw = new Stopwatch(); sw.Start(); StringBuilder logMessage = new StringBuilder(); try { logMessage.AppendLine("Starting ActivityLogDeliveryJob!"); await _service.UpdateEntriesToProcessing(); var entries = await _service.GetProcessingActivityLogs(); if (entries == null || !entries.Any()) { //not entries to send logMessage.AppendLine("No activity log entries ready for delivery."); } else { List <ActivityLog> successLogs = new List <ActivityLog>(); Dictionary <ActivityLog, ActivityLogMessage> failedLogs = new Dictionary <ActivityLog, ActivityLogMessage>(); foreach (var entry in entries) { ActivityLogMessage message = null; try { entry.ActivityLogDelivery.RetryCount++; logMessage.AppendLine($"Building Activity Log: {entry.Id}"); message = _service.BuildLogMessage(entry); logMessage.AppendLine($"Built Activity Log Message: {message.Message}"); logMessage.AppendLine($"Sending Activity Log: {entry.Id}"); var success = _paperPusherClient.SendMessage(message.Message).Result; if (success) { logMessage.AppendLine($"Successfully sent Activity Log: {entry.Id}"); successLogs.Add(entry); } else { logMessage.AppendLine($"[Warning] Failed to send Activity Log: {entry.Id}"); failedLogs.Add(entry, message); } } catch (Exception ex) { logMessage.AppendLine($"[Error] Failed to send Activity Log: {entry.Id} - {ex}"); failedLogs.Add(entry, message); } } _service.EntriesDeliveredSuccessfully(successLogs); await _service.EntriesDeliveryFailed(failedLogs); } } catch (Exception ex) { logMessage.AppendLine($"[Error] An error occurred whilst executing ActivityLogDeliveryJob. - {ex}"); } finally { sw.Stop(); logMessage.AppendLine($"Finished ActivityLogDeliveryJob! - Effort: {sw.ElapsedMilliseconds}"); Logger.Information(logMessage.ToString()); } }