|
@@ -189,6 +189,13 @@ type Estimate struct { |
|
|
Loans []Loan `json:"loans"` |
|
|
Loans []Loan `json:"loans"` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type ETemplate struct { |
|
|
|
|
|
Id int `json:"id"` |
|
|
|
|
|
Estimate Estimate `json:"estimate"` |
|
|
|
|
|
UserId int `json:"userId"` |
|
|
|
|
|
BranchId int `json:"branchId"` |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
type Report struct { |
|
|
type Report struct { |
|
|
Title string |
|
|
Title string |
|
|
Name string |
|
|
Name string |
|
@@ -574,6 +581,19 @@ func deleteFeeTemp(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
if err != nil { w.WriteHeader(500); return } |
|
|
if err != nil { w.WriteHeader(500); return } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func deleteEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
|
|
var estimate Estimate |
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&estimate) |
|
|
|
|
|
if err != nil { w.WriteHeader(422); return } |
|
|
|
|
|
|
|
|
|
|
|
claims, err := getClaims(r) |
|
|
|
|
|
err = estimate.del(db, claims.Id) |
|
|
|
|
|
if err != nil { http.Error(w, err.Error(), 500); return } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getMi(db *sql.DB, loan int) (MI, error) { |
|
|
func getMi(db *sql.DB, loan int) (MI, error) { |
|
|
var mi MI |
|
|
var mi MI |
|
|
|
|
|
|
|
@@ -608,25 +628,23 @@ func getMi(db *sql.DB, loan int) (MI, error) { |
|
|
return mi, nil |
|
|
return mi, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func getBorrower(db *sql.DB, id int) (Borrower, error) { |
|
|
|
|
|
var borrower Borrower |
|
|
|
|
|
|
|
|
func (estimate *Estimate) getBorrower(db *sql.DB) error { |
|
|
|
|
|
query := `SELECT id, credit_score, monthly_income, num FROM borrower |
|
|
|
|
|
WHERE estimate_id = ? LIMIT 1` |
|
|
|
|
|
|
|
|
row := db.QueryRow( |
|
|
|
|
|
"SELECT * FROM borrower " + |
|
|
|
|
|
"WHERE id = ? LIMIT 1", |
|
|
|
|
|
id) |
|
|
|
|
|
|
|
|
row := db.QueryRow(query, estimate.Id) |
|
|
|
|
|
|
|
|
if err := row.Scan( |
|
|
if err := row.Scan( |
|
|
&borrower.Id, |
|
|
|
|
|
&borrower.Credit, |
|
|
|
|
|
&borrower.Income, |
|
|
|
|
|
&borrower.Num, |
|
|
|
|
|
|
|
|
&estimate.Borrower.Id, |
|
|
|
|
|
&estimate.Borrower.Credit, |
|
|
|
|
|
&estimate.Borrower.Income, |
|
|
|
|
|
&estimate.Borrower.Num, |
|
|
) |
|
|
) |
|
|
err != nil { |
|
|
err != nil { |
|
|
return borrower, fmt.Errorf("Borrower scanning error: %v", err) |
|
|
|
|
|
|
|
|
return fmt.Errorf("Borrower scanning error: %v", err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return borrower, nil |
|
|
|
|
|
|
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Query Lender APIs and parse responses into MI structs |
|
|
// Query Lender APIs and parse responses into MI structs |
|
@@ -1740,6 +1758,14 @@ func getLoans(db *sql.DB, e int, id int) ( []Loan, error ) { |
|
|
return loans, nil |
|
|
return loans, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getEstimate(db *sql.DB, id int) ( Estimate, error ) { |
|
|
|
|
|
estimates, err := getEstimates(db, id, 0) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { return Estimate{}, err } |
|
|
|
|
|
|
|
|
|
|
|
return estimates[0], nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func getEstimates(db *sql.DB, id int, user int) ( []Estimate, error ) { |
|
|
func getEstimates(db *sql.DB, id int, user int) ( []Estimate, error ) { |
|
|
var estimates []Estimate |
|
|
var estimates []Estimate |
|
|
var query string |
|
|
var query string |
|
@@ -1749,7 +1775,6 @@ func getEstimates(db *sql.DB, id int, user int) ( []Estimate, error ) { |
|
|
query = `SELECT |
|
|
query = `SELECT |
|
|
id, |
|
|
id, |
|
|
user_id, |
|
|
user_id, |
|
|
borrower_id, |
|
|
|
|
|
transaction, |
|
|
transaction, |
|
|
price, |
|
|
price, |
|
|
property, |
|
|
property, |
|
@@ -1773,7 +1798,6 @@ func getEstimates(db *sql.DB, id int, user int) ( []Estimate, error ) { |
|
|
if err := rows.Scan( |
|
|
if err := rows.Scan( |
|
|
&estimate.Id, |
|
|
&estimate.Id, |
|
|
&estimate.User, |
|
|
&estimate.User, |
|
|
&estimate.Borrower.Id, |
|
|
|
|
|
&estimate.Transaction, |
|
|
&estimate.Transaction, |
|
|
&estimate.Price, |
|
|
&estimate.Price, |
|
|
&estimate.Property, |
|
|
&estimate.Property, |
|
@@ -1785,11 +1809,10 @@ func getEstimates(db *sql.DB, id int, user int) ( []Estimate, error ) { |
|
|
return estimates, err |
|
|
return estimates, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
borrower, err := getBorrower(db, estimate.Borrower.Id) |
|
|
|
|
|
|
|
|
err := estimate.getBorrower(db) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return estimates, err |
|
|
return estimates, err |
|
|
} |
|
|
} |
|
|
estimate.Borrower = borrower |
|
|
|
|
|
estimates = append(estimates, estimate) |
|
|
estimates = append(estimates, estimate) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -1804,33 +1827,80 @@ func getEstimates(db *sql.DB, id int, user int) ( []Estimate, error ) { |
|
|
return estimates, nil |
|
|
return estimates, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getETemplate(db *sql.DB, id int, user int) ( []ETemplate, error ) { |
|
|
|
|
|
var eTemplates []ETemplate |
|
|
|
|
|
var query string |
|
|
|
|
|
var rows *sql.Rows |
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
query = `SELECT |
|
|
|
|
|
id, |
|
|
|
|
|
estimate_id, |
|
|
|
|
|
user_id, |
|
|
|
|
|
branch_id, |
|
|
|
|
|
FROM estimate_template WHERE id = CASE @e := ? WHEN 0 THEN id ELSE @e END AND |
|
|
|
|
|
user_id = CASE @e := ? WHEN 0 THEN user_id ELSE @e END |
|
|
|
|
|
` |
|
|
|
|
|
rows, err = db.Query(query, id, user) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return eTemplates, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
defer rows.Close() |
|
|
|
|
|
|
|
|
|
|
|
for rows.Next() { |
|
|
|
|
|
var e ETemplate |
|
|
|
|
|
|
|
|
|
|
|
if err := rows.Scan( |
|
|
|
|
|
&e.Id, |
|
|
|
|
|
&e.Estimate.Id, |
|
|
|
|
|
&e.UserId, |
|
|
|
|
|
&e.BranchId, |
|
|
|
|
|
) |
|
|
|
|
|
err != nil { |
|
|
|
|
|
return eTemplates, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
e.Estimate, err = getEstimate(db, e.Estimate.Id) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return eTemplates, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
eTemplates = append(eTemplates, e) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return eTemplates, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// Accepts a borrower struct and returns the id of the inserted borrower and |
|
|
// Accepts a borrower struct and returns the id of the inserted borrower and |
|
|
// any related error. |
|
|
// any related error. |
|
|
func insertBorrower(db *sql.DB, borrower Borrower) (int, error) { |
|
|
|
|
|
|
|
|
func (estimate *Estimate) insertBorrower(db *sql.DB) error { |
|
|
var query string |
|
|
var query string |
|
|
var row *sql.Row |
|
|
var row *sql.Row |
|
|
var err error |
|
|
var err error |
|
|
var id int // Inserted loan's id |
|
|
|
|
|
|
|
|
|
|
|
query = `INSERT INTO borrower |
|
|
query = `INSERT INTO borrower |
|
|
( |
|
|
( |
|
|
|
|
|
estimate_id, |
|
|
credit_score, |
|
|
credit_score, |
|
|
monthly_income, |
|
|
monthly_income, |
|
|
num |
|
|
num |
|
|
) |
|
|
) |
|
|
VALUES (?, ?, ?) |
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?) |
|
|
RETURNING id |
|
|
RETURNING id |
|
|
` |
|
|
` |
|
|
row = db.QueryRow(query, |
|
|
row = db.QueryRow(query, |
|
|
borrower.Credit, |
|
|
|
|
|
borrower.Income, |
|
|
|
|
|
borrower.Num, |
|
|
|
|
|
|
|
|
estimate.Id, |
|
|
|
|
|
estimate.Borrower.Credit, |
|
|
|
|
|
estimate.Borrower.Income, |
|
|
|
|
|
estimate.Borrower.Num, |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
err = row.Scan(&id) |
|
|
|
|
|
if err != nil { return 0, err } |
|
|
|
|
|
|
|
|
err = row.Scan(&estimate.Borrower.Id) |
|
|
|
|
|
if err != nil { return err } |
|
|
|
|
|
|
|
|
return id, nil |
|
|
|
|
|
|
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func insertMi(db *sql.DB, mi MI) (int, error) { |
|
|
func insertMi(db *sql.DB, mi MI) (int, error) { |
|
@@ -1968,13 +2038,9 @@ func (estimate *Estimate) insertEstimate(db *sql.DB) (error){ |
|
|
var err error |
|
|
var err error |
|
|
// var id int // Inserted estimate's id |
|
|
// var id int // Inserted estimate's id |
|
|
|
|
|
|
|
|
estimate.Borrower.Id, err = insertBorrower(db, estimate.Borrower) |
|
|
|
|
|
if err != nil { return err } |
|
|
|
|
|
|
|
|
|
|
|
query = `INSERT INTO estimate |
|
|
query = `INSERT INTO estimate |
|
|
( |
|
|
( |
|
|
user_id, |
|
|
user_id, |
|
|
borrower_id, |
|
|
|
|
|
transaction, |
|
|
transaction, |
|
|
price, |
|
|
price, |
|
|
property, |
|
|
property, |
|
@@ -1982,12 +2048,11 @@ func (estimate *Estimate) insertEstimate(db *sql.DB) (error){ |
|
|
zip, |
|
|
zip, |
|
|
pud |
|
|
pud |
|
|
) |
|
|
) |
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?) |
|
|
|
|
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?) |
|
|
RETURNING id |
|
|
RETURNING id |
|
|
` |
|
|
` |
|
|
row = db.QueryRow(query, |
|
|
row = db.QueryRow(query, |
|
|
estimate.User, |
|
|
estimate.User, |
|
|
estimate.Borrower.Id, |
|
|
|
|
|
estimate.Transaction, |
|
|
estimate.Transaction, |
|
|
estimate.Price, |
|
|
estimate.Price, |
|
|
estimate.Property, |
|
|
estimate.Property, |
|
@@ -1998,6 +2063,9 @@ func (estimate *Estimate) insertEstimate(db *sql.DB) (error){ |
|
|
|
|
|
|
|
|
err = row.Scan(&estimate.Id) |
|
|
err = row.Scan(&estimate.Id) |
|
|
if err != nil { return err } |
|
|
if err != nil { return err } |
|
|
|
|
|
|
|
|
|
|
|
err = estimate.insertBorrower(db) |
|
|
|
|
|
if err != nil { return err } |
|
|
|
|
|
|
|
|
for i := range estimate.Loans { |
|
|
for i := range estimate.Loans { |
|
|
estimate.Loans[i].EstimateId = estimate.Id |
|
|
estimate.Loans[i].EstimateId = estimate.Id |
|
@@ -2008,6 +2076,44 @@ func (estimate *Estimate) insertEstimate(db *sql.DB) (error){ |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (estimate *Estimate) del(db *sql.DB, user int) (error) { |
|
|
|
|
|
var query string |
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
query = `DELETE FROM estimate WHERE id = ? AND user_id = ?` |
|
|
|
|
|
|
|
|
|
|
|
_, err = db.Exec(query, estimate.Id, user) |
|
|
|
|
|
if err != nil { return err } |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (eTemplate *ETemplate) insert(db *sql.DB) (error) { |
|
|
|
|
|
var query string |
|
|
|
|
|
var row *sql.Row |
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
query = `INSERT INTO estimate_template |
|
|
|
|
|
( |
|
|
|
|
|
user_id, |
|
|
|
|
|
branch_id, |
|
|
|
|
|
estimate_id, |
|
|
|
|
|
) |
|
|
|
|
|
VALUES (?, ?, ?) |
|
|
|
|
|
RETURNING id |
|
|
|
|
|
` |
|
|
|
|
|
row = db.QueryRow(query, |
|
|
|
|
|
eTemplate.UserId, |
|
|
|
|
|
eTemplate.BranchId, |
|
|
|
|
|
eTemplate.Estimate.Id, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
err = row.Scan(&eTemplate.Id) |
|
|
|
|
|
if err != nil { return err } |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func createEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
func createEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
var estimate Estimate |
|
|
var estimate Estimate |
|
|
err := json.NewDecoder(r.Body).Decode(&estimate) |
|
|
err := json.NewDecoder(r.Body).Decode(&estimate) |
|
@@ -2383,6 +2489,10 @@ func api(w http.ResponseWriter, r *http.Request) { |
|
|
r.Method == http.MethodPost && |
|
|
r.Method == http.MethodPost && |
|
|
guard(r, 1): |
|
|
guard(r, 1): |
|
|
createEstimate(w, db, r) |
|
|
createEstimate(w, db, r) |
|
|
|
|
|
case match(p, "/api/estimate", &args) && |
|
|
|
|
|
r.Method == http.MethodDelete && |
|
|
|
|
|
guard(r, 1): |
|
|
|
|
|
deleteEstimate(w, db, r) |
|
|
case match(p, "/api/estimate/validate", &args) && |
|
|
case match(p, "/api/estimate/validate", &args) && |
|
|
r.Method == http.MethodPost && |
|
|
r.Method == http.MethodPost && |
|
|
guard(r, 1): |
|
|
guard(r, 1): |
|
|