From da111e4c0660d8d0d9a89a01913b0eabf7fe526f Mon Sep 17 00:00:00 2001
From: Immanuel Onyeka <immanuel@onyeka.ca>
Date: Wed, 26 Jul 2023 17:12:56 -0400
Subject: [PATCH] Add deleteFeeTemp endpoint to delete fees

---
 components/app.vue       |  6 +++++-
 components/estimates.vue | 20 +++++++++++++++-----
 skouter.go               | 20 ++++++++++++++++++++
 3 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/components/app.vue b/components/app.vue
index d4b689d..49898d4 100644
--- a/components/app.vue
+++ b/components/app.vue
@@ -16,12 +16,16 @@
 :fees="fees"
 :token="token"
 v-else-if="active == 2" />
+
 <estimates
 :user="user"
 :fees="fees"
 v-else-if="active == 3"
 :token="token" 
-@addFeeTemp="(f) => fees.push(f)" />
+@addFeeTemp="(f) => fees.push(f)"
+@removeFeeTemp="(fee) => fees = fees.filter(f => f.id != fee.id)"
+/>
+
 <settings :user="user" v-else-if="active == 4" />
 <sign-out :user="user" v-else-if="active == 5" />
 </template>
diff --git a/components/estimates.vue b/components/estimates.vue
index 6044b65..8774b6c 100644
--- a/components/estimates.vue
+++ b/components/estimates.vue
@@ -14,7 +14,7 @@
 	{{fee.type}}
 	</label>
 	<img width="21" height="21" src="/assets/image/icon/x-red.svg"
-	@click="() => remove(indx, 1)">
+	@click="() => remove(fee)">
 </div>
 <button @click="() => edit = {}">New Fee</button>
 </section>
@@ -71,7 +71,7 @@ import FeeDialog from "./fee-dialog.vue"
 import { format } from "../helpers.js"
 
 const props = defineProps(['user', 'fees', 'token'])
-const emit = defineEmits(['addFeeTemp'])
+const emit = defineEmits(['addFeeTemp', 'removeFeeTemp'])
 let edit = ref(null)
 let estimates = ref([])
 let estimate = ref()
@@ -105,8 +105,19 @@ function newType() {
 
 }
 
-function remove() {
-
+function remove(fee) {
+    fetch(`/api/fee`,
+		{method: 'DELETE',
+    		headers: {
+        	"Accept": "application/json",
+        	"Authorization": `Bearer ${props.token}`,
+    		},
+    	    body: JSON.stringify(fee)
+	}).then(response => {
+		if (response.ok) { emit('removeFeeTemp', fee) } else {
+		    response.text().then(t => console.log(t))
+		}
+	})
 }
 
 function getEstimates() {
@@ -125,7 +136,6 @@ function getEstimates() {
 		estimates.value = result
 		// console.log(result)
 	})
-
 }
 
 function summarize() {
diff --git a/skouter.go b/skouter.go
index e2934c0..fb40891 100644
--- a/skouter.go
+++ b/skouter.go
@@ -442,6 +442,22 @@ func createFeesTemp(w http.ResponseWriter, db *sql.DB, r *http.Request) {
 	json.NewEncoder(w).Encode(fee)	
 }
 
+// Fetch fees from the database
+func deleteFeeTemp(w http.ResponseWriter, db *sql.DB, r *http.Request) {
+	var fee FeeTemplate
+	var query string
+	var err error
+	
+	// claims, err := getClaims(r)
+	// var id int // Inserted estimate's id
+	err = json.NewDecoder(r.Body).Decode(&fee)
+	if err != nil { w.WriteHeader(422); return }
+
+	query = `DELETE FROM fee_template WHERE id = ?`
+	_, err = db.Exec(query, fee.Id)
+	if err != nil { w.WriteHeader(500); return }
+}
+
 func getMi(db *sql.DB, loan int) (MI, error) {
 	var mi MI
 
@@ -1551,6 +1567,10 @@ func api(w http.ResponseWriter, r *http.Request) {
 	r.Method == http.MethodPost &&
 	guard(r, 1):
 		createFeesTemp(w, db, r)
+	case match(p, "/api/fee", &args) &&
+	r.Method == http.MethodDelete &&
+	guard(r, 1):
+		deleteFeeTemp(w, db, r)
 	case match(p, "/api/estimates", &args) &&
 	r.Method == http.MethodGet &&
 	guard(r, 1):