Skip to content

Commit

Permalink
Merge pull request #408 from bcgov/1.2
Browse files Browse the repository at this point in the history
Version 1.2
  • Loading branch information
ychung-mot authored Apr 15, 2020
2 parents 59b5eb1 + 697a9d6 commit f8e4eed
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 19 deletions.
3 changes: 2 additions & 1 deletion api/Hmcr.Data/Mappings/EntityToModelProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ public EntityToModelProfile()
CreateMap<HmrCodeLookup, CodeLookupDto>();

CreateMap<HmrFeedbackMessage, FeedbackMessageDto>();
CreateMap<HmrFeedbackMessage, FeedbackMessageUpdateDto>();



}
}
}
1 change: 1 addition & 0 deletions api/Hmcr.Data/Mappings/ModelToEntityProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public ModelToEntityProfile()
CreateMap<CodeLookupDto, HmrCodeLookup>();

CreateMap<FeedbackMessageDto, HmrFeedbackMessage>();
CreateMap<FeedbackMessageUpdateDto, HmrFeedbackMessage>();
}
}
}
24 changes: 22 additions & 2 deletions api/Hmcr.Data/Repositories/FeebackMessageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
using Hmcr.Data.Database.Entities;
using Hmcr.Data.Repositories.Base;
using Hmcr.Model.Dtos.FeedbackMessage;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Hmcr.Data.Repositories
{
public interface IFeebackMessageRepository
{
Task<HmrFeedbackMessage> CreateFeedbackMessage(FeedbackMessageDto feedback);
Task<HmrFeedbackMessage> CreateFeedbackMessageAsync(FeedbackMessageDto feedback);
Task UpdateFeedbackMessageAsync(FeedbackMessageUpdateDto feedbackMessage);
Task<IEnumerable<FeedbackMessageUpdateDto>> GetFailedFeedbackMessagesAsync();
}
public class FeebackMessageRepository : HmcrRepositoryBase<HmrFeedbackMessage>, IFeebackMessageRepository
{
Expand All @@ -17,9 +22,24 @@ public FeebackMessageRepository(AppDbContext dbContext, IMapper mapper)
{
}

public async Task<HmrFeedbackMessage> CreateFeedbackMessage(FeedbackMessageDto feedback)
public async Task<HmrFeedbackMessage> CreateFeedbackMessageAsync(FeedbackMessageDto feedback)
{
return await AddAsync(feedback);
}

public async Task UpdateFeedbackMessageAsync(FeedbackMessageUpdateDto feedbackMessage)
{
var entity = await DbSet
.FirstAsync(x => x.FeedbackMessageId == feedbackMessage.FeedbackMessageId);

Mapper.Map(feedbackMessage, entity);
}

public async Task<IEnumerable<FeedbackMessageUpdateDto>> GetFailedFeedbackMessagesAsync()
{
var hourAgo = DateTime.UtcNow.AddHours(-1);

return await GetAllAsync<FeedbackMessageUpdateDto>(x => x.IsSent == false && x.CommunicationDate < hourAgo);
}
}
}
2 changes: 2 additions & 0 deletions api/Hmcr.Domain/Hangfire/Base/ReportJobServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ protected async Task CommitAndSendEmailAsync()
_unitOfWork.Commit();

await _emailService.SendStatusEmailAsync(_submission.SubmissionObjectId);

_logger.LogInformation("[Hangfire] Finishing submission {submissionObjectId}", _submission.SubmissionObjectId);
}

protected void LogRowParseException(decimal rowNum, string exception, ReadingContext context)
Expand Down
57 changes: 57 additions & 0 deletions api/Hmcr.Domain/Hangfire/EmailJobService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Hangfire;
using Hmcr.Data.Repositories;
using Hmcr.Domain.Services;
using Hmcr.Model;
using Hmcr.Model.Dtos.User;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hmcr.Domain.Hangfire
{
public interface IEmailJobService
{
Task ResendEmails();
}

public class EmailJobService : IEmailJobService
{
private IFeebackMessageRepository _feedbackRepo;
private IEmailService _emailService;
private HmcrCurrentUser _user;
private ILogger<EmailJobService> _logger;

public EmailJobService(IFeebackMessageRepository feedbackRepo, IEmailService emailService, HmcrCurrentUser user, ILogger<EmailJobService> logger)
{
_feedbackRepo = feedbackRepo;
_emailService = emailService;
_user = user;
_logger = logger;
}

[SkipSameJob]
[AutomaticRetry(Attempts = 0)]
public async Task ResendEmails()
{
_user.AuthDirName = UserTypeDto.IDIR;
_user.UniversalId = "hangfire";
_user.UserGuid = new Guid();

var feedbackMessages = await _feedbackRepo.GetFailedFeedbackMessagesAsync();
var count = feedbackMessages.Count();

if (count == 0)
return;

_logger.LogInformation($"[Hangfire] The job for resending emails is starting - {count} emails to send");

foreach (var feedbackMessage in feedbackMessages)
{
await _emailService.SendStatusEmailAsync(feedbackMessage.SubmissionObjectId, feedbackMessage);
}
}
}
}
9 changes: 8 additions & 1 deletion api/Hmcr.Domain/Hangfire/SkipSameJobAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ public void OnPerformed(PerformedContext filterContext)

private string GetJobFingerprint(Job job)
{
return $"{job.Type.FullName}-{job.Method.Name}-{JsonConvert.SerializeObject(job.Args)}";
var args = "";

if (job.Args.Count > 0)
{
args = "-" + JsonConvert.SerializeObject(job.Args);
}

return $"{job.Type.FullName}-{job.Method.Name}{args}";
}
}
}
49 changes: 34 additions & 15 deletions api/Hmcr.Domain/Services/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Hmcr.Domain.Services
{
public interface IEmailService
{
Task SendStatusEmailAsync(decimal submissionObjectId);
Task SendStatusEmailAsync(decimal submissionObjectId, FeedbackMessageUpdateDto feedbackDto = null);
}

public class EmailService : IEmailService
Expand Down Expand Up @@ -56,7 +56,7 @@ public EmailService(IConfiguration config, IUserRepository userRepo, ILogger<Ema
_unitOfWork = unitOfWork;
}

public async Task SendStatusEmailAsync(decimal submissionObjectId)
public async Task SendStatusEmailAsync(decimal submissionObjectId, FeedbackMessageUpdateDto feedbackMessage = null)
{
var submissionInfo = await _submissionRepo.GetSubmissionInfoForEmailAsync(submissionObjectId);
submissionInfo.SubmissionDate = DateUtils.ConvertUtcToPacificTime(submissionInfo.SubmissionDate);
Expand All @@ -77,7 +77,7 @@ public async Task SendStatusEmailAsync(decimal submissionObjectId)
var textBody = htmlBody.HtmlToPlainText();

var isSent = true;
var isError = false;
var isError = !submissionInfo.Success;
var errorText = "";

try
Expand All @@ -87,27 +87,46 @@ public async Task SendStatusEmailAsync(decimal submissionObjectId)
catch (Exception ex)
{
isSent = false;
isError = true;
errorText = ex.Message;

_logger.LogError($"Email for the submission {submissionObjectId} failed.");
_logger.LogError(ex.ToString());
}

var feedback = new FeedbackMessageDto
if (feedbackMessage == null)
{
SubmissionObjectId = submissionObjectId,
CommunicationSubject = subject,
CommunicationText = htmlBody,
CommunicationDate = DateTime.UtcNow,
IsSent = isSent,
IsError = isError,
SendErrorText = errorText
};
var feedback = new FeedbackMessageDto
{
SubmissionObjectId = submissionObjectId,
CommunicationSubject = subject,
CommunicationText = htmlBody,
CommunicationDate = DateTime.UtcNow,
IsSent = isSent,
IsError = isError,
SendErrorText = errorText
};

await _feedbackRepo.CreateFeedbackMessageAsync(feedback);
}
else
{
feedbackMessage.SubmissionObjectId = submissionObjectId;
feedbackMessage.CommunicationSubject = subject;
feedbackMessage.CommunicationText = htmlBody;
feedbackMessage.CommunicationDate = DateTime.UtcNow;
feedbackMessage.IsSent = isSent;
feedbackMessage.IsError = isError;
feedbackMessage.SendErrorText = errorText;

await _feedbackRepo.UpdateFeedbackMessageAsync(feedbackMessage);
}

await _feedbackRepo.CreateFeedbackMessage(feedback);
await _unitOfWork.CommitAsync();

_logger.LogInformation("[Hangfire] Finishing submission {submissionObjectId}", submissionObjectId);
var finished = isSent ? "Finished" : "Failed";
var sending = feedbackMessage == null ? "sending" : "resending";

_logger.LogInformation($"[Hangfire] {finished} {sending} email for submission {submissionObjectId}", submissionObjectId);
}

private void SendEmailToUsersInServiceArea(decimal serviceAreaNumber, string subject, string htmlBody, string textBody)
Expand Down
2 changes: 2 additions & 0 deletions api/Hmcr.Hangfire/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ISubmiss

//Inject Code Lookup
validator.CodeLookup = codeLookupRepo.LoadCodeLookupCache();

RecurringJob.AddOrUpdate<EmailJobService>("ResendEmails", x => x.ResendEmails(), Cron.Minutely);
}
}
}
16 changes: 16 additions & 0 deletions api/Hmcr.Model/Dtos/FeedbackMessage/FeedbackMessageUpdateDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace Hmcr.Model.Dtos.FeedbackMessage
{
public class FeedbackMessageUpdateDto
{
public decimal FeedbackMessageId { get; set; }
public decimal SubmissionObjectId { get; set; }
public string CommunicationSubject { get; set; }
public string CommunicationText { get; set; }
public DateTime? CommunicationDate { get; set; }
public bool? IsSent { get; set; }
public bool? IsError { get; set; }
public string SendErrorText { get; set; }
}
}

0 comments on commit f8e4eed

Please sign in to comment.