Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added relationships to GMR #8

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions Cdms.Consumers/GmrConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ public async Task OnHandle(SearchGmrsForDeclarationIdsResponse message)
{
foreach (var gmr in message.Gmrs)
{
var internalGmr = GmrMapper.Map(gmr);
var internalGmr = GrmWithTransformMapper.MapWithTransform(gmr);
var existingGmr = await dbContext.Gmrs.Find(internalGmr.Id);

var auditId = Context.Headers["messageId"].ToString();
if (existingGmr is null)
{

var auditEntry =
AuditEntry.CreateCreatedEntry(internalGmr, internalGmr.Id, 1, gmr.LastUpdated);
AuditEntry.CreateCreatedEntry(internalGmr, auditId, 1, gmr.LastUpdated);
internalGmr.AuditEntries.Add(auditEntry);
await dbContext.Gmrs.Insert(internalGmr);
}
Expand All @@ -32,7 +33,7 @@ public async Task OnHandle(SearchGmrsForDeclarationIdsResponse message)
var auditEntry = AuditEntry.CreateUpdated<Gmr>(
previous: existingGmr,
current: internalGmr,
id: internalGmr.Id,
id: auditId,
version: internalGmr.AuditEntries.Count + 1,
lastUpdated: gmr.LastUpdated);
internalGmr.AuditEntries.Add(auditEntry);
Expand Down
39 changes: 35 additions & 4 deletions Cdms.Model/Extensions/LinksBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,53 @@ public static class LinksBuilder
{
public static class Notification
{
public const string ResourceName = "import-notifications";
public static string BuildSelfNotificationLink(string id)
{
return LinksBuilder.BuildSelfLink("notification", id);
return LinksBuilder.BuildSelfLink(ResourceName, id);
}

public static string BuildRelatedMovementLink(string id)
{
return LinksBuilder.BuildRelatedLink("notification", id, "movements");
return LinksBuilder.BuildRelatedLink(ResourceName, id, "movements");
}
}

public static class Movement
{
public const string ResourceName = "movements";
public static string BuildSelfMovementLink(string id)
{
return LinksBuilder.BuildSelfLink("movements", id);
return LinksBuilder.BuildSelfLink(ResourceName, id);
}

public static string BuildRelatedMovementLink(string id)
{
return LinksBuilder.BuildRelatedLink("movements", id, "notifications");
return LinksBuilder.BuildRelatedLink(ResourceName, id, Notification.ResourceName);
}
}

public static class Gmr
{
public const string ResourceName = "gmr";
public static string BuildSelfRelationshipCustomsLink(string id)
{
return LinksBuilder.BuildSelfRelationshipLink(ResourceName, id, Notification.ResourceName);
}

public static string BuildSelfRelationshipTransitsLink(string id)
{
return LinksBuilder.BuildSelfRelationshipLink(ResourceName, id, Movement.ResourceName);
}

public static string BuildRelatedTransitLink(string id)
{
return LinksBuilder.BuildSelfLink(Movement.ResourceName, id);
}

public static string BuildRelatedCustomsLink(string id)
{
return LinksBuilder.BuildSelfLink(Notification.ResourceName, id);
}
}

Expand All @@ -37,4 +63,9 @@ public static string BuildRelatedLink(string type, string id, string related)
{
return $"/api/{type}/{id}/{related}";
}

public static string BuildSelfRelationshipLink(string type, string id, string relationship)
{
return $"/api/{type}/{id}/relationships/{relationship}";
}
}
5 changes: 5 additions & 0 deletions Cdms.Model/Gvms/Gmr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.Json.Serialization;
using Cdms.Model.Auditing;
using Cdms.Model.Data;
using Cdms.Model.Relationships;
using JsonApiDotNetCore.MongoDb.Resources;
using JsonApiDotNetCore.Resources.Annotations;
using MongoDB.Bson.Serialization.Attributes;
Expand Down Expand Up @@ -37,4 +38,8 @@ public string? StringId
public string? LocalId { get; set; }

[Attr] public List<AuditEntry> AuditEntries { get; set; } = new List<AuditEntry>();

[Attr]
[JsonPropertyName("relationships")]
public GmrRelationships Relationships { get; set; } = new GmrRelationships();
}
8 changes: 0 additions & 8 deletions Cdms.Model/Gvms/Gmr.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ public partial class Gmr //
[System.ComponentModel.Description("")]
public ActualCrossing? ActualCrossing { get; set; }


/// <summary>
///
/// </summary>
[Attr]
[System.ComponentModel.Description("")]
public Declarations? Declarations { get; set; }

}


4 changes: 2 additions & 2 deletions Cdms.Model/Ipaffs/ImportNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class ImportNotification : IMongoIdentifiable, IDataEntity
private int? matchReference;

//// This field is used by the jsonapi-consumer to control the correct casing in the type field
[JsonIgnore] public string Type { get; set; } = "import-notifications";
[JsonIgnore] public string Type { get; set; } = "import-notification";

//[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
[JsonIgnore]
Expand Down Expand Up @@ -125,7 +125,7 @@ public void AddRelationship(string type, TdmRelationshipObject relationship)
}
}

Relationships.Movements.Matched = Relationships.Movements.Data.Any(x => x.Matched);
Relationships.Movements.Matched = Relationships.Movements.Data.Any(x => x.Matched.GetValueOrDefault());
}

public void Changed(AuditEntry auditEntry)
Expand Down
3 changes: 2 additions & 1 deletion Cdms.Model/Movement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public void AddRelationship(string type, TdmRelationshipObject relationship)

Relationships.Notifications.Matched = Items
.Select(x => x.ItemNumber)
.All(itemNumber => Relationships.Notifications.Data.Any(x => x.Matched && x.SourceItem == itemNumber));
.All(itemNumber =>
Relationships.Notifications.Data.Exists(x => x.Matched.GetValueOrDefault() && x.SourceItem == itemNumber));
}

public void Update(AuditEntry auditEntry)
Expand Down
15 changes: 15 additions & 0 deletions Cdms.Model/Relationships/GmrRelationships.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using JsonApiDotNetCore.Resources.Annotations;

namespace Cdms.Model.Relationships;

public class GmrRelationships : ITdmRelationships
{
[Attr] public TdmRelationshipObject Transits { get; set; } = TdmRelationshipObject.CreateDefault();

[Attr] public TdmRelationshipObject Customs { get; set; } = TdmRelationshipObject.CreateDefault();

public List<(string, TdmRelationshipObject)> GetRelationshipObjects()
{
return [("transits", Transits), ("customs", Customs)];
}
}
2 changes: 1 addition & 1 deletion Cdms.Model/Relationships/ITdmRelationships.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace Cdms.Model.Relationships;

public interface ITdmRelationships
{
public (string, TdmRelationshipObject) GetRelationshipObject();
public List<(string, TdmRelationshipObject)> GetRelationshipObjects();
}
4 changes: 2 additions & 2 deletions Cdms.Model/Relationships/MovementTdmRelationships.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class MovementTdmRelationships : ITdmRelationships
{
[Attr] public TdmRelationshipObject Notifications { get; set; } = TdmRelationshipObject.CreateDefault();

public (string, TdmRelationshipObject) GetRelationshipObject()
public List<(string, TdmRelationshipObject)> GetRelationshipObjects()
{
return ("notifications", Notifications);
return [("notifications", Notifications)];
}
}
4 changes: 2 additions & 2 deletions Cdms.Model/Relationships/NotificationTdmRelationships.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class NotificationTdmRelationships : ITdmRelationships
{
[Attr] public TdmRelationshipObject Movements { get; set; } = TdmRelationshipObject.CreateDefault();

public (string, TdmRelationshipObject) GetRelationshipObject()
public List<(string, TdmRelationshipObject)> GetRelationshipObjects()
{
return ("movements", Movements);
return [("movements", Movements)];
}
}
33 changes: 32 additions & 1 deletion Cdms.Model/Relationships/RelationshipDataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Cdms.Model.Relationships;

public sealed class RelationshipDataItem
{
[Attr] public bool Matched { get; set; } = default!;
[Attr] public bool? Matched { get; set; } = default!;

[Attr] public string Type { get; set; }

Expand All @@ -23,6 +23,37 @@ public sealed class RelationshipDataItem

public int? MatchingLevel { get; set; }

public Dictionary<string, object?> ToDictionary()
{
var meta = new Dictionary<string, object?>();
if (Matched.HasValue)
{
meta.Add("matched", Matched);
}

if (SourceItem.HasValue)
{
meta.Add("sourceItem", SourceItem);
}

if (DestinationItem.HasValue)
{
meta.Add("destinationItem", DestinationItem);
}

if (MatchingLevel.HasValue)
{
meta.Add("matchingLevel", MatchingLevel);
}

if (!string.IsNullOrEmpty(Links?.Self))
{
meta.Add("self", Links.Self);
}

return meta;
}

public static RelationshipDataItem CreateFromNotification(ImportNotification notification, Movement movement,
string matchReference, bool matched = true, string reason = null)
{
Expand Down
4 changes: 2 additions & 2 deletions Cdms.Model/Relationships/TdmRelationshipObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace Cdms.Model.Relationships;

public sealed class TdmRelationshipObject
{
[Attr] public bool Matched { get; set; } = default!;
[Attr] public bool? Matched { get; set; } = default!;

[Attr] public RelationshipLinks Links { get; set; }

[Attr] public List<RelationshipDataItem> Data { get; set; } = new List<RelationshipDataItem>();

public static TdmRelationshipObject CreateDefault()
{
return new TdmRelationshipObject() { Matched = false };
return new TdmRelationshipObject();
}
}
7 changes: 0 additions & 7 deletions Cdms.Types.Gvms.Mapping.V1/Class1.cs

This file was deleted.

4 changes: 3 additions & 1 deletion Cdms.Types.Gvms.Mapping.V1/GmrMapper.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#nullable enable


using Cdms.Model.Extensions;

namespace Cdms.Types.Gvms.Mapping;

public static class GmrMapper
Expand All @@ -34,7 +36,7 @@ public static Cdms.Model.Gvms.Gmr Map(Cdms.Types.Gvms.Gmr from)
to.PlannedCrossing = PlannedCrossingMapper.Map(from?.PlannedCrossing);
to.CheckedInCrossing = CheckedInCrossingMapper.Map(from?.CheckedInCrossing);
to.ActualCrossing = ActualCrossingMapper.Map(from?.ActualCrossing);
to.Declarations = DeclarationsMapper.Map(from?.Declarations);

return to;
}
}
Expand Down
54 changes: 54 additions & 0 deletions Cdms.Types.Gvms.Mapping.V1/GrmWithTransformMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Cdms.Model.Extensions;
using Cdms.Model.Relationships;

namespace Cdms.Types.Gvms.Mapping;

public static class GrmWithTransformMapper
{
public static Cdms.Model.Gvms.Gmr MapWithTransform(Cdms.Types.Gvms.Gmr from)
{
if (from is null)
{
return default!;
}

var gmr = GmrMapper.Map(from);
Map(from, gmr);
return gmr;
}

private static void Map(Gmr from, Model.Gvms.Gmr to)
{
if (from.Declarations.Customs is not null)
{
to.Relationships.Customs = new TdmRelationshipObject()
{
Links = new RelationshipLinks()
{
Self = LinksBuilder.Gmr.BuildSelfRelationshipCustomsLink(":id"),
Related = LinksBuilder.Gmr.BuildRelatedCustomsLink(":id"),
},
Data = from.Declarations.Customs.Select(x => new RelationshipDataItem()
{
Id = x.Id, Type = "import-notifications"
}).ToList()
};
}

if (from.Declarations.Transits is not null)
{
to.Relationships.Transits = new TdmRelationshipObject()
{
Links = new RelationshipLinks()
{
Self = LinksBuilder.Gmr.BuildSelfRelationshipTransitsLink(":id"),
Related = LinksBuilder.Gmr.BuildRelatedTransitLink(":id"),
},
Data = from.Declarations.Transits.Select(x => new RelationshipDataItem()
{
Id = x.Id, Type = "movement"
}).ToList()
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ public static void RegisterVehicleMovementsClassMaps()
map.MapProperty("vehicleRegNum").SetName("vehicleRegistrationNumber");
map.MapProperty("updatedDateTime").SetName("lastUpdated").IsDateTime();
map.MapProperty("localDateTimeOfDeparture").SetName("departsAt").IsDateTime();
map.MapProperty("declarations").ExcludeFromInternal();
});

GeneratorClassMap.RegisterClassMap("SearchGmrsForDeclarationIdsResponse",
Expand Down
Loading
Loading