-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mapping from alert events to webhook data (#1715)
- Loading branch information
Showing
28 changed files
with
3,881 additions
and
60 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240920/Responses/AlertResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
TeachingRecordSystem/src/TeachingRecordSystem.Core/ApiSchema/V3/IEventMapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace TeachingRecordSystem.Core.ApiSchema.V3; | ||
|
||
public interface IEventMapper<TEvent, TData> | ||
where TEvent : EventBase | ||
where TData : IWebhookMessageData | ||
{ | ||
Task<TData?> MapEventAsync(TEvent @event); | ||
} |
6 changes: 6 additions & 0 deletions
6
TeachingRecordSystem/src/TeachingRecordSystem.Core/ApiSchema/V3/IWebhookMessageData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace TeachingRecordSystem.Core.ApiSchema.V3; | ||
|
||
public interface IWebhookMessageData | ||
{ | ||
static abstract string CloudEventType { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
.../src/TeachingRecordSystem.Core/ApiSchema/V3/VNext/WebhookData/AlertCreatedNotification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using TeachingRecordSystem.Core.ApiSchema.V3.V20240920.Dtos; | ||
|
||
namespace TeachingRecordSystem.Core.ApiSchema.V3.VNext.WebhookData; | ||
|
||
public record AlertCreatedNotification : IWebhookMessageData | ||
{ | ||
public static string CloudEventType { get; } = "alert.created"; | ||
|
||
public required string Trn { get; init; } | ||
public required Alert Alert { get; init; } | ||
} | ||
|
||
public class AlertCreatedNotificationMapper(PersonInfoCache personInfoCache, ReferenceDataCache referenceDataCache) : | ||
IEventMapper<AlertCreatedEvent, AlertCreatedNotification> | ||
{ | ||
public async Task<AlertCreatedNotification?> MapEventAsync(AlertCreatedEvent @event) | ||
{ | ||
if (@event.Alert.AlertTypeId is not Guid alertTypeId) | ||
{ | ||
throw new NotSupportedException("Alert does not have an AlertType."); | ||
} | ||
|
||
var alertType = await referenceDataCache.GetAlertTypeByIdAsync(alertTypeId); | ||
if (alertType.InternalOnly) | ||
{ | ||
return null; | ||
} | ||
|
||
var person = await personInfoCache.GetPersonInfoAsync(@event.PersonId); | ||
var alert = await Alert.FromEventAsync(@event.Alert, referenceDataCache); | ||
|
||
return new() | ||
{ | ||
Trn = person.Trn, | ||
Alert = alert | ||
}; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../src/TeachingRecordSystem.Core/ApiSchema/V3/VNext/WebhookData/AlertDeletedNotification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using TeachingRecordSystem.Core.ApiSchema.V3.V20240920.Dtos; | ||
|
||
namespace TeachingRecordSystem.Core.ApiSchema.V3.VNext.WebhookData; | ||
|
||
public record AlertDeletedNotification : IWebhookMessageData | ||
{ | ||
public static string CloudEventType { get; } = "alert.deleted"; | ||
|
||
public required string Trn { get; init; } | ||
public required Alert Alert { get; init; } | ||
} | ||
|
||
public class AlertDeletedNotificationMapper(PersonInfoCache personInfoCache, ReferenceDataCache referenceDataCache) : | ||
IEventMapper<AlertDeletedEvent, AlertDeletedNotification> | ||
{ | ||
public async Task<AlertDeletedNotification?> MapEventAsync(AlertDeletedEvent @event) | ||
{ | ||
if (@event.Alert.AlertTypeId is not Guid alertTypeId) | ||
{ | ||
throw new NotSupportedException("Alert does not have an AlertType."); | ||
} | ||
|
||
var alertType = await referenceDataCache.GetAlertTypeByIdAsync(alertTypeId); | ||
if (alertType.InternalOnly) | ||
{ | ||
return null; | ||
} | ||
|
||
var person = await personInfoCache.GetPersonInfoAsync(@event.PersonId); | ||
var alert = await Alert.FromEventAsync(@event.Alert, referenceDataCache); | ||
|
||
return new() | ||
{ | ||
Trn = person.Trn, | ||
Alert = alert | ||
}; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
.../src/TeachingRecordSystem.Core/ApiSchema/V3/VNext/WebhookData/AlertUpdatedNotification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using TeachingRecordSystem.Core.ApiSchema.V3.V20240920.Dtos; | ||
|
||
namespace TeachingRecordSystem.Core.ApiSchema.V3.VNext.WebhookData; | ||
|
||
public record AlertUpdatedNotification : IWebhookMessageData | ||
{ | ||
public static string CloudEventType { get; } = "alert.updated"; | ||
|
||
public required string Trn { get; init; } | ||
public required Alert Alert { get; init; } | ||
} | ||
|
||
public class AlertUpdatedNotificationMapper(PersonInfoCache personInfoCache, ReferenceDataCache referenceDataCache) : | ||
IEventMapper<AlertUpdatedEvent, AlertUpdatedNotification> | ||
{ | ||
public async Task<AlertUpdatedNotification?> MapEventAsync(AlertUpdatedEvent @event) | ||
{ | ||
if ((@event.Changes & (AlertUpdatedEventChanges.DqtSpent | AlertUpdatedEventChanges.DqtSanctionCode)) != 0) | ||
{ | ||
throw new NotSupportedException("Events originating from DQT are not supported."); | ||
} | ||
|
||
if (@event.Alert.AlertTypeId is not Guid alertTypeId) | ||
{ | ||
throw new NotSupportedException("Alert does not have an AlertType."); | ||
} | ||
|
||
var alertType = await referenceDataCache.GetAlertTypeByIdAsync(alertTypeId); | ||
if (alertType.InternalOnly) | ||
{ | ||
return null; | ||
} | ||
|
||
// We don't expose 'ExternalLink' so if that's the only thing that's changed then don't create a message | ||
if (@event.Changes == AlertUpdatedEventChanges.ExternalLink) | ||
{ | ||
return null; | ||
} | ||
|
||
var person = await personInfoCache.GetPersonInfoAsync(@event.PersonId); | ||
var alert = await Alert.FromEventAsync(@event.Alert, referenceDataCache); | ||
|
||
return new() | ||
{ | ||
Trn = person.Trn, | ||
Alert = alert | ||
}; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...VNext/WebhookPayloads/PingNotification.cs → .../V3/VNext/WebhookData/PingNotification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
.../TeachingRecordSystem.Core/ApiSchema/V3/VNext/WebhookPayloads/AlertCreatedNotification.cs
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
.../TeachingRecordSystem.Core/ApiSchema/V3/VNext/WebhookPayloads/AlertDeletedNotification.cs
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
.../TeachingRecordSystem.Core/ApiSchema/V3/VNext/WebhookPayloads/AlertUpdatedNotification.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.