Browse Source

Create and pull model for mortgage insurance

master
Immanuel Onyeka 2 years ago
parent
commit
9c31707dd2
4 changed files with 94 additions and 31 deletions
  1. +16
    -4
      migrations/0_29092022_create_main_tables.sql
  2. +1
    -0
      migrations/reset.sql
  3. +25
    -12
      migrations/seed.sql
  4. +52
    -15
      skouter.go

+ 16
- 4
migrations/0_29092022_create_main_tables.sql View File

@@ -89,10 +89,6 @@ CREATE TABLE loan (
ltv FLOAT(5, 2) DEFAULT 0,
dti FLOAT(5, 2) DEFAULT 1,
hoi INT DEFAULT 0, /* Hazard insurance annual payments */
mi_name VARCHAR(50) NOT NULL,
/* Mortgage insurance title shown in menu */
mi_amount INT, /* Mortgage insurance amount */
lender VARCHAR(30) DEFAULT '',
name VARCHAR(30) DEFAULT '',
PRIMARY KEY (`id`),
FOREIGN KEY (estimate_id) REFERENCES estimate(id),
@@ -100,6 +96,22 @@ CREATE TABLE loan (
ON UPDATE RESTRICT
);

CREATE TABLE mi (
id INT AUTO_INCREMENT,
loan_id INT,
type VARCHAR(10) NOT NULL DEFAULT "",
label VARCHAR(30) NOT NULL DEFAULT "",
lender VARCHAR(20) NOT NULL DEFAULT "",
rate INT DEFAULT 0,
premium INT DEFAULT 0,
upfront INT DEFAULT 0,
five_year_total INT DEFAULT 0,
initial_premium INT DEFAULT 0,
initial_rate INT DEFAULT 0,
PRIMARY KEY (`id`),
FOREIGN KEY (loan_id) REFERENCES loan(id)
);

/* template = true fees are saved for users or branches. If template or default
* are true, estimate_id should be null.*/
CREATE TABLE fee (


+ 1
- 0
migrations/reset.sql View File

@@ -1,3 +1,4 @@
DROP TABLE IF EXISTS mi;
DROP TABLE IF EXISTS fee;
DROP TABLE IF EXISTS fee_template;
DROP TABLE IF EXISTS loan;


+ 25
- 12
migrations/seed.sql View File

@@ -222,9 +222,6 @@ INSERT INTO loan (
ltv,
dti,
hoi,
mi_name,
mi_amount,
lender,
name
) VALUES
(
@@ -236,9 +233,6 @@ INSERT INTO loan (
88.00,
5.00,
0,
"custom mi",
234000,
"National MI",
"For client 1"
),
(
@@ -250,9 +244,6 @@ INSERT INTO loan (
90.00,
6.70,
0,
"maybe MGIC",
234000,
"MGIC",
"For client 2"
),
(
@@ -264,8 +255,30 @@ INSERT INTO loan (
95.00,
4.90,
0,
"custom mi",
234000,
"another lender",
"Random name"
);

INSERT INTO mi (
loan_id,
type,
label,
lender,
rate,
premium,
upfront,
five_year_total,
initial_premium,
initial_rate
) VALUES
(
1,
"BPM",
"National MI test loan",
"National MI",
28,
77000,
0,
4620000,
77000,
28
);

+ 52
- 15
skouter.go View File

@@ -69,16 +69,22 @@ type Loan struct {
Dti float32 `json:"dti"`
Hoi int `json:"hoi"`
Interest int `json:"interest"`
Lender string `json:"lender"`
MiName string `json:"miName"`
MiAmount int `json:"miAmount"`
Mi map[string]interface{} `json:"mi"`
Mi MI `json:"mi"`
Fees []Fee `json:"fees"`
Name string `json:"name"`
}

type MI struct {
Id int
Type string
Label string
Lender string
Rate float32
Premium float32
Upfront float32
FiveYearTotal float32
InitialAllInPremium float32
InitialAllInRate float32
InitialAmount float32
}

type Estimate struct {
@@ -299,12 +305,41 @@ func getFeesTemp(db *sql.DB, user int) ([]FeeTemplate, error) {
return fees, nil
}

func getMi(db *sql.DB, loan int) (MI, error) {
var mi MI

query := `SELECT
type, label, lender, rate, premium, upfront, five_year_total,
initial_premium, initial_rate, initial_amount
FROM mi WHERE loan_id = ?`

row := db.QueryRow(query, loan)

if err := row.Scan(
&mi.Type,
&mi.Label,
&mi.Lender,
&mi.Rate,
&mi.Premium,
&mi.Upfront,
&mi.FiveYearTotal,
&mi.InitialAllInPremium,
&mi.InitialAllInRate,
&mi.InitialAmount,
)
err != nil {
return mi, err
}

return mi, nil
}

func getLoans(db *sql.DB, estimate int) ([]Loan, error) {
var loans []Loan

query := `SELECT
l.id, l.amount, l.term, l.interest, l.ltv, l.dti, l.hoi, l.mi_name,
l.mi_amount, lt.id, lt.user_id, lt.branch_id, lt.name
l.id, l.amount, l.term, l.interest, l.ltv, l.dti, l.hoi,
lt.id, lt.user_id, lt.branch_id, lt.name
FROM loan l INNER JOIN loan_type lt ON l.type_id = lt.id
WHERE l.estimate_id = ?
`
@@ -327,8 +362,6 @@ func getLoans(db *sql.DB, estimate int) ([]Loan, error) {
&loan.Ltv,
&loan.Dti,
&loan.Hoi,
&loan.MiName,
&loan.MiAmount,
&loan.Type.Id,
&loan.Type.User,
&loan.Type.Branch,
@@ -337,7 +370,11 @@ func getLoans(db *sql.DB, estimate int) ([]Loan, error) {
err != nil {
return loans, fmt.Errorf("Loans scanning error: %v", err)
}

mi, err := getMi(db, loan.Id)
if err != nil {
return loans, err
}
loan.Mi = mi
loans = append(loans, loan)
}
@@ -365,7 +402,8 @@ func getBorrower(db *sql.DB, id int) (Borrower, error) {
return borrower, nil
}

func getMi(db *sql.DB, estimate *Estimate, pos int) (*Estimate) {
// Query Lender APIs and parse responses into MI structs
func fetchMi(db *sql.DB, estimate *Estimate, pos int) []MI {
var err error
var loan Loan = estimate.Loans[pos]

@@ -447,11 +485,10 @@ func getMi(db *sql.DB, estimate *Estimate, pos int) (*Estimate) {

if resp.StatusCode != 200 {
log.Printf("the status: %v\nthe resp: %v\n the req: %v\n the body: %v\n",
resp.Status, resp, req.Body, bytes.NewBuffer(body))
resp.Status, resp, req.Body, bytes.NewBuffer(body))
} else {
json.NewDecoder(resp.Body).Decode(&res)
log.Printf("the valid resp: %v", res)
estimate.Loans[pos].Mi = res
// estimate.Loans[pos].Mi = res
}

return estimate
@@ -530,7 +567,7 @@ func api(w http.ResponseWriter, r *http.Request) {
break
}

json.NewEncoder(w).Encode(getMi(db, &est, 0))
json.NewEncoder(w).Encode(fetchMi(db, &est, 0).Loans[0].Mi)

// if err != nil {
// json.NewEncoder(w).Encode(err)


Loading…
Cancel
Save