diff --git a/src/KeeperData.Application/Orchestration/Imports/Sam/Mappings/SamHoldingMapper.cs b/src/KeeperData.Application/Orchestration/Imports/Sam/Mappings/SamHoldingMapper.cs index ac504b9b..bb6bebac 100644 --- a/src/KeeperData.Application/Orchestration/Imports/Sam/Mappings/SamHoldingMapper.cs +++ b/src/KeeperData.Application/Orchestration/Imports/Sam/Mappings/SamHoldingMapper.cs @@ -289,6 +289,9 @@ await ResolveAndAddActivitiesAsync( cancellationToken); } + // If no derived mapping resolved a site type, fall back to the explicit site type code on the representative + derivedSiteType ??= await ResolveSiteTypeAsync(representative.SiteTypeCode, getSiteTypeByCode, cancellationToken); + return (allDerivedActivities, derivedSiteType); } @@ -391,7 +394,7 @@ private static async Task CreateSiteAsync( null, representative.Deleted, isPermanentLandHolding ? null : representative.SecondaryCph, - representative.CphTypeIdentifier, + string.IsNullOrEmpty(representative.CphTypeIdentifier) ? null : representative.CphTypeIdentifier, siteType, location, isPermanentLandHolding ? representative.SecondaryCph : null); @@ -427,7 +430,7 @@ private static async Task UpdateSiteAsync( null, representative.Deleted, isPermanentLandHolding ? null : representative.SecondaryCph, - representative.CphTypeIdentifier, + string.IsNullOrEmpty(representative.CphTypeIdentifier) ? null : representative.CphTypeIdentifier, isPermanentLandHolding ? representative.SecondaryCph : null); var (updatedAddress, updatedCommunication) = await ResolveLocationPartsAsync(addressSource, getCountryById, cancellationToken); diff --git a/tests/KeeperData.Api.Tests.Component/Orchestration/Imports/Sam/SamBulkImportWithAccurateRawDataTests.cs b/tests/KeeperData.Api.Tests.Component/Orchestration/Imports/Sam/SamBulkImportWithAccurateRawDataTests.cs index c851133b..d137f437 100644 --- a/tests/KeeperData.Api.Tests.Component/Orchestration/Imports/Sam/SamBulkImportWithAccurateRawDataTests.cs +++ b/tests/KeeperData.Api.Tests.Component/Orchestration/Imports/Sam/SamBulkImportWithAccurateRawDataTests.cs @@ -51,6 +51,15 @@ public async Task GivenHoldingDataAlreadyExists_WhenTheHolderIsChanged_ShouldCor VerifyGoldSiteGroupMarks(updateResult.GoldSiteGroupMarks!, updateScenarioData.ExpectedGoldSiteGroupMarks!); } + [Fact] + public async Task GivenACommonLandHolding_WhenExecutingTheBulkImportStrategy_ShouldUseCommonLandAddressDetails() + { + var scenarioData = SamTestScenarios.Scenario_CommonLandOnly; + var result = await RunDefaultScenarioAsync(scenarioData); + + VerifyGoldSite(result.GoldSite!, scenarioData.ExpectedGoldSite!); + } + private async Task RunDefaultScenarioAsync(SamTestScenarioData scenarioData, SamHoldingImportContext? existingContext = null) { var partyIds = scenarioData.RawParties.Select(x => x.PARTY_ID).Union(scenarioData.RawHolders.Select(x => x.PARTY_ID)).Distinct().ToList(); diff --git a/tests/KeeperData.Application.Tests.Unit/Orchestration/Imports/Sam/Mappings/SamHoldingMapperToGoldTests.cs b/tests/KeeperData.Application.Tests.Unit/Orchestration/Imports/Sam/Mappings/SamHoldingMapperToGoldTests.cs index 73344442..2973ef65 100644 --- a/tests/KeeperData.Application.Tests.Unit/Orchestration/Imports/Sam/Mappings/SamHoldingMapperToGoldTests.cs +++ b/tests/KeeperData.Application.Tests.Unit/Orchestration/Imports/Sam/Mappings/SamHoldingMapperToGoldTests.cs @@ -987,7 +987,7 @@ private static SiteDocument GetBlankSiteDocument() Id = GoldSiteId, Name = "", Source = "SAM", - HoldingType = string.Empty, + HoldingType = null, Location = new LocationDocument() { IdentifierId = "any-guid", diff --git a/tests/KeeperData.Tests.Common/TestData/CountryData.cs b/tests/KeeperData.Tests.Common/TestData/CountryData.cs index a874f199..a37ec4b7 100644 --- a/tests/KeeperData.Tests.Common/TestData/CountryData.cs +++ b/tests/KeeperData.Tests.Common/TestData/CountryData.cs @@ -41,7 +41,8 @@ public static class CountryData public static (string? id, string? code, string? name) Find(string code, string? internalCode) { var searchCode = DetermineSearchKey(code, internalCode); - var type = GetByCode(searchCode!); + if (searchCode == null) return (null, null, null); + var type = GetByCode(searchCode); if (type == null) return (null, null, null); return (type.IdentifierId, type.Code, type.Name); } diff --git a/tests/KeeperData.Tests.Common/TestData/Sam/ExpectedOutcomes/ExpectedGoldSite.cs b/tests/KeeperData.Tests.Common/TestData/Sam/ExpectedOutcomes/ExpectedGoldSite.cs index 6ef502dd..28aef6e4 100644 --- a/tests/KeeperData.Tests.Common/TestData/Sam/ExpectedOutcomes/ExpectedGoldSite.cs +++ b/tests/KeeperData.Tests.Common/TestData/Sam/ExpectedOutcomes/ExpectedGoldSite.cs @@ -620,4 +620,90 @@ public static class ExpectedGoldSite Activities = [] //Activities = [.. DefaultExpectedSite.Activities.Where(x => x.Type.Code == "RM")] }; + + public static SiteDocument ExpectedSite_CommonLandOnly => + new() + { + Id = Guid.NewGuid().ToString(), + CreatedDate = new DateTime(2025, 1, 1, 0, 0, 0), + LastUpdatedDate = new DateTime(2025, 11, 13, 0, 0, 0), + Type = new SiteTypeSummaryDocument + { + IdentifierId = "cl-site-type-id", + Code = "CL", + Name = "Common Land" + }, + Name = "Premises 22", + State = HoldingStatusType.Active.GetDescription(), + StartDate = default, + EndDate = null, + Source = SourceSystemType.SAM.ToString(), + DestroyIdentityDocumentsFlag = null, + Deleted = false, + + Location = new LocationDocument + { + IdentifierId = Guid.NewGuid().ToString(), + Easting = null, + Northing = null, + OsMapReference = null, + + Address = new AddressDocument + { + IdentifierId = Guid.NewGuid().ToString(), + AddressLine1 = "Holding Street 22", + AddressLine2 = "Town22", + PostTown = "Locality22", + Postcode = "CPH22 222", + LastUpdatedDate = new DateTime(2025, 11, 13, 0, 0, 0) + }, + + Communication = + [ + new() + { + IdentifierId = Guid.NewGuid().ToString(), + Email = null, + Mobile = null, + Landline = null, + PrimaryContactFlag = false, + LastUpdatedDate = new DateTime(2025, 11, 13, 0, 0, 0) + } + ], + + LastUpdatedDate = new DateTime(2025, 11, 13, 0, 0, 0) + }, + + Identifiers = + [ + new() + { + IdentifierId = Guid.NewGuid().ToString(), + Identifier = "00/000/8267", + Type = new SiteIdentifierSummaryDocument + { + IdentifierId = Guid.NewGuid().ToString(), + Code = HoldingIdentifierType.CPHN.ToString(), + Name = HoldingIdentifierType.CPHN.GetDescription()!, + LastUpdatedDate = new DateTime(2025, 11, 13, 0, 0, 0) + }, + LastUpdatedDate = new DateTime(2025, 11, 13, 0, 0, 0) + } + ], + + Parties = [], + Species = [], + Marks = [], + Activities = [], + AssociatedMainHoldings = + [ + new() + { + HoldingIdentifier = "17/050/0003", + ContiguousFlag = false, + StartDate = null, + EndDate = null + } + ] + }; } \ No newline at end of file diff --git a/tests/KeeperData.Tests.Common/TestData/Sam/SamTestScenarios.cs b/tests/KeeperData.Tests.Common/TestData/Sam/SamTestScenarios.cs index 76ab5bec..5de1345d 100644 --- a/tests/KeeperData.Tests.Common/TestData/Sam/SamTestScenarios.cs +++ b/tests/KeeperData.Tests.Common/TestData/Sam/SamTestScenarios.cs @@ -20,6 +20,7 @@ public static (string? associatedSiteTypeCode, string? associatedSiteActivityCod } private static readonly string s_cphNumber = "12/345/6789"; + private static readonly string s_commonLandCph = "00/000/8267"; public static SamTestScenarioData DefaultScenario => new() { @@ -83,6 +84,18 @@ public static SamTestScenarioData Scenario_UpdatedHolderAndParties() }; } + public static SamTestScenarioData Scenario_CommonLandOnly => new() + { + Cph = s_commonLandCph, + RawHoldings = [], + RawHerds = [], + RawHolders = [], + RawParties = [], + RawCommonLandsByCommonCph = [SamCommonLand_22], + RawPorts = [], + ExpectedGoldSite = ExpectedGoldSite.ExpectedSite_CommonLandOnly + }; + public class SamTestScenarioData { public required string Cph { get; init; } @@ -565,4 +578,29 @@ public class SamTestScenarioData PAON_END_NUMBER_SUFFIX = null, PAON_DESCRIPTION = null }; + + private static SamCommonLand SamCommonLand_22 => new() + { + BATCH_ID = 1, + CHANGE_TYPE = "I", + CreatedAtUtc = new DateTime(2025, 1, 1, 0, 0, 0), + UpdatedAtUtc = new DateTime(2025, 11, 13, 0, 0, 0), + IsDeleted = false, + + COMMON_LAND_PREMISE_ID = "554454", + MAIN_CPH = "17/050/0003", + COMMON_CPH = s_commonLandCph, + PREMISES_NAME = "Premises 22", + ADDRESS_LINE_1 = "Holding Street 22", + ADDRESS_LINE_2 = "Town22", + ADDRESS_LINE_3 = "Locality22", + POSTCODE = "CPH22 222", + LOCAL_AUTH_NAME = null, + COUNTRY = null, + EASTING = null, + NORTHING = null, + CONTIGUOUS_COMMON = null, + START_DATE = null, + END_DATE = null + }; } \ No newline at end of file