Skip to content

Commit

Permalink
Add mapping from alert events to webhook data (#1715)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Nov 27, 2024
1 parent e6f8dd5 commit 16ced51
Show file tree
Hide file tree
Showing 28 changed files with 3,881 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using TeachingRecordSystem.Core.ApiSchema.V3.V20240920.Dtos;
using PersonInfo = TeachingRecordSystem.Core.ApiSchema.V3.V20240920.Dtos.PersonInfo;

namespace TeachingRecordSystem.Api.V3.V20240920.Responses;

Expand Down
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);
}
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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,32 @@ public record Alert
public required string? Details { get; init; }
public required DateOnly? StartDate { get; init; }
public required DateOnly? EndDate { get; init; }

public static async Task<Alert> FromEventAsync(EventModels.Alert alert, ReferenceDataCache referenceDataCache)
{
if (alert.AlertTypeId is null)
{
throw new ArgumentNullException("", nameof(alert.AlertTypeId));
}

var alertType = await referenceDataCache.GetAlertTypeByIdAsync(alert.AlertTypeId!.Value);

return new()
{
AlertId = alert.AlertId,
AlertType = new()
{
AlertTypeId = alertType.AlertTypeId,
Name = alertType.Name,
AlertCategory = new()
{
AlertCategoryId = alertType.AlertCategoryId,
Name = alertType.AlertCategory.Name,
}
},
Details = alert.Details,
StartDate = alert.StartDate,
EndDate = alert.EndDate,
};
}
}
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
};
}
}
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
};
}
}
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
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TeachingRecordSystem.Core.ApiSchema.V3.VNext.WebhookPayloads;
namespace TeachingRecordSystem.Core.ApiSchema.V3.VNext.WebhookData;

public record PingNotification
{
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public static class CacheKeys
public static object GetAllEytsStatuses() => "all_eyts_statuses";

public static object GetSubjectTitleKey(string title) => $"subjects_{title}";

public static object PersonInfo(Guid personId) => $"person_info:{personId}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public void Configure(EntityTypeBuilder<WebhookMessage> builder)
builder.Property(m => m.CloudEventId).HasMaxLength(50);
builder.Property(m => m.CloudEventType).HasMaxLength(100);
builder.Property(m => m.ApiVersion).HasMaxLength(50);
builder.HasIndex(m => m.NextDeliveryAttempt).HasFilter("next_delivery_attempt is not null");
}
}
Loading

0 comments on commit 16ced51

Please sign in to comment.