Explorar o código

Add backend route for return an owner a different merchant.

Lee Morgan %!s(int64=5) %!d(string=hai) anos
pai
achega
83429ed498
Modificáronse 3 ficheiros con 43 adicións e 2 borrados
  1. 40 0
      controllers/merchantData.js
  2. 1 0
      routes.js
  3. 2 2
      views/dashboardPage/js/strands/account.js

+ 40 - 0
controllers/merchantData.js

@@ -9,6 +9,46 @@ const bcrypt = require("bcryptjs");
 const mailgun = require("mailgun-js")({apiKey: process.env.MG_SUBLINE_APIKEY, domain: "mail.thesubline.net"});
 
 module.exports = {
+    /*
+    GET: gets a merchant to send back to its owner
+    req.params.id = String (merchant id)
+    response = [Owner, Merchant];
+    */
+    getMerchant: function(req, res){
+        let owner = Owner.findOne({"session.sessionId": req.session.owner}).populate("merchants", "name");
+        let merchant = Merchant.findOne({_id: req.params.id});
+
+        Promise.all([owner, merchant])
+            .then((response)=>{
+                if(response[0] === null || response[1] === null) throw "unfound";
+                if(merchant.owner.toString() !== owner._id.toString()) throw "permissions";
+
+                let responseOwner = {
+                    _id: owner._id,
+                    email: owner.email,
+                    merchants: owner.merchants
+                };
+
+                for(let i = 0; i < responseOwner.merchants.length; i++){
+                    if(merchant._id.toString() === responseOwner.merchants[i]._id.toString()){
+                        responseOwner.merchants.splice(i, 1);
+                        break;
+                    }
+                }
+
+                merchant.owner = undefined;
+                merchant.createdAt = undefined;
+
+                return res.json([responseOwner, merchant]);
+            })
+            .catch((err)=>{
+                console.log(err);
+                if(err === "unfound") return res.json("UNABLE TO FIND THAT MERCHANT");
+                if(err === "permissions") return res.json("YOU DO NOT HAVE PERMISSION TO DO THAT");
+                return res.json("ERROR: UNABLE TO RETRIEVE DATA");
+            });
+    },
+
     /*
     POST - Create a new merchant with no POS system
     req.body = {

+ 1 - 0
routes.js

@@ -24,6 +24,7 @@ module.exports = function(app){
     app.get("/resetpassword/*", renderer.displayPassReset);
     
     //Merchant
+    app.get("/merchant/:id", merchantData.getMerchant);
     app.post("/merchant/create/none", merchantData.createMerchantNone);
     app.post("/merchant/add/none", session, merchantData.addMerchantNone);
     app.put("/merchant/ingredients/update", session, merchantData.updateIngredientQuantities); //also updates some data in ingredients

+ 2 - 2
views/dashboardPage/js/strands/account.js

@@ -112,10 +112,10 @@ let account = {
     },
 
     switchMerchant: function(id){
-        console.log("switching");
+        
     },
 
-    deleteMerchant: function(){
+    deleteMerchant: function(id){
         console.log("deleting");
     }
 }