Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,40 @@ public async Task<ActionResult<IEnumerable<SupplierListItem>>> GetSuppliers(stri
}
var suppliers = (await messagingClient.Send(query)).Items;

return Ok(mapper.Map<IEnumerable<SupplierListItem>>(suppliers, opt => opt.Items["UserTeamId"] = teamId));
// Find all team IDs that gave mutual aid
var givenByTeamIds = suppliers
.SelectMany(s => s.MutualAids)
.Where(ma => !string.IsNullOrEmpty(ma.GivenByTeamId))
.Select(ma => ma.GivenByTeamId)
.Distinct()
.ToList();

Dictionary<string, string> teamNames = new Dictionary<string, string>();

if (givenByTeamIds.Any())
{
foreach (var teamIdToLookup in givenByTeamIds)
{
var teamQuery = new SuppliersQuery { TeamId = teamIdToLookup };
var teamSuppliers = (await messagingClient.Send(teamQuery)).Items;

//Extract team information from supplier data
var teamRef = teamSuppliers
.SelectMany(s => s.PrimaryTeams)
.FirstOrDefault(t => t.Id.Equals(teamIdToLookup));

if (teamRef != null)
{
teamNames[teamIdToLookup] = teamRef.Name;
}
}
}

return Ok(mapper.Map<IEnumerable<SupplierListItem>>(suppliers, opt =>
{
opt.Items["UserTeamId"] = teamId;
opt.Items["TeamNames"] = teamNames;
}));
}

/// <summary>
Expand Down Expand Up @@ -296,6 +329,7 @@ public enum SupplierStatus
public class MutualAid
{
public string GivenByTeamId { get; set; }
public string GivenByTeamName { get; set; }
public DateTime GivenOn { get; set; }
public SupplierTeam GivenToTeam { get; set; }
}
Expand All @@ -308,7 +342,11 @@ public SuppliersMapping()
.ForMember(d => d.Status, opts => opts.MapFrom(s => s.Status == ESS.Shared.Contracts.Teams.SupplierStatus.Active ? SupplierStatus.Active : SupplierStatus.Deactivated))
.ForMember(d => d.IsPrimarySupplier, opts => opts.MapFrom((s, dst, arg, context) => context.Items.ContainsKey("UserTeamId") && s.PrimaryTeams.Any(t => t.Id.Equals(context.Items["UserTeamId"]))))
.ForMember(d => d.ProvidesMutualAid, opts => opts.MapFrom((s, dst, arg, context) => context.Items.ContainsKey("UserTeamId") && s.PrimaryTeams.Any(t => t.Id.Equals(context.Items["UserTeamId"])) && s.MutualAids.Any()))
.ForMember(d => d.MutualAid, opts => opts.MapFrom((s, dst, arg, context) => s.MutualAids.SingleOrDefault(ma => context.Items.ContainsKey("UserTeamId") && ma.GivenToTeam != null && ma.GivenToTeam.Equals(context.Items["UserTeamId"]))))
.ForMember(d => d.MutualAid, opts => opts.MapFrom((s, dst, arg, context) =>
s.MutualAids.SingleOrDefault(ma =>
context.Items.ContainsKey("UserTeamId") &&
ma.GivenToTeam != null &&
ma.GivenToTeam.Id.Equals(context.Items["UserTeamId"]))))
;

CreateMap<ESS.Shared.Contracts.Teams.Supplier, Supplier>()
Expand Down Expand Up @@ -346,7 +384,18 @@ public SuppliersMapping()
;

CreateMap<MutualAid, ESS.Shared.Contracts.Teams.MutualAid>();
CreateMap<ESS.Shared.Contracts.Teams.MutualAid, MutualAid>();
CreateMap<ESS.Shared.Contracts.Teams.MutualAid, MutualAid>()
.ForMember(d => d.GivenByTeamName, opts => opts.MapFrom((s, dst, arg, context) =>
{
if (context.Items.ContainsKey("TeamNames"))
{
var teamNames = (Dictionary<string, string>)context.Items["TeamNames"];
if (teamNames.TryGetValue(s.GivenByTeamId, out string teamName))
return teamName;
}

return null;
}));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { SupplierTeam } from '../models/supplier-team';
export interface MutualAid {
givenByTeamId?: string | null;
givenByTeamName?: string | null;
givenOn?: string;
givenToTeam?: SupplierTeam;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
@if (column?.ref === 'mutualAid') {
<span>
<span>{{ row[column?.ref]?.givenToTeam?.name }}</span>
<span>{{ row[column?.ref]?.givenByTeamName }}</span>
</span>
}
@if (column?.ref === 'status') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export class SuppliersTableComponent implements AfterViewInit, OnChanges {
return compare(a.name.toLowerCase(), b.name.toLowerCase(), isAsc);
case 'mutualAid':
return compare(
a.mutualAid?.givenToTeam?.name.toLowerCase(),
b.mutualAid?.givenToTeam?.name.toLowerCase(),
a.mutualAid?.givenByTeamName?.toLowerCase(),
b.mutualAid?.givenByTeamName?.toLowerCase(),
isAsc
);
case 'status':
Expand Down