-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from bcgov/staging
Staging
- Loading branch information
Showing
36 changed files
with
6,151 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Quartz; | ||
|
||
namespace SS.Api.cronjobs | ||
{ | ||
public static class QuartzConfigService | ||
{ | ||
public static void AddJobAndTrigger<T>( | ||
this IServiceCollectionQuartzConfigurator quartz, | ||
IConfiguration config) | ||
where T : IJob | ||
{ | ||
|
||
string jobName = typeof(T).Name; | ||
var configKey = $"{jobName}"; | ||
var cronSchedule = config[configKey]; | ||
|
||
if (string.IsNullOrEmpty(cronSchedule)) | ||
{ | ||
throw new Exception($"No Quartz.NET Cron schedule found for job in configuration at {configKey}"); | ||
} | ||
|
||
var jobKey = new JobKey(jobName); | ||
quartz.AddJob<T>(opts => opts.WithIdentity(jobKey)); | ||
|
||
quartz.AddTrigger(opts => opts | ||
.ForJob(jobKey) | ||
.WithIdentity(jobName + "-trigger") | ||
.WithCronSchedule(cronSchedule)); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Quartz; | ||
using SS.Api.services; | ||
using SS.Api.services.usermanagement; | ||
using SS.Db.models.sheriff; | ||
|
||
namespace SS.Api.cronjobs | ||
{ | ||
[DisallowConcurrentExecution] | ||
public class TrainingNotification: IJob | ||
{ | ||
private readonly ILogger<TrainingNotification> Logger; | ||
public IServiceProvider Services { get; } | ||
|
||
public TrainingNotification(ILogger<TrainingNotification> logger, IServiceProvider services, ManageTypesService manageTypesService) | ||
{ | ||
Logger = logger; | ||
Services = services; | ||
} | ||
|
||
public async void ProcessTrainings() | ||
{ | ||
using var scope = Services.CreateScope(); | ||
var TrainingService = scope.ServiceProvider.GetRequiredService<TrainingService>(); | ||
var ChesEmailService = scope.ServiceProvider.GetRequiredService<ChesEmailService>(); | ||
|
||
var trainings = await TrainingService.GetTrainings(); | ||
foreach(var training in trainings) | ||
{ | ||
var noticeDate = DateTimeOffset.UtcNow.AddDays(training.TrainingType.AdvanceNotice); | ||
|
||
Logger.LogInformation(training.TrainingCertificationExpiry.ToString()); | ||
Logger.LogInformation((training.TrainingCertificationExpiry < noticeDate).ToString()); | ||
Logger.LogInformation(training.Sheriff.Email); | ||
|
||
if(training.TrainingCertificationExpiry < noticeDate){ | ||
var emailBody = GetEmailBody(training); | ||
var emailSent = await ChesEmailService.SendEmail( | ||
emailBody, | ||
"Training Expiry Notice", | ||
training.Sheriff.Email | ||
); | ||
if(emailSent) | ||
await TrainingService.UpdateTraining(training.Id); | ||
} | ||
} | ||
Logger.LogInformation("CronJob Done"); | ||
} | ||
|
||
public string GetEmailBody(SheriffTraining training) | ||
{ | ||
var expiryDate = training.TrainingCertificationExpiry.Value.ToString("MMMM dd, yyyy"); | ||
var emailBody = | ||
$"Dear {training.Sheriff.FirstName} {training.Sheriff.LastName}, \n"+ | ||
$"Your \'{training.TrainingType.Code}\' certification will expire on \'{expiryDate}\'. \n"+ | ||
"Please ensure your certification is renewed before this date."; | ||
|
||
return emailBody; | ||
} | ||
|
||
public Task Execute(IJobExecutionContext context) | ||
{ | ||
Logger.LogInformation("___Running CronJob___"); | ||
ProcessTrainings(); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using SS.Api.helpers.extensions; | ||
using SS.Api.infrastructure.exceptions; | ||
using SS.Db.models; | ||
using SS.Db.models.sheriff; | ||
|
||
namespace SS.Api.services.usermanagement | ||
{ | ||
|
||
public class TrainingService | ||
{ | ||
private SheriffDbContext Db { get; } | ||
|
||
public TrainingService(SheriffDbContext db) | ||
{ | ||
Db = db; | ||
} | ||
|
||
|
||
public async Task<List<SheriffTraining>> GetTrainings() | ||
{ | ||
var sheriffTrainingQuery = Db.SheriffTraining.AsNoTracking() | ||
.AsSplitQuery() | ||
.Where(t => t.ExpiryDate == null) | ||
.Where(t => t.FirstNotice != true) | ||
.Where(t => t.TrainingType.AdvanceNotice > 0) | ||
.Include(t => t.TrainingType) | ||
.Include(t => t.Sheriff); | ||
|
||
return await sheriffTrainingQuery.ToListAsync(); | ||
} | ||
|
||
public async Task UpdateTraining(int trainingId) | ||
{ | ||
var training = await Db.SheriffTraining.FindAsync(trainingId); | ||
training.ThrowBusinessExceptionIfNull( | ||
$"{nameof(SheriffTraining)} with the id: {trainingId} could not be found. "); | ||
|
||
if (training.ExpiryDate.HasValue) | ||
throw new BusinessLayerException($"{nameof(SheriffTraining)} with the id: {trainingId} has been expired"); | ||
|
||
training.FirstNotice = true; | ||
await Db.SaveChangesAsync(); | ||
} | ||
} | ||
} |
Oops, something went wrong.