ソースを参照

Create otherData.js and remove homeController

Lee Morgan 6 年 前
コミット
a6ed9d597b
2 ファイル変更19 行追加41 行削除
  1. 15 31
      controllers/otherData.js
  2. 4 10
      routes.js

+ 15 - 31
controllers/home.js → controllers/otherData.js

@@ -1,14 +1,16 @@
-const axios = require("axios");
 const bcrypt = require("bcryptjs");
 
-const Merchant = require("../models/merchant");
-const nonPosTransaction = require("../models/nonPosTransaction");
-
-const token = "b48068eb-411a-918e-ea64-52007147e42c";
+const NonPosTransaction = require("../models/nonPosTransaction");
+const Merchant = require("merchant");
 
 module.exports = {
+    //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){
-        let transaction = new nonPosTransaction({
+        let transaction = new NonPosTransaction({
             date: Date.now(),
             author: "None",
             merchant: req.session.user,
@@ -51,31 +53,11 @@ module.exports = {
             });
     },
 
-    getCloverRecipes: function(req, res){
-        if(!req.session.user){
-            return res.render("error");
-        }
-
-        Merchant.findOne({_id: req.session.user})
-            .then((merchant)=>{
-                axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${token}`)
-                    .then((recipes)=>{
-                        return res.json(recipes);
-                    })
-                    .catch((err)=>{
-                        return res.json(err);
-                    });
-            })
-            .catch((err)=>{
-                console.log(err);
-                return res.render("error");
-            });
-    },
-
-    unregistered: function(req, res){
-        return res.redirect("/");
-    },
-
+    //POST - logs the user in
+    //Inputs:
+    //  req.body.email
+    //  req.body.password
+    //Redirects to "/inventory" on success
     login: function(req, res){
         Merchant.findOne({email: req.body.email.toLowerCase()})
             .then((merchant)=>{
@@ -101,6 +83,8 @@ module.exports = {
             });
     },
 
+    //GET - logs the user out
+    //Redirects to "/"
     logout: function(req, res){
         req.session.user = undefined;
 

+ 4 - 10
routes.js

@@ -1,7 +1,7 @@
-const home = require("./controllers/home");
 const render = require("./controllers/render");
 const merchantData = require("./controllers/merchantData");
 const ingredientData = require("./controllers/ingredientData");
+const otherData = require("./controllers/otherData");
 
 module.exports = function(app){
     //Render page
@@ -27,14 +27,8 @@ module.exports = function(app){
     app.post("/ingredients/create", ingredientData.createNewIngredients);
     app.post("/ingredients/createone", ingredientData.createIngredient);  //also adds to merchant
 
-    //Transactions
-    app.post("/transactions/create", home.createTransaction);
-
-    //Clover API
-    app.get("/getrecipes", home.getCloverRecipes);
-
     //Other
-    app.get("/unregistered", home.unregistered);
-    app.post("/login", home.login);
-    app.get("/logout", home.logout);
+    app.post("/transactions/create", otherData.createTransaction);  //Creates transaction for non-pos merchant
+    app.post("/login", otherData.login);
+    app.get("/logout", otherData.logout);
 }