فهرست منبع

Update backend with session

Lee Morgan 6 سال پیش
والد
کامیت
e93489cc12
4فایلهای تغییر یافته به همراه15 افزوده شده و 12 حذف شده
  1. 10 9
      controllers/home.js
  2. 2 0
      views/inventory/inventory.ejs
  3. 2 2
      views/inventory/inventory.js
  4. 1 1
      views/recipesPage/recipes.js

+ 10 - 9
controllers/home.js

@@ -15,6 +15,7 @@ module.exports = {
             .populate("inventory.ingredient")
             .then((merchant)=>{
                 if(merchant){
+                    req.session.user = merchant._id;
                     axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
                         .then((result)=>{
                             for(let order of result.data.elements){
@@ -76,7 +77,7 @@ module.exports = {
     },
 
     updateMerchant: function(req, res){
-        Merchant.updateOne({_id: req.body._id}, req.body)
+        Merchant.updateOne({_id: req.session.user}, req.body)
             .then((merchant)=>{
                 return res.json(merchant);
             })
@@ -147,13 +148,13 @@ module.exports = {
     },
 
     createIngredient: function(req, res){
-        Ingredient.create(req.body.ingredient.ingredient)
+        Ingredient.create(req.body.ingredient)
             .then((ingredient)=>{
-                Merchant.findOne({_id: req.body.merchantId})
+                Merchant.findOne({_id: req.session.user})
                     .then((merchant)=>{
                         let item = {
                             ingredient: ingredient,
-                            quantity: req.body.ingredient.quantity
+                            quantity: req.body.quantity
                         }
                         merchant.inventory.push(item);
                         merchant.save()
@@ -178,7 +179,7 @@ module.exports = {
     },
 
     displayRecipes: function(req, res){
-        Merchant.findOne({posId: merchantId})
+        Merchant.findOne({_id: req.session.user})
             .populate("recipes.ingredients.ingredient")
             .populate("inventory.ingredient")
             .then((merchant)=>{
@@ -217,7 +218,7 @@ module.exports = {
     },
 
     updateRecipes: function(req, res){
-        Merchant.findOne({posId: merchantId})
+        Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
                 axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
                     .then((result)=>{
@@ -289,9 +290,9 @@ module.exports = {
     },
 
     addMerchantIngredient: function(req, res){
-        Merchant.findOne({_id: req.body.merchantId})
+        Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
-                merchant.inventory.push(req.body.ingredient);
+                merchant.inventory.push(req.body);
                 merchant.save()
                     .then((newMerchant)=>{
                         return res.json(newMerchant);
@@ -308,7 +309,7 @@ module.exports = {
     },
 
     addRecipeIngredient: function(req, res){
-        Merchant.findOne({_id: req.body.merchantId})
+        Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
                 let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
                 recipe.ingredients.push(req.body.item)

+ 2 - 0
views/inventory/inventory.ejs

@@ -28,6 +28,8 @@
                     <tbody></tbody>
                 </thead>
             </table>
+
+            <button id="viewRecipes">View Recipes</button>
         </div>
 
         <div class="add-ingredient">

+ 2 - 2
views/inventory/inventory.js

@@ -241,7 +241,7 @@ let addIngredient = (item)=>{
     if(validator.ingredient.all(item.ingredient, item.quantity)){
         merchant.inventory.push(item);
         if(item.ingredient._id){
-            axios.post("/merchant/ingredients/create", {ingredient: item, merchantId: merchant._id})
+            axios.post("/merchant/ingredients/create", item)
                 .then((newMerchant)=>{
                     filter();
                     banner.createNotification("The new ingredient has been successfully added to your inventory");
@@ -251,7 +251,7 @@ let addIngredient = (item)=>{
                     console.log(err);
                 });
         }else{
-            axios.post("/ingredients/createone", {ingredient: item, merchantId: merchant._id})
+            axios.post("/ingredients/createone", item)
                 .then((newMerchant)=>{
                     filter();
                     banner.createNotification("The new ingredient has been successfully added to your inventory");

+ 1 - 1
views/recipesPage/recipes.js

@@ -120,7 +120,7 @@ let recipesPage = {
             quantity: quantity
         }
         
-        axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item, merchantId: merchant._id})
+        axios.post("/merchant/recipes/ingredients/create", {recipeId: recipe._id, item: item})
             .then((newMerchant)=>{
                 let addIngredient = merchant.inventory.find(i => i.ingredient._id === ingredientId);
                 recipe.ingredients.push({