diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/Endpoints/IdentityWebHooks/GetAnIdentityEndpoints.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/Endpoints/IdentityWebHooks/GetAnIdentityEndpoints.cs index bb97b7c97..99ecca446 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/Endpoints/IdentityWebHooks/GetAnIdentityEndpoints.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/Endpoints/IdentityWebHooks/GetAnIdentityEndpoints.cs @@ -80,6 +80,11 @@ IDistributedLockProvider distributedLockProvider { user = userCreatedMessage.User; } + else if (notification.Message is UserMergedMessage userMergeMessage) + { + await dataverseAdapter.ClearTeacherIdentityInfo(userMergeMessage.MergedUserId, notification.TimeUtc); + return CreateResult(); + } else { return CreateResult(); diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Api/Endpoints/IdentityWebHooks/Messages/UserMergedMessage.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Api/Endpoints/IdentityWebHooks/Messages/UserMergedMessage.cs index 52b9e89b4..fe2e65058 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Api/Endpoints/IdentityWebHooks/Messages/UserMergedMessage.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Api/Endpoints/IdentityWebHooks/Messages/UserMergedMessage.cs @@ -3,4 +3,7 @@ namespace TeachingRecordSystem.Api.Endpoints.IdentityWebHooks.Messages; public class UserMergedMessage : INotificationMessage { public const string MessageTypeName = "UserMerged"; + + public required Guid MergedUserId { get; init; } + public required User MasterUser { get; init; } } diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/Endpoints/IdentityWebHooks/GetAnIdentityEndpointsTests.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/Endpoints/IdentityWebHooks/GetAnIdentityEndpointsTests.cs index 1f298322f..000e7e2e1 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/Endpoints/IdentityWebHooks/GetAnIdentityEndpointsTests.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.Api.Tests/Endpoints/IdentityWebHooks/GetAnIdentityEndpointsTests.cs @@ -156,6 +156,50 @@ public async Task Post_WithInvalidMessageFormat_ThrowsJsonException() await Assert.ThrowsAsync(() => httpClient.SendAsync(request)); } + [Fact] + public async Task Post_WithValidUserMergedMessage_ReturnsNoContent() + { + // Arrange + var now = DateTime.UtcNow; + var masterContactId = Guid.NewGuid(); + var mergedContactId = Guid.NewGuid(); + var content = new + { + NotificationId = Guid.NewGuid(), + TimeUtc = now, + MessageType = UserMergedMessage.MessageTypeName, + Message = new + { + MasterUser = new + { + UserId = masterContactId, + EmailAddress = Faker.Internet.Email(), + Trn = "7654321", + MobileNumber = "07968987654" + }, + MergedUserId = mergedContactId, + Changes = new { } + } + }; + + var jsonContent = JsonSerializer.Serialize(content, GetAnIdentityEndpoints.SerializerOptions); + var signature = GenerateSignature(GetAnIdentityOptions.Value.WebHookClientSecret, jsonContent); + var httpClient = HostFixture.CreateClient(); + httpClient.DefaultRequestHeaders.Add("X-Hub-Signature-256", signature); + + var request = new HttpRequestMessage(HttpMethod.Post, "/webhooks/identity") + { + Content = CreateJsonContent(jsonContent) + }; + + // Act + var response = await httpClient.SendAsync(request); + + // Assert + Assert.Equal(StatusCodes.Status204NoContent, (int)response.StatusCode); + DataverseAdapterMock.Verify(mock => mock.ClearTeacherIdentityInfo(mergedContactId, now)); + } + [Fact] public async Task Post_WithValidUserUpdatedMessage_ReturnsNoContent() {