From 274ea0581d8074ac767459e902318954c2672d6f Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Sun, 29 Oct 2023 03:10:08 -0400 Subject: [PATCH] Fix duplicate loan_id error At most one result should exist for each loan stored but not redeclaring the 'estimate' variable within the seed loop caused the loan entries of the first estimate to be copied onto the second, which caused an error while seeding result entries. --- components/estimates.vue | 1 + migrations/0_29092022_setup_tables.sql | 2 +- skouter.go | 44 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/components/estimates.vue b/components/estimates.vue index 395a596..8f98a91 100644 --- a/components/estimates.vue +++ b/components/estimates.vue @@ -149,6 +149,7 @@ function summarize() { response.text().then(t => console.log(t)) } }).then(result => { + console.log(result) if (!result || !result.length) return // Exit if token is invalid or no fees are saved console.log('done', result) }) diff --git a/migrations/0_29092022_setup_tables.sql b/migrations/0_29092022_setup_tables.sql index 4703d55..945a4a4 100644 --- a/migrations/0_29092022_setup_tables.sql +++ b/migrations/0_29092022_setup_tables.sql @@ -178,7 +178,7 @@ CREATE TABLE fee_template ( CREATE TABLE estimate_result ( id INT AUTO_INCREMENT, - loan_id INT NOT NULL, + loan_id INT UNIQUE NOT NULL, loan_payment INT NOT NULL, total_monthly INT NOT NULL, total_fees INT NOT NULL, diff --git a/skouter.go b/skouter.go index c18e9c9..51628be 100644 --- a/skouter.go +++ b/skouter.go @@ -1619,6 +1619,44 @@ func getResults(db *sql.DB, e int, id int) ( []Result, error ) { return results, nil } +// Retrieve an estimate result with a specified loan id +func getResult(db *sql.DB, loan int) ( Result, error ) { + var result Result + var query string + var err error + + query = `SELECT + r.id, + loan_id, + loan_payment, + total_monthly, + total_fees, + total_credits, + cash_to_close + FROM estimate_result r + INNER JOIN loan + ON r.loan_id = loan.id + WHERE loan.Id = ? + ` + row := db.QueryRow(query, loan) + + err = row.Scan( + &result.Id, + &result.LoanId, + &result.LoanPayment, + &result.TotalMonthly, + &result.TotalFees, + &result.TotalCredits, + &result.CashToClose, + ) + + if err != nil { + return result, err + } + + return result, nil +} + // Must have an estimate ID 'e', but not necessarily a loan id 'id' func getLoans(db *sql.DB, e int, id int) ( []Loan, error ) { var loans []Loan @@ -1680,6 +1718,11 @@ func getLoans(db *sql.DB, e int, id int) ( []Loan, error ) { } loan.Fees = fees + loan.Result, err = getResult(db, loan.Id) + if err != nil { + return loans, err + } + loan.Type, err = getLoanType(db, loan.Type.Id) if err != nil { return loans, err @@ -2571,6 +2614,7 @@ func seedEstimates(db *sql.DB, users []User, ltypes []LoanType) []Estimate { var err error for i := 0; i < 15; i++ { + estimate = Estimate{} estimate.User = users[gofakeit.Number(0, len(users) - 1)].Id estimate.Borrower = Borrower{ Credit: gofakeit.Number(600, 800),