quick refactor for clarity
This commit is contained in:
parent
8934a09cf3
commit
d4eadc5919
4 changed files with 67 additions and 41 deletions
8
src/Auth/AuthAPI/AuthSettings.cs
Normal file
8
src/Auth/AuthAPI/AuthSettings.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
namespace AuthAPI;
|
||||
|
||||
public class AuthSettings
|
||||
{
|
||||
public string ApiUrl { get; set; } = string.Empty;
|
||||
public string ApiKey { get; set; } = string.Empty;
|
||||
public string AssociationNumber { get; set; } = string.Empty;
|
||||
}
|
||||
42
src/Auth/AuthAPI/MemberValidationService.cs
Normal file
42
src/Auth/AuthAPI/MemberValidationService.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace AuthAPI;
|
||||
|
||||
public class MemberValidationService(IOptions<AuthSettings> authSettings)
|
||||
{
|
||||
private readonly AuthSettings _settings = authSettings.Value;
|
||||
|
||||
public Result GetRequestWithApiKey(Request request)
|
||||
{
|
||||
if (ValidateRequest(request))
|
||||
return Result.CreateFailure(
|
||||
"Invalid request. Either SSN or both Email and FirstName must be provided.");
|
||||
|
||||
return Result.CreateSuccess(
|
||||
new ValidateMembersRoot(
|
||||
new ValidateMembersRequest(
|
||||
"confirm_membership",
|
||||
_settings.AssociationNumber,
|
||||
_settings.ApiKey,
|
||||
YearId: null,
|
||||
Firstname: request.FirstName ?? null,
|
||||
Lastname: null,
|
||||
SSN: request.Ssn ?? null,
|
||||
Email: request.Email ?? null,
|
||||
Phone: null,
|
||||
MemberNick: null,
|
||||
DiscordId: null
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private static bool ValidateRequest(Request validationRequest) => (
|
||||
(string.IsNullOrWhiteSpace(validationRequest.Ssn) &&
|
||||
(string.IsNullOrWhiteSpace(validationRequest.Email) ||
|
||||
string.IsNullOrWhiteSpace(validationRequest.FirstName))) ||
|
||||
(!string.IsNullOrWhiteSpace(validationRequest.Email) &&
|
||||
string.IsNullOrWhiteSpace(validationRequest.FirstName))) ||
|
||||
(!string.IsNullOrWhiteSpace(validationRequest.Ssn) &&
|
||||
long.TryParse(validationRequest.Ssn ?? "", out _) == false);
|
||||
}
|
||||
|
|
@ -1,8 +1,12 @@
|
|||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using AuthAPI;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddHttpClient();
|
||||
builder.Services.Configure<AuthSettings>(
|
||||
builder.Configuration.GetSection("EnviromentVariables"));
|
||||
builder.Services.AddScoped<MemberValidationService>();
|
||||
builder.Services.ConfigureHttpJsonOptions(options =>
|
||||
{
|
||||
options.SerializerOptions.AllowTrailingCommas = true;
|
||||
|
|
@ -10,19 +14,25 @@ builder.Services.ConfigureHttpJsonOptions(options =>
|
|||
|
||||
var app = builder.Build();
|
||||
|
||||
var enviromentVariables = builder.Configuration.GetSection("EnviromentVariables");
|
||||
|
||||
app.MapGet("/validate", async ([FromBody] Request validationRequest, HttpClient httpClient) =>
|
||||
app.MapGet("/validate", async (
|
||||
[FromBody] Request validationRequest,
|
||||
HttpClient httpClient,
|
||||
MemberValidationService memberService,
|
||||
IOptions<AuthSettings> settings) =>
|
||||
{
|
||||
var request = Request.GetRequestWithApiKey(validationRequest, enviromentVariables);
|
||||
var request = memberService.GetRequestWithApiKey(validationRequest);
|
||||
if (request.IsFailure)
|
||||
return Results.BadRequest(request.Failure);
|
||||
|
||||
var response = await httpClient.PostAsJsonAsync(enviromentVariables["ApiUrl"], request.Success);
|
||||
var response = await httpClient.PostAsJsonAsync(
|
||||
settings.Value.ApiUrl,
|
||||
request.Success);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return Results.StatusCode((int)response.StatusCode);
|
||||
|
||||
return Results.Ok(response.Content.ReadAsStringAsync().Result.Contains("\"member_found\":true,"));
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
return Results.Ok(content.Contains("\"member_found\":true,"));
|
||||
})
|
||||
.WithName("ValidateMember");
|
||||
|
||||
|
|
|
|||
|
|
@ -7,40 +7,6 @@ public class Request(string? ssn, string? email, string? firstName)
|
|||
public string? Ssn { get; } = ssn;
|
||||
public string? Email { get; } = email;
|
||||
public string? FirstName { get; } = firstName;
|
||||
|
||||
public static Result GetRequestWithApiKey(Request request, IConfigurationSection enviromentVariables)
|
||||
{
|
||||
if (ValidateRequest(request))
|
||||
return Result.CreateFailure(
|
||||
"Invalid request. Either SSN or both Email and FirstName must be provided.");
|
||||
|
||||
return Result.CreateSuccess(
|
||||
new ValidateMembersRoot(
|
||||
new ValidateMembersRequest(
|
||||
"confirm_membership",
|
||||
enviromentVariables["AssociationNumber"] ?? "",
|
||||
enviromentVariables["ApiKey"] ?? "",
|
||||
YearId: null,
|
||||
Firstname: request.FirstName ?? null,
|
||||
Lastname: null,
|
||||
SSN: request.Ssn ?? null,
|
||||
Email: request.Email ?? null,
|
||||
Phone: null,
|
||||
MemberNick: null,
|
||||
DiscordId: null
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private static bool ValidateRequest(Request validationRequest) => (
|
||||
(string.IsNullOrWhiteSpace(validationRequest.Ssn) &&
|
||||
(string.IsNullOrWhiteSpace(validationRequest.Email) ||
|
||||
string.IsNullOrWhiteSpace(validationRequest.FirstName))) ||
|
||||
(!string.IsNullOrWhiteSpace(validationRequest.Email) &&
|
||||
string.IsNullOrWhiteSpace(validationRequest.FirstName))) ||
|
||||
(!string.IsNullOrWhiteSpace(validationRequest.Ssn) &&
|
||||
long.TryParse(validationRequest.Ssn ?? "", out _) == false);
|
||||
};
|
||||
|
||||
public record ValidateMembersRoot(
|
||||
|
|
|
|||
Loading…
Reference in a new issue