Skip to content

Commit c3abf91

Browse files
authored
Merge branch 'dev' into feature/ULITP-5407
2 parents 6672768 + 76cdd94 commit c3abf91

9 files changed

Lines changed: 813 additions & 102 deletions

File tree

src/KeeperData.Application/Orchestration/Imports/Sam/Holdings/Steps/SamHoldingImportGoldMappingStep.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ protected override async Task ExecuteCoreAsync(SamHoldingImportContext context,
2828
{
2929
if (context.SilverHoldings.Count > 0)
3030
{
31-
var representative = context.SilverHoldings.Any(x => x.HoldingStatus == HoldingStatusType.Active.GetDescription())
32-
? context.SilverHoldings.Where(x => x.HoldingStatus == HoldingStatusType.Active.GetDescription()).OrderByDescending(h => h.LastUpdatedDate).First()
33-
: context.SilverHoldings.OrderByDescending(h => h.LastUpdatedDate).First();
31+
// Prefer SAM Holding over Common Land when selecting representative
32+
var representative = SamHoldingMapper.SelectRepresentativeHolding(context.SilverHoldings);
3433

3534
var existingHoldingFilter = Builders<SiteDocument>.Filter.ElemMatch(
3635
x => x.Identifiers,
@@ -70,7 +69,7 @@ protected override async Task ExecuteCoreAsync(SamHoldingImportContext context,
7069
siteTypeDerivedCodeLookupService,
7170
cancellationToken);
7271

73-
await EnrichWithCommonLandDataAsync(context, representative, cancellationToken);
72+
await EnrichWithCommonLandDataAsync(context, context.SilverHoldings, cancellationToken);
7473

7574
logger.LogInformation("Associated main sites queued for update: {Count} for CPH {Cph}",
7675
context.AssociatedMainSites?.Count ?? 0, context.Cph);
@@ -87,14 +86,21 @@ protected override async Task ExecuteCoreAsync(SamHoldingImportContext context,
8786
}
8887
}
8988

90-
private async Task EnrichWithCommonLandDataAsync(SamHoldingImportContext context, SamHoldingDocument representative, CancellationToken cancellationToken)
89+
private async Task EnrichWithCommonLandDataAsync(SamHoldingImportContext context, List<SamHoldingDocument> silverHoldings, CancellationToken cancellationToken)
9190
{
9291
var goldSite = context.GoldSite;
9392
if (goldSite == null) return;
9493

95-
goldSite.LocalAuthorityName = representative.LocalAuthorityName;
94+
// Merge LocalAuthorityName - prefer non-null value from any holding
95+
goldSite.LocalAuthorityName = silverHoldings
96+
.Select(h => h.LocalAuthorityName)
97+
.FirstOrDefault(name => !string.IsNullOrWhiteSpace(name));
9698

97-
goldSite.AssociatedMainHoldings = representative.AssociatedMainHoldings
99+
// Merge AssociatedMainHoldings from all holdings, removing duplicates
100+
var allMainHoldings = silverHoldings
101+
.SelectMany(h => h.AssociatedMainHoldings)
102+
.GroupBy(r => r.HoldingIdentifier)
103+
.Select(g => g.OrderByDescending(r => r.StartDate).First())
98104
.Select(r => new AssociatedHoldingDocument
99105
{
100106
HoldingIdentifier = r.HoldingIdentifier,
@@ -104,13 +110,17 @@ private async Task EnrichWithCommonLandDataAsync(SamHoldingImportContext context
104110
})
105111
.ToList();
106112

113+
goldSite.AssociatedMainHoldings = allMainHoldings;
114+
107115
if (goldSite.AssociatedMainHoldings?.Count > 0)
108116
{
109-
await FindAndUpdateMainSiteIfExists(context, representative, goldSite.AssociatedMainHoldings, cancellationToken);
117+
// Get the CPH from any holding (they all have the same CPH)
118+
var cph = silverHoldings.First().CountyParishHoldingNumber;
119+
await FindAndUpdateMainSiteIfExists(context, cph, goldSite.AssociatedMainHoldings, cancellationToken);
110120
}
111121
}
112122

113-
private async Task FindAndUpdateMainSiteIfExists(SamHoldingImportContext context, SamHoldingDocument representative, List<AssociatedHoldingDocument> mainHoldings, CancellationToken cancellationToken)
123+
private async Task FindAndUpdateMainSiteIfExists(SamHoldingImportContext context, string commonCph, List<AssociatedHoldingDocument> mainHoldings, CancellationToken cancellationToken)
114124
{
115125
foreach (var mainHolding in mainHoldings)
116126
{
@@ -133,7 +143,7 @@ private async Task FindAndUpdateMainSiteIfExists(SamHoldingImportContext context
133143

134144
var commonForMain = new AssociatedHoldingDocument
135145
{
136-
HoldingIdentifier = representative.CountyParishHoldingNumber,
146+
HoldingIdentifier = commonCph,
137147
ContiguousFlag = mainHolding.ContiguousFlag,
138148
StartDate = mainHolding.StartDate,
139149
EndDate = mainHolding.EndDate

src/KeeperData.Application/Orchestration/Imports/Sam/Holdings/Steps/SamHoldingImportSilverMappingStep.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ protected override async Task ExecuteCoreAsync(SamHoldingImportContext context,
2525
countryIdentifierLookupService.FindAsync,
2626
cancellationToken);
2727

28-
var commonLandHoldings = SamCommonLandMapper.ToSilver(context.RawCommonLandsByCommonCph);
28+
var commonLandHoldings = await SamCommonLandMapper.ToSilver(
29+
context.RawCommonLandsByCommonCph,
30+
countryIdentifierLookupService.FindAsync,
31+
cancellationToken);
2932
if (commonLandHoldings.Count > 0)
3033
{
3134
context.SilverHoldings.AddRange(commonLandHoldings);

src/KeeperData.Application/Orchestration/Imports/Sam/Mappings/SamCommonLandMapper.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public static class SamCommonLandMapper
1010
private const string CommonLandSiteTypeCode = "CL";
1111
private const string CommonLandBusinessUsage = "Common Land";
1212

13-
public static List<SamHoldingDocument> ToSilver(List<SamCommonLand> rawCommonLands)
13+
public static async Task<List<SamHoldingDocument>> ToSilver(
14+
List<SamCommonLand> rawCommonLands,
15+
Func<string?, string?, CancellationToken, Task<(string? countryId, string? countryCode, string? countryName)>> resolveCountry,
16+
CancellationToken cancellationToken)
1417
{
1518
if (rawCommonLands == null || rawCommonLands.Count == 0)
1619
return [];
@@ -33,6 +36,8 @@ public static List<SamHoldingDocument> ToSilver(List<SamCommonLand> rawCommonLan
3336
});
3437
}
3538

39+
var (countryId, countryCode, _) = await resolveCountry(representative.COUNTRY, null, cancellationToken);
40+
3641
var holding = new SamHoldingDocument
3742
{
3843
LastUpdatedBatchId = representative.BATCH_ID,
@@ -64,10 +69,11 @@ public static List<SamHoldingDocument> ToSilver(List<SamCommonLand> rawCommonLan
6469
{
6570
IdentifierId = Guid.NewGuid().ToString(),
6671
AddressLine = representative.ADDRESS_LINE_1,
67-
AddressLocality = representative.ADDRESS_LINE_2,
68-
AddressStreet = representative.ADDRESS_LINE_3,
72+
AddressStreet = representative.ADDRESS_LINE_2,
73+
AddressTown = representative.ADDRESS_LINE_3,
6974
AddressPostCode = representative.POSTCODE,
70-
CountryCode = representative.COUNTRY
75+
CountryCode = countryCode,
76+
CountryIdentifier = countryId
7177
}
7278
}
7379
};

0 commit comments

Comments
 (0)