Sfoglia il codice sorgente

Add frontend for deleting merchant.
Update backend for deleting merchant so that it sends all of the necessary information.

Lee Morgan 5 anni fa
parent
commit
fdb523a8dd
2 ha cambiato i file con 50 aggiunte e 4 eliminazioni
  1. 19 3
      controllers/merchantData.js
  2. 31 1
      views/dashboardPage/js/strands/account.js

+ 19 - 3
controllers/merchantData.js

@@ -345,7 +345,7 @@ module.exports = {
 
     /*
     DELETE: remove a merchant from its owner
-    response = Merchant (the next)
+    response = [Owner, Merchant (the next), [Transaction]]
     */
     deleteMerchant: function(req, res){
         if(res.locals.owner.merchants.length === 1) throw "one";
@@ -360,9 +360,25 @@ module.exports = {
 
         let merchant = Merchant.findOne({_id: res.locals.owner.merchants[0]._id});
 
-        Promise.all([merchant, res.locals.owner.save(), res.locals.merchant.save()])
+        Promise.all([
+            merchant,
+            res.locals.owner.save(),
+            res.locals.merchant.save(),
+            res.locals.owner.populate("merchants", "name"),
+            Transaction.find({merchant: res.locals.owner.merchants[0]._id})
+        ])
             .then((response)=>{
-                return res.json(response[0]);
+                console.log(res.locals.owner);
+                let responseOwner = {
+                    _id: res.locals.owner._id,
+                    email: res.locals.owner.email,
+                    merchants: res.locals.owner.merchants.slice(1)
+                };
+
+                response[1].owner = undefined;
+                response[1].createdAt = undefined;
+
+                return res.json([responseOwner, response[0], response[4]]);
             })
             .catch((err)=>{
                 console.log(err);

+ 31 - 1
views/dashboardPage/js/strands/account.js

@@ -149,7 +149,37 @@ let account = {
     },
 
     deleteMerchant: function(id){
-        console.log("deleting");
+        let loader = document.getElementById("loaderContainer");
+        loader.style.display = "flex";
+
+        fetch("/merchant", {method: "delete"})
+            .then(response => response.json())
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    controller.createBanner(response, "error");
+                }else{
+                    window.merchant = new Merchant(
+                        response[1].name,
+                        response[0].email,
+                        response[1].pos,
+                        response[1].inventory,
+                        response[1].recipes,
+                        response[2],
+                        response[0]
+                    );
+                    state.updateMerchant();
+
+                    controller.closeModal();
+                    controller.openStrand("home");
+                }
+            })
+            .catch((err)=>{
+                console.log(err);
+                controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
+            })
+            .finally(()=>{
+                loader.style.display = "none";
+            });
     }
 }