Bläddra i källkod

Create backend for "deleting" a merchant.
Remove delete option for all merchants.
Only the currently active merchant can be deleted.

Lee Morgan 5 år sedan
förälder
incheckning
92a9c13ff7

+ 28 - 0
controllers/merchantData.js

@@ -341,5 +341,33 @@ module.exports = {
                 return res.json("INCORRECT PASSWORD");
             }
         });
+    },
+
+    /*
+    DELETE: remove a merchant from its owner
+    response = Merchant (the next)
+    */
+    deleteMerchant: function(req, res){
+        if(res.locals.owner.merchants.length === 1) throw "one";
+        for(let i = 0; i < res.locals.owner.merchants.length; i++){
+            if(res.locals.owner.merchants[i]._id.toString() === res.locals.merchant._id.toString()){
+                res.locals.owner.merchants.splice(i, 1);
+                break;
+            }
+        }
+
+        res.locals.merchant.removed = true;
+
+        let merchant = Merchant.findOne({_id: res.locals.owner.merchants[0]._id});
+
+        Promise.all([merchant, res.locals.owner.save(), res.locals.merchant.save()])
+            .then((response)=>{
+                return res.json(response[0]);
+            })
+            .catch((err)=>{
+                console.log(err);
+                if(err === "one") return res.json("YOU CANNOT DELETE YOUR ONLY MERCHANT");
+                return res.json("ERROR: UNABLE TO DELETE THE MERCHANT");
+            });
     }
 }

+ 4 - 1
models/merchant.js

@@ -44,7 +44,10 @@ const MerchantSchema = new mongoose.Schema({
         type: mongoose.Schema.Types.ObjectId,
         ref: "Recipe"
     }],
-    
+    removed: {
+        type: Boolean,
+        default: false
+    }
 });
 
 module.exports = mongoose.model("Merchant", MerchantSchema);

+ 1 - 0
routes.js

@@ -31,6 +31,7 @@ module.exports = function(app){
     app.post("/merchant/password", merchantData.updatePassword); //TODO: change to work with session
     app.put("/merchant/update", session, merchantData.updateData);
     app.put("/merchant/password", session, merchantData.changePassword);
+    app.delete("/merchant", session, merchantData.deleteMerchant);
 
     //Ingredients
     app.post("/ingredients/create", session, ingredientData.createIngredient);  //also adds to merchant

+ 1 - 4
views/dashboardPage/ejs/strands/account.ejs

@@ -59,10 +59,7 @@
             <div class="locationDiv">
                 <p></p>
 
-                <div>
-                    <button class="button">USE</button>
-                    <button class="button">DELETE</button>
-                </div>
+                <button class="button">USE</button>
             </div>
         </template>
     </div>

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

@@ -19,9 +19,7 @@ let account = {
         for(let i = 0; i < merchant.owner.merchants.length; i++){
             let div = template.cloneNode(true);
             div.children[0].innerText = merchant.owner.merchants[i].name;
-            div.children[1].children[0].onclick = ()=>{this.switchMerchant(merchant.owner.merchants[i]._id)};
-            div.children[1].children[1].onclick = ()=>{this.deleteMerchant(merchant.owner.merchants[i]._id)};
-            div.children[1].children[1].onclick = ()=>{controller.openModal("confirmDeleteMerchant", merchant.owner.merchants[i]._id)};
+            div.children[1].onclick = ()=>{this.switchMerchant(merchant.owner.merchants[i]._id)};
             container.appendChild(div);
         }