Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
vbelinschi committed Dec 12, 2024
1 parent 5aa1cd7 commit 14eceb5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<GetGridAreasResponse> Handle(GetRelevantGridAreasCommand reque
var endDate = request.Period.End.ToDateTimeOffset();

var gridAreas = await _gridAreaRepository.GetAsync().ConfigureAwait(false);
var filteredByDateGridAreas = gridAreas.Where(ga => IsRelevantGridArea(ga, startDate, endDate));
var filteredByDateGridAreas = gridAreas.Where(ga => DoDatesOverlap(ga, startDate, endDate));

var actor = await _actorRepository.GetAsync(new ActorId(_userContext.CurrentUser.ActorId)).ConfigureAwait(false);
NotFoundValidationException.ThrowIfNull(actor, _userContext.CurrentUser.ActorId);
Expand All @@ -65,15 +65,18 @@ public async Task<GetGridAreasResponse> Handle(GetRelevantGridAreasCommand reque
gridArea.ValidTo)));
}

private static bool IsRelevantGridArea(GridArea gridArea, DateTimeOffset startDate, DateTimeOffset endDate)
private static bool DoDatesOverlap(GridArea gridArea, DateTimeOffset startDate, DateTimeOffset endDate)
{
var convertedStartDate = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(startDate, "Romance Standard Time");
var convertedEndDate = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(endDate.AddMilliseconds(-1), "Romance Standard Time");

if (!gridArea.ValidTo.HasValue)
{
return gridArea.ValidFrom <= endDate;
return gridArea.ValidFrom <= convertedEndDate;
}

// formula from https://www.baeldung.com/java-check-two-date-ranges-overlap
var overlap = Math.Min(gridArea.ValidTo.Value.Ticks, endDate.Ticks) - Math.Max(gridArea.ValidFrom.Ticks, startDate.Ticks);
var overlap = Math.Min(gridArea.ValidTo.Value.Ticks, convertedEndDate.Ticks) - Math.Max(gridArea.ValidFrom.Ticks, convertedStartDate.Ticks);
return overlap >= 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public async Task Handle_ValidDates_ReturnsGridAreas(string validFrom, string? v

[Theory]
[InlineData("01/01/2023 00:00:00", "31/12/2025 23:59:59", "01/01/2022 00:00:00", "31/01/2022 23:59:59")]
[InlineData("01/01/2023 00:00:00", "31/12/2025 23:59:59", "01/01/2027 00:00:00", "31/01/2027 23:59:59")]
[InlineData("01/01/2023 00:00:00", "31/12/2025 23:59:59", "01/01/2026 00:00:00", "31/01/2026 23:59:59")]
[InlineData("01/01/2027 00:00:00", null, "31/12/2025 23:59:59", "31/01/2026 23:59:59")]
public async Task Handle_InvalidDates_ReturnsEmpty(string validFrom, string? validTo, string periodStart, string periodEnd)
{
Expand Down

0 comments on commit 14eceb5

Please sign in to comment.