-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserRolesChangedNotifier.cs
More file actions
97 lines (79 loc) · 3.3 KB
/
UserRolesChangedNotifier.cs
File metadata and controls
97 lines (79 loc) · 3.3 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Microsoft.Extensions.Logging;
using Nhs.Appointments.Api.Functions;
using Nhs.Appointments.Core.Bookings;
using Nhs.Appointments.Core.Messaging;
using Nhs.Appointments.Core.Sites;
using Nhs.Appointments.Core.Users;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Nhs.Appointments.Audit.Services;
namespace Nhs.Appointments.Api.Notifications;
// Depends on an email template hosted in Gov Notify. See template.txt for details
public class UserRolesChangedNotifier(
ISendNotifications notificationClient,
INotificationConfigurationService notificationConfigurationService,
IRolesStore rolesStore,
ISiteService siteService,
IAuditWriteService auditWriteService
) : IUserRolesChangedNotifier
{
private const string NotificationRunningUser = "ServiceBusFunctionApp";
public async Task Notify(string eventType, string user, string siteId, string[] rolesAdded, string[] rolesRemoved)
{
var hasRoleChanges = rolesAdded.Any() || rolesRemoved.Any();
if (hasRoleChanges)
{
var roles = await rolesStore.GetRoles();
var rolesAddedNames = GetRoleNames(roles, rolesAdded);
var rolesRemovedNames = GetRoleNames(roles, rolesRemoved);
var site = await siteService.GetSiteByIdAsync(siteId);
var siteName = site == null ? $"Unknown site ({siteId})" : site.Name;
var templateValues = new Dictionary<string, dynamic>
{
{"user", user},
{"rolesAdded", GetRolesAddedText(rolesAddedNames)},
{"rolesRemoved", GetRolesRemovedText(rolesRemovedNames)},
{"site", siteName }
};
var templateConfig = await notificationConfigurationService.GetNotificationConfigurationsAsync(eventType);
var success = await notificationClient.SendEmailAsync(user, templateConfig.EmailTemplateId, templateValues);
if (success)
{
await auditWriteService.RecordNotification(
$"{Guid.NewGuid()}",
DateTime.UtcNow,
NotificationRunningUser,
user,
eventType,
templateConfig.EmailTemplateId,
"Email",
null);
}
}
}
private string[] GetRoleNames(IEnumerable<Role> roles, IEnumerable<string> roleIds)
{
return roles.Where(role =>
roleIds.Contains(role.Id) ||
roleIds.Select(GetRoleIdPortion).Contains(role.Id)
).Select(role => role.Name).ToArray();
}
// we assemble some text here because the Gov Notify templating engine doesn't quite do what we need:
private static string GetRolesAddedText(string[] rolesAdded)
{
if (rolesAdded.Length == 0) return "";
return $"You have been given the permissions of: {string.Join(", ", rolesAdded)}.";
}
private static string GetRolesRemovedText(string[] rolesRemoved)
{
if (rolesRemoved.Length == 0) return "";
return $"You have been removed from: {string.Join(", ", rolesRemoved)}.";
}
private static string GetRoleIdPortion(string roleName)
{
if(roleName.Contains(':')) return roleName.Split(':')[1];
return roleName;
}
}