Преглед изворни кода

Completely finish merchant setup page

Lee Morgan пре 6 година
родитељ
комит
a5e8b5aa79

+ 37 - 35
controllers/home.js

@@ -29,7 +29,6 @@ module.exports = {
             .then((ingredients)=>{
                 axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
                     .then((recipes)=>{
-                        console.log(recipes);
                         return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
                     })
                     .catch((err)=>{
@@ -46,7 +45,6 @@ module.exports = {
     getRecipes: (req, res)=>{
         axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
             .then((recipes)=>{
-                
                 return res.json(recipes);
             })
             .catch((err)=>{
@@ -55,52 +53,56 @@ module.exports = {
     },
 
     createMerchant: (req, res)=>{
-        let newIngredient = [];
         let data = JSON.parse(req.body.data);
-        for(let ingredient of data.new){
-            newIngredient.push({
-                name: ingredient.name,
-                category: ingredient.category,
-                unitType: ingredient.unitType
-            });
-        }
-
-        Ingredient.create(newIngredient)
-            .then((ingredients)=>{
-                ingredientIds = [];
-                for(let i = 0; i < ingredients.length; i++){
-                    data.existing.push({
-                        id: ingredient[i]._id,
-                        quantity: data.new[i].quantity
-                    });
-                    ingredientIds.push({
-                        tempId: data.new[i].id,
-                        permId: ingredients[i]._id
-                    });
-                }
 
-                Recipe.create()
+        Recipe.create(data.recipes)
+            .then((recipes)=>{
                 axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
                     .then((merchant)=>{
-                        Merchant.create({
-                            cloverId: merchant._id,
+                        console.log(merchant.data);
+                        let newMerchant = new Merchant({
+                            name: merchant.data.name,
+                            cloverId: merchant.data.id,
                             lastUpdateTime: Date.now,
-                            ingredients: data.existing
-                        })
-                            .then((merchant)=>{
-                                console.log(merchant);
+                            ingredients: [],
+                            recipes: []
+                        });
+
+                        for(let ingredient of data.ingredients){
+                            let newIngredient = {
+                                id: ingredient.id,
+                                quantity: parseInt(ingredient.quantity)
+                            }
+                            newMerchant.ingredients.push(newIngredient);
+                        }
+
+                        for(let recipe of recipes){
+                            newMerchant.recipes.push(recipe._id);
+                        }
+
+                        newMerchant.save()
+                            .then((newMerchant)=>{
+                                return res.redirect("/");
                             })
                             .catch((err)=>{
                                 console.log(err);
                                 return res.render("error");
-                            });
+                            })
                     })
                     .catch((err)=>{
                         console.log(err);
-                        return res.render("error");
-                    })
-                
+                    });
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
+    },
 
+    createNewIngredients: (req, res)=>{
+        Ingredient.create(req.body)
+            .then((ingredients)=>{
+                return res.json(ingredients);
             })
             .catch((err)=>{
                 console.log(err);

+ 8 - 4
models/merchant.js

@@ -1,6 +1,10 @@
 const mongoose = require("mongoose");
 
 const MerchantSchema = new mongoose.Schema({
+    name: {
+        type: String,
+        required: true
+    },
     cloverId: {
         type: String,
         required: true
@@ -9,10 +13,6 @@ const MerchantSchema = new mongoose.Schema({
         type: Date,
         default: Date.now
     },
-    recipes: [{
-        type: mongoose.Schema.Types.ObjectId,
-        ref: "Recipe"
-    }],
     ingredients: [{
         id: {
             type: mongoose.Schema.Types.ObjectId,
@@ -22,6 +22,10 @@ const MerchantSchema = new mongoose.Schema({
             type: Number,
             min: [0, "Quantity cannot be less than 0"]
         }
+    }],
+    recipes: [{
+        type: mongoose.Schema.Types.ObjectId,
+        ref: "Recipe"
     }]
 });
 

+ 8 - 6
models/recipe.js

@@ -10,13 +10,15 @@ const RecipeSchema = new mongoose.Schema({
         required: true,
         minlength: [3, "Name of recipe must contain at least three characters"]
     },
-    // merchant: {
-    //     type: mongoose.Schema.Types.ObjectId,
-    //     ref: "Merchant"
-    // },
     ingredients: [{
-        type: mongoose.Schema.Types.ObjectId,
-        ref: "Ingredient"
+        id: {
+            type: mongoose.Schema.Types.ObjectId,
+            ref: "Ingredient"
+        },
+        quantity: {
+            type: Number,
+            required: true
+        }
     }]
 });
 

Разлика између датотеке није приказан због своје велике величине
+ 1157 - 0
package-lock.json


+ 2 - 1
package.json

@@ -20,6 +20,7 @@
     "axios": "^0.19.0",
     "ejs": "^2.7.1",
     "express": "^4.17.1",
-    "mongoose": "^5.7.1"
+    "mongoose": "^5.7.1",
+    "nodemon": "^1.19.3"
   }
 }

+ 1 - 0
routes.js

@@ -5,4 +5,5 @@ module.exports = function(app){
     app.get("/merchant/new", home.merchantSetup);
     app.get("/getrecipes", home.getRecipes);
     app.post("/merchant/create", home.createMerchant);
+    app.post("/ingredients/create", home.createNewIngredients);
 }

+ 1 - 0
views/merchantSetupPage/merchantSetup.ejs

@@ -75,6 +75,7 @@
             let ingredients = <%- JSON.stringify(ingredients) %>;
             let recipes = <%- JSON.stringify(recipes) %>;
         </script>
+        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
         <script src="/merchantSetupPage/merchantSetup.js"></script>
     </body>
 </html>

+ 40 - 28
views/merchantSetupPage/merchantSetup.js

@@ -36,7 +36,6 @@ let updateState = (num)=>{
         newIngredients.style.display = "none";
         createRecipes.style.display = "flex";
         createIngredientsList();
-        showRecipe();
     }
 }
 
@@ -118,30 +117,54 @@ let newIngredientField = ()=>{
 }
 
 let createIngredientsList = ()=>{
-    data.existing = [];
+    data.ingredients = [];  //changed
     for(let ingredient of existingIngredientElements){
         if(ingredient.childNodes[0].childNodes[0].checked){
-            data.existing.push({
+            data.ingredients.push({
                 id: ingredient.id,
                 name: ingredient.childNodes[1].childNodes[0].nodeValue,
-                category: ingredient.childNodes[2].childNodes[0].nodeValue,
-                quantity: ingredient.childNodes[3].childNodes[0].value
+                quantity: ingredient.childNodes[3].childNodes[0].value,
+                unitType: ingredient.childNodes[4].childNodes[0].nodeValue
             });
         }
     }
 
-    data.new = [];
-    let id = 0;
+    let newIngredient = [];
+    let newIngredientQuantity = [];
     for(let ingredient of newIngredientElements){
-        data.new.push({
-            id: id,
+        newIngredient.push({
             name: ingredient.childNodes[0].childNodes[0].value,
             category: ingredient.childNodes[1].childNodes[0].value,
-            quantity: ingredient.childNodes[2].childNodes[0].value,
             unitType: ingredient.childNodes[3].childNodes[0].value
         });
-        id++;
+        newIngredientQuantity.push({
+            name: ingredient.childNodes[0].childNodes[0].value,
+            quantity: ingredient.childNodes[2].childNodes[0].value
+        })
     }
+
+    axios.post("/ingredients/create", newIngredient)
+        .then((result)=>{
+            for(let ingredient of result.data){
+                let newIngredient = {
+                    id: ingredient._id,
+                    name: ingredient.name,
+                    unitType: ingredient.unitType
+                }
+
+                for(let item of newIngredientQuantity){
+                    if(ingredient.name === item.name){
+                        newIngredient.quantity = item.quantity;
+                    }
+                }
+
+                data.ingredients.push(newIngredient);
+            }
+            showRecipe();
+        })
+        .catch((err)=>{
+            console.log(err);
+        });
 }
 
 //Recipe functions
@@ -157,18 +180,13 @@ let showRecipe = ()=>{
         let ingTd = document.createElement("td");
         row.appendChild(ingTd);
         let ingName = document.createElement("select");
-        for(let ingredient of data.existing){
+        console.log(data.ingredients);
+        for(let ingredient of data.ingredients){
             let newOption = document.createElement("option");
             newOption.innerText = ingredient.name;
             newOption.value = ingredient.id;
             ingName.appendChild(newOption);
         }
-        for(let ingredient of data.new){
-            let newOption = document.createElement("option");
-            newOption.innerText = ingredient.name;
-            newOption.value = ingredient.name;
-            ingName.appendChild(newOption);
-        }
         ingTd.appendChild(ingName);
 
         let quantTd = document.createElement("td");
@@ -206,13 +224,7 @@ let addRecipeIngredientField = ()=>{
     let ingTd = document.createElement("td");
     row.appendChild(ingTd);
     let ingName = document.createElement("select");
-    for(let ingredient of data.existing){
-        let newOption = document.createElement("option");
-        newOption.innerText = ingredient.name;
-        newOption.value = ingredient.id;
-        ingName.appendChild(newOption);
-    }
-    for(let ingredient of data.new){
+    for(let ingredient of data.ingredients){
         let newOption = document.createElement("option");
         newOption.innerText = ingredient.name;
         newOption.value = ingredient.id;
@@ -252,7 +264,8 @@ let submitAll = ()=>{
 
     for(let recipe of recipeData){
         let newRecipe = {
-            id: recipe.id,
+            cloverId: recipe.id,
+            name: recipe.name,
             ingredients: []
         };
         for(let ingredient of recipe.ingredients){
@@ -276,8 +289,7 @@ let submitAll = ()=>{
 
     form.appendChild(dataInput);
     document.body.appendChild(form);
-    console.log(data);
-    // form.submit();
+    form.submit();
 }
 
 populateIngredients();

Неке датотеке нису приказане због велике количине промена