Browse Source

Remove unused code from backend

Lee Morgan 6 years ago
parent
commit
2a87e1e327
5 changed files with 1 additions and 444 deletions
  1. 0 237
      controllers/merchantData.js
  2. 0 91
      controllers/otherData.js
  3. 0 104
      controllers/transactionData.js
  4. 0 11
      routes.js
  5. 1 1
      views/dashboardPage/orders.js

+ 0 - 237
controllers/merchantData.js

@@ -115,84 +115,6 @@ module.exports = {
             });
     },
 
-    //GET - Checks clover for new or deleted recipes
-    //Returns: 
-    //  merchant: Full merchant (recipe ingredients populated)
-    //  count: Number of new recipes
-    updateRecipes: 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})
-            .populate("recipes")
-            .then((merchant)=>{
-                axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/items?access_token=${merchant.posAccessToken}`)
-                    .then((result)=>{
-                        let deletedRecipes = merchant.recipes.slice();
-                        for(let i = 0; i < result.data.elements.length; i++){
-                            for(let j = 0; j < deletedRecipes.length; j++){
-                                if(result.data.elements[i].id === deletedRecipes[j].posId){
-                                    result.data.elements.splice(i, 1);
-                                    deletedRecipes.splice(j, 1);
-                                    i--;
-                                    break;
-                                }
-                            }
-                        }
-
-                        for(let recipe of deletedRecipes){
-                            for(let i = 0; i < merchant.recipes.length; i++){
-                                if(recipe._id === merchant.recipes[i]._id){
-                                    merchant.recipes.splice(i, 1);
-                                    break;
-                                }
-                            }
-                        }
-
-                        let newRecipes = []
-                        for(let recipe of result.data.elements){
-                            let newRecipe = new Recipe({
-                                posId: recipe.id,
-                                merchant: merchant._id,
-                                name: recipe.name,
-                                ingredients: []
-                            });
-
-                            merchant.recipes.push(newRecipe);
-                            newRecipes.push(newRecipe);
-                        }
-
-                        Recipe.create(newRecipes)
-                            .catch((err)=>{
-                                return res.json("Error: unable to create recipes");
-                            });
-
-                        merchant.save()
-                            .then((newMerchant)=>{
-                                newMerchant.populate(["recipes.ingredients.ingredient", "inventory.ingredient"]).execPopulate()
-                                    .then((newestMerchant)=>{
-                                        merchant.password = undefined;
-                                        return res.json(newestMerchant);
-                                    })
-                                    .catch((err)=>{
-                                        return res.json("Error: unable to retrieve user data");
-                                    });
-                            })
-                            .catch((err)=>{
-                                return res.json("Error: unable to retrieve user data");
-                            });
-                    })
-                    .catch((err)=>{
-                        return res.json("Error: unable to retrieve data from Clover");
-                    });
-            })
-            .catch((err)=>{
-                return res.json("Error: unable to retrieve user data");
-            });
-    },
-
     //DELETE - removes a single recipe
     removeRecipe: function(req, res){
         if(!req.session.user){
@@ -348,165 +270,6 @@ module.exports = {
             });        
     },
 
-    //POST - Adds an ingredient to a recipe
-    //Inputs:
-    //  req.body.recipeId: Id of recipe to change
-    //  req.body.item:  Ingredient to add with a quantity
-    //Returns: 
-    //  recipe: Updated recipe with populated ingredients
-    addRecipeIngredient: function(req, res){
-        if(!req.session.user){
-            req.session.error = "Must be logged in to do that";
-            return res.redirect("/");
-        }
-
-        Recipe.findOne({_id: req.body.recipeId})
-            .then((recipe)=>{
-                recipe.ingredients.push({
-                    ingredient: req.body.item.ingredient,
-                    quantity: req.body.item.quantity
-                });
-                
-                recipe.save()
-                    .then((recipe)=>{
-                        recipe.populate("ingredients.ingredient", (err)=>{
-                            if(err){
-                                return res.json("Error: could not retrieve ingredients.  Please refresh page to see changes");
-                            }
-                            res.json(recipe);
-
-                            let rc = new RecipeChange({
-                                recipe: recipe,
-                                ingredient: req.body.item.ingredient,
-                                change: req.body.item.quantity
-                            });
-                            rc.save().catch((err)=>{});
-
-                            return;
-                        })
-                    })
-                    .catch((err)=>{
-                        return res.json("Error: unable to save recipe data");
-                    });
-            })
-            .catch((err)=>{
-                return res.json("Error: unable to retrieve recipe data");
-            });
-    },
-
-    //POST - Change quantity of a recipe's ingredient
-    //Inputs:
-    //  req.body.recipeId: Id of recipe containing the ingredient
-    //  req.body.ingredient: The ingredient to update (_id and quantity)
-    //Returns: Nothing
-    updateRecipeIngredient: function(req, res){
-        if(!req.session.user){
-            req.session.error = "Must be logged in to do that";
-            return res.redirect("/");
-        }
-
-        Recipe.findOne({_id: req.body.recipeId})
-            .then((recipe)=>{
-                for(let ingredient of recipe.ingredients){
-                    if(ingredient._id.toString() === req.body.ingredient._id){
-                        let change = Number(req.body.ingredient.quantity) - ingredient.quantity;
-                        ingredient.quantity = req.body.ingredient.quantity;
-
-                        recipe.save()
-                            .then((recipe)=>{
-                                res.json({});
-
-                                let rc = new RecipeChange({
-                                    recipe: recipe,
-                                    ingredient: ingredient.ingredient,
-                                    change: change
-                                });
-                                rc.save().catch((err)=>{});
-
-                                return;
-                            })
-                            .catch((err)=>{
-                                return res.json("Error: Could not save recipe data");
-                            });
-                    }
-                }
-            })
-            .catch((err)=>{
-                return res.json("Error: unable to retrieve recipe data");
-            });
-    },
-
-    //POST - Remove an ingredient from a recipe
-    //Inputs:
-    //  req.body.ingredientId: Id of ingredient to be removed
-    //  req.body.recipeId: Id of recipe to remove ingredient from
-    //  req.body.quantity: quantity of recipe ingredient for storing
-    //Returns: Nothing
-    removeRecipeIngredient: function(req, res){
-        if(!req.session.user){
-            req.session.error = "Must be logged in to do that";
-            return res.redirect("/");
-        }
-
-        Recipe.findOne({_id: req.body.recipeId})
-            .then((recipe)=>{
-                for(let i = 0; i < recipe.ingredients.length; i++){
-                    if(recipe.ingredients[i].ingredient._id.toString() === req.body.ingredientId){
-                        recipe.ingredients.splice(i, 1);
-                        break;
-                    }
-                }
-
-                recipe.save()
-                    .then((recipe)=>{
-                        res.json({});
-
-                        let rc = new RecipeChange({
-                            recipe: req.body.recipeId,
-                            ingredient: req.body.ingredientId,
-                            change: -req.body.quantity
-                        });
-                        rc.save().catch((err)=>{});
-
-                        return;
-                    })
-                    .catch((err)=>{
-                        return res.json("Error: unable to save recipe data");
-                    });
-            })
-            .catch((err)=>{
-                return res.json("Error: unable to retrieve recipe data");
-            });
-    },
-
-    //POST - Update merchant information
-    //Inputs:
-    //  req.body.name: name update
-    //  req.body.email: email update
-    //Returns: Nothing
-    updateMerchant: 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)=>{
-                merchant.name = req.body.name;
-                merchant.email = req.body.email;
-                merchant.save()
-                    .then((updatedMerchant)=>{
-                        return res.json({});
-                    })
-                    .catch((err)=>{
-                        return res.json("Error: unable to save merchant data");
-                    });
-            })
-            .catch((err)=>{
-                return res.json("Error: unable to retrieve merchant data");
-            });
-    },
-
     //POST - Update merchant password
     //Inputs:
     //  req.body.oldPass:  current merchant password (supposedly)

+ 0 - 91
controllers/otherData.js

@@ -6,42 +6,6 @@ const Order = require("../models/order");
 const Transaction = require("../models/transaction");
 
 module.exports = {
-    //POST - Creates a new order for a merchant
-    //Inputs:
-    //  req.body: list of orders (ingredient id and quantity)
-    createOrder: 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)=>{
-                for(let order of req.body){
-                    let merchantIngredient = merchant.inventory.find(i => i.ingredient._id.toString() === order.ingredient);
-                    merchantIngredient.quantity += Number(order.quantity);
-                }
-                
-                merchant.save()
-                    .then((merchant)=>{
-                        res.json({});
-                    })
-                    .catch((err)=>{
-                        return res.json("Error: Unable to save data");
-                    });
-            })
-            .catch((err)=>{
-                return res.json("Error: Unable to retrieve user data");
-            });
-
-            let order = new Order({
-                merchant: req.session.user,
-                date: Date.now(),
-                ingredients: req.body
-            });
-            order.save().catch((err)=>{});
-    },
-
     //POST - logs the user in
     //Inputs:
     //  req.body.email
@@ -132,61 +96,6 @@ module.exports = {
             });
     },
 
-    //POST - Gets transactions and orders between 2 dates for a merchant
-    //Inputs:
-    //  req.body.from = start date
-    //  req.body.to = end date
-    //Returns:
-    //  transactions = list of transactions between the dates provided
-    //  orders = list of orders between the dates provided
-    getData: function(req, res){
-        if(!req.session.user){
-            req.session.error = "Must be logged in to do that";
-            return res.redirect("/");
-        }
-
-        let promiseList = [];
-
-        for(let i = 0; i < req.body.dates.length; i+=2){
-            promiseList.push(new Promise((resolve, reject)=>{
-                Transaction.find({merchant: req.session.user, date: {$gte: req.body.dates[i], $lt: req.body.dates[i+1]}},
-                    {date: 1, recipes: 1, _id: 0},
-                    {sort: {date: 1}})
-                    .then((transactions)=>{
-                        resolve(transactions);
-                    })
-                    .catch((err)=>{});
-            }));
-
-            promiseList.push(new Promise((resolve, reject)=>{
-                Order.find({merchant: req.session.user, date: {$gte: req.body.dates[i], $lt: req.body.dates[i+1]}},
-                    {date: 1, ingredients: 1, _id: 0},
-                    {sort: {date: 1}})
-                    .then((orders)=>{
-                        resolve(orders);
-                    })
-                    .catch((err)=>{})
-            }));
-        }
-
-        Promise.all(promiseList)
-            .then((response)=>{
-                let newList = [];
-
-                for(let i = 0; i < response.length; i+=2){
-                    newList.push({
-                        transactions: response[i],
-                        orders: response[i+1]
-                    });
-                }
-
-                return res.json(newList);
-            })
-            .catch((err)=>{
-                return res.json("Error: unable to retrieve user data");
-            });
-    },
-
     resetPassword: function(req, res){
         Merchant.findOne({password: req.body.hash})
             .then((merchant)=>{

+ 0 - 104
controllers/transactionData.js

@@ -3,110 +3,6 @@ const Order = require("../models/order");
 const Merchant = require("../models/merchant");
 
 module.exports = {
-    //POST - returns all transactions for a merchant between given dates
-    //Inputs:
-    //  req.body.from: start date
-    //  req.body.to: end date
-    //Returns:
-    //  List of transactions
-    getTransactions: function(req, res){
-        if(!req.session.user){
-            req.session.error = "You must be logged in to view that page";
-            return res.redirect("/");
-        }
-
-        let date = new Date();
-        let firstDay, lastDay;
-
-        if(req.body.from && req.body.to){
-            firstDay = new Date(req.body.from);
-            lastDay = new Date(req.body.to);
-        }else{
-            firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
-            lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
-        }
-
-        Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}},
-            {_id: 0, date: 1, recipes: 1},
-            {sort: {date: 1}})
-            .then((transactions)=>{
-                return res.json(transactions);
-            })
-            .catch((err)=>{
-                return res.json("Error: could not retrieve sales data");
-            });
-    },
-
-    getOrders: function(req, res){
-        if(!req.session.user){
-            req.session.error = "You must be logged in to view that page";
-            return res.redirect("/");
-        }
-
-        Order.find({merchant: req.session.user})
-            .then((orders)=>{
-                return res.json(orders);
-            })
-            .catch((err)=>{
-                return res.json("Error: could not retrieve order data");
-            })
-    },
-
-    //POST - Update non-pos merchant inventory and create a transaction
-    //Inputs:
-    //  recipesSold: list of recipes sold and how much (recipe._id and quantity)
-    //Returns:
-    //  merchant.inventory: entire merchant inventory after being updated
-    createTransaction: function(req, res){
-        if(!req.session.user){
-            res.session.error = "Must be logged in to do that";
-            return res.redirect("/");
-        }
-
-        let transaction = new Transaction({
-            date: Date.now(),
-            merchant: req.session.user,
-            recipes: []
-        });
-
-        for(let recipe of req.body){
-            transaction.recipes.push({
-                recipe: recipe.id,
-                quantity: recipe.quantity
-            });
-        }
-
-        //Calculate all ingredients used, store to list
-        Merchant.findOne({_id: req.session.user})
-            .populate("recipes")
-            .then((merchant)=>{
-                for(let reqRecipe of req.body){
-                    let merchRecipe = merchant.recipes.find(r => r._id.toString() === reqRecipe.id);
-                    for(let recipeIngredient of merchRecipe.ingredients){
-                        let merchInvIngredient = merchant.inventory.find(i => i.ingredient.toString() === recipeIngredient.ingredient.toString());
-                        merchInvIngredient.quantity -= recipeIngredient.quantity * reqRecipe.quantity;
-                    }
-                }
-
-                merchant.save()
-                    .then((merchant)=>{
-                        res.json({});
-                    })
-                    .catch((err)=>{
-                        return res.json("Error: unable to save user data");
-                    });
-            })
-            .catch((err)=>{
-                return res.json("Error: unable to retrieve user data");
-            });
-
-        transaction.save()
-            .then((transaction)=>{
-                return;
-            })
-            .catch((err)=>{});
-    },
-
     populate: function(req, res){
         if(!req.session.user){
             res.session.error = "Must be logged in to do that";

+ 0 - 11
routes.js

@@ -16,15 +16,10 @@ module.exports = function(app){
     //Merchant
     app.post("/merchant/create/none", merchantData.createMerchantNone);
     app.post("/merchant/create/clover", merchantData.createMerchantClover);
-    // app.get("/merchant/recipes/update", merchantData.updateRecipes);
     app.delete("/merchant/recipes/remove/:id", merchantData.removeRecipe);
     app.put("/merchant/ingredients/add", merchantData.addMerchantIngredient);
     app.delete("/merchant/ingredients/remove/:id", merchantData.removeMerchantIngredient);
     app.put("/merchant/ingredients/update", merchantData.updateMerchantIngredient);
-    // app.post("/merchant/recipes/ingredients/create", merchantData.addRecipeIngredient);
-    // app.post("/merchant/recipes/ingredients/update", merchantData.updateRecipeIngredient);
-    // app.post("/merchant/recipes/ingredients/remove", merchantData.removeRecipeIngredient);
-    // app.post("/merchant/update", merchantData.updateMerchant);
     // app.post("/merchant/password", merchantData.updatePassword);
 
     //Ingredients
@@ -40,18 +35,12 @@ module.exports = function(app){
     app.post("/order", orderData.createOrder);
 
     //Other
-    // app.post("/purchases/create", otherData.createOrder);
     app.post("/login", otherData.login);
     app.get("/logout", otherData.logout);
     app.get("/cloverlogin", otherData.cloverRedirect);
     app.get("/cloverauth*", otherData.cloverAuth);
     app.post("/resetpassword", otherData.resetPassword);
 
-    // app.post("/getdata", otherData.getData);
-
     //Transactions
-    // app.post("/transactions", transactionData.getTransactions);
-    app.get("/orders", transactionData.getOrders);
-    // app.post("/transactions/create", transactionData.createTransaction);  //Creates transaction for non-pos merchant
     app.get("/populatesometransactions", transactionData.populate);
 }

+ 1 - 1
views/dashboardPage/orders.js

@@ -5,7 +5,7 @@ window.ordersStrandObj = {
         if(!this.isPopulated){
             window.orders = [];
 
-            fetch("/orders", {
+            fetch("/order", {
                 method: "GET",
                 headers: {
                     "Content-Type": "application/json;charset=utf-8"