diff --git a/functions/index.js b/functions/index.js index bda262b..2f3d673 100644 --- a/functions/index.js +++ b/functions/index.js @@ -160,7 +160,32 @@ app.post('/purchase-api', function(request, responce){ let product = request.body.item var productRef = database.ref("/products/"+product) productRef.update({owner:uid, status:"sold"}) - responce.send({success:true, error:null}) + .then(function(){ + responce.send({success:true, error:null}) + }) + .catch(e => {console.log(e)}) + }) + .catch(function(error) { + responce.send({ error: 'invalid auth token' }) + }); + } + else { + responce.send({ error: 'invalid data' }) + } +}) + +app.post('/place-on-sale-api', function(request, responce){ + if (request.body.item && request.body.user) { + admin.auth().verifyIdToken(request.body.user) + .then(function(decodedToken) { + let uid = decodedToken.uid; + let product = request.body.item + var productRef = database.ref("/products/"+product) + productRef.update({status:"on-sale"}) + .then(function(){ + responce.send({success:true, error:null}) + }) + .catch(e => {console.log(e)}) }) .catch(function(error) { responce.send({ error: 'invalid auth token' }) diff --git a/functions/views/js/fdb.js b/functions/views/js/fdb.js index ffd8c1f..af7d8e5 100644 --- a/functions/views/js/fdb.js +++ b/functions/views/js/fdb.js @@ -140,17 +140,17 @@ function purchaseItem() { firebase.auth().currentUser.getIdToken().then(function(idToken) { var item_id_element = document.getElementById("productID") var item_id = item_id_element.getAttribute("product_id") - const url = 'purchase-api'; + const url = 'purchase-api' // The data we are going to send in our request let data = { - item: item_id, - user: idToken + item: item_id, + user: idToken } // The parameters we are gonna pass to the fetch function let postData = { - method: 'POST', - body: JSON.stringify(data), - headers: {'Content-Type': 'application/json'} + method: 'POST', + body: JSON.stringify(data), + headers: {'Content-Type': 'application/json'} } fetch(url, postData) .then(response => response.json()) @@ -167,8 +167,6 @@ function purchaseItem() { document.getElementById("api-response").innerHTML = "Error: " + result.error document.getElementById("api-response").setAttribute("style", "display: block") } - //check return value - //update element accordingly }) }) } @@ -178,6 +176,9 @@ function purchaseItem() { } } + else if (purchaseBtn.innerHTML == "Item Purchased, Congratulations!") { + window.location.replace("home") + } else { purchaseBtn.disabled = true } @@ -188,15 +189,48 @@ function edit_item() { firebase.auth().currentUser.getIdToken().then(function(idToken) { var item_id_element = document.getElementById("productID") var item_id = item_id_element.getAttribute("product_id") - console.log("edit-item?uToken=" + idToken + '&productid=' + item_id) window.location.replace("edit-item?uToken=" + idToken + '&productid=' + item_id) }) .catch(e => {console.log(e)}) } +function place_item_on_sale() { + firebase.auth().currentUser.getIdToken().then(function(idToken) { + var item_id_element = document.getElementById("productID") + var item_id = item_id_element.getAttribute("product_id") + var place_on_sale_button = document.getElementById("sellBtn") + const url = 'place-on-sale-api' + let data = { + item: item_id, + user: idToken + } + let postData = { + method: 'POST', + body: JSON.stringify(data), + headers: {'Content-Type': 'application/json'} + } + fetch(url, postData) + .then(response => response.json()) + .then(function(result){ + console.log(result) + if (result.success == true) { + place_on_sale_button.setAttribute("style", "background: #00DD00; width:200px;") + place_on_sale_button.innerHTML = "Item on sale!" + place_on_sale_button.disabled = true + } + else { + place_on_sale_button.setAttribute("style", "background: red;") + place_on_sale_button.disabled = true + place_on_sale_button.innerHTML = "ERROR" + console.log(result.error) + } + }) + }) + .catch(e => {console.log(e)}) +} + function my_product(productID) { firebase.auth().currentUser.getIdToken().then(function(idToken) { - console.log("my-products?uToken=" + idToken + '&productid=' + productID) window.location.replace("my-product?uToken=" + idToken + '&productid=' + productID) }) .catch(e => {console.log(e)}) diff --git a/functions/views/my-product.ejs b/functions/views/my-product.ejs index 8c652e9..d6b8bf8 100644 --- a/functions/views/my-product.ejs +++ b/functions/views/my-product.ejs @@ -65,16 +65,16 @@

Seller <%= item.owner %>
+Seller: You
Location: <%= item.location %>
In stock: <%= item.stock %>
- <% var publicField %> - <% if (item.public == "") {publicField = "On sale"} else {publicField = "Hidden"}%> -Status: <%= publicField %>
+Status: <%= item.status %>
Price: £<%= item.price %>
diff --git a/server.js b/server.js index f38ec88..261b85b 100644 --- a/server.js +++ b/server.js @@ -166,7 +166,32 @@ app.post('/purchase-api', function(request, responce){ let product = request.body.item var productRef = database.ref("/products/"+product) productRef.update({owner:uid, status:"sold"}) - responce.send({success:true, error:null}) + .then(function(){ + responce.send({success:true, error:null}) + }) + .catch(e => {console.log(e)}) + }) + .catch(function(error) { + responce.send({ error: 'invalid auth token' }) + }); + } + else { + responce.send({ error: 'invalid data' }) + } +}) + +app.post('/place-on-sale-api', function(request, responce){ + if (request.body.item && request.body.user) { + admin.auth().verifyIdToken(request.body.user) + .then(function(decodedToken) { + let uid = decodedToken.uid; + let product = request.body.item + var productRef = database.ref("/products/"+product) + productRef.update({status:"on-sale"}) + .then(function(){ + responce.send({success:true, error:null}) + }) + .catch(e => {console.log(e)}) }) .catch(function(error) { responce.send({ error: 'invalid auth token' })