Skip to content

Commit

Permalink
Really simple cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
RossBugginsNHS committed Nov 21, 2023
1 parent 956aff9 commit cb15264
Show file tree
Hide file tree
Showing 5 changed files with 407 additions and 0 deletions.
26 changes: 26 additions & 0 deletions commcheck-api/cli/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net8.0/cli.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions commcheck-api/cli/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/cli.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/cli.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/cli.sln"
],
"problemMatcher": "$msCompile"
}
]
}
299 changes: 299 additions & 0 deletions commcheck-api/cli/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
using System.CommandLine;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Xml.Schema;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;


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

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

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

return await rootCommand.InvokeAsync(args);



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


var location = response.Headers.Location;

var content = await Result(location);

var options = new System.Text.Json.JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
WriteIndented = true
};

options.Converters.Add(new JsonStringEnumConverter());

var str = JsonSerializer.Serialize(content, options);

logger.LogInformation("{str}", str);
return content;
}

public async Task<CommsCheckAnswerResponseDto> Result(Uri location)
{
var response = await client.GetAsync(location);
var options = new System.Text.Json.JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
};

options.Converters.Add(new JsonStringEnumConverter());

var o = await response.Content.ReadFromJsonAsync<CommsCheckAnswerResponseDto>(options);
return o;
}
}



public readonly record struct CommsCheckQuestionRequestDto(
DateOnly DateOfBirth,
DateOnly DateOfSmsMostRecentUpdate,
DateOnly DateOfEmailMostRecentUpdate,
DateOnly DateOfAppMostRecentUpdate,
DateOnly DateOfPostalMostRecentUpdate,
DateOnly DateOfReasonForRemovalMostRecentUpdate,
DeathStatus? DeathStatusValue,
ReasonForRemovals? RfR)
{

public static CommsCheckQuestionRequestDto DobOnly(
DateOnly dateOfBirth,
ReasonForRemovals?reasonForRemoval) =>
new CommsCheckQuestionRequestDto(
dateOfBirth,
DateOnly.MinValue,
DateOnly.MinValue,
DateOnly.MinValue,
DateOnly.MinValue,
DateOnly.MinValue,
DeathStatus.None,
reasonForRemoval);
}



[Flags]
public enum ReasonForRemovals
{
/// <summary>
/// None
/// </summary>
None = 0,

/// <summary>
/// REASON_DEATH
/// </summary>
DEA = 1,

/// <summary>
/// REASON_EMBARKATION
/// </summary>
EMB= 2,

/// <summary>
/// REASON_TRANSFERRED_TO_SCOTLAND
/// </summary>
SCT = 4,

/// <summary>
/// REASON_TRANSFERRED_TO_NORTHERN_IRELAND
/// </summary>
NIT = 8,

/// <summary>
/// REASON_ARMEDFORCES_FORCESN
/// </summary>
AFN = 16,

/// <summary>
/// REASON_ARMEDFORCES_LOCALN
/// </summary>
AFL = 32,

/// <summary>
/// REASON_SERVICEDEPENDENT_FORCESN
/// </summary>
SDN = 64,

/// <summary>
/// REASON_SERVICEDEPENDENT_LOCALN
/// </summary>
SDL = 128,

/// <summary>
/// REASON_TEMPORARY_RESIDENT_NOT_RETURNED
/// </summary>
TRA = 256,

/// <summary>
/// REASON_REMOVAL_FROM_RESIDENTIAL_INSTITUTE
/// </summary>
RFI = 512,

/// <summary>
/// REASON_PRACTICE_REQUEST_IMMEDIATE_REMOVAL
/// </summary>
RDI = 1024,

/// <summary>
/// REASON_PRACTICE_REQUEST
/// </summary>
RDR = 2048,

/// <summary>
/// REASON_PATIENT_REQUEST
/// </summary>
RPR = 4096,

/// <summary>
/// REASON_OUT_OF_PRACTICE_AREA
/// </summary>
OPA = 8192,

/// <summary>
/// REASON_GONE_AWAY
/// </summary>
CGA = 16384,

/// <summary>
/// REASON_CANCELLATION
/// </summary>
CAN = 32768,

/// <summary>
/// REASON_OTHER
/// </summary>
ORR = 65536,

/// <summary>
/// REASON_LOGICAL_DELETION
/// </summary>
LDN = 131072,

/// <summary>
/// REASON_PRACTICE_DISSOLUTION
/// </summary>
DIS = 262144,

/// <summary>
/// REASON_X
/// </summary>
X = 524288
}


public enum DeathStatus
{
/// <summary>
/// No death status recorded.
/// </summary>
None = 0,

/// <summary>
/// Informal death status. Dealth certificate.
/// </summary>
INFORMAL = 1,

/// <summary>
/// Updated from registrars office.
/// </summary>
FORMAL = 2
}

public readonly record struct CommsCheckAnswerResponseDto(
string ResultId,
CommsCheckQuestionRequestDto Request,
CommsCheckAnswerDto Response);


public readonly record struct CommsCheckAnswerDto(
DateOnly RelativeDate,
DateTime StartedAt,
DateTime UpdatedAt,
int UpdatedCount,
CommAllowed App,
CommAllowed Email,
CommAllowed SMS,
CommAllowed Postal,
string AppReason,
string EmailReason,
string SMSReason,
string PostalReason);

public enum CommAllowed
{
Allowed,
Blocked,
Unknown
}



public record RuleOutcomesDto(params RuleResultSummary[] Summaries);

public readonly record struct RuleResultSummary(
string RuleSet,
string Method,
string MethodToLog,
bool Enabled,
bool Success,
string RuleName,
string RuleExpression,
string SuccessEvent,
string ErrorMessage,
string ExceptionMessage);

public readonly record struct CommsCheckItem(
DateOnly UtcDateCheckItemCreated,
DateOnly DateOfBirth,
DateOnly DateOfSmsMostRecentUpdate,
DateOnly DateOfEmailMostRecentUpdate,
DateOnly DateOfAppMostRecentUpdate,
DateOnly DateOfPostalMostRecentUpdate,
DateOnly DateOfReasonForRemovalMostRecentUpdate,
object ReasonForRemoval,
object DeathStatus,
CommsCheckQuestionRequestDtoCopy CopyOfSource);

public readonly record struct CommsCheckQuestionRequestDtoCopy(DateOnly DateOfBirth,
DateOnly DateOfSmsMostRecentUpdate,
DateOnly DateOfEmailMostRecentUpdate,
DateOnly DateOfAppMostRecentUpdate,
DateOnly DateOfPostalMostRecentUpdate,
DateOnly DateOfReasonForRemovalMostRecentUpdate,
DeathStatus? DeathStatusValue,
ReasonForRemovals? RfR
);
16 changes: 16 additions & 0 deletions commcheck-api/cli/cli.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

</Project>
Loading

0 comments on commit cb15264

Please sign in to comment.