Skip to content

Commit

Permalink
Merge pull request #2125 from ytqsl/stories/EMBCESSMOD-5265
Browse files Browse the repository at this point in the history
EMBCESSMOD-5265: changed groceries and shelter allowance self serve support structure to match UI changes
  • Loading branch information
ytqsl authored May 9, 2024
2 parents ebe15c9 + c57ccdf commit 3d9822f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Mapping()
;

CreateMap<SelfServeFoodGroceriesSupport, FoodGroceriesSupport>()
.ForMember(d => d.IncludedHouseholdMembers, opts => opts.MapFrom(s => s.Nights.SelectMany(n => n.IncludedHouseholdMembers).Distinct()))
.ForMember(d => d.IncludedHouseholdMembers, opts => opts.MapFrom(s => s.IncludedHouseholdMembers))
.ForMember(d => d.NumberOfDays, opts => opts.MapFrom(s => s.Nights.Count()))
.ForMember(d => d.ApproverName, opts => opts.Ignore())
;
Expand All @@ -100,7 +100,7 @@ public Mapping()
;

CreateMap<SelfServeShelterAllowanceSupport, ShelterAllowanceSupport>()
.ForMember(d => d.IncludedHouseholdMembers, opts => opts.MapFrom(s => s.Nights.SelectMany(n => n.IncludedHouseholdMembers).Distinct()))
.ForMember(d => d.IncludedHouseholdMembers, opts => opts.MapFrom(s => s.IncludedHouseholdMembers))
.ForMember(d => d.NumberOfNights, opts => opts.MapFrom(s => s.Nights.Count()))
.ForMember(d => d.ContactEmail, opts => opts.Ignore())
.ForMember(d => d.ContactPhone, opts => opts.Ignore())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ private static SelfServeShelterAllowanceSupport CreateShelterAllowanceSupport(Da
{
var support = new SelfServeShelterAllowanceSupport
{
Nights = CreateSupportDays(from, to).Select(d => new SupportDay(DateOnly.FromDateTime(d), householdMembers.Select(hm => hm.Id).ToList())).ToList(),
Nights = CreateSupportDays(from, to).Select(DateOnly.FromDateTime).ToList(),
IncludedHouseholdMembers = householdMembers.Select(hm => hm.Id).ToList(),
TotalAmount = 0d
};
support.TotalAmount = CalculateSelfServeSupportAmount(support, householdMembers);
Expand All @@ -106,35 +107,30 @@ private static SelfServeShelterAllowanceSupport CreateShelterAllowanceSupport(Da

private static double CalculateSelfServeSupportAmount(SelfServeShelterAllowanceSupport support, IEnumerable<SelfServeHouseholdMember> householdMembers)
{
var amount = 0d;
var hmList = householdMembers.OrderByDescending(hm => hm.IsMinor).ToList();
foreach (var hmId in support.Nights.SelectMany(d => d.IncludedHouseholdMembers))
{
if (amount.Equals(0d))
{
// first occupant
amount = 30d;
continue;
}
var hm = hmList.Find(i => i.Id == hmId)!;
// adult or minor additional amount
amount += hm.IsMinor ? 5d : 10d;
}
return amount;
var hmList = householdMembers.ToList();
var numberOfNights = support.Nights.Count();
var numberOfAdults = hmList.Count(hm => !hm.IsMinor);
var numberOfMinors = hmList.Count(hm => hm.IsMinor);

//compensate for first adult
if (numberOfAdults >= 1) numberOfAdults--;

return (30d + numberOfAdults * 10d + numberOfMinors * 5d) * numberOfNights;
}

private static SelfServeFoodGroceriesSupport CreateSelfServeFoodGroceriesSupport(DateTime from, DateTime to, IEnumerable<SelfServeHouseholdMember> householdMembers)
{
var support = new SelfServeFoodGroceriesSupport
{
Nights = CreateSupportDays(from, to).Select(d => new SupportDay(DateOnly.FromDateTime(d), householdMembers.Select(hm => hm.Id).ToList())).ToList(),
Nights = CreateSupportDays(from, to).Select(DateOnly.FromDateTime).ToList(),
IncludedHouseholdMembers = householdMembers.Select(hm => hm.Id).ToList(),
TotalAmount = 0d
};
support.TotalAmount = CalculateSelfServeSupportAmount(support);
return support;
}

private static double CalculateSelfServeSupportAmount(SelfServeFoodGroceriesSupport support) => support.Nights.Aggregate(0d, (amount, night) => amount + night.IncludedHouseholdMembers.Count() * 22.5d);
private static double CalculateSelfServeSupportAmount(SelfServeFoodGroceriesSupport support) => support.IncludedHouseholdMembers.Count() * support.Nights.Count() * 22.5d;

private static SelfServeFoodRestaurantSupport CreateSelfServeFoodRestaurantSupport(DateTime from, DateTime to, IEnumerable<SelfServeHouseholdMember> householdMembers)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public async Task ProcessSelfServeSupports_Supports_SupportsCreated()
var from = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
var fromDay = DateOnly.FromDateTime(from);
var householdMembers = file.HouseholdMembers.Select(hm => hm.Id);
var supportDays = (new[] { fromDay, fromDay.AddDays(1), fromDay.AddDays(2) }).Select(d => new SupportDay(d, householdMembers));
var supportDays = (new[] { fromDay, fromDay.AddDays(1), fromDay.AddDays(2) });
var etransferDetails = new ETransferDetails
{
ContactEmail = registrant.Email,
Expand All @@ -460,8 +460,8 @@ public async Task ProcessSelfServeSupports_Supports_SupportsCreated()
{
new SelfServeClothingSupport{ TotalAmount = 100d, IncludedHouseholdMembers = householdMembers },
new SelfServeClothingSupport{ TotalAmount = 100d, IncludedHouseholdMembers = householdMembers },
new SelfServeFoodGroceriesSupport{ TotalAmount = 100d, Nights = supportDays },
new SelfServeShelterAllowanceSupport{ TotalAmount = 100d, Nights = supportDays },
new SelfServeFoodGroceriesSupport{ TotalAmount = 100d, Nights = supportDays, IncludedHouseholdMembers = householdMembers },
new SelfServeShelterAllowanceSupport{ TotalAmount = 100d, Nights = supportDays, IncludedHouseholdMembers = householdMembers },
};

await manager.Handle(new ProcessSelfServeSupportsCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ public SelfServeSupportGenerationTests()
public async Task Generate_ShelterAllowance_Created()
{
var support = await GenerateSelfServeSupports<SelfServeShelterAllowanceSupport>(IdentifiedNeed.ShelterAllowance);
support.Nights.Select(n => n.Date).ShouldBe(expectedDays);
foreach (var night in support.Nights)
{
night.IncludedHouseholdMembers.ShouldBe(expectedHouseholdMemberIds);
}
support.TotalAmount.ShouldBe(170d);
support.Nights.ShouldBe(expectedDays);
support.IncludedHouseholdMembers.ShouldBe(expectedHouseholdMemberIds);
support.TotalAmount.ShouldBe(210d);
}

[Fact]
public async Task Generate_ShelterAllowanceWithMinors_Created()
{
var householdMembersWithMinors = householdMembers.ToList();
householdMembersWithMinors[4] = new SelfServeHouseholdMember(householdMembersWithMinors[4].Id, true);
var support = await GenerateSelfServeSupports<SelfServeShelterAllowanceSupport>(IdentifiedNeed.ShelterAllowance, householdMembersWithMinors);
support.Nights.ShouldBe(expectedDays);
support.IncludedHouseholdMembers.ShouldBe(expectedHouseholdMemberIds);
support.TotalAmount.ShouldBe(195d);
}

[Fact]
Expand All @@ -67,11 +75,9 @@ public async Task Generate_FoodGroceries_Created()
response.Supports.Count().ShouldBe(2);

var groceries = (SelfServeFoodGroceriesSupport)response.Supports.Single(s => s is SelfServeFoodGroceriesSupport);
groceries.Nights.Select(n => n.Date).ShouldBe(expectedDays);
foreach (var night in groceries.Nights)
{
night.IncludedHouseholdMembers.ShouldBe(expectedHouseholdMemberIds);
}
groceries.Nights.ShouldBe(expectedDays);
groceries.IncludedHouseholdMembers.ShouldBe(expectedHouseholdMemberIds);

groceries.TotalAmount.ShouldBe(337.5d);

var restaurant = (SelfServeFoodRestaurantSupport)response.Supports.Single(s => s is SelfServeFoodRestaurantSupport);
Expand All @@ -89,9 +95,9 @@ public async Task Generate_FoodGroceries_Created()
restaurant.TotalAmount.ShouldBe(795d);
}

private async Task<T> GenerateSelfServeSupports<T>(IdentifiedNeed forNeed) where T : SelfServeSupport
private async Task<T> GenerateSelfServeSupports<T>(IdentifiedNeed forNeed, IEnumerable<SelfServeHouseholdMember>? overrideHouseholdMembers = null) where T : SelfServeSupport
{
var response = (GenerateSelfServeSupportsResponse)await strategy.Generate(new GenerateSelfServeSupports([forNeed], startDate, endDate, startDate, endDate, householdMembers), default);
var response = (GenerateSelfServeSupportsResponse)await strategy.Generate(new GenerateSelfServeSupports([forNeed], startDate, endDate, startDate, endDate, overrideHouseholdMembers ?? this.householdMembers), default);
return response.Supports.ShouldHaveSingleItem().ShouldBeOfType<T>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public record EligibilityCheck

public record DraftSupports
{
public IEnumerable<SelfServeSupport> Items { get; set; } = Array.Empty<SelfServeSupport>();
public IEnumerable<HouseholdMember> HouseholdMembers { get; set; } = Array.Empty<HouseholdMember>();
public IEnumerable<SelfServeSupport> Items { get; set; } = [];
public IEnumerable<HouseholdMember> HouseholdMembers { get; set; } = [];
}

[JsonDerivedType(typeof(SelfServeShelterAllowanceSupport), typeDiscriminator: nameof(SelfServeShelterAllowanceSupport))]
Expand All @@ -135,26 +135,22 @@ public abstract record SelfServeSupport

public record SelfServeShelterAllowanceSupport : SelfServeSupport
{
public IEnumerable<SupportDay> Nights { get; set; } = Array.Empty<SupportDay>();
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = [];
public IEnumerable<DateOnly> Nights { get; set; } = [];
public override SelfServeSupportType Type => SelfServeSupportType.ShelterAllowance;
}

public record SupportDay
{
public DateOnly Date { get; set; }
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = Array.Empty<string>();
}

public record SelfServeFoodGroceriesSupport : SelfServeSupport
{
public IEnumerable<SupportDay> Nights { get; set; } = Array.Empty<SupportDay>();
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = [];
public IEnumerable<DateOnly> Nights { get; set; } = [];
public override SelfServeSupportType Type => SelfServeSupportType.FoodGroceries;
}

public record SelfServeFoodRestaurantSupport : SelfServeSupport
{
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = Array.Empty<string>();
public IEnumerable<SupportDayMeals> Meals { get; set; } = Array.Empty<SupportDayMeals>();
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = [];
public IEnumerable<SupportDayMeals> Meals { get; set; } = [];
public override SelfServeSupportType Type => SelfServeSupportType.FoodRestaurant;
}

Expand All @@ -168,13 +164,13 @@ public record SupportDayMeals

public record SelfServeIncidentalsSupport : SelfServeSupport
{
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = Array.Empty<string>();
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = [];
public override SelfServeSupportType Type => SelfServeSupportType.Incidentals;
}

public record SelfServeClothingSupport : SelfServeSupport
{
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = Array.Empty<string>();
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = [];
public override SelfServeSupportType Type => SelfServeSupportType.Clothing;
}

Expand Down
3 changes: 0 additions & 3 deletions registrants/src/API/EMBC.Registrants.API/Mappers/Mappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,6 @@ public Mappings()
CreateMap<ESS.Shared.Contracts.Events.SelfServe.SelfServeFoodRestaurantSupport, SelfServeFoodRestaurantSupport>()
.ReverseMap();

CreateMap<ESS.Shared.Contracts.Events.SelfServe.SupportDay, SupportDay>()
.ReverseMap();

CreateMap<ESS.Shared.Contracts.Events.SelfServe.SupportDayMeals, SupportDayMeals>()
.ReverseMap();

Expand Down
20 changes: 10 additions & 10 deletions shared/src/EMBC.ESS.Shared.Contracts/Events/SelfServeSupports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public record DraftSelfServeSupportQuery : Query<DraftSelfServeSupportQueryRespo

public record DraftSelfServeSupportQueryResponse
{
public IEnumerable<HouseholdMember> HouseholdMembers { get; set; } = Array.Empty<HouseholdMember>();
public IEnumerable<SelfServeSupport> Items { get; set; } = Array.Empty<SelfServeSupport>();
public IEnumerable<HouseholdMember> HouseholdMembers { get; set; } = [];
public IEnumerable<SelfServeSupport> Items { get; set; } = [];
}

public record ProcessSelfServeSupportsCommand : Command
{
public string EvacuationFileId { get; set; }
public IEnumerable<SelfServeSupport> Supports { get; set; } = Array.Empty<SelfServeSupport>();
public IEnumerable<SelfServeSupport> Supports { get; set; } = [];
public ETransferDetails ETransferDetails { get; set; }
}

Expand All @@ -55,20 +55,20 @@ public abstract record SelfServeSupport

public record SelfServeShelterAllowanceSupport : SelfServeSupport
{
public IEnumerable<SupportDay> Nights { get; set; } = Array.Empty<SupportDay>();
public IEnumerable<string> IncludedHouseholdMembers { get; set; }
public IEnumerable<DateOnly> Nights { get; set; } = [];
}

public record SupportDay(DateOnly Date, IEnumerable<string> IncludedHouseholdMembers);

public record SelfServeFoodGroceriesSupport : SelfServeSupport
{
public IEnumerable<SupportDay> Nights { get; set; } = Array.Empty<SupportDay>();
public IEnumerable<string> IncludedHouseholdMembers { get; set; }
public IEnumerable<DateOnly> Nights { get; set; } = [];
}

public record SelfServeFoodRestaurantSupport : SelfServeSupport
{
public IEnumerable<string> IncludedHouseholdMembers { get; set; }
public IEnumerable<SupportDayMeals> Meals { get; set; } = Array.Empty<SupportDayMeals>();
public IEnumerable<SupportDayMeals> Meals { get; set; } = [];
}
public record SupportDayMeals(DateOnly Date)
{
Expand All @@ -79,12 +79,12 @@ public record SupportDayMeals(DateOnly Date)

public record SelfServeIncidentalsSupport : SelfServeSupport
{
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = Array.Empty<string>();
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = [];
}

public record SelfServeClothingSupport : SelfServeSupport
{
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = Array.Empty<string>();
public IEnumerable<string> IncludedHouseholdMembers { get; set; } = [];
}

public record ETransferDetails
Expand Down

0 comments on commit 3d9822f

Please sign in to comment.