Skip to content

Commit

Permalink
CDMS-146 changes the way MatchingService identifies non-linked docume… (
Browse files Browse the repository at this point in the history
#82)

* CDMS-146 changes the way MatchingService identifies non-linked documents - it should only expect CHEDs to be linked, not other documents such as IUU docs

* Need to handle lowercase

* Fix sonarqube issue
  • Loading branch information
craigedmunds authored Jan 27, 2025
1 parent 89efdcd commit dcdd5c2
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 36 deletions.
11 changes: 11 additions & 0 deletions 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Fix sonarqube issue

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon Jan 27 15:11:15 2025 +0000
#
# On branch feature/cdms-146
# Changes to be committed:
# modified: Btms.Business/Services/Matching/MatchingService.cs
#
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class DecisionCodeTests(ITestOutputHelper output) : MultipleScenarioGener
[InlineData(typeof(ChedPn04ScenarioGenerator), "N04")]
[InlineData(typeof(ChedPn07ScenarioGenerator), "N07")]
[InlineData(typeof(MissingChedScenarioGenerator), "X00")]
[InlineData(typeof(IuuScenarioGenerator), "X00")]
[InlineData(typeof(IuuScenarioGenerator), "C03")]
public void ShouldHaveCorrectDecisionCode(Type generatorType, string expectedDecisionCode)
{
base.TestOutputHelper.WriteLine("Generator : {0}, Decision Code : {1}", generatorType!.FullName, expectedDecisionCode);
Expand Down
32 changes: 7 additions & 25 deletions Btms.Business/Extensions/MovementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@ namespace Btms.Business.Extensions;

public static class MovementExtensions
{
public static bool AreNumbersComplete<T>(this IEnumerable<T> source, Func<T, int?> getNumbers)
{
var numbers = source
.Select(getNumbers)
.Where(n => n.HasValue())
.Order()
.ToList();

if (numbers.Distinct().Count() != numbers.Count())
{
//Contains duplicates
return false;
}
else if (numbers.Count() != numbers.Last())
{
//Some missing
// should be contiguous

return false;
}

return true;
}

public static string[] UniqueDocumentReferences(this Movement movement)
{
Expand All @@ -50,13 +27,18 @@ public static List<string> UniqueNotificationRelationshipIds(this Movement movem
.ToList();
}

public static bool IsChed(this Document doc)
{
return doc.DocumentReference != null &&
doc.DocumentReference.ToUpper().StartsWith("GBCHD");
}

public static List<string> UniqueDocumentReferenceIdsThatShouldLink(this Movement movement)
{
return movement.Items
.SelectMany(i => i.Documents ?? [])
// Only CHED document refs should result in links
.Where(d => d.DocumentReference != null &&
d.DocumentReference.StartsWith("GBCHD"))
.Where(d => d.IsChed())
.Select(d => d.DocumentReference!.TrimUniqueNumber())
.Distinct()
.ToList();
Expand Down
17 changes: 12 additions & 5 deletions Btms.Business/Services/Matching/MatchingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Btms.Business.Extensions;
using Btms.Model;

namespace Btms.Business.Services.Matching;
Expand All @@ -14,23 +15,29 @@ public Task<MatchingResult> Process(MatchingContext matchingContext, Cancellatio
{
if (item.Documents == null) continue;

foreach (var documentGroup in item.Documents.GroupBy(d => d.DocumentReference))
var groupedDocuments = item
.Documents
.Where(d => d.IsChed())
.GroupBy(d => d.DocumentReference)
.Select(d => d.Key);

foreach (var documentGroup in groupedDocuments)
{
Debug.Assert(documentGroup.Key != null);
Debug.Assert(documentGroup != null);
Debug.Assert(movement.Id != null, "movement.Id != null");
Debug.Assert(item.ItemNumber != null, "item.ItemNumber != null");

var notification = matchingContext.Notifications.Find(x =>
x._MatchReference == MatchIdentifier.FromCds(documentGroup.Key).Identifier);
x._MatchReference == MatchIdentifier.FromCds(documentGroup).Identifier);

if (notification is null)
{
matchingResult.AddDocumentNoMatch(movement.Id, item.ItemNumber.Value, documentGroup.Key);
matchingResult.AddDocumentNoMatch(movement.Id, item.ItemNumber.Value, documentGroup);
}
else
{
Debug.Assert(notification?.Id != null, "notification.Id != null");
matchingResult.AddMatch(notification.Id, movement.Id, item.ItemNumber.Value, documentGroup.Key);
matchingResult.AddMatch(notification.Id, movement.Id, item.ItemNumber.Value, documentGroup);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,52 @@
{"serviceHeader":{"sourceSystem":"CDS","destinationSystem":"ALVS","correlationId":"1307513","serviceCallTimestamp":1733099125000},"header":{"entryReference":"24GBDC4TW6DUQYIAR5","entryVersionNumber":1,"previousVersionNumber":null,"declarationUCR":"4GB823723242000-SLHA24017238","declarationPartNumber":null,"declarationType":"S","arrivalDateTime":null,"submitterTURN":null,"declarantId":"GB823723242000","declarantName":"GB823723242000","dispatchCountryCode":"IL","goodsLocationCode":"FEMLHRPDX","masterUCR":"HPDX11463348040"},"items":[{"itemNumber":1,"customsProcedureCode":"4000000","taricCommodityCode":"1211908620","goodsDescription":"BASIL","consigneeId":"GB101487738000","consigneeName":"GB101487738000","itemNetMass":640,"itemSupplementaryUnits":0,"itemThirdQuantity":null,"itemOriginCountryCode":"IL","documents":[{"documentCode":"N851","documentReference":"gbchd2024.5229377","documentStatus":"JE","documentControl":"P","documentQuantity":null}],"checks":[{"checkCode":"H219","departmentCode":"PHSI"}]}]}
{
"serviceHeader": {
"sourceSystem": "CDS",
"destinationSystem": "ALVS",
"correlationId": "1307513",
"serviceCallTimestamp": 1733099125000
},
"header": {
"entryReference": "24GBDC4TW6DUQYIAR5",
"entryVersionNumber": 1,
"previousVersionNumber": null,
"declarationUCR": "4GB823723242000-SLHA24017238",
"declarationPartNumber": null,
"declarationType": "S",
"arrivalDateTime": null,
"submitterTURN": null,
"declarantId": "GB823723242000",
"declarantName": "GB823723242000",
"dispatchCountryCode": "IL",
"goodsLocationCode": "FEMLHRPDX",
"masterUCR": "HPDX11463348040"
},
"items": [
{
"itemNumber": 1,
"customsProcedureCode": "4000000",
"taricCommodityCode": "1211908620",
"goodsDescription": "BASIL",
"consigneeId": "GB101487738000",
"consigneeName": "GB101487738000",
"itemNetMass": 640,
"itemSupplementaryUnits": 0,
"itemThirdQuantity": null,
"itemOriginCountryCode": "IL",
"documents": [
{
"documentCode": "N851",
"documentReference": "gbchd2024.5229377",
"documentStatus": "JE",
"documentControl": "P",
"documentQuantity": null
}
],
"checks": [
{
"checkCode": "H219",
"departmentCode": "PHSI"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1 +1,52 @@
{"serviceHeader":{"sourceSystem":"CDS","destinationSystem":"ALVS","correlationId":"1307514","serviceCallTimestamp":1733099196000},"header":{"entryReference":"24GBDC4TW6DUQYIAR5","entryVersionNumber":2,"previousVersionNumber":1,"declarationUCR":"4GB823723242000-SLHA24017238","declarationPartNumber":null,"declarationType":"S","arrivalDateTime":null,"submitterTURN":null,"declarantId":"GB823723242000","declarantName":"GB823723242000","dispatchCountryCode":"IL","goodsLocationCode":"FEMLHRPDX","masterUCR":"HPDX11463348040"},"items":[{"itemNumber":1,"customsProcedureCode":"4000000","taricCommodityCode":"1211908620","goodsDescription":"BASIL","consigneeId":"GB101487738000","consigneeName":"GB101487738000","itemNetMass":640,"itemSupplementaryUnits":0,"itemThirdQuantity":null,"itemOriginCountryCode":"IL","documents":[{"documentCode":"N851","documentReference":"gbchd2024.5229377","documentStatus":"JE","documentControl":"P","documentQuantity":null}],"checks":[{"checkCode":"H219","departmentCode":"PHSI"}]}]}
{
"serviceHeader": {
"sourceSystem": "CDS",
"destinationSystem": "ALVS",
"correlationId": "1307514",
"serviceCallTimestamp": 1733099196000
},
"header": {
"entryReference": "24GBDC4TW6DUQYIAR5",
"entryVersionNumber": 2,
"previousVersionNumber": 1,
"declarationUCR": "4GB823723242000-SLHA24017238",
"declarationPartNumber": null,
"declarationType": "S",
"arrivalDateTime": null,
"submitterTURN": null,
"declarantId": "GB823723242000",
"declarantName": "GB823723242000",
"dispatchCountryCode": "IL",
"goodsLocationCode": "FEMLHRPDX",
"masterUCR": "HPDX11463348040"
},
"items": [
{
"itemNumber": 1,
"customsProcedureCode": "4000000",
"taricCommodityCode": "1211908620",
"goodsDescription": "BASIL",
"consigneeId": "GB101487738000",
"consigneeName": "GB101487738000",
"itemNetMass": 640,
"itemSupplementaryUnits": 0,
"itemThirdQuantity": null,
"itemOriginCountryCode": "IL",
"documents": [
{
"documentCode": "N851",
"documentReference": "gbchd2024.5229377",
"documentStatus": "JE",
"documentControl": "P",
"documentQuantity": null
}
],
"checks": [
{
"checkCode": "H219",
"departmentCode": "PHSI"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
{"serviceHeader":{"sourceSystem":"ALVS","destinationSystem":"CDS","correlationId":"000","serviceCallTimestamp":1733045601631},"header":{"entryReference":"24GBDB6P6GWPVLKAR7","entryVersionNumber":1,"decisionNumber":1},"items":[{"itemNumber":1,"documents":[],"checks":[{"checkCode":"H219","decisionCode":"C03","decisionValidUntil":null,"decisionReasons":[]},{"checkCode":"H223","decisionCode":"C03","decisionValidUntil":null,"decisionReasons":[]}]},{"itemNumber":2,"documents":[],"checks":[{"checkCode":"H218","decisionCode":"C03","decisionValidUntil":null,"decisionReasons":[]},{"checkCode":"H219","decisionCode":"C03","decisionValidUntil":null,"decisionReasons":[]},{"checkCode":"H223","decisionCode":"H01","decisionValidUntil":null,"decisionReasons":[]}]},{"itemNumber":6,"documents":[],"checks":[{"checkCode":"H219","decisionCode":"C03","decisionValidUntil":null,"decisionReasons":[]}]}]}
{
"serviceHeader": {
"sourceSystem": "ALVS",
"destinationSystem": "CDS",
"correlationId": "000",
"serviceCallTimestamp": 1733045601631
},
"header": {
"entryReference": "24GBDB6P6GWPVLKAR7",
"entryVersionNumber": 1,
"decisionNumber": 1
},
"items": [
{
"itemNumber": 1,
"documents": [],
"checks": [
{
"checkCode": "H219",
"decisionCode": "C03",
"decisionValidUntil": null,
"decisionReasons": []
},
{
"checkCode": "H223",
"decisionCode": "C03",
"decisionValidUntil": null,
"decisionReasons": []
}
]
},
{
"itemNumber": 2,
"documents": [],
"checks": [
{
"checkCode": "H218",
"decisionCode": "C03",
"decisionValidUntil": null,
"decisionReasons": []
},
{
"checkCode": "H219",
"decisionCode": "C03",
"decisionValidUntil": null,
"decisionReasons": []
},
{
"checkCode": "H223",
"decisionCode": "H01",
"decisionValidUntil": null,
"decisionReasons": []
}
]
},
{
"itemNumber": 6,
"documents": [],
"checks": [
{
"checkCode": "H219",
"decisionCode": "C03",
"decisionValidUntil": null,
"decisionReasons": []
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
{"serviceHeader":{"sourceSystem":"ALVS","destinationSystem":"CDS","correlationId":"000","serviceCallTimestamp":1733062869974},"header":{"entryReference":"24GBDB6P6GWPVLKAR7","entryVersionNumber":2,"decisionNumber":2},"items":[{"itemNumber":1,"documents":[],"checks":[{"checkCode":"H219","decisionCode":"C03","decisionValidUntil":null,"decisionReasons":[]},{"checkCode":"H223","decisionCode":"C03","decisionValidUntil":null,"decisionReasons":[]}]},{"itemNumber":2,"documents":[],"checks":[{"checkCode":"H218","decisionCode":"C03","decisionValidUntil":null,"decisionReasons":[]},{"checkCode":"H219","decisionCode":"C03","decisionValidUntil":null,"decisionReasons":[]},{"checkCode":"H223","decisionCode":"H01","decisionValidUntil":null,"decisionReasons":[]}]},{"itemNumber":6,"documents":[],"checks":[{"checkCode":"H219","decisionCode":"C03","decisionValidUntil":null,"decisionReasons":[]}]}]}
{
"serviceHeader": {
"sourceSystem": "ALVS",
"destinationSystem": "CDS",
"correlationId": "000",
"serviceCallTimestamp": 1733062869974
},
"header": {
"entryReference": "24GBDB6P6GWPVLKAR7",
"entryVersionNumber": 2,
"decisionNumber": 2
},
"items": [
{
"itemNumber": 1,
"documents": [],
"checks": [
{
"checkCode": "H219",
"decisionCode": "C03",
"decisionValidUntil": null,
"decisionReasons": []
},
{
"checkCode": "H223",
"decisionCode": "C03",
"decisionValidUntil": null,
"decisionReasons": []
}
]
},
{
"itemNumber": 2,
"documents": [],
"checks": [
{
"checkCode": "H218",
"decisionCode": "C03",
"decisionValidUntil": null,
"decisionReasons": []
},
{
"checkCode": "H219",
"decisionCode": "C03",
"decisionValidUntil": null,
"decisionReasons": []
},
{
"checkCode": "H223",
"decisionCode": "H01",
"decisionValidUntil": null,
"decisionReasons": []
}
]
},
{
"itemNumber": 6,
"documents": [],
"checks": [
{
"checkCode": "H219",
"decisionCode": "C03",
"decisionValidUntil": null,
"decisionReasons": []
}
]
}
]
}
Loading

0 comments on commit dcdd5c2

Please sign in to comment.