-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotificationConfigurationService.cs
More file actions
48 lines (38 loc) · 1.84 KB
/
NotificationConfigurationService.cs
File metadata and controls
48 lines (38 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Nhs.Appointments.Core.Bookings;
using Nhs.Appointments.Core.Caching;
namespace Nhs.Appointments.Core.Messaging;
public class NotificationConfigurationService(ICacheService cacheService, INotificationConfigurationStore notificationConfigurationStore) : INotificationConfigurationService
{
private const string CacheKey = "notification_configuration";
private readonly TimeSpan _cacheDuration = TimeSpan.FromMinutes(60);
public async Task<NotificationConfiguration> GetNotificationConfigurationsAsync(string eventType)
{
var config = await LoadNotificationConfiguration();
return config.SingleOrDefault(x => x.EventType == eventType);
}
public async Task<NotificationConfiguration> GetNotificationConfigurationsAsync(string eventType, string service)
{
var config = await LoadNotificationConfiguration();
var matchingConfigs = config
.Where(x => x.EventType == eventType && x.Services.Contains(service))
.ToList();
if (matchingConfigs.Count == 0)
return null;
var combinedConfig = new NotificationConfiguration
{
EventType = eventType,
Services = [ service ],
EmailTemplateId = matchingConfigs.Select(x => x.EmailTemplateId).FirstOrDefault(t => !string.IsNullOrWhiteSpace(t)),
SmsTemplateId = matchingConfigs.Select(x => x.SmsTemplateId).FirstOrDefault(t => !string.IsNullOrWhiteSpace(t))
};
return combinedConfig;
}
private async Task<IEnumerable<NotificationConfiguration>> LoadNotificationConfiguration()
{
return await cacheService.GetCacheValue(
CacheKey,
new CacheOptions<IEnumerable<NotificationConfiguration>>(
notificationConfigurationStore.GetNotificationConfiguration,
_cacheDuration));
}
}