Skip to content

Commit

Permalink
refinements to start trial job. add in blocked email domain code.
Browse files Browse the repository at this point in the history
  • Loading branch information
scoxen1 committed Apr 16, 2024
1 parent f5074a2 commit ff18803
Show file tree
Hide file tree
Showing 7 changed files with 4,839 additions and 5 deletions.
3 changes: 3 additions & 0 deletions api/CESMII.Marketplace.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<IMongoRepository<JobLog>, MongoRepository<JobLog>>();
services.AddScoped<IMongoRepository<JobDefinition>, MongoRepository<JobDefinition>>();

//one off tables
services.AddScoped<IMongoRepository<JobManager.Models.BlockedEmailDomain>, MongoRepository<JobManager.Models.BlockedEmailDomain>>();

//DAL objects
services.AddScoped<UserDAL>(); //this one has extra methods outside of the IDal interface
services.AddScoped<OrganizationDAL>();
Expand Down
4 changes: 2 additions & 2 deletions api/CESMII.Marketplace.JobManager/JobFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task<string> ExecuteJob(JobPayloadModel model, UserModel user)
{
Messages = new List<JobLogMessage>() {
new JobLogMessage() {
Message = $"Starting job '{jobDef.Name}' for '{item.DisplayName}'...",
Message = $"Starting '{jobDef.DisplayName}' for '{item.DisplayName}'...",
Created = DateTime.UtcNow
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ private async Task ExecuteJobInternal(JobDefinitionModel jobDef, string payload,
try
{
//instantiate job class
var job = InstantiateJob(jobDef.TypeName, logger, httpFactory, dalJobLog, dalUser, configuration, mailRelay);
var job = InstantiateJob(jobDef.TypeName, _serviceScopeFactory, logger, httpFactory, dalJobLog, dalUser, configuration, mailRelay);

//initialize job
job.Initialize(jobDef, payload, logId, user);
Expand Down
4 changes: 4 additions & 0 deletions api/CESMII.Marketplace.JobManager/Jobs/JobBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
using CESMII.Marketplace.Common;
using CESMII.Marketplace.Common.Enums;
using CESMII.Common.SelfServiceSignUp.Services;
using Microsoft.Extensions.DependencyInjection;

namespace CESMII.Marketplace.JobManager.Jobs
{
public abstract class JobBase : IJob, IDisposable
{
protected readonly IServiceScopeFactory _serviceScopeFactory;
protected readonly ILogger<IJob> _logger;
protected bool _disposed = false;
protected readonly IHttpApiFactory _httpFactory;
Expand All @@ -29,13 +31,15 @@ public abstract class JobBase : IJob, IDisposable
public event JobRunEventHandler JobRun; // event

public JobBase(
IServiceScopeFactory serviceScopeFactory,
ILogger<IJob> logger,
IHttpApiFactory httpFactory,
IDal<JobLog, JobLogModel> dalJobLog,
UserDAL dalUser,
IConfiguration configuration,
MailRelayService mailRelayService)
{
_serviceScopeFactory = serviceScopeFactory;
_logger = logger;
_httpFactory = httpFactory;
_dalJobLog = dalJobLog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using System.Net.Http;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
Expand All @@ -25,13 +26,14 @@ namespace CESMII.Marketplace.JobManager.Jobs
public class JobBorgConnectActivate : JobBase
{
public JobBorgConnectActivate(
IServiceScopeFactory serviceScopeFactory,
ILogger<IJob> logger,
IHttpApiFactory httpFactory,
IDal<JobLog, JobLogModel> dalJobLog,
UserDAL dalUser,
IConfiguration configuration,
MailRelayService mailRelayService) :
base(logger, httpFactory, dalJobLog, dalUser, configuration, mailRelayService)
base(serviceScopeFactory, logger, httpFactory, dalJobLog, dalUser, configuration, mailRelayService)
{
//wire up run async event
base.JobRun += JobRunBorg;
Expand Down
47 changes: 45 additions & 2 deletions api/CESMII.Marketplace.JobManager/Jobs/JobOntimeEdgeTrial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
using System.Linq;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net.Http;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

using CESMII.Marketplace.Data.Repositories;
using CESMII.Marketplace.Data.Entities;
using CESMII.Marketplace.DAL;
using CESMII.Marketplace.DAL.Models;
Expand All @@ -17,7 +20,7 @@
using CESMII.Common.SelfServiceSignUp.Services;
using CESMII.Marketplace.SmipGraphQlClient;
using CESMII.Marketplace.SmipGraphQlClient.Models;
using System.Net.Http;
using CESMII.Marketplace.JobManager.Models;

namespace CESMII.Marketplace.JobManager.Jobs
{
Expand All @@ -28,13 +31,14 @@ namespace CESMII.Marketplace.JobManager.Jobs
public class JobOnTimeEdgeTrial : JobBase
{
public JobOnTimeEdgeTrial(
IServiceScopeFactory serviceScopeFactory,
ILogger<IJob> logger,
IHttpApiFactory httpFactory,
IDal<JobLog, JobLogModel> dalJobLog,
UserDAL dalUser,
IConfiguration configuration,
MailRelayService mailRelayService) :
base(logger, httpFactory, dalJobLog, dalUser, configuration, mailRelayService)
base(serviceScopeFactory, logger, httpFactory, dalJobLog, dalUser, configuration, mailRelayService)
{
//wire up run async event
base.JobRun += JobInitiateTrial;
Expand Down Expand Up @@ -234,6 +238,14 @@ private bool ValidatePayload(JobOnTimeEdgePayload payload, out string message)
sbResult.AppendLine("Email is required.");
_logger.LogInformation($"JobOnTimeEdgeTrial|ValidatePayload|Email is required.");
}
else
{
var msgEmailDomain = "";
if (!ValidateEmailDomain(payload.FormData?.Email, out msgEmailDomain))
{
sbResult.AppendLine(msgEmailDomain);
}
}
if (string.IsNullOrEmpty(payload.FormData?.Phone))
{
sbResult.AppendLine("Phone is required.");
Expand All @@ -244,6 +256,37 @@ private bool ValidatePayload(JobOnTimeEdgePayload payload, out string message)
return sbResult.Length == 0;
}

private bool ValidateEmailDomain(string email, out string message)
{
var sbResult = new System.Text.StringBuilder();
if (string.IsNullOrEmpty(email))
{
sbResult.AppendLine("Email is required.");
_logger.LogError($"JobOnTimeEdgeTrial|ValidateEmailDomain|Email is required.");
}
else
{
//wrap in scope so that we don't lose the scope of the dependency injected objects once the
//web api request completes and disposes of the import service object (and its module vars)
using (var scope = _serviceScopeFactory.CreateScope())
{
//initialize scoped services for DI, initialize job class
var repoBlockedDomains = scope.ServiceProvider.GetService<IMongoRepository<BlockedEmailDomain>>();
var numMatches = repoBlockedDomains.Count(x => email.Trim().ToLower().EndsWith(x.domain.ToLower()));

if (numMatches > 0)
{
sbResult.AppendLine("This trial does not permit use of free email domains. Please use a different email.");
_logger.LogWarning($"JobOnTimeEdgeTrial|ValidateEmailDomain|Blocked|Email '{email}' has a blocked email domain and is not permitted.");
}
}
}

message = sbResult.ToString();
return sbResult.Length == 0;
}


[System.Obsolete("ValidateSmipSettings de-scoped")]
private bool ValidateSmipSettings(SmipSettingsOnTimeEdge settings, out string message)
{
Expand Down
13 changes: 13 additions & 0 deletions api/CESMII.Marketplace.JobManager/Models/BlockedEmailDomain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CESMII.Marketplace.Data.Entities;

namespace CESMII.Marketplace.JobManager.Models
{
/// <summary>
/// This is a one off table used to check for blocked email domains on the Apogean blocked list.
/// </summary>
/// <seealso cref="CESMII.Marketplace.JobManager.Jobs.OnTimeEdgeApiConfig"/>
public class BlockedEmailDomain : AbstractEntity
{
public string domain { get; set; }
}
}
Loading

0 comments on commit ff18803

Please sign in to comment.