Skip to content

Commit

Permalink
ER-1242 🔌 - ApplicationsSubmitted Aggregate Notifications Plugin +sem…
Browse files Browse the repository at this point in the history
…ver: skip
  • Loading branch information
sachpatel committed Sep 24, 2019
1 parent b74935d commit 2032585
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public interface ITemplateIdProvider
/// This has to be unique value across all the possible services
/// Example value: VacancyServices.Recruit.Employer
string ProviderServiceName { get; }
Task<string> GetTemplateId(CommunicationMessage message, bool isAggregate = false);
Task<string> GetTemplateIdAsync(CommunicationMessage message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public static class Vacancy
public const string EmployerName = "employer-name";
}

public static class Application
{
public const string ApplicationsSubmittedAggregateHeader = "applications-submitted-aggregate-header";
public const string ApplicationsSubmittedAggregateBodySnippets = "applications-submitted-aggregate-body-snippets";
}

public static class ApprenticeshipService
{
public const string ApprenticeshipServiceUrl = "apprenticeship-service-url";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Communication.Types;
using Communication.Types.Interfaces;
using Esfa.Recruit.Vacancies.Client.Application.Communications;
using Humanizer;

namespace Recruit.Vacancies.Client.Application.Communications.CompositeDataItemProviderPlugins
{
public class ApplicationsSubmittedCompositeDataItemPlugin : ICompositeDataItemProvider
{
public string ProviderName => GetType().Name;

public Task<IEnumerable<CommunicationDataItem>> GetConsolidatedMessageDataItemsAsync(CommunicationMessage aggregateMessage, IEnumerable<CommunicationMessage> messages)
{
var dataItems = new List<CommunicationDataItem>();
var vacancyRefs = GetUniqueVacancyRefs(messages);
var header = GetNoApplicationsSubmittedHeader(messages, vacancyRefs);
var vacanciesAndApplicationsSubmittedSnippets = GetPerVacancyApplicationsSubmittedSnippet(messages, vacancyRefs);
var apprenticeshipServiceUrlDataItem = messages.First().DataItems.First(di => di.Key == CommunicationConstants.DataItemKeys.ApprenticeshipService.ApprenticeshipServiceUrl);

dataItems.Add(new CommunicationDataItem(CommunicationConstants.DataItemKeys.Application.ApplicationsSubmittedAggregateHeader, header));
dataItems.Add(new CommunicationDataItem(CommunicationConstants.DataItemKeys.Application.ApplicationsSubmittedAggregateBodySnippets, vacanciesAndApplicationsSubmittedSnippets));
dataItems.Add(apprenticeshipServiceUrlDataItem);

return Task.FromResult(dataItems.AsEnumerable());
}

private string GetPerVacancyApplicationsSubmittedSnippet(IEnumerable<CommunicationMessage> messages, List<string> vacancyRefs)
{
var vacanciesAndNewApplicationCounts = GetUniqueVacanciesAndNewApplicationCounts(messages, vacancyRefs);

var sb = new StringBuilder();

foreach (var vr in vacancyRefs)
{
var msg = messages.First(m => m.DataItems.Exists(di => di.Key == CommunicationConstants.DataItemKeys.Vacancy.VacancyReference && di.Value == vr));
var applicationCount = vacanciesAndNewApplicationCounts.First(va => va.Key == long.Parse(vr)).Value;
var vacancyTitle = msg.DataItems.Single(di => di.Key == CommunicationConstants.DataItemKeys.Vacancy.VacancyTitle).Value;
var employerName = msg.DataItems.Single(di => di.Key == CommunicationConstants.DataItemKeys.Vacancy.VacancyTitle).Value;

sb.AppendLine($"VAC{vr}: {vacancyTitle}");
sb.AppendLine(employerName);
sb.AppendLine($"{applicationCount} new {"application".ToQuantity(applicationCount, ShowQuantityAs.None)}");
sb.AppendLine();
sb.AppendLine("---");
sb.AppendLine();
}

return sb.ToString();
}

private string GetNoApplicationsSubmittedHeader(IEnumerable<CommunicationMessage> messages, List<string> vacancyRefs)
{
var totalNoOfVacancies = vacancyRefs.Count();
var totalNoOfApplications = messages.Count();

var header = $"There has been {totalNoOfApplications} new {"application".ToQuantity(totalNoOfApplications, ShowQuantityAs.None)} for {totalNoOfVacancies} {"vacancy".ToQuantity(totalNoOfVacancies, ShowQuantityAs.None)}";
return header;
}

private List<string> GetUniqueVacancyRefs(IEnumerable<CommunicationMessage> messages)
{
return messages
.SelectMany(m => m.DataItems
.Where(di => di.Key == CommunicationConstants.DataItemKeys.Vacancy.VacancyReference)
.Select(di => di.Value)
)
.Distinct()
.ToList();
}

private IEnumerable<KeyValuePair<long, int>> GetUniqueVacanciesAndNewApplicationCounts(IEnumerable<CommunicationMessage> messages, List<string> vacancyRefs)
{
var vacancyRefApplicationCounts = messages
.SelectMany(m => m.DataItems
.Where(di => di.Key == CommunicationConstants.DataItemKeys.Vacancy.VacancyReference && vacancyRefs.Contains(di.Value))
.GroupBy(di => di.Value)
)
.GroupBy(di => di.Key)
.ToDictionary(
vacancyApplicationGroup => long.Parse(vacancyApplicationGroup.Key),
vacancyApplicationGroup => vacancyApplicationGroup.Count()
);
return vacancyRefApplicationCounts;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TemplateIdProviderPlugin : ITemplateIdProvider
{
public string ProviderServiceName => CommunicationConstants.ServiceName;

public Task<string> GetTemplateId(CommunicationMessage message, bool isAggregate = false)
public Task<string> GetTemplateIdAsync(CommunicationMessage message)
{
var templateId = string.Empty;
switch(message.RequestType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ItemGroup>
<PackageReference Include="CsvHelper" Version="12.1.2" />
<PackageReference Include="HtmlSanitizer" Version="4.0.210" />
<PackageReference Include="Humanizer.Core.uk" Version="2.6.2" />
<PackageReference Include="MediatR" Version="4.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="4.0.0" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.1.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public class TemplateIdProviderPluginTests
public async Task ReturnRespectiveTemplateId(string requestType, DeliveryFrequency frequency, string expectedTemplateId)
{
var plugin = new TemplateIdProviderPlugin();

var message = new CommunicationMessage()
{
RequestType = requestType,
Frequency = frequency
};

var templateId = await plugin.GetTemplateId(message);
var templateId = await plugin.GetTemplateIdAsync(message);

templateId.Should().Be(expectedTemplateId);
}
Expand Down

0 comments on commit 2032585

Please sign in to comment.