Auto-populate loan details from Plaid liabilities and pre-fill setup form
Fetch loan data (mortgage/student) from Plaid's Liabilities API during sync and auto-create LoanDetail records. For accounts without liabilities data, pre-populate the setup form with balance, interest rate, and smart term defaults. Also fixes maturity date computation in the frontend form. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Purrse.Core.DTOs;
|
||||
using Purrse.Core.Enums;
|
||||
using Purrse.Core.Interfaces.Services;
|
||||
using Purrse.Core.Models;
|
||||
using Purrse.Data;
|
||||
@@ -154,6 +155,28 @@ public class LoanService : ILoanService
|
||||
totalInterestOriginal - totalInterestNew, monthsSaved, newEntries);
|
||||
}
|
||||
|
||||
public async Task<LoanDefaultsResponse> GetLoanDefaultsAsync(Guid userId, Guid accountId)
|
||||
{
|
||||
var account = await _db.Accounts
|
||||
.Include(a => a.LinkedAccounts)
|
||||
.FirstOrDefaultAsync(a => a.Id == accountId && a.UserId == userId)
|
||||
?? throw new KeyNotFoundException("Account not found");
|
||||
|
||||
var originalBalance = Math.Abs(account.Balance);
|
||||
var interestRate = account.InterestRate ?? 0;
|
||||
var hasSyncData = account.LinkedAccounts.Any() && (account.InterestRate.HasValue || account.Balance != 0);
|
||||
|
||||
var termMonths = account.Type switch
|
||||
{
|
||||
AccountType.Mortgage => 360,
|
||||
AccountType.AutoLoan => 60,
|
||||
AccountType.PersonalLoan => 60,
|
||||
_ => 360
|
||||
};
|
||||
|
||||
return new LoanDefaultsResponse(originalBalance, interestRate, termMonths, hasSyncData);
|
||||
}
|
||||
|
||||
private static List<AmortizationEntry> GenerateAmortizationSchedule(LoanDetail loan)
|
||||
{
|
||||
var entries = new List<AmortizationEntry>();
|
||||
|
||||
Reference in New Issue
Block a user