@@ -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
0 commit comments