/// <summary> /// Cancel the specified 'notification' so that it will not be sent. /// </summary> /// <param name="notification"></param> /// <returns></returns> public async Task CancelAsync(Entity.NotificationQueue notification) { if (notification == null) { throw new ArgumentNullException(nameof(notification)); } if (!notification.ChesMessageId.HasValue) { throw new InvalidOperationException("Notification does not exist in CHES."); } try { var response = await CancelAsync(notification.ChesMessageId.Value); notification.Status = (Entity.NotificationStatus)Enum.Parse(typeof(Entity.NotificationStatus), response.Status, true); } catch (HttpClientRequestException ex) { // Ignore 409 - as these have already been cancelled. if (ex.StatusCode != System.Net.HttpStatusCode.Conflict) { throw; } notification.Status = Entity.NotificationStatus.Cancelled; } }
/// <summary> /// Generate the specified 'notification' Subject and Body based on the Template and specified 'model'. /// Modifies the 'notification.Subject' and 'notification.Body' values by building the Razor template. /// </summary> /// <typeparam name="TModel"></typeparam> /// <param name="notification"></param> /// <param name="model"></param> public void Generate <TModel>(Entity.NotificationQueue notification, TModel model) { var email = new Model.EmailTemplate() { Subject = notification.Template.Subject, BodyType = (Model.EmailBodyTypes)Enum.Parse(typeof(Model.EmailBodyTypes), notification.Template.BodyType.ToString()), Body = notification.Template.Body, }; Build($"{notification.TemplateId}-{notification.Template.RowVersion.ConvertRowVersion()}", email, model); notification.Subject = email.Subject; notification.Body = email.Body; }
/// <summary> /// Send the specified 'notification' to CHES. /// Change the notification status based on the response. /// </summary> /// <param name="notification"></param> /// <returns></returns> public async Task SendAsync(Entity.NotificationQueue notification) { if (notification == null) { throw new ArgumentNullException(nameof(notification)); } try { // Send notifications to CHES. var response = await SendAsync(new Model.Email() { To = notification.To.Split(";").Select(v => v.Trim()), Cc = notification.Cc?.Split(";").Select(v => v.Trim()), Bcc = notification.Bcc?.Split(";").Select(v => v.Trim()), Encoding = (Model.EmailEncodings)Enum.Parse(typeof(Model.EmailEncodings), notification.Encoding.ToString()), BodyType = (Model.EmailBodyTypes)Enum.Parse(typeof(Model.EmailBodyTypes), notification.BodyType.ToString()), Priority = (Model.EmailPriorities)Enum.Parse(typeof(Model.EmailPriorities), notification.Priority.ToString()), Subject = notification.Subject, Body = notification.Body, SendOn = notification.SendOn, Tag = notification.Tag, }); notification.ChesTransactionId = response.TransactionId; notification.ChesMessageId = response.Messages.First().MessageId; } catch (HttpClientRequestException ex) { notification.Status = Entity.NotificationStatus.Failed; var data = await ex.Response.Content.ReadAsStringAsync(); _logger.LogError(ex, $"Failed to send email to CHES - Template:{notification.TemplateId}{Environment.NewLine}CHES StatusCode:{ex.StatusCode}{Environment.NewLine}{ex.Message}{Environment.NewLine}{data}"); throw; } catch (Exception ex) { notification.Status = Entity.NotificationStatus.Failed; _logger.LogError(ex, $"Failed to send email to CHES - notification:{notification.Id}, template:{notification.TemplateId}{Environment.NewLine}{ex.Message}"); throw; } }
/// <summary> /// Create a new instance of a ProjectAgencyResponse class, initializes with specified arguments. /// </summary> /// <param name="project"></param> /// <param name="agency"></param> /// <param name="notification"></param> /// <param name="response"></param> /// <param name="receivedOn"></param> public ProjectAgencyResponse(Project project, Agency agency, NotificationQueue notification, NotificationResponses response, DateTime?receivedOn = null) : this(project, agency, response, receivedOn) { this.NotificationId = notification?.Id ?? throw new ArgumentNullException(nameof(notification)); this.Notification = notification; }