Skip to content

Commit

Permalink
Version 1.2.24-pre - Various fixes (#249)
Browse files Browse the repository at this point in the history
* Number of fixes related to VSTO and working with emails

* HtmlBody made nullable for EmailLog

* Version 1.2.23-pre

* An option to skip failed email schedules and move on

* Version 1.2.24-pre

* Missing mapping for Unsubscribe and UnsubscribeDtos added

* Taking aliases into account in SyncEsTask

---------

Co-authored-by: peterliapin <[email protected]>
  • Loading branch information
peterliapin and peterliapin authored Mar 9, 2024
1 parent 7d13dae commit f6c95f0
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 55 deletions.
2 changes: 1 addition & 1 deletion OnlineSales.sln
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ Global
$0.DotNetNamingPolicy = $1
$1.DirectoryNamespaceAssociation = PrefixedHierarchical
$0.TextStylePolicy = $3
version = 1.2.23-pre
version = 1.2.24-pre
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ReleaseVersion>1.2.23-pre</ReleaseVersion>
<ReleaseVersion>1.2.24-pre</ReleaseVersion>
<EnableDynamicLoading>true</EnableDynamicLoading>
<Configurations>Debug;Release;Migration</Configurations>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<Nullable>enable</Nullable>
<ReleaseVersion>1.2.23-pre</ReleaseVersion>
<ReleaseVersion>1.2.24-pre</ReleaseVersion>
<Configurations>Release;Debug</Configurations>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ReleaseVersion>1.2.23-pre</ReleaseVersion>
<ReleaseVersion>1.2.24-pre</ReleaseVersion>
<EnableDynamicLoading>true</EnableDynamicLoading>
<Configurations>Debug;Release;Migration</Configurations>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ReleaseVersion>1.2.23-pre</ReleaseVersion>
<ReleaseVersion>1.2.24-pre</ReleaseVersion>
<EnableDynamicLoading>true</EnableDynamicLoading>
<Configurations>Debug;Release;Migration</Configurations>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ReleaseVersion>1.2.23-pre</ReleaseVersion>
<ReleaseVersion>1.2.24-pre</ReleaseVersion>
<EnableDynamicLoading>true</EnableDynamicLoading>
<Configurations>Debug;Release;Migration</Configurations>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ReleaseVersion>1.2.23-pre</ReleaseVersion>
<ReleaseVersion>1.2.24-pre</ReleaseVersion>
<EnableDynamicLoading>true</EnableDynamicLoading>
<Configurations>Debug;Release;Migration</Configurations>
</PropertyGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/OnlineSales/Configuration/AutoMapperProfiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ public AutoMapperProfiles()

CreateMap<ActivityLog, ActivityLogDetailsDto>()
.ForAllMembers(m => m.Condition(PropertyNeedsMapping));

CreateMap<Unsubscribe, UnsubscribeDto>().ReverseMap();
CreateMap<Unsubscribe, UnsubscribeDetailsDto>()
.ForAllMembers(m => m.Condition(PropertyNeedsMapping));
CreateMap<UnsubscribeImportDto, Unsubscribe>()
.ForAllMembers(m => m.Condition(PropertyNeedsMapping));
}

private static bool PropertyNeedsMapping(object source, object target, object sourceValue, object targetValue)
Expand Down
1 change: 1 addition & 0 deletions src/OnlineSales/Entities/ContactEmailSchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum ScheduleStatus
{
Pending = 0,
Completed = 1,
Failed = 2,
}

[Table("contact_email_schedule")]
Expand Down
2 changes: 1 addition & 1 deletion src/OnlineSales/OnlineSales.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RootNamespace>OnlineSales</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<UserSecretsId>98270385-43f2-4d3c-98d5-02d0d77fc2d9</UserSecretsId>
<ReleaseVersion>1.2.23-pre</ReleaseVersion>
<ReleaseVersion>1.2.24-pre</ReleaseVersion>
<AssemblyName>OnlineSales</AssemblyName>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>OnlineSales</PackageId>
Expand Down
96 changes: 52 additions & 44 deletions src/OnlineSales/Tasks/ContactScheduledEmailTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,65 +35,73 @@ public override async Task<bool> Execute(TaskExecutionLog currentJob)

foreach (var schedule in schedules)
{
EmailTemplate? nextEmailTemplateToSend;
var retryDelay = 0;
try
{
EmailTemplate? nextEmailTemplateToSend;
var retryDelay = 0;

var lastEmailLog = dbContext.EmailLogs!.Where(
e => e.ScheduleId == schedule.ScheduleId &&
e.ContactId == schedule.ContactId).OrderByDescending(x => x.CreatedAt).FirstOrDefault();
var lastEmailLog = dbContext.EmailLogs!.Where(
e => e.ScheduleId == schedule.ScheduleId &&
e.ContactId == schedule.ContactId).OrderByDescending(x => x.CreatedAt).FirstOrDefault();

var lastEmailTemplate = lastEmailLog is not null
? dbContext.EmailTemplates!.FirstOrDefault(e => e.Id == lastEmailLog!.TemplateId)
: null;
var lastEmailTemplate = lastEmailLog is not null
? dbContext.EmailTemplates!.FirstOrDefault(e => e.Id == lastEmailLog!.TemplateId)
: null;

// Retry logic if the last email is not sent
if (lastEmailLog is not null && lastEmailLog!.Status is EmailStatus.NotSent)
{
var emailNotSentCount = dbContext.EmailLogs!.Count(
e => e.ScheduleId == schedule.ScheduleId
&& e.ContactId == schedule.ContactId
&& e.TemplateId == lastEmailLog.TemplateId
&& e.Status == EmailStatus.NotSent);

// If all retry attempts completed, get the next email template to send.
if (emailNotSentCount > lastEmailTemplate!.RetryCount)
// Retry logic if the last email is not sent
if (lastEmailLog is not null && lastEmailLog!.Status is EmailStatus.NotSent)
{
nextEmailTemplateToSend = GetNextEmailTemplateToSend(lastEmailTemplate, schedule.Schedule!.GroupId);
var emailNotSentCount = dbContext.EmailLogs!.Count(
e => e.ScheduleId == schedule.ScheduleId
&& e.ContactId == schedule.ContactId
&& e.TemplateId == lastEmailLog.TemplateId
&& e.Status == EmailStatus.NotSent);

// If all retry attempts completed, get the next email template to send.
if (emailNotSentCount > lastEmailTemplate!.RetryCount)
{
nextEmailTemplateToSend = GetNextEmailTemplateToSend(lastEmailTemplate, schedule.Schedule!.GroupId);
}
else
{
// Retry attempt available. sending the same email template.
nextEmailTemplateToSend = dbContext.EmailTemplates!.FirstOrDefault(t => t.Id == lastEmailTemplate.Id);
retryDelay = lastEmailTemplate!.RetryInterval;
}
}
else
{
// Retry attempt available. sending the same email template.
nextEmailTemplateToSend = dbContext.EmailTemplates!.FirstOrDefault(t => t.Id == lastEmailTemplate.Id);
retryDelay = lastEmailTemplate!.RetryInterval;
nextEmailTemplateToSend = GetNextEmailTemplateToSend(lastEmailTemplate!, schedule.Schedule!.GroupId);
}
}
else
{
nextEmailTemplateToSend = GetNextEmailTemplateToSend(lastEmailTemplate!, schedule.Schedule!.GroupId);
}

// All emails in the schedule are sent for the given contact.
if (nextEmailTemplateToSend is null)
{
var contactSchedule = dbContext.ContactEmailSchedules!.FirstOrDefault(c => c.Id == schedule.Id);
contactSchedule!.Status = ScheduleStatus.Completed;
await dbContext.SaveChangesAsync();

break;
}
// All emails in the schedule are sent for the given contact.
if (nextEmailTemplateToSend is null)
{
schedule.Status = ScheduleStatus.Completed;
await dbContext.SaveChangesAsync();

var nextExecutionTime = GetNextExecutionTime(schedule, retryDelay, lastEmailLog);
break;
}

if (nextExecutionTime is not null)
{
// check IsRightTimeToExecute()
var executeNow = IsRightTimeToExecute(nextExecutionTime.Value);
var nextExecutionTime = GetNextExecutionTime(schedule, retryDelay, lastEmailLog);

if (executeNow)
if (nextExecutionTime is not null)
{
await emailFromTemplateService.SendToContactAsync(schedule.ContactId, nextEmailTemplateToSend!.Name, GetTemplateArguments(), null, schedule.ScheduleId);
// check IsRightTimeToExecute()
var executeNow = IsRightTimeToExecute(nextExecutionTime.Value);

if (executeNow)
{
await emailFromTemplateService.SendToContactAsync(schedule.ContactId, nextEmailTemplateToSend!.Name, GetTemplateArguments(), null, schedule.ScheduleId);
}
}
}
catch (Exception ex)
{
Log.Error(ex, $"Failed to complete email sending for contact schedule Id = {schedule.Id}");
schedule.Status = ScheduleStatus.Failed;
await dbContext.SaveChangesAsync();
}
}

return true;
Expand Down
7 changes: 5 additions & 2 deletions src/OnlineSales/Tasks/SyncEsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,15 @@ private string GetIndexName(string loggedTypeName)
private HashSet<string> GetExistedIndices()
{
var response = esDbContext.ElasticClient.Indices.GetAlias(Indices.All);

if (!response.IsValid)
{
throw new ESSyncTaskException("Cannot get existed indices list");
throw new ESSyncTaskException("Failed to read all existing indices and aliases from Elastic database");
}

return response.Indices.Keys.Select(k => k.Name).ToHashSet();
return response.Indices
.SelectMany(index => new[] { index.Key.Name }.Concat(index.Value.Aliases.Keys))
.ToHashSet();
}

public class ESSyncTaskException : Exception
Expand Down
2 changes: 1 addition & 1 deletion tests/OnlineSales.Tests/OnlineSales.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<ReleaseVersion>1.2.23-pre</ReleaseVersion>
<ReleaseVersion>1.2.24-pre</ReleaseVersion>
<Configurations>Debug;Release;Migration</Configurations>
</PropertyGroup>

Expand Down

0 comments on commit f6c95f0

Please sign in to comment.