From eb91253e97f8ea2ff51b4277cd30510c3c9d897e Mon Sep 17 00:00:00 2001
From: Immanuel Onyeka <immanuel@debian-BULLSEYE-live-builder-AMD64>
Date: Tue, 9 Jul 2024 14:14:20 -0400
Subject: [PATCH] Fix errors

---
 components/app.vue                      |  5 ++--
 components/new/new.vue                  |  4 +++-
 migrations/0_29092022_setup_tables.sql  | 32 +++++++++++++++++++++++--
 migrations/1_12062024_seed_defaults.sql | 27 +++++++++++++++++++++
 skouter.go                              | 25 +++++++++++++++----
 5 files changed, 83 insertions(+), 10 deletions(-)
 create mode 100644 migrations/1_12062024_seed_defaults.sql

diff --git a/components/app.vue b/components/app.vue
index 79cace8..ee8eb0e 100644
--- a/components/app.vue
+++ b/components/app.vue
@@ -243,9 +243,8 @@ function start() {
 			this.refreshToken()
 			fetchUser().then( u => {
 			  if (u.sub.customerId &&
-			  validStatuses.includes(u.sub.paymentStatus) &&
-			  this.user.status != "Unsubscribed" ||
-			  this.user.status != "Subscribed") {
+			  validStatuses.includes(u.sub.status) &&
+			  this.user.status != "Unsubscribed") {
 			    return
 			  }
 			  // Payment must be updated
diff --git a/components/new/new.vue b/components/new/new.vue
index 702385f..f1b2934 100644
--- a/components/new/new.vue
+++ b/components/new/new.vue
@@ -91,7 +91,6 @@ const example = {
 		pud: true, // Property under development
 		zip: '',
 		fees: [],
-		mi: { monthly: false, rate: 0 }
 }
 
 // The default loans on a new estimate
@@ -121,6 +120,9 @@ function create() {
 		    example, {fees: this.createFees()}
 		)
 	)
+	
+	// Move current loan to last inserted
+	this.sel = this.estimate.loans.length - 1
 }
 
 
diff --git a/migrations/0_29092022_setup_tables.sql b/migrations/0_29092022_setup_tables.sql
index 73d9a0b..f3ecb0b 100644
--- a/migrations/0_29092022_setup_tables.sql
+++ b/migrations/0_29092022_setup_tables.sql
@@ -97,8 +97,8 @@ CREATE TABLE license (
  * Types with branch_id and user_id values of 0 will be general cases. */
 CREATE TABLE loan_type (
 	id 				INT AUTO_INCREMENT,
-	branch_id 		INT,
-	user_id 		INT,
+	branch_id 		INT DEFAULT NULL,
+	user_id 		INT DEFAULT NULL,
 	name 			VARCHAR(30) UNIQUE NOT NULL,
 	FOREIGN KEY (branch_id) REFERENCES branch(id),
 	FOREIGN KEY (user_id) REFERENCES user(id),
@@ -229,3 +229,31 @@ CREATE TABLE estimate_result (
 	PRIMARY KEY (`id`),
 	FOREIGN KEY (loan_id) REFERENCES loan(id) ON DELETE CASCADE
 );
+
+INSERT INTO loan_type (
+	branch_id,
+	user_id,
+	name
+)
+VALUES (NULL, NULL, 'Conventional');
+
+INSERT INTO loan_type (
+	branch_id,
+	user_id,
+	name
+)
+VALUES (NULL, NULL, 'FHA');
+
+INSERT INTO loan_type (
+	branch_id,
+	user_id,
+	name
+)
+VALUES (NULL, NULL, 'USDA');
+
+INSERT INTO loan_type (
+	branch_id,
+	user_id,
+	name
+)
+VALUES (NULL, NULL, 'VA');
diff --git a/migrations/1_12062024_seed_defaults.sql b/migrations/1_12062024_seed_defaults.sql
new file mode 100644
index 0000000..2a5a11b
--- /dev/null
+++ b/migrations/1_12062024_seed_defaults.sql
@@ -0,0 +1,27 @@
+INSERT INTO loan_type (
+	branch_id,
+	user_id,
+	name
+)
+VALUES (NULL, NULL, 'Conventional');
+
+INSERT INTO loan_type (
+	branch_id,
+	user_id,
+	name
+)
+VALUES (NULL, NULL, 'FHA');
+
+INSERT INTO loan_type (
+	branch_id,
+	user_id,
+	name
+)
+VALUES (NULL, NULL, 'USDA');
+
+INSERT INTO loan_type (
+	branch_id,
+	user_id,
+	name
+)
+VALUES (NULL, NULL, 'VA');
diff --git a/skouter.go b/skouter.go
index 791cd34..1015c62 100644
--- a/skouter.go
+++ b/skouter.go
@@ -2346,7 +2346,7 @@ func getEstimates(db *sql.DB, id int, user int) ([]Estimate, error) {
 	}
 
 	// Prevents runtime panics
-	if len(estimates) == 0 {
+	if len(estimates) == 0 && id > 0 {
 		return estimates, errors.New("Estimate not found.")
 	}
 
@@ -3594,7 +3594,7 @@ func verificationToken(id int) (string, error) {
 func verifyUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
 	var claims VerificationClaims
 	
-	params, err := url.ParseQuery(r.URL.Path)
+	params, err := url.ParseQuery(r.URL.RawQuery)
 	if err != nil {
 		w.WriteHeader(500)
 		log.Println(err)
@@ -3611,7 +3611,7 @@ func verifyUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
 
 	if err != nil {
 		w.WriteHeader(500)
-		log.Println("Could not parse verification claim.")
+		log.Println("Could not parse verification claim.", err.Error())
 		return
 	}
 
@@ -3626,7 +3626,10 @@ func verifyUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
 	err = user.update(db)
 	
 	log.Println("User verified:", user.Id, user.Email)
-	w.Write([]byte("Verification complete."))
+	http.Redirect(w,
+	r, "https://"+os.Getenv("SKOUTER_DOMAIN"),
+	http.StatusTemporaryRedirect)
+	// w.Write([]byte("Verification complete."))
 }
 
 func (user *User) sendVerificationEmail() {
@@ -3901,6 +3904,18 @@ func dbReset(db *sql.DB) {
 	}
 }
 
+func dbDefaults(db *sql.DB) {
+	b, err := migrations.ReadFile("migrations/1_12062024_seed_defaults.sql")
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	_, err = db.Exec(string(b))
+	if err != nil {
+		log.Fatal(err)
+	}
+}
+
 func generateFees(loan Loan) []Fee {
 	var fees []Fee
 	var fee Fee
@@ -4187,6 +4202,8 @@ func dev(args []string) {
 		dbSeed(db)
 	case "reset":
 		dbReset(db)
+	case "defaults":
+		dbDefaults(db)
 	default:
 		return
 	}