From ae79dbacdd1ada7e1ff09427bb80a6f6ac0e56e7 Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Tue, 22 Nov 2022 16:51:19 -0500 Subject: [PATCH] Fix loan query and its errors --- migrations/0_29092022_create_main_tables.sql | 4 +- migrations/seed.sql | 71 +++++++++++--------- skouter.go | 56 ++++++++------- 3 files changed, 73 insertions(+), 58 deletions(-) diff --git a/migrations/0_29092022_create_main_tables.sql b/migrations/0_29092022_create_main_tables.sql index 6f56d96..076600b 100644 --- a/migrations/0_29092022_create_main_tables.sql +++ b/migrations/0_29092022_create_main_tables.sql @@ -80,7 +80,7 @@ CREATE TABLE estimate ( ); CREATE TABLE loan ( - id INT, + id INT AUTO_INCREMENT, estimate_id INT NOT NULL, type_id INT NOT NULL, amount INT NOT NULL, @@ -94,7 +94,7 @@ CREATE TABLE loan ( name VARCHAR(30) DEFAULT '', PRIMARY KEY (`id`), FOREIGN KEY (estimate_id) REFERENCES estimate(id), - FOREIGN KEY (loan_type_id) REFERENCES loan_type(id) + FOREIGN KEY (type_id) REFERENCES loan_type(id) ON UPDATE RESTRICT ); diff --git a/migrations/seed.sql b/migrations/seed.sql index 37af5ec..c75bb59 100644 --- a/migrations/seed.sql +++ b/migrations/seed.sql @@ -175,15 +175,48 @@ INSERT IGNORE INTO borrower ( INSERT INTO estimate ( user_id, borrower_id, - comparison_id, transaction, - loan_type_id, - loan_amount, price, property, occupancy, zip, - pud, + pud +) VALUES +( + (SELECT id FROM user WHERE email="test@example.com" LIMIT 1), + (SELECT id FROM borrower ORDER BY id DESC LIMIT 1), + 'Purchase', + 100000000, + 1, + 1, + '95051', + false +), +( + (SELECT id FROM user WHERE email="manager@example.com" LIMIT 1), + (SELECT id FROM borrower ORDER BY id DESC LIMIT 1), + 'Purchase', + 25000000, + 2, + 1, + '95051', + false +), +( + (SELECT id FROM user WHERE email="test@example.com" LIMIT 1), + (SELECT id FROM borrower ORDER BY id DESC LIMIT 1), + 'Refinance', + 50000000, + 3, + 2, + '95051', + false +); + +INSERT INTO loan ( + estimate_id, + type_id, + amount, term, interest, hoi, @@ -193,17 +226,9 @@ INSERT INTO estimate ( name ) VALUES ( - (SELECT id FROM user WHERE email="test@example.com" LIMIT 1), - (SELECT id FROM borrower ORDER BY id DESC LIMIT 1), - 0, - 'Purchase', + 1, (SELECT id FROM loan_type WHERE name="Conventional"), 3300000, - 100000000, - 1, - 1, - '95051', - false, 30, 375, 10000, @@ -213,17 +238,9 @@ INSERT INTO estimate ( "For client 1" ), ( - (SELECT id FROM user WHERE email="manager@example.com" LIMIT 1), - (SELECT id FROM borrower ORDER BY id DESC LIMIT 1), - 0, - 'Purchase', + 1, (SELECT id FROM loan_type WHERE name="FHA"), 2510000, - 25000000, - 2, - 1, - '95051', - false, 30, 300, 10000, @@ -233,17 +250,9 @@ INSERT INTO estimate ( "For client 2" ), ( - (SELECT id FROM user WHERE email="test@example.com" LIMIT 1), - (SELECT id FROM borrower ORDER BY id DESC LIMIT 1), - 0, - 'Refinance', + 2, (SELECT id FROM loan_type WHERE name="USDA"), 8000000, - 50000000, - 3, - 2, - '95051', - false, 10, 125, 15000, diff --git a/skouter.go b/skouter.go index 650cd20..76a5e29 100644 --- a/skouter.go +++ b/skouter.go @@ -34,7 +34,7 @@ type FeeTemplate struct { Branch int `json:"branch"` Amount int `json:"amount"` Perc int `json:"perc"` - Ftype string `json:"type"` + Type string `json:"type"` Notes string `json:"notes"` Name string `json:"name"` Category string `json:"category"` @@ -63,7 +63,7 @@ type Loan struct { Id int `json:id` EstimateId int `json:estimate_id` Type LoanType `json:"loanType"` - LoanAmount int `json:"loanAmount"` + Amount int `json:"loanAmount"` Term int `json:"term"` Ltv int `json:"ltv"` Dti int `json:"dti"` @@ -224,14 +224,14 @@ func getFees(db *sql.DB, loan int) ([]Fee, error) { defer rows.Close() for rows.Next() { - var fee FeeTemplate + var fee Fee if err := rows.Scan( &fee.Id, &fee.LoanId, &fee.Amount, &fee.Perc, - &fee.Ftype, + &fee.Type, &fee.Notes, &fee.Name, &fee.Category, @@ -289,15 +289,17 @@ func getFeesTemp(db *sql.DB, user int) ([]FeeTemplate, error) { return fees, nil } -func getLoans(db *sql.DB, estimate int) { +func getLoans(db *sql.DB, estimate int) ([]Loan, error) { var loans []Loan - rows, err := db.Query( - "SELECT loan.id, loan.amount, loan.term, loan.interest, loan.ltv, - loan.dti, loan.hoi, loan.mi_name, loan.mi_amount FROM loan " + - "INNER JOIN loan_type ON loan.type_id = loan_type(id)" + - "WHERE loan.estimate_id = ?", - estimate) + query := `SELECT + loan.id, loan.amount, loan.term, loan.interest, loan.ltv, loan.dti, + loan.hoi, loan.mi_name, loan.mi_amount + loan_type.id, loan_type.user_id, loan_type.branch_id, loan_type.name + FROM loan INNER JOIN loan_type ON loan.type_id = loan_type(id) + WHERE loan.estimate_id = ? + ` + rows, err := db.Query(query, estimate) if err != nil { return nil, fmt.Errorf("Loan query error %v", err) @@ -310,27 +312,31 @@ func getLoans(db *sql.DB, estimate int) { if err := rows.Scan( &loan.Id, - &loan.EstimateId, - &loan.Branch, - &fee.Amount, - &fee.Perc, - &fee.Ftype, - &fee.Notes, - &fee.Name, - &fee.Category, - &fee.Auto) + &loan.Amount, + &loan.Term, + &loan.Interest, + &loan.Ltv, + &loan.Dti, + &loan.Hoi, + &loan.MiName, + &loan.MiAmount, + &loan.Type.Id, + &loan.Type.User, + &loan.Type.Branch, + &loan.Type.Name, + ) err != nil { - return nil, fmt.Errorf("FeesTemplate scanning error: %v", err) + return loans, fmt.Errorf("Loans scanning error: %v", err) } - fees = append(fees, fee) + loans = append(loans, loan) } est, err := getEstimate(db, 1) - fmt.Printf("the estimate: %v,\nthe error %v\n", est, err) + fmt.Printf("the loan: %v,\nthe error %v\n", est, err) // getMi(db, getEstimate(db, 1), getBorrower(db, 1)) - return fees, nil + return loans, nil } func getBorrower(db *sql.DB, id int) (Borrower, error) { @@ -403,7 +409,7 @@ func getMi(db *sql.DB, estimate Estimate, borrower Borrower) { "propertyTypeCode": propertyCodes[estimate.Property], "occupancyTypeCode": "PRS", "loanPurposeCode": "PUR", - "loanAmount": strconv.Itoa(estimate.LoanAmount / 100), + "loanAmount": strconv.Itoa(500000 / 100), "loanToValue": "LTV95", "amortizationTerm": "A30", "loanTypeCode": "FXD",