feat(api,core,infra): refactor endpoint routing and add institution account discovery
- Move login and refresh endpoints under a unified /auth route group
- Relocate institution connection logic from /accounts to /institutions
- Implement GET /institutions/{id}/available to fetch real-time Plaid
accounts
- Introduce HttpRequestBuilder utility to streamline integration testing
- Enhance PlaidService with GetAccountsAsync to support account fetching
- Clean up global usings and project structure for better domain
isolation
This commit is contained in:
@@ -10,7 +10,6 @@ internal static class AccountsExtensions
|
||||
.RequireAuthorization();
|
||||
|
||||
accountsGroup.MapAddEndpoint();
|
||||
accountsGroup.MapConnectEndpoint();
|
||||
|
||||
return accountsGroup;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
using FiscalOS.Core.Accounts;
|
||||
|
||||
namespace FiscalOS.API.Accounts.Add;
|
||||
|
||||
internal static class Endpoint
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
namespace FiscalOS.API.Accounts.Add;
|
||||
|
||||
public record Request : IValidatableObject
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
using FiscalOS.Core.Accounts;
|
||||
|
||||
namespace FiscalOS.API.Accounts.Connect;
|
||||
|
||||
internal static class Endpoint
|
||||
{
|
||||
private const string Route = "/connect";
|
||||
|
||||
public static RouteHandlerBuilder MapConnectEndpoint(this RouteGroupBuilder groupBuilder)
|
||||
{
|
||||
return groupBuilder.MapPost(Route, HandleAsync);
|
||||
}
|
||||
|
||||
private static async Task<IResult> HandleAsync(
|
||||
HttpContext httpContext,
|
||||
[FromBody] Request request,
|
||||
[FromServices] AppDbContext appDbContext,
|
||||
[FromServices] PlaidService plaidService,
|
||||
[FromServices] IEncryptor encryptor,
|
||||
CancellationToken ct
|
||||
)
|
||||
{
|
||||
var userId = httpContext.GetUserId();
|
||||
|
||||
var user = await appDbContext.Users
|
||||
.Include(u => u.Institutions
|
||||
.Where(
|
||||
i => i.Metadata is PlaidMetadata &&
|
||||
((PlaidMetadata)i.Metadata).PlaidId == request.PlaidInstitutionId
|
||||
)
|
||||
)
|
||||
.ThenInclude(i => i.Metadata)
|
||||
.SingleOrDefaultAsync(u => u.Id == userId, ct);
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
if (user.Institutions.Any())
|
||||
{
|
||||
return Results.Conflict();
|
||||
}
|
||||
|
||||
var (itemId, accessToken) = await plaidService.ExchangeTokenAsync(request.PublicToken);
|
||||
var item = await plaidService.GetItemAsync(accessToken);
|
||||
|
||||
var encryptedAccessToken = await encryptor.EncryptAsyncFor(user, accessToken, ct);
|
||||
|
||||
var plaidMetadata = PlaidMetadata.From(item.InstitutionId, item.InstitutionName, encryptedAccessToken);
|
||||
var institution = Institution.From(item.InstitutionName, plaidMetadata);
|
||||
|
||||
user.AddInstitution(institution);
|
||||
|
||||
await appDbContext.SaveChangesAsync(ct);
|
||||
|
||||
return Results.Ok();
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
namespace FiscalOS.API.Accounts.Connect;
|
||||
|
||||
public record Request : IValidatableObject
|
||||
{
|
||||
public string PublicToken { get; init; } = string.Empty;
|
||||
public string PlaidInstitutionId { get; init; } = string.Empty;
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(PublicToken))
|
||||
{
|
||||
var fieldName = nameof(PublicToken);
|
||||
yield return new($"The {fieldName} field is required.", [fieldName]);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(PlaidInstitutionId))
|
||||
{
|
||||
var fieldName = nameof(PlaidInstitutionId);
|
||||
yield return new($"The {fieldName} field is required.", [fieldName]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user