Skip to content

Commit

Permalink
Block zz99 for postal.
Browse files Browse the repository at this point in the history
  • Loading branch information
RossBugginsNHS committed Nov 22, 2023
1 parent cb15264 commit d80ae61
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 16 deletions.
2 changes: 1 addition & 1 deletion commcheck-api/cli/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net8.0/cli.dll",
"args": [],
"args": ["--dob=2020-01-01", "--rfr=DEA"],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
Expand Down
37 changes: 28 additions & 9 deletions commcheck-api/cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,58 @@
using Microsoft.Extensions.Logging;


var fileOption = new Option<DateOnly>(
var dobOption = new Option<DateOnly>(
name: "--dob",
description: "Date of birth (yyyy-mm-dd)");
description: "Date of birth (yyyy-mm-dd)")
{
IsRequired = true
};

var baseUrl = new Option<Uri>(
name: "--host",
description: "Base url. defaults to https://commchecks.azurewebsites.net");
baseUrl.SetDefaultValue(new Uri("https://commchecks.azurewebsites.net"));

var rfrCode = new Option<string>(
name: "--rfr",
description: "Reason for removal code")
{
IsRequired = true
};

var rootCommand = new RootCommand("Commcheck cli v1");
rootCommand.AddOption(fileOption);
rootCommand.AddOption(dobOption);
rootCommand.AddOption(baseUrl);
rootCommand.AddOption(rfrCode);

rootCommand.SetHandler(async (dob) =>
rootCommand.SetHandler(async (dob, url, rfr) =>
{
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHttpClient<CommsCheck>(options=>
{
options.BaseAddress = new Uri("https://commchecks.azurewebsites.net");
options.BaseAddress = url;
});
var host = builder.Build();
var check = host.Services.GetRequiredService<CommsCheck>();
var content = await check.Check(dob);
var content = await check.Check(dob, Enum.Parse<ReasonForRemovals>(rfr));
},
fileOption);
dobOption, baseUrl, rfrCode);

return await rootCommand.InvokeAsync(args);



public class CommsCheck(HttpClient client, ILogger<CommsCheck> logger)
{
public async Task<CommsCheckAnswerResponseDto> Check(DateOnly dob)
public async Task<CommsCheckAnswerResponseDto> Check(
DateOnly dob,
ReasonForRemovals rfr)
{
var response = await client.PostAsJsonAsync(
"check",
CommsCheckQuestionRequestDto.DobOnly(
dob,
ReasonForRemovals.None));
rfr));


var location = response.Headers.Location;
Expand Down
7 changes: 7 additions & 0 deletions commcheck-api/src/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@
"ErrorMessage": "",
"Enabled": false,
"Expression": "true"
},
{
"RuleName": "Block ZZ99",
"SuccessEvent": "Explicit Postal Block 99: Block ZZ99",
"ErrorMessage": "",
"Enabled": true,
"Expression": "item.PostCode.IsZZ99 == true"
}
]
},
Expand Down
1 change: 1 addition & 0 deletions commcheck-api/src/src/Api/CommsCheckAnswerResponseDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ public static CommsCheckAnswerResponseDto FromCommsCheckAnswer(CommsCheckAnswer
);
}
}

50 changes: 48 additions & 2 deletions commcheck-api/src/src/Api/CommsCheckQuestionRequestDto.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
namespace CommsCheck;

using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

public class PostalCodeJsonConverter : JsonConverter<PostalCode>
{
public override PostalCode Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options) =>
PostalCode.Parse(reader.GetString()!);

public override void Write(
Utf8JsonWriter writer,
PostalCode postCodeValue,
JsonSerializerOptions options) =>
writer.WriteStringValue(postCodeValue.PostCode);
}

public readonly record struct PostalCode
{
public string PostCode {get;init;}

public bool IsZZ99 => PostCode!=null && PostCode.StartsWith("ZZ99");

[JsonConstructor]
public PostalCode(string postCode)
{
ArgumentNullException.ThrowIfNullOrWhiteSpace(postCode, nameof(postCode));
PostCode = ParsePostcodeString(postCode);
}

public static PostalCode Parse(string postCode)
{
return new PostalCode(postCode);
}

private static string ParsePostcodeString(string postcode)
{
ArgumentNullException.ThrowIfNullOrWhiteSpace(postcode, nameof(postcode));
return postcode.ToUpper().Replace(" ", "");
}

public static PostalCode Empty => new PostalCode();
}

public readonly record struct CommsCheckQuestionRequestDto(
DateOnly DateOfBirth,
DateOnly DateOfSmsMostRecentUpdate,
Expand All @@ -10,7 +54,8 @@ public readonly record struct CommsCheckQuestionRequestDto(
DateOnly DateOfPostalMostRecentUpdate,
DateOnly DateOfReasonForRemovalMostRecentUpdate,
DeathStatus? DeathStatusValue,
ReasonForRemovals? RfR)
ReasonForRemovals? RfR,
PostalCode PostCode)
{

public static CommsCheckQuestionRequestDto DobOnly(
Expand All @@ -24,5 +69,6 @@ public static CommsCheckQuestionRequestDto DobOnly(
DateOnly.MinValue,
DateOnly.MinValue,
DeathStatus.None,
reasonForRemoval);
reasonForRemoval,
PostalCode.Empty);
}
11 changes: 8 additions & 3 deletions commcheck-api/src/src/CommsCheck/CommsCheckItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public readonly record struct CommsCheckItem(
DateOnly DateOfReasonForRemovalMostRecentUpdate,
IReasonForRemoval ReasonForRemoval,
IDeathStatus DeathStatus,
PostalCode PostCode,
CommsCheckQuestionRequestDtoCopy CopyOfSource)
{
public int DaysOld =>
Expand Down Expand Up @@ -56,6 +57,7 @@ public CommsCheckItem FromDtoRelativeToToday(CommsCheckQuestionRequestDto dto)
dto.DateOfReasonForRemovalMostRecentUpdate,
IReasonForRemoval.FromEnum(dto.RfR),
IDeathStatus.FromEnum(dto.DeathStatusValue),
dto.PostCode,
CommsCheckQuestionRequestDtoCopy.FromDto(dto));
}
}
Expand All @@ -67,7 +69,8 @@ public readonly record struct CommsCheckQuestionRequestDtoCopy(DateOnly DateOfBi
DateOnly DateOfPostalMostRecentUpdate,
DateOnly DateOfReasonForRemovalMostRecentUpdate,
DeathStatus? DeathStatusValue,
ReasonForRemovals? RfR
ReasonForRemovals? RfR,
PostalCode PostCode
)
{
public static CommsCheckQuestionRequestDtoCopy FromDto(CommsCheckQuestionRequestDto dto) =>
Expand All @@ -79,7 +82,8 @@ public static CommsCheckQuestionRequestDtoCopy FromDto(CommsCheckQuestionRequest
dto.DateOfPostalMostRecentUpdate,
dto.DateOfReasonForRemovalMostRecentUpdate,
dto.DeathStatusValue,
dto.RfR);
dto.RfR,
dto.PostCode);

public CommsCheckQuestionRequestDto ToDto() =>
new CommsCheckQuestionRequestDto(
Expand All @@ -90,5 +94,6 @@ public CommsCheckQuestionRequestDto ToDto() =>
DateOfPostalMostRecentUpdate,
DateOfReasonForRemovalMostRecentUpdate,
DeathStatusValue,
RfR);
RfR,
PostCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace CommsCheck;
using System.Text;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Converters;
using RulesEngine;
using RulesEngine.Models;

Expand Down
2 changes: 2 additions & 0 deletions commcheck-api/src/src/Extensions/CommsCheckOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ public CommsCheckOptions AddJsonConfig()
options.SerializerOptions.WriteIndented = true;
options.SerializerOptions.IncludeFields = true;
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.SerializerOptions.Converters.Add(new PostalCodeJsonConverter());
options.SerializerOptions.PropertyNameCaseInsensitive = true;
});

_services.Configure<JsonOptions>(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.Converters.Add(new PostalCodeJsonConverter());
});
return this;
}
Expand Down
6 changes: 6 additions & 0 deletions commcheck-api/src/src/Extensions/SwaggerGenExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public static SwaggerGenOptions AddDateAndEnumFormatters(
Format = "date"
});

options.MapType<PostalCode>(() => new OpenApiSchema()
{
Type = "string"
});


var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
Expand Down

0 comments on commit d80ae61

Please sign in to comment.