Skip to content

Commit

Permalink
Handle exemptions without a reason
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad committed Feb 7, 2025
1 parent 7da6bf3 commit 9620652
Show file tree
Hide file tree
Showing 11 changed files with 4,181 additions and 8 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TeachingRecordSystem.Core.DataStore.Postgres.Migrations
{
/// <inheritdoc />
public partial class InductionExemptWithoutReason : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "induction_exempt_without_reason",
table: "persons",
type: "boolean",
nullable: false,
defaultValue: false);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "induction_exempt_without_reason",
table: "persons");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("date")
.HasColumnName("induction_completed_date");

b.Property<bool>("InductionExemptWithoutReason")
.HasColumnType("boolean")
.HasColumnName("induction_exempt_without_reason");

b.Property<Guid[]>("InductionExemptionReasonIds")
.IsRequired()
.HasColumnType("uuid[]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Person
public InductionStatus InductionStatus { get; private set; }
public InductionStatus InductionStatusWithoutExemption { get; private set; }
public Guid[] InductionExemptionReasonIds { get; private set; } = [];
public bool InductionExemptWithoutReason { get; internal set; } // internally set-able for testing
public DateOnly? InductionStartDate { get; private set; }
public DateOnly? InductionCompletedDate { get; private set; }
public DateTime? InductionModifiedOn { get; private set; }
Expand Down Expand Up @@ -158,12 +159,15 @@ public void SetInductionStatus(
InductionExemptionReasonIds = [];
}

InductionExemptWithoutReason = false;

var changes = PersonInductionUpdatedEventChanges.None |
(InductionStatus != oldEventInduction.Status ? PersonInductionUpdatedEventChanges.InductionStatus : 0) |
(InductionStatusWithoutExemption != oldEventInduction.StatusWithoutExemption ? PersonInductionUpdatedEventChanges.InductionStatusWithoutExemption : 0) |
(InductionStartDate != oldEventInduction.StartDate ? PersonInductionUpdatedEventChanges.InductionStartDate : 0) |
(InductionCompletedDate != oldEventInduction.CompletedDate ? PersonInductionUpdatedEventChanges.InductionCompletedDate : 0) |
(!InductionExemptionReasonIds.ToHashSet().SetEquals(oldEventInduction.ExemptionReasonIds) ? PersonInductionUpdatedEventChanges.InductionExemptionReasons : 0);
(!InductionExemptionReasonIds.ToHashSet().SetEquals(oldEventInduction.ExemptionReasonIds) ? PersonInductionUpdatedEventChanges.InductionExemptionReasons : 0) |
(InductionExemptWithoutReason != oldEventInduction.InductionExemptWithoutReason ? PersonInductionUpdatedEventChanges.InductionExemptWithoutReason : 0);

if (changes == PersonInductionUpdatedEventChanges.None)
{
Expand Down Expand Up @@ -249,7 +253,7 @@ public bool RemoveInductionExemptionReason(
InductionExemptionReasonIds = InductionExemptionReasonIds.Except([exemptionReasonId]).ToArray();
InductionModifiedOn = now;

if (InductionStatus is InductionStatus.Exempt && InductionExemptionReasonIds.Length == 0)
if (InductionStatus is InductionStatus.Exempt && (InductionExemptionReasonIds.Length == 0 && !InductionExemptWithoutReason))
{
InductionStatus = InductionStatusWithoutExemption;
changes |= PersonInductionUpdatedEventChanges.InductionStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public record Induction
public required DateOnly? CompletedDate { get; init; }
public required Guid[] ExemptionReasonIds { get; init; }
public required Option<DateTime> CpdCpdModifiedOn { get; init; }
public required bool InductionExemptWithoutReason { get; init; }

public static Induction FromModel(Person person) => new()
{
Expand All @@ -19,6 +20,7 @@ public record Induction
StartDate = person.InductionStartDate,
CompletedDate = person.InductionCompletedDate,
ExemptionReasonIds = person.InductionExemptionReasonIds,
CpdCpdModifiedOn = person.CpdInductionCpdModifiedOn.ToOption()
CpdCpdModifiedOn = person.CpdInductionCpdModifiedOn.ToOption(),
InductionExemptWithoutReason = person.InductionExemptWithoutReason
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public enum PersonInductionUpdatedEventChanges
InductionCompletedDate = 1 << 2,
InductionExemptionReasons = 1 << 3,
InductionStatusWithoutExemption = 1 << 7,
InductionExemptWithoutReason = 1 << 8
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table trs_persons add induction_exemption_without_reason bit default 0
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public async Task SyncAuditAsync(
InductionStartDate = induction?.dfeta_StartDate.ToDateOnlyWithDqtBstFix(isLocalTime: true),
InductionCompletedDate = induction?.dfeta_CompletionDate.ToDateOnlyWithDqtBstFix(isLocalTime: true),
InductionExemptionReasonIds = exemptionReasonIds,
DqtModifiedOn = induction?.ModifiedOn
DqtModifiedOn = induction?.ModifiedOn,
InductionExemptWithoutReason = contact.dfeta_InductionStatus.ToInductionStatus() is InductionStatus.Exempt && exemptionReasonIds.Length == 0
};
}

Expand Down Expand Up @@ -1109,7 +1110,8 @@ private static ModelTypeSyncInfo GetModelTypeSyncInfoForInduction()
"induction_start_date",
"induction_status",
"induction_modified_on",
"dqt_induction_modified_on"
"dqt_induction_modified_on",
"induction_exempt_without_reason"
};

var columnsToUpdate = columnNames.Except(new[] { "person_id" }).ToArray();
Expand All @@ -1126,7 +1128,8 @@ CREATE TEMP TABLE {tempTableName}
induction_start_date date,
induction_status integer,
induction_modified_on timestamp with time zone,
dqt_induction_modified_on timestamp with time zone
dqt_induction_modified_on timestamp with time zone,
induction_exempt_without_reason boolean
)
""";

Expand Down Expand Up @@ -1166,6 +1169,7 @@ RETURNING t.person_id
writer.WriteValueOrNull((int?)induction.InductionStatus, NpgsqlDbType.Integer);
writer.WriteValueOrNull(induction.DqtModifiedOn, NpgsqlDbType.TimestampTz);
writer.WriteValueOrNull(induction.DqtModifiedOn, NpgsqlDbType.TimestampTz);
writer.WriteValueOrNull(induction.InductionExemptWithoutReason, NpgsqlDbType.Boolean);
};

return new ModelTypeSyncInfo<InductionInfo>()
Expand Down Expand Up @@ -1627,6 +1631,7 @@ private record InductionInfo
public required DateOnly? InductionStartDate { get; init; }
public required InductionStatus InductionStatus { get; init; }
public required DateTime? DqtModifiedOn { get; init; }
public required bool InductionExemptWithoutReason { get; init; }
}

private record AuditInfo<TEntity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<EmbeddedResource Include="Services\DqtReporting\Migrations\0040_PersonInductionExemptionReasons.sql" />
<EmbeddedResource Include="Services\DqtReporting\Migrations\0041_ProfessionalStatus.sql" />
<EmbeddedResource Include="Services\DqtReporting\Migrations\0042_InductionStatusWithoutExemption.sql" />
<EmbeddedResource Include="Services\DqtReporting\Migrations\0043_InductionExemptWithoutReason.sql" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,37 @@ public void RemoveInductionExemptionReason_StatusIsHigherPriorityToExempt_DoesNo
Assert.Equal(currentStatus, person.InductionStatus);
}

[Fact]
public void RemoveInductionExemptionReason_PersonIsAlsoExemptWithoutReason_DoesNotChangeStatus()
{
// Arrange
var person = CreatePerson();

person.SetInductionStatus(
InductionStatus.Exempt,
startDate: null,
completedDate: null,
exemptionReasonIds: [InductionExemptionReason.QtlsId],
changeReason: null,
changeReasonDetail: null,
evidenceFile: null,
updatedBy: SystemUser.SystemUserId,
now: Clock.UtcNow,
out _);

person.InductionExemptWithoutReason = true;

// Act
person.RemoveInductionExemptionReason(
InductionExemptionReason.QtlsId,
updatedBy: SystemUser.SystemUserId,
now: Clock.UtcNow,
out _);

// Assert
Assert.Equal(InductionStatus.Exempt, person.InductionStatus);
}

private Person CreatePerson() => new Person
{
PersonId = Guid.NewGuid(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ public async Task Person_WithPersonInductionUpdatedEvent_RendersExpectedContent(
Status = changes.HasFlag(PersonInductionUpdatedEventChanges.InductionStatus) && !newValueIsDefault ? inductionStatus : InductionStatus.None,
StatusWithoutExemption = InductionStatus.RequiredToComplete,
ExemptionReasonIds = changes.HasFlag(PersonInductionUpdatedEventChanges.InductionExemptionReasons) && !newValueIsDefault ? exemptionReasons : [],
CpdCpdModifiedOn = Option.None<DateTime>()
CpdCpdModifiedOn = Option.None<DateTime>(),
InductionExemptWithoutReason = false
};

var oldInduction = new EventModels.Induction
Expand All @@ -600,7 +601,8 @@ public async Task Person_WithPersonInductionUpdatedEvent_RendersExpectedContent(
Status = changes.HasFlag(PersonInductionUpdatedEventChanges.InductionStatus) && !previousValueIsDefault ? oldInductionStatus : InductionStatus.None,
StatusWithoutExemption = InductionStatus.RequiredToComplete,
ExemptionReasonIds = changes.HasFlag(PersonInductionUpdatedEventChanges.InductionExemptionReasons) && !previousValueIsDefault ? oldExemptionReasons : [],
CpdCpdModifiedOn = Option.None<DateTime>()
CpdCpdModifiedOn = Option.None<DateTime>(),
InductionExemptWithoutReason = false
};

var updatedEvent = new PersonInductionUpdatedEvent
Expand Down

0 comments on commit 9620652

Please sign in to comment.