Przeglądaj źródła

Merge branch 'buggies' into development

Lee Morgan 6 lat temu
rodzic
commit
691490fec1

+ 35 - 0
controllers/merchantData.js

@@ -163,6 +163,41 @@ module.exports = {
             });
     },
 
+    //POST - removes a single recipe
+    //Inputs:
+    // req.body: the id of the recipe to be removed
+    removeRecipe: function(req, res){
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                if(merchant.pos === "clover"){
+                    return res.json("Error: you must edit your recipes inside Clover");
+                }
+                
+                for(let i = 0; i < merchant.recipes.length; i++){
+                    if(merchant.recipes[i].toString() === req.body.id){
+                        merchant.recipes.splice(i, 1);
+                        break;
+                    }
+                }
+
+                merchant.save()
+                    .then((updatedMerchant)=>{
+                        return res.json({});
+                    })
+                    .catch((err)=>{
+                        return res.json("Error: unable to save data")
+                    })
+            })
+            .catch((err)=>{
+                return res.json("Error: unable to retrieve merchant data");
+            });
+    },
+
     //POST - Adds an ingredient to merchant's inventory
     //Inputs:
     //  req.body: array of objects containing ingredient id and quantity

+ 13 - 101
controllers/renderer.js

@@ -33,7 +33,7 @@ module.exports = {
             return res.redirect("/");
         }
 
-        Merchant.findOne({_id: req.session.user})
+        Merchant.findOne({_id: req.session.user}, {password: 0, createdAt: 0})
             .populate("inventory.ingredient")
             .populate({
                 path: "recipes",
@@ -95,9 +95,8 @@ module.exports = {
 
                             merchant.save()
                                 .then((updatedMerchant)=>{
-                                    updatedMerchant.password = undefined;
                                     updatedMerchant.accessToken = undefined;
-                                    res.render("inventoryPage/inventory", {merchant: updatedMerchant, error: undefined});
+                                    res.render("dashboardPage/dashboard", {merchant: updatedMerchant, error: undefined});
                                     Transaction.create(transactions);
                                     return;
                                 })
@@ -105,18 +104,18 @@ module.exports = {
                                     let errorMessage = "Error: unable to save user data";
                                     
                                     merchant.password = undefined;
-                                    return res.render("inventoryPage/inventory", {merchant: updatedMerchant, error: errorMessage});
+                                    return res.render("dashboardPage/dashboard", {merchant: updatedMerchant, error: errorMessage});
                                 });
                         })
                         .catch((err)=>{
                             let errorMessage = "There was an error and we could not retrieve your transactions from Clover";
 
                             merchant.password = undefined;
-                            return res.render("inventoryPage/inventory", {merchant: merchant, error: errorMessage});
+                            return res.render("dashboardPage/dashboard", {merchant: merchant, error: errorMessage});
                         });
                 }else if(merchant.pos === "none"){
                     merchant.password = undefined;
-                    return res.render("inventoryPage/inventory", {merchant: merchant, error: undefined});
+                    return res.render("dashboardPage/dashboard", {merchant: merchant, error: undefined});
                 }else{
                     req.session.error = "Error: WEBSITE PANIC";
                     
@@ -130,103 +129,13 @@ module.exports = {
             });
     },
 
-    //GET - Renders the merchant setup page for a clover client
-    //Returns:
-    //  ingredients: all ingredients from database
-    //  recipes: recipes from the users clover account
-    //  error: returns error (if any) from session
-    //Renders merchantSetupPage
-    merchantSetupClover: function(req, res){
-        let errorMessage = {};
-        if(req.session.error){
-            errorMessage = req.session.error;
-            req.session.error = undefined;
-        }else{
-            errorMessage = null;
-        }
-        
-        Ingredient.find()
-            .then((ingredients)=>{
-                axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}/items?access_token=${req.session.accessToken}`)
-                    .then((recipes)=>{
-                        return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data, error: errorMessage});
-                    })
-                    .catch((err)=>{
-                        req.session.error = "Error: unable to retrieve data from Clover";
-                        
-                        return res.redirect("/");
-                    });
-            })
-            .catch((err)=>{
-                req.session.error = "Error: data for new merchants could not be retrieved";
-                
-                return res.redirect("/");
-            });
-    },
-
-    //GET - Renders the merchant setup page for a non-pos client
-    //Returns:
-    //  ingredients: all ingredients from database
-    //  recipes: null (to signify non-post client)
-    //  error: returns error (if any) from session
-    //Renders merchantSetupPage
-    merchantSetupNone: function(req, res){
-        let errorMessage = {};
-        if(req.session.error){
-            errorMessage = req.session.error;
-            req.session.error = undefined;
-        }else{
-            errorMessage = null;
-        }
-
-        Ingredient.find()
-            .then((ingredients)=>{
-                return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null, error: errorMessage});
-            })
-            .catch((err)=>{
-                req.session.error = "Error: data for new merchants could not be retrieved";
-
-                return res.redirect("/");
-            });
-    },
-
-    //GET - Renders the recipe display page
-    //Returns:
-    //  merchant: merchant with recipes and recipe ingredients populated
-    //Renders recipesPage
-    displayRecipes: function(req, res){
-        if(!req.session.user){
-            req.session.error = "You must be logged in to view that page";
-            return res.redirect("/");
-        }
-
-        Merchant.findOne({_id: req.session.user})
-            .populate({
-                path: "recipes",
-                model: "Recipe",
-                populate: {
-                    path: "ingredients.ingredient",
-                    model: "Ingredient"
-                }
-            })
-            .populate("inventory.ingredient")
-            .then((merchant)=>{
-                merchant.password = undefined;
-                return res.render("recipesPage/recipes", {merchant: merchant});
-            })
-            .catch((err)=>{
-                req.session.error = "Error: unable to retrieve user data";
-            
-                return res.redirect("/");
-            });
-    },
-
     //GET - Renders the information page
-    //Renders information page
     displayLegal: function(req, res){
         return res.render("informationPage/information");
     },
 
+    //GET - Renders the data display page
+    //Returns data
     displayData: function(req, res){
         if(!req.session.user){
             req.session.error = "You must be logged in to view that page";
@@ -234,7 +143,8 @@ module.exports = {
         }
 
         let merchTransPromise = new Promise((resolve, reject)=>{
-            Merchant.findOne({_id: req.session.user})
+            Merchant.findOne({_id: req.session.user}, 
+                {name: 1, inventory: 1, recipes: 1, _id: 0})
                 .populate("recipes")
                 .populate("inventory.ingredient")
                 .then((merchant)=>{
@@ -242,7 +152,8 @@ module.exports = {
                     let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
                     let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
 
-                    Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}})
+                    Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}},
+                        {date: 1, recipes: 1, _id: 0})
                         .then((transactions)=>{
                             resolve({merchant: merchant, transactions: transactions});
                         })
@@ -252,7 +163,8 @@ module.exports = {
         });
 
         let purchasePromise = new Promise((resolve, reject)=>{
-            Purchase.find({merchant: req.session.user})
+            Purchase.find({merchant: req.session.user},
+                {date: 1, ingredients: 1, _id: 0})
                 .then((purchases)=>{
                     resolve(purchases);
                 })

+ 1 - 1
routes.js

@@ -9,7 +9,6 @@ module.exports = function(app){
     //Render page
     app.get("/", renderer.landingPage);
     app.get("/inventory", renderer.displayInventory);
-    app.get("/recipes", renderer.displayRecipes);
     app.get("/information", renderer.displayLegal);
     app.get("/data", renderer.displayData);
 
@@ -17,6 +16,7 @@ module.exports = function(app){
     app.post("/merchant/create/none", merchantData.createMerchantNone);
     app.get("/merchant/create/clover", merchantData.createMerchantClover);
     app.get("/merchant/recipes/update", merchantData.updateRecipes);
+    app.post("/merchant/recipes/remove", merchantData.removeRecipe);
     app.post("/merchant/ingredients/create", merchantData.addMerchantIngredient);
     app.post("/merchant/ingredients/remove", merchantData.removeMerchantIngredient);
     app.post("/merchant/ingredients/update", merchantData.updateMerchantIngredient);

+ 0 - 0
views/inventoryPage/account.js → views/dashboardPage/account.js


+ 0 - 0
views/inventoryPage/addIngredient.js → views/dashboardPage/addIngredient.js


+ 0 - 0
views/inventoryPage/inventory.css → views/dashboardPage/dashboard.css


+ 12 - 9
views/inventoryPage/inventory.ejs → views/dashboardPage/dashboard.ejs

@@ -4,7 +4,7 @@
         <meta charset="UTF-8">
         <title>The Subline</title>
         <link rel="icon" type="img/png" href="/shared/images/logo.png">
-        <link rel="stylesheet" href="/inventoryPage/inventory.css">
+        <link rel="stylesheet" href="/dashboardPage/dashboard.css">
         <link rel="stylesheet" href="/shared/shared.css">
     </head>
     <body>
@@ -213,8 +213,11 @@
         <div id="singleRecipeAction" class="action">
             <h2 id="recipeName"></h2>
 
-            <div class="buttonsDiv">
+            <div class="buttonBox">
                 <button class="button" id="addButton">Add Ingredient</button>
+                <% if(merchant.pos === "none"){ %>
+                    <button class="button" id="removeButton">Remove</button>
+                <% } %>
             </div>
 
             <table>
@@ -241,13 +244,13 @@
         <script>let merchant = <%- JSON.stringify(merchant) %>;</script>
         <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
         <script src="../shared/validation.js"></script>
-        <script src="/inventoryPage/inventory.js"></script>
-        <script src="/inventoryPage/recipes.js"></script>
-        <script src="/inventoryPage/account.js"></script>
-        <script src="/inventoryPage/addIngredient.js"></script>
-        <script src="/inventoryPage/enterTransactions.js"></script>
-        <script src="/inventoryPage/enterPurchase.js"></script>
-        <script src="/inventoryPage/singleRecipe.js"></script>
+        <script src="/dashboardPage//inventory.js"></script>
+        <script src="/dashboardPage//recipes.js"></script>
+        <script src="/dashboardPage//account.js"></script>
+        <script src="/dashboardPage//addIngredient.js"></script>
+        <script src="/dashboardPage//enterTransactions.js"></script>
+        <script src="/dashboardPage//enterPurchase.js"></script>
+        <script src="/dashboardPage//singleRecipe.js"></script>
         <script src="/shared/controller.js"></script>
     </body>
 </html>

+ 0 - 0
views/inventoryPage/enterPurchase.js → views/dashboardPage/enterPurchase.js


+ 0 - 0
views/inventoryPage/enterTransactions.js → views/dashboardPage/enterTransactions.js


+ 0 - 0
views/inventoryPage/inventory.js → views/dashboardPage/inventory.js


+ 0 - 0
views/inventoryPage/recipes.js → views/dashboardPage/recipes.js


+ 23 - 0
views/inventoryPage/singleRecipe.js → views/dashboardPage/singleRecipe.js

@@ -11,6 +11,7 @@ window.singleRecipeObj = {
 
         document.querySelector("#recipeName").innerText = recipe.name;
         document.querySelector("#addButton").onclick = ()=>{this.displayAdd(recipe)};
+        document.querySelector("#removeButton").onclick = ()=>{this.removeRecipe(recipe)};
 
         for(let ingredient of recipe.ingredients){
             let row = document.createElement("tr");
@@ -259,4 +260,26 @@ window.singleRecipeObj = {
             td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
         }
     },
+
+    removeRecipe(recipe){
+        axios.post("/merchant/recipes/remove", {id: recipe._id})
+            .then((response)=>{
+                if(typeof(response.data) === "string"){
+                    banner.createError(response.data);
+                }else{
+                    for(let i = 0; i < merchant.recipes.length; i++){
+                        if(merchant.recipes[i]._id === recipe._id){
+                            merchant.recipes.splice(i, 1);
+                            break;
+                        }
+                    }
+
+                    window.recipesObj.isPopulated = false;
+                    window.recipesObj.display();
+                }
+            })
+            .catch((err)=>{
+                banner.createError("Warning:  Something went wrong, try refreshing the page");
+            });
+    }
 }