Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloDinella committed Dec 13, 2024
1 parent 04ee1de commit 448960f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 28 deletions.
18 changes: 10 additions & 8 deletions src/CareTogether.Core/Engines/PolicyEvaluation/ChildLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ public sealed record ChildLocation(
Guid ChildLocationFamilyId,
DateOnly Date,
bool Paused // means "from now on, we stop checking for completion until resuming"
)
: IComparable<ChildLocation>
) : IComparable<ChildLocation>
{
public int CompareTo(ChildLocation? other)
{
return other == null
? 1
: DateTime.Compare(
new DateTime(Date, new TimeOnly()),
new DateTime(other.Date, new TimeOnly())
);
if (other is null)
return 1; // Current instance is greater than null

int dateComparison = Date.CompareTo(other.Date);
if (dateComparison != 0)
return dateComparison;

int familyIdComparison = ChildLocationFamilyId.CompareTo(other.ChildLocationFamilyId);
return familyIdComparison;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ ImmutableList<DateOnly> completionDates
Guid? filterToFamilyId = null
)
{
var dateRanges = GenerateDateRanges(childLocations).ToImmutableList();
var dateRanges = GenerateDateRanges(childLocations, filterToFamilyId).ToImmutableList();

var filteredDateRanges = (
filterToFamilyId != null ? dateRanges.Where(item => item.Tag == filterToFamilyId) : dateRanges
Expand All @@ -517,40 +517,60 @@ ImmutableList<DateOnly> completionDates
return new DateOnlyTimeline(filteredDateRanges);
}

private static IEnumerable<DateRange<Guid>> GenerateDateRanges(ImmutableList<ChildLocation> childLocations)
private static IEnumerable<DateRange<Guid>> GenerateDateRanges(
ImmutableList<ChildLocation> childLocations,
Guid? filterToFamilyId
)
{
(DateOnly, Guid)? entry = null;
(DateOnly Date, Guid ChildLocationFamilyId)? previousChildLocation = null;

foreach (var childLocation in childLocations)
{
if (!entry.HasValue && !childLocation.Paused)
if (!previousChildLocation.HasValue && !childLocation.Paused)
{
entry = (childLocation.Date, childLocation.ChildLocationFamilyId);
previousChildLocation = (childLocation.Date, childLocation.ChildLocationFamilyId);
continue;
}

if (entry.HasValue && !childLocation.Paused)
// if (
// previousChildLocation.HasValue
// && previousChildLocation.Value.Date == childLocation.Date
// && previousChildLocation.Value.ChildLocationFamilyId == childLocation.ChildLocationFamilyId
// )
// {
// continue;
// }

if (previousChildLocation.HasValue && !childLocation.Paused)
{
yield return new DateRange<Guid>(
entry.Value.Item1,
childLocation.Date.AddDays(-1),
entry.Value.Item2
previousChildLocation.Value.Date,
childLocation.Date,
previousChildLocation.Value.ChildLocationFamilyId
);
entry = (childLocation.Date, childLocation.ChildLocationFamilyId);

previousChildLocation = (childLocation.Date, childLocation.ChildLocationFamilyId);
continue;
}

if (entry.HasValue && childLocation.Paused)
if (previousChildLocation.HasValue && childLocation.Paused)
{
yield return new DateRange<Guid>(entry.Value.Item1, childLocation.Date, entry.Value.Item2);
entry = null;
yield return new DateRange<Guid>(
previousChildLocation.Value.Date,
childLocation.Date,
previousChildLocation.Value.ChildLocationFamilyId
);
previousChildLocation = null;
continue;
}
}

if (entry.HasValue)
if (previousChildLocation.HasValue)
{
yield return new DateRange<Guid>(entry.Value.Item1, entry.Value.Item2);
yield return new DateRange<Guid>(
previousChildLocation.Value.Date,
previousChildLocation.Value.ChildLocationFamilyId
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ public sealed record ReferralEntry(
ImmutableDictionary<Guid, ArrangementEntry> Arrangements
);

public sealed record CompletedRequirementInfo(
string RequirementName,
DateOnly CompletedAt,
DateOnly? ExpiresAt
);
public sealed record CompletedRequirementInfo(string RequirementName, DateOnly CompletedAt, DateOnly? ExpiresAt);

public sealed record ExemptedRequirementInfo(
string RequirementName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,68 @@ public void CreateTimelineFilteredByFamilyIdNoPauses()
new DateOnlyTimeline([new DateRange(DateOnly.FromDateTime(H.DateTime(1, 20)))])
);
}

[TestMethod]
public void CreateTimelineFilteredByFamilyIdMultipleChangesInSameDay()
{
var hist = H.ChildLocationHistory(
(H.Id('0'), ChildLocationPlan.WithParent, 1, 1),
(H.Id('1'), ChildLocationPlan.DaytimeChildCare, 1, 10),
(H.Id('2'), ChildLocationPlan.DaytimeChildCare, 1, 10),
(H.Id('1'), ChildLocationPlan.DaytimeChildCare, 1, 10),
(H.Id('0'), ChildLocationPlan.WithParent, 1, 15)
);

var result = ReferralCalculations.CreateChildLocationBasedTimeline(hist.ToImmutableList(), H.Id('1'));

AssertEx.SequenceIs(
result,
new DateOnlyTimeline(
[new DateRange(DateOnly.FromDateTime(H.DateTime(1, 10)), DateOnly.FromDateTime(H.DateTime(1, 15)))]
)
);
}

[TestMethod]
public void CreateTimelineFilteredByFamilyIdMultipleChangesInSameDay2()
{
var hist = H.ChildLocationHistory(
(H.Id('0'), ChildLocationPlan.WithParent, 1, 1),
(H.Id('1'), ChildLocationPlan.DaytimeChildCare, 1, 10),
(H.Id('2'), ChildLocationPlan.DaytimeChildCare, 1, 10),
(H.Id('1'), ChildLocationPlan.DaytimeChildCare, 1, 10),
(H.Id('0'), ChildLocationPlan.WithParent, 1, 15)
);

var result = ReferralCalculations.CreateChildLocationBasedTimeline(hist.ToImmutableList(), H.Id('2'));

AssertEx.SequenceIs(
result,
new DateOnlyTimeline(
[new DateRange(DateOnly.FromDateTime(H.DateTime(1, 10)), DateOnly.FromDateTime(H.DateTime(1, 10)))]
)
);
}

[TestMethod]
public void CreateTimelineMultipleChangesInSameDay()
{
var hist = H.ChildLocationHistory(
(H.Id('0'), ChildLocationPlan.WithParent, 1, 1),
(H.Id('1'), ChildLocationPlan.DaytimeChildCare, 1, 10),
(H.Id('2'), ChildLocationPlan.DaytimeChildCare, 1, 10),
(H.Id('1'), ChildLocationPlan.DaytimeChildCare, 1, 10),
(H.Id('0'), ChildLocationPlan.WithParent, 1, 15)
);

var result = ReferralCalculations.CreateChildLocationBasedTimeline(hist.ToImmutableList());

AssertEx.SequenceIs(
result,
new DateOnlyTimeline(
[new DateRange(DateOnly.FromDateTime(H.DateTime(1, 10)), DateOnly.FromDateTime(H.DateTime(1, 15)))]
)
);
}
}
}
11 changes: 11 additions & 0 deletions test/CareTogether.Core.Test/ReferralCalculationTests/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ public static ImmutableSortedSet<ChildLocation> ChildLocationHistory(
))
.ToImmutableSortedSet();

public static ImmutableList<ChildLocation> ChildLocationHistoryL(
params (Guid childLocationFamilyId, ChildLocationPlan plan, int month, int day)[] values
) =>
values
.Select(value => new ChildLocation(
value.childLocationFamilyId,
DateOnly.FromDateTime(DateTime(value.month, value.day)),
value.plan == ChildLocationPlan.WithParent
))
.ToImmutableList();

public static ArrangementFunction FunctionWithoutEligibility(
string arrangementFunction,
FunctionRequirement requirement
Expand Down

0 comments on commit 448960f

Please sign in to comment.