Просмотр исходного кода

Create file for render functions

Lee Morgan 6 лет назад
Родитель
Сommit
b867b551e0
4 измененных файлов с 155 добавлено и 144 удалено
  1. 1 1
      app.js
  2. 0 138
      controllers/home.js
  3. 148 0
      controllers/render.js
  4. 6 5
      routes.js

+ 1 - 1
app.js

@@ -9,7 +9,7 @@ mongoose.connect("mongodb://localhost/InventoryManagement", {useNewUrlParser: tr
 app.set("view engine", "ejs");
 
 app.use(session({
-    secret: "Super Secret Subline",
+    secret: "Super Secret Subline Subliminally Saving Secrets So Sneaky Snakes Stay Sullen",
     cookie: {secure: false},
     saveUninitialized: true,
     resave: false

+ 0 - 138
controllers/home.js

@@ -4,150 +4,12 @@ const bcrypt = require("bcryptjs");
 const Merchant = require("../models/merchant");
 const Ingredient = require("../models/ingredient");
 const Recipe = require("../models/recipe");
-const Transaction = require("../models/transaction");
 const nonPosTransaction = require("../models/nonPosTransaction");
 const InventoryAdjustment = require("../models/inventoryAdjustment");
 
 const token = "b48068eb-411a-918e-ea64-52007147e42c";
 
 module.exports = {
-    landingPage: function(req, res){
-        let error = {};
-        if(req.session.error){
-            error = req.session.error;
-            req.session.error = undefined;
-        }else{
-            error = undefined;
-        }
-
-        return res.render("landingPage/landing", {error: error});
-    },
-
-    displayInventory: function(req, res){
-        if(!req.session.user){
-            return res.redirect("/");
-        }
-
-        Merchant.findOne({_id: req.session.user})
-            .populate("inventory.ingredient")
-            .populate("recipes")
-            .then((merchant)=>{
-                if(merchant.pos === "clover"){
-                    axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
-                        .then((result)=>{
-                            let transactions = [];
-
-                            for(let order of result.data.elements){
-                                let newTransaction = new Transaction({
-                                    merchant: merchant._id,
-                                    date: new Date(order.createdTime),
-                                    device: order.device.id
-                                });
-
-                                for(let item of order.lineItems.elements){
-                                    let recipe = merchant.recipes.find(r => r.posId === item.item.id);
-                                    if(recipe){
-                                        newTransaction.recipes.push(recipe._id);
-                                        for(let ingredient of recipe.ingredients){
-                                            let inventoryIngredient = {};
-                                            for(let invItem of merchant.inventory){
-                                                if(invItem.ingredient._id.toString() === ingredient.ingredient.toString()){
-                                                    inventoryIngredient = invItem;
-                                                }
-                                            }
-                                            inventoryIngredient.quantity -= ingredient.quantity;
-                                        }
-                                    }
-                                }
-
-                                transactions.push(newTransaction);
-                            }
-                            merchant.lastUpdatedTime = Date.now();
-
-                            merchant.save()
-                                .then((updatedMerchant)=>{
-                                    res.render("inventoryPage/inventory", {merchant: updatedMerchant});
-                                    Transaction.create(transactions);
-                                    return;
-                                })
-                                .catch((err)=>{
-                                    console.log(err);
-                                    return res.render("error");
-                                });
-                        })
-                        .catch((err)=>{
-                            console.log(err);
-                        });
-                }else if(merchant.pos === "none"){
-                    return res.render("inventoryPage/inventory", {merchant: merchant})
-                }else{
-                    return res.redirect("/");
-                }
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });
-    },
-
-    //Display page to set up new merchant with Clover POS
-    //TODO: This is for development, needs updating for production
-    merchantSetupClover: function(req, res){
-        req.session.posId = "YHVPCQMVB1P81";
-        
-        Ingredient.find()
-            .then((ingredients)=>{
-                axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}/items?access_token=${token}`)
-                    .then((recipes)=>{
-                        return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
-                    })
-                    .catch((err)=>{
-                        console.log(err);
-                        return res.render("error");
-                    });
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });
-    },
-
-    //Display page to set up merchant with no POS system
-    merchantSetupNone: function(req, res){
-        Ingredient.find()
-            .then((ingredients)=>{
-                return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null});
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });
-    },
-
-    displayRecipes: function(req, res){
-        if(!req.session.user){
-            return res.render("error");
-        }
-
-        Merchant.findOne({_id: req.session.user})
-            .populate({
-                path: "recipes",
-                model: "Recipe",
-                populate: {
-                    path: "ingredients.ingredient",
-                    model: "Ingredient"
-                }
-            })
-            .populate("inventory.ingredient")
-            .then((merchant)=>{
-                return res.render("recipesPage/recipes", {merchant: merchant});
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });
-    },
-
     updateRecipes: function(req, res){
         if(!req.session.user){
             return res.render("error");

+ 148 - 0
controllers/render.js

@@ -0,0 +1,148 @@
+const axios = require("axios");
+
+const Merchant = require("../models/merchant");
+const Ingredient = require("../models/ingredient");
+const Transaction = require("../models/transaction");
+
+const token = "b48068eb-411a-918e-ea64-52007147e42c";
+
+module.exports = {
+    //Public page including login and registration
+    landingPage: function(req, res){
+        let error = {};
+        if(req.session.error){
+            error = req.session.error;
+            req.session.error = undefined;
+        }else{
+            error = undefined;
+        }
+
+        return res.render("landingPage/landing", {error: error});
+    },
+
+    //Render the main page for merchants
+    displayInventory: function(req, res){
+        if(!req.session.user){
+            return res.redirect("/");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .populate("inventory.ingredient")
+            .populate("recipes")
+            .then((merchant)=>{
+                if(merchant.pos === "clover"){
+                    axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
+                        .then((result)=>{
+                            let transactions = [];
+
+                            for(let order of result.data.elements){
+                                let newTransaction = new Transaction({
+                                    merchant: merchant._id,
+                                    date: new Date(order.createdTime),
+                                    device: order.device.id
+                                });
+
+                                for(let item of order.lineItems.elements){
+                                    let recipe = merchant.recipes.find(r => r.posId === item.item.id);
+                                    if(recipe){
+                                        newTransaction.recipes.push(recipe._id);
+                                        for(let ingredient of recipe.ingredients){
+                                            let inventoryIngredient = {};
+                                            for(let invItem of merchant.inventory){
+                                                if(invItem.ingredient._id.toString() === ingredient.ingredient.toString()){
+                                                    inventoryIngredient = invItem;
+                                                }
+                                            }
+                                            inventoryIngredient.quantity -= ingredient.quantity;
+                                        }
+                                    }
+                                }
+
+                                transactions.push(newTransaction);
+                            }
+                            merchant.lastUpdatedTime = Date.now();
+
+                            merchant.save()
+                                .then((updatedMerchant)=>{
+                                    res.render("inventoryPage/inventory", {merchant: updatedMerchant});
+                                    Transaction.create(transactions);
+                                    return;
+                                })
+                                .catch((err)=>{
+                                    console.log(err);
+                                    return res.render("error");
+                                });
+                        })
+                        .catch((err)=>{
+                            console.log(err);
+                        });
+                }else if(merchant.pos === "none"){
+                    return res.render("inventoryPage/inventory", {merchant: merchant})
+                }else{
+                    return res.redirect("/");
+                }
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+    },
+
+    //Display Merchant Setup Page
+    merchantSetupClover: function(req, res){
+        req.session.posId = "YHVPCQMVB1P81";
+        
+        Ingredient.find()
+            .then((ingredients)=>{
+                axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}/items?access_token=${token}`)
+                    .then((recipes)=>{
+                        return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
+                    })
+                    .catch((err)=>{
+                        console.log(err);
+                        return res.render("error");
+                    });
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+    },
+
+    //Display page to set up merchant with no POS system
+    merchantSetupNone: function(req, res){
+        Ingredient.find()
+            .then((ingredients)=>{
+                return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null});
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+    },
+
+    //Display page with recipe information
+    displayRecipes: function(req, res){
+        if(!req.session.user){
+            return res.render("error");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .populate({
+                path: "recipes",
+                model: "Recipe",
+                populate: {
+                    path: "ingredients.ingredient",
+                    model: "Ingredient"
+                }
+            })
+            .populate("inventory.ingredient")
+            .then((merchant)=>{
+                return res.render("recipesPage/recipes", {merchant: merchant});
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+    }
+}

+ 6 - 5
routes.js

@@ -1,12 +1,13 @@
 const home = require("./controllers/home");
+const render = require("./controllers/render");
 
 module.exports = function(app){
     //Render page
-    app.get("/", home.landingPage);
-    app.get("/inventory", home.displayInventory);
-    app.get("/merchant/new/clover", home.merchantSetupClover);
-    app.get("/merchant/new/none", home.merchantSetupNone);
-    app.get("/recipes", home.displayRecipes);
+    app.get("/", render.landingPage);
+    app.get("/inventory", render.displayInventory);
+    app.get("/merchant/new/clover", render.merchantSetupClover);
+    app.get("/merchant/new/none", render.merchantSetupNone);
+    app.get("/recipes", render.displayRecipes);
 
     //Merchant
     app.get("/merchant/recipes/update", home.updateRecipes);