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

Do not amend local date time value from HMRC to UTC #121

Open
wants to merge 6 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
20 changes: 3 additions & 17 deletions Btms.Backend.Cli/Features/GenerateModels/ClassMaps/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public static void GeneratorClassMaps()
RegisterAlvsClassMaps();
RegisterIpaffsClassMaps();
RegisterIpaffsEnumMaps();

RegisterVehicleMovementsClassMaps();
}

Expand All @@ -24,8 +23,6 @@ public static void RegisterAlvsClassMaps()
map.MapProperty("DeclarationUCR").SetName("DeclarationUcr");
});



GeneratorClassMap.RegisterClassMap("Check", map =>
{
map.AddProperty(new PropertyDescriptor("decisionCode", "decisionCode", "string",
Expand All @@ -40,13 +37,11 @@ public static void RegisterAlvsClassMaps()
"", false, true,
IpaffsDescriptorBuilder.ClassNamePrefix));


map.MapProperty("MasterUCR").SetName("MasterUcr");
map.MapProperty("SubmitterTURN").SetName("SubmitterTurn");
map.MapProperty("DeclarationUCR").SetName("DeclarationUcr");
});


GeneratorClassMap.RegisterClassMap("ServiceHeader",
map => { map.MapProperty("ServiceCallTimestamp").IsDateTime().SetInternalName("ServiceCalled"); });

Expand Down Expand Up @@ -142,7 +137,6 @@ public static void RegisterIpaffsClassMaps()
map.MapProperty("sampleTime").IsTime();
map.MapDateOnlyAndTimeOnlyToDateTimeProperty("sampleDate", "sampleTime", "sampledOn");
});


GeneratorClassMap.RegisterClassMap("BillingInformation",
map =>
Expand Down Expand Up @@ -359,7 +353,6 @@ public static void RegisterIpaffsClassMaps()
map.MapProperty("telephone").IsSensitive();
});


GeneratorClassMap.RegisterClassMap("Party", map =>
{
map.MapProperty("email").IsSensitive();
Expand All @@ -384,7 +377,6 @@ public static void RegisterIpaffsClassMaps()
});
}


public static void RegisterVehicleMovementsClassMaps()
{
GeneratorClassMap.RegisterClassMap("GmrsByVRN",
Expand All @@ -397,7 +389,6 @@ public static void RegisterVehicleMovementsClassMaps()
map.MapProperty("haulierEORI").SetName("haulierEori");
map.MapProperty("vehicleRegNum").SetName("vehicleRegistrationNumber");
map.MapProperty("updatedDateTime").SetName("updatedSource").IsDateTime();
map.MapProperty("localDateTimeOfDeparture").SetName("departsAt").IsDateTime();
map.MapProperty("declarations").ExcludeFromInternal();
});

Expand All @@ -413,18 +404,13 @@ public static void RegisterVehicleMovementsClassMaps()

GeneratorClassMap.RegisterClassMap("searchGmrsResponse", map => { map.MapProperty("Gmrs").SetType("Gmr[]"); });


GeneratorClassMap.RegisterClassMap("plannedCrossing",
map =>
{
map.MapProperty("localDateTimeOfArrival").IsDateTime().SetName("arrivesAt");
map.MapProperty("localDateTimeOfDeparture").IsDateTime().SetName("departsAt");
});
map => { map.MapProperty("localDateTimeOfDeparture").IsLocalDateTimeNoSeconds().SetName("departsAt"); });

GeneratorClassMap.RegisterClassMap("actualCrossing",
map => { map.MapProperty("localDateTimeOfArrival").IsDateTime().SetName("arrivesAt"); });
map => { map.MapProperty("localDateTimeOfArrival").IsLocalDateTimeNoSeconds().SetName("arrivesAt"); });

GeneratorClassMap.RegisterClassMap("checkedInCrossing",
map => { map.MapProperty("localDateTimeOfArrival").IsDateTime().SetName("arrivesAt"); });
map => { map.MapProperty("localDateTimeOfArrival").IsLocalDateTimeNoSeconds().SetName("arrivesAt"); });
}
}
13 changes: 13 additions & 0 deletions Btms.Backend.Cli/Features/GenerateModels/ClassMaps/PropertyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public PropertyMap IsDateTime()
return this;
}

public PropertyMap IsDateTimeOffset()
{
SetType("DateTimeOffset");
return this;
}

public PropertyMap IsDate()
{
SetType("DateOnly");
Expand Down Expand Up @@ -182,4 +188,11 @@ public PropertyMap NoAttribute(Model model)
NoAttributes = true;
return this;
}

public PropertyMap IsLocalDateTimeNoSeconds()
{
IsDateTimeOffset();
AddAttribute("[JsonConverter(typeof(LocalDateTimeNoSecondsJsonConverter))]", Model.Internal);
return this;
}
}
25 changes: 25 additions & 0 deletions Btms.Backend.IntegrationTests/GmrTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,29 @@ await Client.MakeSyncGmrsRequest(new SyncGmrsCommand
jsonClientResponse.Data.Relationships?["customs"]?.Data.ManyValue?[0].Id.Should().Be("56GB123456789AB043");
jsonClientResponse.Data.Relationships?["customs"]?.Data.ManyValue?[0].Type.Should().Be("import-notifications");
}

[Fact]
public async Task GmrImport_PreservesLocalDateTimes()
{
await Client.ClearDb();
await Client.MakeSyncGmrsRequest(new SyncGmrsCommand
{
SyncPeriod = SyncPeriod.All,
RootFolder = "SmokeTest"
});

var result = await Client.AsHttpClient().GetStringAsync("api/gmrs/GMRAPOQSPDUG");

// The exact input provided by HMRC should be retained
result.Should().Contain("\"departsAt\": \"2024-11-11T00:25\"");

var gmr = Factory.GetDbContext().Gmrs
.FirstOrDefault(x => x.Id != null && x.Id.Equals("GMRAPOQSPDUG", StringComparison.OrdinalIgnoreCase));

// Strong DateTime should stay as Unspecified as that is how incoming values are deserialized and if it were
// Local it would serialize differently from the locale of this API.
gmr.Should().NotBeNull();
gmr?.PlannedCrossing.Should().NotBeNull();
gmr?.PlannedCrossing?.DepartsAt.GetValueOrDefault().DateTime.Kind.Should().Be(DateTimeKind.Unspecified);
}
}
3 changes: 2 additions & 1 deletion Btms.Model/Gvms/ActualCrossing.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public partial class ActualCrossing //
/// </summary>
[Attr]
[System.ComponentModel.Description("The planned date and time of arrival, in local time of the arrival port. Must not include seconds, time zone or UTC marker")]
public DateTime? ArrivesAt { get; set; }
[JsonConverter(typeof(LocalDateTimeNoSecondsJsonConverter))]
public DateTimeOffset? ArrivesAt { get; set; }

}

Expand Down
3 changes: 2 additions & 1 deletion Btms.Model/Gvms/CheckedInCrossing.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public partial class CheckedInCrossing //
/// </summary>
[Attr]
[System.ComponentModel.Description("The planned date and time of arrival, in local time of the arrival port. Must not include seconds, time zone or UTC marker")]
public DateTime? ArrivesAt { get; set; }
[JsonConverter(typeof(LocalDateTimeNoSecondsJsonConverter))]
public DateTimeOffset? ArrivesAt { get; set; }

}

Expand Down
3 changes: 2 additions & 1 deletion Btms.Model/Gvms/PlannedCrossing.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public partial class PlannedCrossing //
/// </summary>
[Attr]
[System.ComponentModel.Description("The planned date and time of departure, in local time of the departure port. Must not include seconds, time zone or UTC marker")]
public DateTime? DepartsAt { get; set; }
[JsonConverter(typeof(LocalDateTimeNoSecondsJsonConverter))]
public DateTimeOffset? DepartsAt { get; set; }

}

Expand Down
18 changes: 18 additions & 0 deletions Btms.Model/LocalDateTimeNoSecondsJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Btms.Model;

public class LocalDateTimeNoSecondsJsonConverter : JsonConverter<DateTimeOffset>
{
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}

public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-ddTHH:mm", CultureInfo.InvariantCulture));
}
}
2 changes: 1 addition & 1 deletion Btms.Types.Gvms.V1/ActualCrossing.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public partial class ActualCrossing //
/// The planned date and time of arrival, in local time of the arrival port. Must not include seconds, time zone or UTC marker
/// </summary>
[JsonPropertyName("localDateTimeOfArrival")]
public DateTime? ArrivesAt { get; set; }
public DateTimeOffset? ArrivesAt { get; set; }

}

Expand Down
2 changes: 1 addition & 1 deletion Btms.Types.Gvms.V1/CheckedInCrossing.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public partial class CheckedInCrossing //
/// The planned date and time of arrival, in local time of the arrival port. Must not include seconds, time zone or UTC marker
/// </summary>
[JsonPropertyName("localDateTimeOfArrival")]
public DateTime? ArrivesAt { get; set; }
public DateTimeOffset? ArrivesAt { get; set; }

}

Expand Down
2 changes: 1 addition & 1 deletion Btms.Types.Gvms.V1/PlannedCrossing.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public partial class PlannedCrossing //
/// The planned date and time of departure, in local time of the departure port. Must not include seconds, time zone or UTC marker
/// </summary>
[JsonPropertyName("localDateTimeOfDeparture")]
public DateTime? DepartsAt { get; set; }
public DateTimeOffset? DepartsAt { get; set; }

}

Expand Down
2 changes: 2 additions & 0 deletions TestGenerator.IntegrationTesting.Backend/BtmsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,6 @@ public JsonApiClient.JsonApiClient AsJsonApiClient()
{
return new JsonApiClient.JsonApiClient(client);
}

public HttpClient AsHttpClient() => client;
}
Loading