Skip to content

Commit

Permalink
Add MQ deactivated event to change log (#1062)
Browse files Browse the repository at this point in the history
  • Loading branch information
hortha authored Jan 8, 2024
1 parent 390dcb7 commit 6d074cc
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using TeachingRecordSystem.Core.Events.Models;

namespace TeachingRecordSystem.Core.Events;

public record MandatoryQualificationDqtDeactivatedEvent : EventBase, IEventWithPersonId, IEventWithMandatoryQualification
{
public required Guid PersonId { get; init; }
public required MandatoryQualification MandatoryQualification { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ users as u ON
END = u.user_id
WHERE
e.payload ->> 'PersonId' = {personIdString}
AND e.event_name = 'MandatoryQualificationDeletedEvent'
AND e.event_name in ('MandatoryQualificationDeletedEvent', 'MandatoryQualificationDqtDeactivatedEvent')
""")
.ToListAsync();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@using TeachingRecordSystem.Core.Events
@model TimelineItem<TimelineEvent<MandatoryQualificationDqtDeactivatedEvent>>
@{
var deactivatedEvent = Model.ItemModel.Event;
var mandatoryQualification = deactivatedEvent.MandatoryQualification;
var raisedByUser = Model.ItemModel.RaisedByUser;
var raisedBy = deactivatedEvent.RaisedBy.IsDqtUser ? (raisedByUser.DqtUser?.Name ?? "Unknown") : (raisedByUser.User?.Name) ?? "Unknown";
}

<div class="moj-timeline__item govuk-!-padding-bottom-2" data-testid="timeline-item">
<div class="moj-timeline__header">
<h2 class="moj-timeline__title">Mandatory qualification deactivated</h2>
</div>
<p class="moj-timeline__date">
<span data-testid="raised-by">By @raisedBy on</span>
<time datetime="@deactivatedEvent.CreatedUtc.ToString("O")" data-testid="timeline-item-time">
@deactivatedEvent.CreatedUtc.ToString("dd MMMMM yyyy 'at' h:mm tt")
</time>
</p>
<div class="moj-timeline__description">
<govuk-summary-list>
<govuk-summary-list-row>
<govuk-summary-list-row-key>Training provider</govuk-summary-list-row-key>
<govuk-summary-list-row-value data-testid="provider">@(mandatoryQualification.Provider is not null ? mandatoryQualification.Provider.Name : "None")</govuk-summary-list-row-value>
</govuk-summary-list-row>
<govuk-summary-list-row>
<govuk-summary-list-row-key>Specialism</govuk-summary-list-row-key>
<govuk-summary-list-row-value data-testid="specialism">@(mandatoryQualification.Specialism.HasValue ? mandatoryQualification.Specialism.Value.GetTitle() : "None")</govuk-summary-list-row-value>
</govuk-summary-list-row>
<govuk-summary-list-row>
<govuk-summary-list-row-key>Start date</govuk-summary-list-row-key>
<govuk-summary-list-row-value data-testid="start-date">@(mandatoryQualification.StartDate.HasValue ? mandatoryQualification.StartDate.Value.ToString("d MMMM yyyy") : "None")</govuk-summary-list-row-value>
</govuk-summary-list-row>
<govuk-summary-list-row>
<govuk-summary-list-row-key>Status</govuk-summary-list-row-key>
<govuk-summary-list-row-value data-testid="status">@(mandatoryQualification.Status.HasValue ? mandatoryQualification.Status.Value.GetTitle() : "None")</govuk-summary-list-row-value>
</govuk-summary-list-row>
<govuk-summary-list-row>
<govuk-summary-list-row-key>End date</govuk-summary-list-row-key>
<govuk-summary-list-row-value data-testid="end-date">@(mandatoryQualification.EndDate.HasValue ? mandatoryQualification.EndDate.Value.ToString("d MMMM yyyy") : "None")</govuk-summary-list-row-value>
</govuk-summary-list-row>
</govuk-summary-list>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,91 @@ await TestData.DeleteMandatoryQualification(
Assert.Equal(deletedEvents[0].MandatoryQualification.Status!.Value.GetTitle(), changes[1].GetElementByTestId("status")!.TextContent.Trim());
Assert.Equal(deletedEvents[0].MandatoryQualification.EndDate.HasValue ? deletedEvents[0].MandatoryQualification.EndDate!.Value.ToString("d MMMM yyyy") : "None", changes[1].GetElementByTestId("end-date")!.TextContent.Trim());
}

[Fact]
public async Task Get_WithPersonIdForPersonWithDqtDeactivatedMandatoryQualification_DisplaysChangesAsExpected()
{
// Arrange
var person = await TestData.CreatePerson(b => b.WithMandatoryQualification().WithMandatoryQualification());
var mqs = new (bool RaisedByDqtUser, TestData.MandatoryQualificationInfo Mq, DateTime CreatedUtc)[]
{
(true, person.MandatoryQualifications[0], Clock.UtcNow.AddSeconds(-2)),
(false, person.MandatoryQualifications[1], Clock.UtcNow)
};

var dqtUserId = await TestData.GetCurrentCrmUserId();
var user = await TestData.CreateUser();

var deactivatedEvents = new List<MandatoryQualificationDqtDeactivatedEvent>();

await WithDbContext(async dbContext =>
{
foreach (var mqInfo in mqs)
{
var mq = mqInfo.Mq;
var establishment = mq.DqtMqEstablishmentValue is string establishmentValue ?
await TestData.ReferenceDataCache.GetMqEstablishmentByValue(mq.DqtMqEstablishmentValue) :
null;
Core.DataStore.Postgres.Models.MandatoryQualificationProvider.TryMapFromDqtMqEstablishment(establishment, out var provider);

var deactivatedEvent = new MandatoryQualificationDqtDeactivatedEvent()
{
EventId = Guid.NewGuid(),
CreatedUtc = mqInfo.CreatedUtc,
RaisedBy = mqInfo.RaisedByDqtUser ? RaisedByUserInfo.FromDqtUser(dqtUserId, "Test User") : RaisedByUserInfo.FromUserId(user.UserId),
PersonId = person.ContactId,
MandatoryQualification = new()
{
QualificationId = mq.QualificationId,
Provider = new()
{
MandatoryQualificationProviderId = provider?.MandatoryQualificationProviderId,
Name = provider?.Name,
DqtMqEstablishmentId = establishment?.Id,
DqtMqEstablishmentName = establishment?.dfeta_name
},
Specialism = mq.Specialism,
Status = mq.Status,
StartDate = mq.StartDate,
EndDate = mq.EndDate,
}
};

deactivatedEvents.Add(deactivatedEvent);
dbContext.AddEvent(deactivatedEvent);
}

await dbContext.SaveChangesAsync();
});

var request = new HttpRequestMessage(HttpMethod.Get, $"/persons/{person.ContactId}/changelog");

// Act
var response = await HttpClient.SendAsync(request);

// Assert
Assert.Equal(StatusCodes.Status200OK, (int)response.StatusCode);

var doc = await response.GetDocument();
var changes = doc.GetAllElementsByTestId("timeline-item");
Assert.NotEmpty(changes);
Assert.Equal(2, changes.Count);
Assert.Null(changes[0].GetElementByTestId("timeline-item-status"));
Assert.Equal($"By {user.Name} on", changes[0].GetElementByTestId("raised-by")!.TextContent.Trim());
Assert.NotNull(changes[0].GetElementByTestId("timeline-item-time"));
Assert.Equal(deactivatedEvents[1].MandatoryQualification.Provider!.Name, changes[0].GetElementByTestId("provider")!.TextContent.Trim());
Assert.Equal(deactivatedEvents[1].MandatoryQualification.Specialism!.Value.GetTitle(), changes[0].GetElementByTestId("specialism")!.TextContent.Trim());
Assert.Equal(deactivatedEvents[1].MandatoryQualification.StartDate!.Value.ToString("d MMMM yyyy"), changes[0].GetElementByTestId("start-date")!.TextContent.Trim());
Assert.Equal(deactivatedEvents[1].MandatoryQualification.Status!.Value.GetTitle(), changes[0].GetElementByTestId("status")!.TextContent.Trim());
Assert.Equal(deactivatedEvents[1].MandatoryQualification.EndDate.HasValue ? deactivatedEvents[1].MandatoryQualification.EndDate!.Value.ToString("d MMMM yyyy") : "None", changes[0].GetElementByTestId("end-date")!.TextContent.Trim());

Assert.Null(changes[1].GetElementByTestId("timeline-item-status"));
Assert.Equal($"By Test User on", changes[1].GetElementByTestId("raised-by")!.TextContent.Trim());
Assert.NotNull(changes[0].GetElementByTestId("timeline-item-time"));
Assert.Equal(deactivatedEvents[0].MandatoryQualification.Provider!.Name, changes[1].GetElementByTestId("provider")!.TextContent.Trim());
Assert.Equal(deactivatedEvents[0].MandatoryQualification.Specialism!.Value.GetTitle(), changes[1].GetElementByTestId("specialism")!.TextContent.Trim());
Assert.Equal(deactivatedEvents[0].MandatoryQualification.StartDate!.Value.ToString("d MMMM yyyy"), changes[1].GetElementByTestId("start-date")!.TextContent.Trim());
Assert.Equal(deactivatedEvents[0].MandatoryQualification.Status!.Value.GetTitle(), changes[1].GetElementByTestId("status")!.TextContent.Trim());
Assert.Equal(deactivatedEvents[0].MandatoryQualification.EndDate.HasValue ? deactivatedEvents[0].MandatoryQualification.EndDate!.Value.ToString("d MMMM yyyy") : "None", changes[1].GetElementByTestId("end-date")!.TextContent.Trim());
}
}

0 comments on commit 6d074cc

Please sign in to comment.