diff --git a/src/FiscalOS.API/Institutions/Get/Endpoint.cs b/src/FiscalOS.API/Institutions/Get/Endpoint.cs index a315cd2..5df5a15 100644 --- a/src/FiscalOS.API/Institutions/Get/Endpoint.cs +++ b/src/FiscalOS.API/Institutions/Get/Endpoint.cs @@ -18,6 +18,7 @@ internal static class Endpoint var user = await appDbContext.Users .Include(u => u.Institutions) + .ThenInclude(i => i.Accounts) .FirstOrDefaultAsync(u => u.Id == userId); if (user is null) @@ -26,7 +27,7 @@ internal static class Endpoint } var institutionDtos = user.Institutions - .Select(InstitutionDto.FromInstitution) + .Select(InstitutionDto.From) .OrderBy(dto => dto.Name); return Results.Ok(Response.From(institutionDtos)); diff --git a/src/FiscalOS.API/Institutions/Get/Response.cs b/src/FiscalOS.API/Institutions/Get/Response.cs index 85d5dac..934bcec 100644 --- a/src/FiscalOS.API/Institutions/Get/Response.cs +++ b/src/FiscalOS.API/Institutions/Get/Response.cs @@ -18,22 +18,44 @@ internal sealed record Response } } +internal sealed record AccountDto +{ + public string Id { get; init; } = string.Empty; + public string Name { get; init; } = string.Empty; + + [JsonConstructor] + private AccountDto() + { + } + + public static AccountDto From(Account account) + { + return new() + { + Id = account.Id.ToString(), + Name = account.Name, + }; + } +} + internal sealed record InstitutionDto { public string Id { get; init; } = string.Empty; public string Name { get; init; } = string.Empty; + public IEnumerable Accounts { get; init; } = []; [JsonConstructor] private InstitutionDto() { } - public static InstitutionDto FromInstitution(Institution institution) + public static InstitutionDto From(Institution institution) { - return new InstitutionDto + return new() { Id = institution.Id.ToString(), Name = institution.Name, + Accounts = institution.Accounts.Select(AccountDto.From), }; } } \ No newline at end of file diff --git a/src/FiscalOS.API/Institutions/GetAvailable/Endpoint.cs b/src/FiscalOS.API/Institutions/GetAvailable/Endpoint.cs index 18e4c86..5845067 100644 --- a/src/FiscalOS.API/Institutions/GetAvailable/Endpoint.cs +++ b/src/FiscalOS.API/Institutions/GetAvailable/Endpoint.cs @@ -44,7 +44,7 @@ internal static class Endpoint var accessToken = await encryptor.DecryptAsyncFor(user, plaidMetadata.EncryptedAccessToken, ct); var accounts = await plaidService.GetAccountsAsync(accessToken); - var accountsDtos = accounts.Select(AccountDto.FromPlaidAccount); + var accountsDtos = accounts.Select(a => AvailableAccountDto.From(plaidMetadata, a)); return Results.Ok(Response.From(accountsDtos)); } diff --git a/src/FiscalOS.API/Institutions/GetAvailable/Response.cs b/src/FiscalOS.API/Institutions/GetAvailable/Response.cs index a7c6dd1..ba81b16 100644 --- a/src/FiscalOS.API/Institutions/GetAvailable/Response.cs +++ b/src/FiscalOS.API/Institutions/GetAvailable/Response.cs @@ -2,14 +2,14 @@ namespace FiscalOS.API.Institutions.GetAvailable; internal sealed record Response { - public IEnumerable Accounts { get; init; } = []; + public IEnumerable Accounts { get; init; } = []; [JsonConstructor] private Response() { } - public static Response From(IEnumerable accounts) + public static Response From(IEnumerable accounts) { return new Response { @@ -18,8 +18,9 @@ internal sealed record Response } } -internal sealed record AccountDto +internal sealed record AvailableAccountDto { + public string ProviderInstitutionId { get; init; } = string.Empty; public string ProviderId { get; init; } = string.Empty; public string ProviderName { get; init; } = string.Empty; public decimal CurrentBalance { get; init; } @@ -27,14 +28,18 @@ internal sealed record AccountDto public string CurrencyCode { get; init; } = string.Empty; [JsonConstructor] - private AccountDto() + private AvailableAccountDto() { } - public static AccountDto FromPlaidAccount(Going.Plaid.Entity.Account plaidAccount) + public static AvailableAccountDto From( + PlaidMetadata plaidMetadata, + Going.Plaid.Entity.Account plaidAccount + ) { - return new AccountDto + return new AvailableAccountDto { + ProviderInstitutionId = plaidMetadata.PlaidId, ProviderId = plaidAccount.AccountId, ProviderName = plaidAccount.Name, AvailableBalance = plaidAccount.Available,