Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

1740 строки
37 KiB

  1. package main
  2. import (
  3. "net/http"
  4. "net/mail"
  5. "log"
  6. "sync"
  7. "regexp"
  8. "html/template"
  9. "database/sql"
  10. _ "github.com/go-sql-driver/mysql"
  11. "fmt"
  12. "encoding/json"
  13. "strconv"
  14. "bytes"
  15. "time"
  16. "errors"
  17. "strings"
  18. "math"
  19. "io"
  20. // pdf "github.com/SebastiaanKlippert/go-wkhtmltopdf"
  21. "github.com/golang-jwt/jwt/v4"
  22. )
  23. type User struct {
  24. Id int `json:"id"`
  25. Email string `json:"email"`
  26. FirstName string `json:"firstName"`
  27. LastName string `json:"lastName"`
  28. BranchId int `json:"branchId"`
  29. Status string `json:"status"`
  30. Country string `json:"country"`
  31. Title string `json:"title"`
  32. Verified bool `json:"verified"`
  33. Role string `json:"role"`
  34. Password string `json:"password,omitempty"`
  35. }
  36. type UserClaims struct {
  37. Id int `json:"id"`
  38. Role string `json:"role"`
  39. Exp string `json:"exp"`
  40. }
  41. type Page struct {
  42. tpl *template.Template
  43. Title string
  44. Name string
  45. }
  46. type Borrower struct {
  47. Id int `json:"id"`
  48. Credit int `json:"credit"`
  49. Income int `json:"income"`
  50. Num int `json:"num"`
  51. }
  52. type FeeTemplate struct {
  53. Id int `json:"id"`
  54. User int `json:"user"`
  55. Branch int `json:"branch"`
  56. Amount int `json:"amount"`
  57. Perc float32 `json:"perc"`
  58. Type string `json:"type"`
  59. Notes string `json:"notes"`
  60. Name string `json:"name"`
  61. Category string `json:"category"`
  62. Auto bool `json:"auto"`
  63. }
  64. type Fee struct {
  65. Id int `json:"id"`
  66. LoanId int `json:"loan_id"`
  67. Amount int `json:"amount"`
  68. Perc float32 `json:"perc"`
  69. Type string `json:"type"`
  70. Notes string `json:"notes"`
  71. Name string `json:"name"`
  72. Category string `json:"category"`
  73. }
  74. type LoanType struct {
  75. Id int `json:"id"`
  76. User int `json:"user"`
  77. Branch int `json:"branch"`
  78. Name string `json:"name"`
  79. }
  80. type Loan struct {
  81. Id int `json:"id"`
  82. EstimateId int `json:"estimateId"`
  83. Type LoanType `json:"type"`
  84. Amount int `json:"amount"`
  85. Amortization string `json:"amortization"`
  86. Term int `json:"term"`
  87. Ltv float32 `json:"ltv"`
  88. Dti float32 `json:"dti"`
  89. Hoi int `json:"hoi"`
  90. Hazard int `json:"hazard"`
  91. Tax int `json:"tax"`
  92. Interest float32 `json:"interest"`
  93. Mi MI `json:"mi"`
  94. Fees []Fee `json:"fees"`
  95. Name string `json:"title"`
  96. }
  97. type MI struct {
  98. Type string `json:"user"`
  99. Label string `json:"label"`
  100. Lender string `json:"lender"`
  101. Rate float32 `json:"rate"`
  102. Premium float32 `json:"premium"`
  103. Upfront float32 `json:"upfront"`
  104. Monthly bool `json:"monthly"`
  105. FiveYearTotal float32 `json:"fiveYearTotal"`
  106. InitialAllInPremium float32 `json:"initialAllInPremium"`
  107. InitialAllInRate float32 `json:"initialAllInRate"`
  108. InitialAmount float32 `json:"initialAmount"`
  109. }
  110. type Result struct {
  111. Id int `json:"id"`
  112. LoanId int `json:"loanId"`
  113. LoanPayment int `json:"loanPayment"`
  114. TotalMonthly int `json:"totalMonthly"`
  115. TotalFees int `json:"totalFees"`
  116. TotalCredits int `json:"totalCredits"`
  117. CashToClose int `json:"cashToClose"`
  118. }
  119. type Estimate struct {
  120. Id int `json:"id"`
  121. User int `json:"user"`
  122. Borrower Borrower `json:"borrower"`
  123. Transaction string `json:"transaction"`
  124. Price int `json:"price"`
  125. Property string `json:"property"`
  126. Occupancy string `json:"occupancy"`
  127. Zip string `json:"zip"`
  128. Pud bool `json:"pud"`
  129. Loans []Loan `json:"loans"`
  130. Results []Result `json:"results"`
  131. }
  132. var (
  133. regexen = make(map[string]*regexp.Regexp)
  134. relock sync.Mutex
  135. address = "127.0.0.1:8001"
  136. )
  137. var paths = map[string]string {
  138. "home": "views/home.tpl",
  139. "terms": "views/terms.tpl",
  140. "app": "views/app.tpl",
  141. "test": "views/test.tpl",
  142. }
  143. var pages = map[string]Page {
  144. "home": cache("home", "Home"),
  145. "terms": cache("terms", "Terms and Conditions"),
  146. "test": cache("test", "PDF test"),
  147. "app": cache("app", "App"),
  148. }
  149. var roles = map[string]int{
  150. "User": 1,
  151. "Manager": 2,
  152. "Admin": 3,
  153. }
  154. // Used to validate claim in JWT token body. Checks if user id is greater than
  155. // zero and time format is valid
  156. func (c UserClaims) Valid() error {
  157. if c.Id < 1 { return errors.New("Invalid id") }
  158. t, err := time.Parse(time.UnixDate, c.Exp)
  159. if err != nil { return err }
  160. if t.Before(time.Now()) { return errors.New("Token expired.") }
  161. return err
  162. }
  163. func cache(name string, title string) Page {
  164. var p = []string{"views/master.tpl", paths[name]}
  165. tpl := template.Must(template.ParseFiles(p...))
  166. return Page{tpl: tpl,
  167. Title: title,
  168. Name: name,
  169. }
  170. }
  171. func (page Page) Render(w http.ResponseWriter) {
  172. err := page.tpl.Execute(w, page)
  173. if err != nil {
  174. log.Print(err)
  175. }
  176. }
  177. func match(path, pattern string, args *[]string) bool {
  178. relock.Lock()
  179. defer relock.Unlock()
  180. regex := regexen[pattern]
  181. if regex == nil {
  182. regex = regexp.MustCompile("^" + pattern + "$")
  183. regexen[pattern] = regex
  184. }
  185. matches := regex.FindStringSubmatch(path)
  186. if len(matches) <= 0 {
  187. return false
  188. }
  189. *args = matches[1:]
  190. return true
  191. }
  192. func makeResults(estimate Estimate) ([]Result) {
  193. var results []Result
  194. amortize := func(principle float64, rate float64, periods float64) int {
  195. exp := math.Pow(1+rate, periods)
  196. return int(principle * rate * exp / (exp - 1))
  197. }
  198. for _, loan := range estimate.Loans {
  199. var result Result
  200. // Monthly payments use amortized loan payment formula plus monthly MI,
  201. // plus all other recurring fees
  202. result.LoanPayment = amortize(float64(loan.Amount),
  203. float64(loan.Interest / 100 / 12),
  204. float64(loan.Term * 12))
  205. result.TotalMonthly = result.LoanPayment + loan.Hoi + loan.Tax + loan.Hazard
  206. if loan.Mi.Monthly {
  207. result.TotalMonthly = result.TotalMonthly +
  208. int(loan.Mi.Rate/100*float32(loan.Amount))
  209. }
  210. for i := range loan.Fees {
  211. if loan.Fees[i].Amount > 0 {
  212. result.TotalFees = result.TotalFees + loan.Fees[i].Amount
  213. } else {
  214. result.TotalCredits = result.TotalCredits + loan.Fees[i].Amount
  215. }
  216. }
  217. result.CashToClose =
  218. result.TotalFees + result.TotalCredits + (estimate.Price - loan.Amount)
  219. result.LoanId = loan.Id
  220. results = append(results, result)
  221. }
  222. return results
  223. }
  224. func summarize(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  225. var estimate Estimate
  226. err := json.NewDecoder(r.Body).Decode(&estimate)
  227. if err != nil { http.Error(w, "Invalid estimate.", 422); return }
  228. results := makeResults(estimate)
  229. json.NewEncoder(w).Encode(results)
  230. }
  231. func getLoanType( db *sql.DB, id int) (LoanType, error) {
  232. types, err := getLoanTypes(db, id, 0, 0)
  233. if err != nil { return LoanType{Id: id}, err }
  234. if len(types) == 0 {
  235. return LoanType{Id: id}, errors.New("No type with that id")
  236. }
  237. return types[0], nil
  238. }
  239. func getLoanTypes( db *sql.DB, id int, user int, branch int ) (
  240. []LoanType, error) {
  241. var loans []LoanType
  242. var query = `SELECT
  243. id,
  244. user_id,
  245. branch_id,
  246. name
  247. FROM loan_type WHERE loan_type.id = CASE @e := ? WHEN 0 THEN id ELSE @e END
  248. OR
  249. loan_type.user_id = CASE @e := ? WHEN 0 THEN id ELSE @e END
  250. OR
  251. loan_type.branch_id = CASE @e := ? WHEN 0 THEN id ELSE @e END`
  252. // Should be changed to specify user
  253. rows, err := db.Query(query, id, user, branch)
  254. if err != nil {
  255. return nil, fmt.Errorf("loan_type error: %v", err)
  256. }
  257. defer rows.Close()
  258. for rows.Next() {
  259. var loan LoanType
  260. if err := rows.Scan(
  261. &loan.Id,
  262. &loan.User,
  263. &loan.Branch,
  264. &loan.Name)
  265. err != nil {
  266. log.Printf("Error occured fetching loan: %v", err)
  267. return nil, fmt.Errorf("Error occured fetching loan: %v", err)
  268. }
  269. loans = append(loans, loan)
  270. }
  271. return loans, nil
  272. }
  273. func getFees(db *sql.DB, loan int) ([]Fee, error) {
  274. var fees []Fee
  275. query := `SELECT id, loan_id, amount, perc, type, notes, name, category
  276. FROM fee
  277. WHERE loan_id = ?`
  278. rows, err := db.Query(query, loan)
  279. if err != nil {
  280. return nil, fmt.Errorf("Fee query error %v", err)
  281. }
  282. defer rows.Close()
  283. for rows.Next() {
  284. var fee Fee
  285. if err := rows.Scan(
  286. &fee.Id,
  287. &fee.LoanId,
  288. &fee.Amount,
  289. &fee.Perc,
  290. &fee.Type,
  291. &fee.Notes,
  292. &fee.Name,
  293. &fee.Category,
  294. )
  295. err != nil {
  296. return nil, fmt.Errorf("Fees scanning error: %v", err)
  297. }
  298. fees = append(fees, fee)
  299. }
  300. return fees, nil
  301. }
  302. func fetchFeesTemp(db *sql.DB, user int, branch int) ([]FeeTemplate, error) {
  303. var fees []FeeTemplate
  304. query := `SELECT
  305. id, user_id, COALESCE(branch_id, 0), amount, perc, type, notes, name,
  306. category, auto
  307. FROM fee_template
  308. WHERE user_id = ? OR branch_id = ?
  309. `
  310. rows, err := db.Query(query, user, branch)
  311. if err != nil {
  312. return nil, fmt.Errorf("Fee template query error %v", err)
  313. }
  314. defer rows.Close()
  315. for rows.Next() {
  316. var fee FeeTemplate
  317. if err := rows.Scan(
  318. &fee.Id,
  319. &fee.User,
  320. &fee.Branch,
  321. &fee.Amount,
  322. &fee.Perc,
  323. &fee.Type,
  324. &fee.Notes,
  325. &fee.Name,
  326. &fee.Category,
  327. &fee.Auto)
  328. err != nil {
  329. return nil, fmt.Errorf("FeesTemplate scanning error: %v", err)
  330. }
  331. fees = append(fees, fee)
  332. }
  333. return fees, nil
  334. }
  335. // Fetch fees from the database
  336. func getFeesTemp(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  337. var fees []FeeTemplate
  338. claims, err := getClaims(r)
  339. if err != nil { w.WriteHeader(500); return }
  340. users, err := queryUsers(db, claims.Id)
  341. if err != nil { w.WriteHeader(422); return }
  342. fees, err = fetchFeesTemp(db, claims.Id, users[0].BranchId)
  343. json.NewEncoder(w).Encode(fees)
  344. }
  345. // Fetch fees from the database
  346. func createFeesTemp(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  347. var fee FeeTemplate
  348. var query string
  349. var row *sql.Row
  350. var err error
  351. claims, err := getClaims(r)
  352. // var id int // Inserted estimate's id
  353. err = json.NewDecoder(r.Body).Decode(&fee)
  354. if err != nil { w.WriteHeader(422); return }
  355. query = `INSERT INTO fee_template
  356. (
  357. user_id,
  358. branch_id,
  359. amount,
  360. perc,
  361. type,
  362. notes,
  363. name,
  364. auto
  365. )
  366. VALUES (?, NULL, ?, ?, ?, ?, ?, ?)
  367. RETURNING id
  368. `
  369. row = db.QueryRow(query,
  370. claims.Id,
  371. fee.Amount,
  372. fee.Perc,
  373. fee.Type,
  374. fee.Notes,
  375. fee.Name,
  376. fee.Auto,
  377. )
  378. err = row.Scan(&fee.Id)
  379. if err != nil { w.WriteHeader(500); return }
  380. json.NewEncoder(w).Encode(fee)
  381. }
  382. // Fetch fees from the database
  383. func deleteFeeTemp(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  384. var fee FeeTemplate
  385. var query string
  386. var err error
  387. // claims, err := getClaims(r)
  388. // var id int // Inserted estimate's id
  389. err = json.NewDecoder(r.Body).Decode(&fee)
  390. if err != nil { w.WriteHeader(422); return }
  391. query = `DELETE FROM fee_template WHERE id = ?`
  392. _, err = db.Exec(query, fee.Id)
  393. if err != nil { w.WriteHeader(500); return }
  394. }
  395. func getMi(db *sql.DB, loan int) (MI, error) {
  396. var mi MI
  397. query := `SELECT
  398. type, label, lender, rate, premium, upfront, five_year_total,
  399. initial_premium, initial_rate, initial_amount
  400. FROM mi WHERE loan_id = ?`
  401. rows, err := db.Query(query, loan)
  402. if err != nil { return mi, err }
  403. defer rows.Close()
  404. if (!rows.Next()) { return mi, nil }
  405. if err := rows.Scan(
  406. &mi.Type,
  407. &mi.Label,
  408. &mi.Lender,
  409. &mi.Rate,
  410. &mi.Premium,
  411. &mi.Upfront,
  412. &mi.FiveYearTotal,
  413. &mi.InitialAllInPremium,
  414. &mi.InitialAllInRate,
  415. &mi.InitialAmount,
  416. )
  417. err != nil {
  418. return mi, err
  419. }
  420. return mi, nil
  421. }
  422. func getBorrower(db *sql.DB, id int) (Borrower, error) {
  423. var borrower Borrower
  424. row := db.QueryRow(
  425. "SELECT * FROM borrower " +
  426. "WHERE id = ? LIMIT 1",
  427. id)
  428. if err := row.Scan(
  429. &borrower.Id,
  430. &borrower.Credit,
  431. &borrower.Income,
  432. &borrower.Num,
  433. )
  434. err != nil {
  435. return borrower, fmt.Errorf("Borrower scanning error: %v", err)
  436. }
  437. return borrower, nil
  438. }
  439. // Query Lender APIs and parse responses into MI structs
  440. func fetchMi(db *sql.DB, estimate *Estimate, pos int) []MI {
  441. var err error
  442. var loan Loan = estimate.Loans[pos]
  443. var ltv = func(l float32) string {
  444. switch {
  445. case l > 95: return "LTV97"
  446. case l > 90: return "LTV95"
  447. case l > 85: return "LTV90"
  448. default: return "LTV85"
  449. }
  450. }
  451. var term = func(t int) string {
  452. switch {
  453. case t <= 10: return "A10"
  454. case t <= 15: return "A15"
  455. case t <= 20: return "A20"
  456. case t <= 25: return "A25"
  457. case t <= 30: return "A30"
  458. default: return "A40"
  459. }
  460. }
  461. var propertyCodes = map[string]string {
  462. "Single Attached": "SFO",
  463. "Single Detached": "SFO",
  464. "Condo Lo-rise": "CON",
  465. "Condo Hi-rise": "CON",
  466. }
  467. var purposeCodes = map[string]string {
  468. "Purchase": "PUR",
  469. "Refinance": "RRT",
  470. }
  471. body, err := json.Marshal(map[string]any{
  472. "zipCode": estimate.Zip,
  473. "stateCode": "CA",
  474. "address": "",
  475. "propertyTypeCode": propertyCodes[estimate.Property],
  476. "occupancyTypeCode": "PRS",
  477. "loanPurposeCode": purposeCodes[estimate.Transaction],
  478. "loanAmount": loan.Amount,
  479. "loanToValue": ltv(loan.Ltv),
  480. "amortizationTerm": term(loan.Term),
  481. "loanTypeCode": "FXD",
  482. "duLpDecisionCode": "DAE",
  483. "loanProgramCodes": []any{},
  484. "debtToIncome": loan.Dti,
  485. "wholesaleLoan": 0,
  486. "coveragePercentageCode": "L30",
  487. "productCode": "BPM",
  488. "renewalTypeCode": "CON",
  489. "numberOfBorrowers": 1,
  490. "coBorrowerCreditScores": []any{},
  491. "borrowerCreditScore": strconv.Itoa(estimate.Borrower.Credit),
  492. "masterPolicy": nil,
  493. "selfEmployedIndicator": false,
  494. "armType": "",
  495. "userId": 44504,
  496. })
  497. if err != nil {
  498. log.Printf("Could not marshal NationalMI body: \n%v\n%v\n",
  499. bytes.NewBuffer(body), err)
  500. }
  501. req, err := http.NewRequest("POST",
  502. "https://rate-gps.nationalmi.com/rates/productRateQuote",
  503. bytes.NewBuffer(body))
  504. req.Header.Add("Content-Type", "application/json")
  505. req.AddCookie(&http.Cookie{
  506. Name: "nmirategps_email",
  507. Value: config["NationalMIEmail"]})
  508. resp, err := http.DefaultClient.Do(req)
  509. var res map[string]interface{}
  510. var result []MI
  511. if resp.StatusCode != 200 {
  512. log.Printf("the status: %v\nthe resp: %v\n the req: %v\n the body: %v\n",
  513. resp.Status, resp, req.Body, bytes.NewBuffer(body))
  514. } else {
  515. json.NewDecoder(resp.Body).Decode(&res)
  516. // estimate.Loans[pos].Mi = res
  517. // Parse res into result here
  518. }
  519. return result
  520. }
  521. // Make comparison PDF
  522. func generatePDF(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  523. }
  524. func login(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  525. var id int
  526. var role string
  527. var err error
  528. var user User
  529. json.NewDecoder(r.Body).Decode(&user)
  530. row := db.QueryRow(
  531. `SELECT id, role FROM user WHERE email = ? AND password = sha2(?, 256)`,
  532. user.Email, user.Password,
  533. )
  534. err = row.Scan(&id, &role)
  535. if err != nil {
  536. http.Error(w, "Invalid Credentials.", http.StatusUnauthorized)
  537. return
  538. }
  539. token := jwt.NewWithClaims(jwt.SigningMethodHS256,
  540. UserClaims{ Id: id, Role: role,
  541. Exp: time.Now().Add(time.Minute * 30).Format(time.UnixDate)})
  542. tokenStr, err := token.SignedString([]byte(config["JWT_SECRET"]))
  543. if err != nil {
  544. log.Println("Token could not be signed: ", err, tokenStr)
  545. http.Error(w, "Token generation error.", http.StatusInternalServerError)
  546. return
  547. }
  548. cookie := http.Cookie{Name: "skouter",
  549. Value: tokenStr,
  550. Path: "/",
  551. Expires: time.Now().Add(time.Hour * 24)}
  552. http.SetCookie(w, &cookie)
  553. _, err = w.Write([]byte(tokenStr))
  554. if err != nil {
  555. http.Error(w,
  556. "Could not complete token write.",
  557. http.StatusInternalServerError)}
  558. }
  559. func getToken(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  560. claims, err := getClaims(r)
  561. // Will verify existing signature and expiry time
  562. token := jwt.NewWithClaims(jwt.SigningMethodHS256,
  563. UserClaims{ Id: claims.Id, Role: claims.Role,
  564. Exp: time.Now().Add(time.Minute * 30).Format(time.UnixDate)})
  565. tokenStr, err := token.SignedString([]byte(config["JWT_SECRET"]))
  566. if err != nil {
  567. log.Println("Token could not be signed: ", err, tokenStr)
  568. http.Error(w, "Token generation error.", http.StatusInternalServerError)
  569. return
  570. }
  571. cookie := http.Cookie{Name: "skouter",
  572. Value: tokenStr,
  573. Path: "/",
  574. Expires: time.Now().Add(time.Hour * 24)}
  575. http.SetCookie(w, &cookie)
  576. _, err = w.Write([]byte(tokenStr))
  577. if err != nil {
  578. http.Error(w,
  579. "Could not complete token write.",
  580. http.StatusInternalServerError)}
  581. }
  582. func getClaims(r *http.Request) (UserClaims, error) {
  583. claims := new(UserClaims)
  584. _, tokenStr, found := strings.Cut(r.Header.Get("Authorization"), " ")
  585. if !found {
  586. return *claims, errors.New("Token not found")
  587. }
  588. // Pull token payload into UserClaims
  589. _, err := jwt.ParseWithClaims(tokenStr, claims,
  590. func(token *jwt.Token) (any, error) {
  591. return []byte(config["JWT_SECRET"]), nil
  592. })
  593. if err != nil {
  594. return *claims, err
  595. }
  596. if err = claims.Valid(); err != nil {
  597. return *claims, err
  598. }
  599. return *claims, nil
  600. }
  601. func guard(r *http.Request, required int) bool {
  602. claims, err := getClaims(r)
  603. if err != nil { return false }
  604. if roles[claims.Role] < required { return false }
  605. return true
  606. }
  607. func queryUsers(db *sql.DB, id int) ( []User, error ) {
  608. var users []User
  609. var query string
  610. var rows *sql.Rows
  611. var err error
  612. query = `SELECT
  613. u.id,
  614. u.email,
  615. u.first_name,
  616. u.last_name,
  617. u.branch_id,
  618. u.country,
  619. u.title,
  620. u.status,
  621. u.verified,
  622. u.role
  623. FROM user u WHERE u.id = CASE @e := ? WHEN 0 THEN u.id ELSE @e END
  624. `
  625. rows, err = db.Query(query, id)
  626. if err != nil {
  627. return users, err
  628. }
  629. defer rows.Close()
  630. for rows.Next() {
  631. var user User
  632. if err := rows.Scan(
  633. &user.Id,
  634. &user.Email,
  635. &user.FirstName,
  636. &user.LastName,
  637. &user.BranchId,
  638. &user.Country,
  639. &user.Title,
  640. &user.Status,
  641. &user.Verified,
  642. &user.Role,
  643. )
  644. err != nil {
  645. return users, err
  646. }
  647. users = append(users, user)
  648. }
  649. // Prevents runtime panics
  650. if len(users) == 0 { return users, errors.New("User not found.") }
  651. return users, nil
  652. }
  653. func insertResults(db *sql.DB, results []Result) (error){
  654. var query string
  655. var row *sql.Row
  656. var err error
  657. query = `INSERT INTO estimate_result
  658. (
  659. loan_id,
  660. loan_payment,
  661. total_monthly,
  662. total_fees,
  663. total_credits,
  664. cash_to_close
  665. )
  666. VALUES (?, ?, ?, ?, ?, ?, ?)
  667. RETURNING id
  668. `
  669. for i := range results {
  670. db.QueryRow(query,
  671. results[i].LoanId,
  672. results[i].LoanPayment,
  673. results[i].TotalMonthly,
  674. results[i].TotalFees,
  675. results[i].TotalCredits,
  676. results[i].CashToClose,
  677. )
  678. err = row.Scan(&results[i].Id)
  679. if err != nil { return err }
  680. }
  681. return nil
  682. }
  683. func insertUser(db *sql.DB, user User) (User, error){
  684. var query string
  685. var row *sql.Row
  686. var err error
  687. var id int // Inserted user's id
  688. query = `INSERT INTO user
  689. (
  690. email,
  691. first_name,
  692. last_name,
  693. password,
  694. created,
  695. role,
  696. verified,
  697. last_login
  698. )
  699. VALUES (?, ?, ?, sha2(?, 256), NOW(), ?, ?, NOW())
  700. RETURNING id
  701. `
  702. row = db.QueryRow(query,
  703. user.Email,
  704. user.FirstName,
  705. user.LastName,
  706. user.Password,
  707. user.Role,
  708. user.Verified,
  709. )
  710. err = row.Scan(&id)
  711. if err != nil { return User{}, err }
  712. users, err := queryUsers(db, id)
  713. if err != nil { return User{}, err }
  714. return users[0], nil
  715. }
  716. func updateUser(user User, db *sql.DB) error {
  717. query := `
  718. UPDATE user
  719. SET
  720. email = CASE @e := ? WHEN '' THEN email ELSE @e END,
  721. first_name = CASE @fn := ? WHEN '' THEN first_name ELSE @fn END,
  722. last_name = CASE @ln := ? WHEN '' THEN last_name ELSE @ln END,
  723. role = CASE @r := ? WHEN '' THEN role ELSE @r END,
  724. password = CASE @p := ? WHEN '' THEN password ELSE sha2(@p, 256) END
  725. WHERE id = ?
  726. `
  727. _, err := db.Exec(query,
  728. user.Email,
  729. user.FirstName,
  730. user.LastName,
  731. user.Role,
  732. user.Password,
  733. user.Id,
  734. )
  735. return err
  736. }
  737. func getUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  738. claims, err := getClaims(r)
  739. if err != nil { w.WriteHeader(500); return }
  740. users, err := queryUsers(db, claims.Id)
  741. if err != nil { w.WriteHeader(422); log.Println(err); return }
  742. json.NewEncoder(w).Encode(users)
  743. }
  744. func getUsers(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  745. users, err := queryUsers(db, 0)
  746. if err != nil {
  747. w.WriteHeader(http.StatusInternalServerError)
  748. return
  749. }
  750. json.NewEncoder(w).Encode(users)
  751. }
  752. // Updates a user using only specified values in the JSON body
  753. func patchUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  754. var user User
  755. err := json.NewDecoder(r.Body).Decode(&user)
  756. _, err = mail.ParseAddress(user.Email)
  757. if err != nil { http.Error(w, "Invalid email.", 422); return }
  758. if roles[user.Role] == 0 {
  759. http.Error(w, "Invalid role.", 422)
  760. return
  761. }
  762. err = updateUser(user, db)
  763. if err != nil { http.Error(w, "Bad form values.", 422); return }
  764. users, err := queryUsers(db, user.Id)
  765. if err != nil { http.Error(w, "Bad form values.", 422); return }
  766. json.NewEncoder(w).Encode(users[0])
  767. }
  768. // Update specified fields of the user specified in the claim
  769. func patchSelf(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  770. claim, err := getClaims(r)
  771. var user User
  772. json.NewDecoder(r.Body).Decode(&user)
  773. // First check that the target user to be updated is the same as the claim id, and
  774. // their role is unchanged.
  775. if err != nil || claim.Id != user.Id {
  776. http.Error(w, "Target user's id does not match claim.", 401)
  777. return
  778. }
  779. if claim.Role != user.Role && user.Role != "" {
  780. http.Error(w, "Administrator required to escalate role.", 401)
  781. return
  782. }
  783. patchUser(w, db, r)
  784. }
  785. func deleteUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  786. var user User
  787. err := json.NewDecoder(r.Body).Decode(&user)
  788. if err != nil {
  789. http.Error(w, "Invalid fields.", 422)
  790. return
  791. }
  792. query := `DELETE FROM user WHERE id = ?`
  793. _, err = db.Exec(query, user.Id)
  794. if err != nil {
  795. http.Error(w, "Could not delete.", 500)
  796. }
  797. }
  798. func createUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  799. var user User
  800. err := json.NewDecoder(r.Body).Decode(&user)
  801. if err != nil { http.Error(w, "Invalid fields.", 422); return }
  802. _, err = mail.ParseAddress(user.Email)
  803. if err != nil { http.Error(w, "Invalid email.", 422); return }
  804. if roles[user.Role] == 0 {
  805. http.Error(w, "Invalid role.", 422)
  806. }
  807. user, err = insertUser(db, user)
  808. if err != nil { http.Error(w, "Error creating user.", 422); return }
  809. json.NewEncoder(w).Encode(user)
  810. }
  811. func fetchAvatar(db *sql.DB, user int) ( []byte, error ) {
  812. var img []byte
  813. var query string
  814. var err error
  815. query = `SELECT
  816. avatar
  817. FROM user WHERE user.id = ?
  818. `
  819. row := db.QueryRow(query, user)
  820. err = row.Scan(&img)
  821. if err != nil {
  822. return img, err
  823. }
  824. return img, nil
  825. }
  826. func insertAvatar(db *sql.DB, user int, img []byte) error {
  827. query := `UPDATE user
  828. SET avatar = ?
  829. WHERE id = ?
  830. `
  831. _, err := db.Exec(query, img, user)
  832. if err != nil {
  833. return err
  834. }
  835. return nil
  836. }
  837. func setAvatar(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  838. var validTypes []string = []string{"image/png", "image/jpeg"}
  839. var isValidType bool
  840. claims, err := getClaims(r)
  841. if err != nil { http.Error(w, "Invalid token.", 422); return }
  842. img, err := io.ReadAll(r.Body)
  843. if err != nil { http.Error(w, "Invalid file.", 422); return }
  844. for _, v := range validTypes {
  845. if v == http.DetectContentType(img) { isValidType = true }
  846. }
  847. if !isValidType { http.Error(w, "Invalid file type.", 422); return }
  848. err = insertAvatar(db, claims.Id, img)
  849. if err != nil { http.Error(w, "Could not insert.", 500); return }
  850. }
  851. func getAvatar(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  852. claims, err := getClaims(r)
  853. if err != nil { http.Error(w, "Invalid token.", 422); return }
  854. img, err := fetchAvatar(db, claims.Id)
  855. if err != nil { http.Error(w, "Could not retrieve.", 500); return }
  856. w.Header().Set("Content-Type", http.DetectContentType(img))
  857. w.Write(img)
  858. }
  859. func queryBorrower(db *sql.DB, id int) ( Borrower, error ) {
  860. var borrower Borrower
  861. var query string
  862. var err error
  863. query = `SELECT
  864. l.id,
  865. l.credit_score,
  866. l.num,
  867. l.monthly_income
  868. FROM borrower l WHERE l.id = ?
  869. `
  870. row := db.QueryRow(query, id)
  871. err = row.Scan(
  872. borrower.Id,
  873. borrower.Credit,
  874. borrower.Num,
  875. borrower.Income,
  876. )
  877. if err != nil {
  878. return borrower, err
  879. }
  880. return borrower, nil
  881. }
  882. // Must have an estimate ID 'e', but not necessarily a loan id 'id'
  883. func getResults(db *sql.DB, e int, id int) ( []Result, error ) {
  884. var results []Result
  885. var query string
  886. var rows *sql.Rows
  887. var err error
  888. query = `SELECT
  889. r.id,
  890. loan_id,
  891. loan_payment,
  892. total_monthly,
  893. total_fees,
  894. total_credits,
  895. cash_to_close
  896. FROM estimate_result r
  897. INNER JOIN loan
  898. ON r.loan_id = loan.id
  899. WHERE r.id = CASE @e := ? WHEN 0 THEN r.id ELSE @e END
  900. AND loan.estimate_id = ?
  901. `
  902. rows, err = db.Query(query, id, e)
  903. if err != nil {
  904. return results, err
  905. }
  906. defer rows.Close()
  907. for rows.Next() {
  908. var result Result
  909. if err := rows.Scan(
  910. &result.Id,
  911. &result.LoanId,
  912. &result.LoanPayment,
  913. &result.TotalMonthly,
  914. &result.TotalFees,
  915. &result.TotalCredits,
  916. &result.CashToClose,
  917. )
  918. err != nil {
  919. return results, err
  920. }
  921. results = append(results, result)
  922. }
  923. // Prevents runtime panics
  924. // if len(results) == 0 { return results, errors.New("Result not found.") }
  925. return results, nil
  926. }
  927. // Must have an estimate ID 'e', but not necessarily a loan id 'id'
  928. func getLoans(db *sql.DB, e int, id int) ( []Loan, error ) {
  929. var loans []Loan
  930. var query string
  931. var rows *sql.Rows
  932. var err error
  933. query = `SELECT
  934. l.id,
  935. l.type_id,
  936. l.estimate_id,
  937. l.amount,
  938. l.term,
  939. l.interest,
  940. l.ltv,
  941. l.dti,
  942. l.hoi,
  943. l.tax,
  944. l.name
  945. FROM loan l WHERE l.id = CASE @e := ? WHEN 0 THEN l.id ELSE @e END AND
  946. l.estimate_id = ?
  947. `
  948. rows, err = db.Query(query, id, e)
  949. if err != nil {
  950. return loans, err
  951. }
  952. defer rows.Close()
  953. for rows.Next() {
  954. var loan Loan
  955. if err := rows.Scan(
  956. &loan.Id,
  957. &loan.Type.Id,
  958. &loan.EstimateId,
  959. &loan.Amount,
  960. &loan.Term,
  961. &loan.Interest,
  962. &loan.Ltv,
  963. &loan.Dti,
  964. &loan.Hoi,
  965. &loan.Tax,
  966. &loan.Name,
  967. )
  968. err != nil {
  969. return loans, err
  970. }
  971. mi, err := getMi(db, loan.Id)
  972. if err != nil {
  973. return loans, err
  974. }
  975. loan.Mi = mi
  976. fees, err := getFees(db, loan.Id)
  977. if err != nil {
  978. return loans, err
  979. }
  980. loan.Fees = fees
  981. loan.Type, err = getLoanType(db, loan.Type.Id)
  982. if err != nil {
  983. return loans, err
  984. }
  985. loans = append(loans, loan)
  986. }
  987. // Prevents runtime panics
  988. if len(loans) == 0 { return loans, errors.New("Loan not found.") }
  989. return loans, nil
  990. }
  991. func getEstimates(db *sql.DB, id int, user int) ( []Estimate, error ) {
  992. var estimates []Estimate
  993. var query string
  994. var rows *sql.Rows
  995. var err error
  996. query = `SELECT
  997. id,
  998. user_id,
  999. borrower_id,
  1000. transaction,
  1001. price,
  1002. property,
  1003. occupancy,
  1004. zip,
  1005. pud
  1006. FROM estimate WHERE id = CASE @e := ? WHEN 0 THEN id ELSE @e END AND
  1007. user_id = CASE @e := ? WHEN 0 THEN user_id ELSE @e END
  1008. `
  1009. rows, err = db.Query(query, id, user)
  1010. if err != nil {
  1011. return estimates, err
  1012. }
  1013. defer rows.Close()
  1014. for rows.Next() {
  1015. var estimate Estimate
  1016. if err := rows.Scan(
  1017. &estimate.Id,
  1018. &estimate.User,
  1019. &estimate.Borrower.Id,
  1020. &estimate.Transaction,
  1021. &estimate.Price,
  1022. &estimate.Property,
  1023. &estimate.Occupancy,
  1024. &estimate.Zip,
  1025. &estimate.Pud,
  1026. )
  1027. err != nil {
  1028. return estimates, err
  1029. }
  1030. borrower, err := getBorrower(db, estimate.Borrower.Id)
  1031. if err != nil {
  1032. return estimates, err
  1033. }
  1034. estimate.Borrower = borrower
  1035. estimate.Results, err = getResults(db, estimate.Id, 0)
  1036. if err != nil {
  1037. return estimates, err
  1038. }
  1039. estimates = append(estimates, estimate)
  1040. }
  1041. // Prevents runtime panics
  1042. if len(estimates) == 0 { return estimates, errors.New("Estimate not found.") }
  1043. for i := range estimates {
  1044. estimates[i].Loans, err = getLoans(db, estimates[i].Id, 0)
  1045. if err != nil { return estimates, err }
  1046. }
  1047. return estimates, nil
  1048. }
  1049. // Accepts a borrower struct and returns the id of the inserted borrower and
  1050. // any related error.
  1051. func insertBorrower(db *sql.DB, borrower Borrower) (int, error) {
  1052. var query string
  1053. var row *sql.Row
  1054. var err error
  1055. var id int // Inserted loan's id
  1056. query = `INSERT INTO borrower
  1057. (
  1058. credit_score,
  1059. monthly_income,
  1060. num
  1061. )
  1062. VALUES (?, ?, ?)
  1063. RETURNING id
  1064. `
  1065. row = db.QueryRow(query,
  1066. borrower.Credit,
  1067. borrower.Income,
  1068. borrower.Num,
  1069. )
  1070. err = row.Scan(&id)
  1071. if err != nil { return 0, err }
  1072. return id, nil
  1073. }
  1074. func insertMi(db *sql.DB, mi MI) (int, error) {
  1075. var id int
  1076. query := `INSERT INTO mi
  1077. (
  1078. type,
  1079. label,
  1080. lender,
  1081. rate,
  1082. premium,
  1083. upfront,
  1084. five_year_total,
  1085. initial_premium,
  1086. initial_rate,
  1087. initial_amount
  1088. )
  1089. VALUES (?, ?, ?, ?, ?, ?, ?)
  1090. RETURNING id`
  1091. row := db.QueryRow(query,
  1092. &mi.Type,
  1093. &mi.Label,
  1094. &mi.Lender,
  1095. &mi.Rate,
  1096. &mi.Premium,
  1097. &mi.Upfront,
  1098. &mi.FiveYearTotal,
  1099. &mi.InitialAllInPremium,
  1100. &mi.InitialAllInRate,
  1101. &mi.InitialAmount,
  1102. )
  1103. err := row.Scan(&id)
  1104. if err != nil { return 0, err }
  1105. return id, nil
  1106. }
  1107. func insertFee(db *sql.DB, fee Fee) (int, error) {
  1108. var id int
  1109. query := `INSERT INTO fee
  1110. (loan_id, amount, perc, type, notes, name, category)
  1111. VALUES (?, ?, ?, ?, ?, ?, ?)
  1112. RETURNING id`
  1113. row := db.QueryRow(query,
  1114. fee.LoanId,
  1115. fee.Amount,
  1116. fee.Perc,
  1117. fee.Type,
  1118. fee.Notes,
  1119. fee.Name,
  1120. fee.Category,
  1121. )
  1122. err := row.Scan(&id)
  1123. if err != nil { return 0, err }
  1124. return id, nil
  1125. }
  1126. func insertLoan(db *sql.DB, loan Loan) (Loan, error){
  1127. var query string
  1128. var row *sql.Row
  1129. var err error
  1130. var id int // Inserted loan's id
  1131. query = `INSERT INTO loan
  1132. (
  1133. estimate_id,
  1134. type_id,
  1135. amount,
  1136. term,
  1137. interest,
  1138. ltv,
  1139. dti,
  1140. hoi,
  1141. tax,
  1142. name
  1143. )
  1144. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  1145. RETURNING id
  1146. `
  1147. row = db.QueryRow(query,
  1148. loan.EstimateId,
  1149. loan.Type.Id,
  1150. loan.Amount,
  1151. loan.Term,
  1152. loan.Interest,
  1153. loan.Ltv,
  1154. loan.Dti,
  1155. loan.Hoi,
  1156. loan.Tax,
  1157. loan.Name,
  1158. )
  1159. err = row.Scan(&id)
  1160. if err != nil { return loan, err }
  1161. _, err = insertMi(db, loan.Mi)
  1162. if err != nil { return loan, err }
  1163. for i := range loan.Fees {
  1164. _, err := insertFee(db, loan.Fees[i])
  1165. if err != nil { return loan, err }
  1166. }
  1167. loans, err := getLoans(db, id, 0)
  1168. if err != nil { return Loan{}, err }
  1169. return loans[0], nil
  1170. }
  1171. func insertEstimate(db *sql.DB, estimate Estimate) (Estimate, error){
  1172. var query string
  1173. var row *sql.Row
  1174. var err error
  1175. // var id int // Inserted estimate's id
  1176. estimate.Borrower.Id, err = insertBorrower(db, estimate.Borrower)
  1177. if err != nil { return Estimate{}, err }
  1178. query = `INSERT INTO estimate
  1179. (
  1180. user_id,
  1181. borrower_id,
  1182. transaction,
  1183. price,
  1184. property,
  1185. occupancy,
  1186. zip,
  1187. pud
  1188. )
  1189. VALUES (?, ?, ?, ?, ?, ?, ?, ?)
  1190. RETURNING id
  1191. `
  1192. row = db.QueryRow(query,
  1193. estimate.User,
  1194. estimate.Borrower.Id,
  1195. estimate.Transaction,
  1196. estimate.Price,
  1197. estimate.Property,
  1198. estimate.Occupancy,
  1199. estimate.Zip,
  1200. estimate.Pud,
  1201. )
  1202. err = row.Scan(&estimate.Id)
  1203. if err != nil { return Estimate{}, err }
  1204. for _, l := range estimate.Loans {
  1205. l.EstimateId = estimate.Id
  1206. _, err = insertLoan(db, l)
  1207. if err != nil { return estimate, err }
  1208. }
  1209. estimates, err := getEstimates(db, estimate.Id, 0)
  1210. if err != nil { return Estimate{}, err }
  1211. return estimates[0], nil
  1212. }
  1213. func createEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  1214. var estimate Estimate
  1215. err := json.NewDecoder(r.Body).Decode(&estimate)
  1216. if err != nil { http.Error(w, "Invalid fields.", 422); return }
  1217. claims, err := getClaims(r)
  1218. estimate.User = claims.Id
  1219. estimate, err = insertEstimate(db, estimate)
  1220. if err != nil { http.Error(w, err.Error(), 422); return }
  1221. estimate.Results = makeResults(estimate)
  1222. err = insertResults(db, estimate.Results)
  1223. if err != nil { http.Error(w, err.Error(), 500); return }
  1224. e, err := getEstimates(db, estimate.Id, 0)
  1225. if err != nil { http.Error(w, err.Error(), 500); return }
  1226. json.NewEncoder(w).Encode(e[0])
  1227. }
  1228. // Query all estimates that belong to the current user
  1229. func fetchEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  1230. var estimates []Estimate
  1231. claims, err := getClaims(r)
  1232. estimates, err = getEstimates(db, 0, claims.Id)
  1233. if err != nil { http.Error(w, err.Error(), 500); return }
  1234. json.NewEncoder(w).Encode(estimates)
  1235. }
  1236. func checkConventional(l Loan, b Borrower) error {
  1237. if b.Credit < 620 {
  1238. return errors.New("Credit score too low for conventional loan")
  1239. }
  1240. // Buyer needs to put down 20% to avoid mortgage insurance
  1241. if (l.Ltv > 80 && l.Mi.Rate == 0) {
  1242. return errors.New(fmt.Sprintln(
  1243. l.Name,
  1244. "down payment must be 20% to avoid insurance",
  1245. ))
  1246. }
  1247. return nil
  1248. }
  1249. func checkFHA(l Loan, b Borrower) error {
  1250. if b.Credit < 500 {
  1251. return errors.New("Credit score too low for FHA loan")
  1252. }
  1253. if (l.Ltv > 96.5) {
  1254. return errors.New("FHA down payment must be at least 3.5%")
  1255. }
  1256. if (b.Credit < 580 && l.Ltv > 90) {
  1257. return errors.New("FHA down payment must be at least 10%")
  1258. }
  1259. // Debt-to-income must be below 45% if credit score is below 580.
  1260. if (b.Credit < 580 && l.Dti > 45) {
  1261. return errors.New(fmt.Sprintln(
  1262. l.Name, "debt to income is too high for credit score.",
  1263. ))
  1264. }
  1265. return nil
  1266. }
  1267. // Loan option for veterans with no set rules
  1268. func checkVA(l Loan, b Borrower) error {
  1269. return nil
  1270. }
  1271. // Loan option for residents of rural areas with no set rules
  1272. func checkUSDA(l Loan, b Borrower) error {
  1273. return nil
  1274. }
  1275. // Should also check loan amount limit maybe with an API.
  1276. func checkEstimate(e Estimate) error {
  1277. if e.Property == "" { return errors.New("Empty property type") }
  1278. if e.Price == 0 { return errors.New("Empty property price") }
  1279. if e.Borrower.Num == 0 {
  1280. return errors.New("Missing number of borrowers")
  1281. }
  1282. if e.Borrower.Credit == 0 {
  1283. return errors.New("Missing borrower credit score")
  1284. }
  1285. if e.Borrower.Income == 0 {
  1286. return errors.New("Missing borrower credit income")
  1287. }
  1288. for _, l := range e.Loans {
  1289. if l.Amount == 0 {
  1290. return errors.New(fmt.Sprintln(l.Name, "loan amount cannot be zero"))
  1291. }
  1292. if l.Term == 0 {
  1293. return errors.New(fmt.Sprintln(l.Name, "loan term cannot be zero"))
  1294. }
  1295. if l.Interest == 0 {
  1296. return errors.New(fmt.Sprintln(l.Name, "loan interest cannot be zero"))
  1297. }
  1298. // Can be used to check rules for specific loan types
  1299. var err error
  1300. switch l.Type.Id {
  1301. case 1:
  1302. err = checkConventional(l, e.Borrower)
  1303. case 2:
  1304. err = checkFHA(l, e.Borrower)
  1305. case 3:
  1306. err = checkVA(l, e.Borrower)
  1307. case 4:
  1308. err = checkUSDA(l, e.Borrower)
  1309. default:
  1310. err = errors.New("Invalid loan type")
  1311. }
  1312. if err != nil { return err }
  1313. }
  1314. return nil
  1315. }
  1316. func validateEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) {
  1317. var estimate Estimate
  1318. err := json.NewDecoder(r.Body).Decode(&estimate)
  1319. if err != nil { http.Error(w, err.Error(), 422); return }
  1320. err = checkEstimate(estimate)
  1321. if err != nil { http.Error(w, err.Error(), 406); return }
  1322. }
  1323. func showPDF(w http.ResponseWriter, r *http.Request) {
  1324. // var args []string
  1325. // p := r.URL.Path
  1326. db, err := sql.Open("mysql",
  1327. fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/skouter",
  1328. config["DBUser"],
  1329. config["DBPass"]))
  1330. // w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  1331. err = db.Ping()
  1332. if err != nil {
  1333. fmt.Println("Bad database configuration: %v\n", err)
  1334. panic(err)
  1335. // maybe os.Exit(1) instead
  1336. }
  1337. var pa = template.Must(template.ParseFiles("views/master.tpl",
  1338. "views/test.tpl"))
  1339. // claims, err := getClaims(r)
  1340. if err != nil { w.WriteHeader(500); return }
  1341. users, err := queryUsers(db, 1)
  1342. info := struct {
  1343. Title string
  1344. Name string
  1345. User User
  1346. }{
  1347. Title: "test PDF",
  1348. Name: "idk-random-name",
  1349. User: users[0],
  1350. }
  1351. // fmt.Println(info)
  1352. err = pa.Execute(w, info)
  1353. if err != nil {fmt.Println(err)}
  1354. }
  1355. func api(w http.ResponseWriter, r *http.Request) {
  1356. var args []string
  1357. p := r.URL.Path
  1358. db, err := sql.Open("mysql",
  1359. fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/skouter",
  1360. config["DBUser"],
  1361. config["DBPass"]))
  1362. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  1363. err = db.Ping()
  1364. if err != nil {
  1365. fmt.Println("Bad database configuration: %v\n", err)
  1366. panic(err)
  1367. // maybe os.Exit(1) instead
  1368. }
  1369. switch {
  1370. case match(p, "/api/login", &args) &&
  1371. r.Method == http.MethodPost:
  1372. login(w, db, r)
  1373. case match(p, "/api/token", &args) &&
  1374. r.Method == http.MethodGet && guard(r, 1):
  1375. getToken(w, db, r)
  1376. case match(p, "/api/users", &args) && // Array of all users
  1377. r.Method == http.MethodGet && guard(r, 3):
  1378. getUsers(w, db, r)
  1379. case match(p, "/api/user", &args) &&
  1380. r.Method == http.MethodGet && guard(r, 1):
  1381. getUser(w, db, r)
  1382. case match(p, "/api/user", &args) &&
  1383. r.Method == http.MethodPost &&
  1384. guard(r, 3):
  1385. createUser(w, db, r)
  1386. case match(p, "/api/user", &args) &&
  1387. r.Method == http.MethodPatch &&
  1388. guard(r, 3): // For admin to modify any user
  1389. patchUser(w, db, r)
  1390. case match(p, "/api/user", &args) &&
  1391. r.Method == http.MethodPatch &&
  1392. guard(r, 2): // For employees to modify own accounts
  1393. patchSelf(w, db, r)
  1394. case match(p, "/api/user", &args) &&
  1395. r.Method == http.MethodDelete &&
  1396. guard(r, 3):
  1397. deleteUser(w, db, r)
  1398. case match(p, "/api/user/avatar", &args) &&
  1399. r.Method == http.MethodGet &&
  1400. guard(r, 1):
  1401. getAvatar(w, db, r)
  1402. case match(p, "/api/user/avatar", &args) &&
  1403. r.Method == http.MethodPost &&
  1404. guard(r, 1):
  1405. setAvatar(w, db, r)
  1406. case match(p, "/api/fees", &args) &&
  1407. r.Method == http.MethodGet &&
  1408. guard(r, 1):
  1409. getFeesTemp(w, db, r)
  1410. case match(p, "/api/fee", &args) &&
  1411. r.Method == http.MethodPost &&
  1412. guard(r, 1):
  1413. createFeesTemp(w, db, r)
  1414. case match(p, "/api/fee", &args) &&
  1415. r.Method == http.MethodDelete &&
  1416. guard(r, 1):
  1417. deleteFeeTemp(w, db, r)
  1418. case match(p, "/api/estimates", &args) &&
  1419. r.Method == http.MethodGet &&
  1420. guard(r, 1):
  1421. fetchEstimate(w, db, r)
  1422. case match(p, "/api/estimate", &args) &&
  1423. r.Method == http.MethodPost &&
  1424. guard(r, 1):
  1425. createEstimate(w, db, r)
  1426. case match(p, "/api/estimate/validate", &args) &&
  1427. r.Method == http.MethodPost &&
  1428. guard(r, 1):
  1429. validateEstimate(w, db, r)
  1430. case match(p, "/api/estimate/summarize", &args) &&
  1431. r.Method == http.MethodPost &&
  1432. guard(r, 1):
  1433. summarize(w, db, r)
  1434. default:
  1435. http.Error(w, "Invalid route or token", 404)
  1436. }
  1437. db.Close()
  1438. }
  1439. func route(w http.ResponseWriter, r *http.Request) {
  1440. var page Page
  1441. var args []string
  1442. p := r.URL.Path
  1443. switch {
  1444. case r.Method == "GET" && match(p, "/", &args):
  1445. page = pages[ "home" ]
  1446. case match(p, "/terms", &args):
  1447. page = pages[ "terms" ]
  1448. case match(p, "/app", &args):
  1449. page = pages[ "app" ]
  1450. case match(p, "/test", &args):
  1451. showPDF(w, r)
  1452. return
  1453. default:
  1454. http.NotFound(w, r)
  1455. return
  1456. }
  1457. page.Render(w)
  1458. }
  1459. func main() {
  1460. files := http.FileServer(http.Dir(""))
  1461. http.Handle("/assets/", files)
  1462. http.HandleFunc("/api/", api)
  1463. http.HandleFunc("/", route)
  1464. log.Fatal(http.ListenAndServe(address, nil))
  1465. }