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

Create backend for creating a new merchant with no pos system.

Lee Morgan 6 лет назад
Родитель
Сommit
c010cd2032

+ 42 - 1
controllers/home.js

@@ -1,4 +1,5 @@
 const axios = require("axios");
+const bcrypt = require("bcryptjs");
 
 const Merchant = require("../models/merchant");
 const Ingredient = require("../models/ingredient");
@@ -258,8 +259,48 @@ module.exports = {
 
     createMerchantNone: function(req, res){
         let data = JSON.parse(req.body.data);
+
+        let salt = bcrypt.genSaltSync(10);
+        let hash = bcrypt.hashSync(data.password, salt);
         
-        console.log(data);
+        let merchant = new Merchant({
+            name: data.name,
+            email: data.email,
+            password: hash,
+            pos: "none"
+        });
+
+        for(let item of data.inventory){
+            merchant.inventory.push({
+                ingredient: item.ingredient.id,
+                quantity: item.quantity
+            });
+        }
+
+        for(let recipe of data.recipes){
+            recipe.merchant = merchant._id;
+        }
+
+        Recipe.create(data.recipes)
+            .then((recipes)=>{
+                for(let recipe of recipes){
+                    merchant.recipes.push(recipe._id);
+                }
+
+                merchant.save()
+                    .then((merchant)=>{
+                        req.session.user = merchant._id;
+                        return res.redirect("/inventory");
+                    })
+                    .catch((err)=>{
+                        console.log(err);
+                        return res.render("error");
+                    });
+            })
+            .catch((err)=>{
+                console.log(err);
+                return res.render("error");
+            });
     },
 
     addMerchantIngredient: function(req, res){

+ 5 - 0
package-lock.json

@@ -27,6 +27,11 @@
         "is-buffer": "^2.0.2"
       }
     },
+    "bcryptjs": {
+      "version": "2.4.3",
+      "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
+      "integrity": "sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms="
+    },
     "bluebird": {
       "version": "3.5.1",
       "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz",

+ 1 - 0
package.json

@@ -18,6 +18,7 @@
   "homepage": "https://github.com/The-Subline/Inventory-Management#readme",
   "dependencies": {
     "axios": "^0.19.0",
+    "bcryptjs": "^2.4.3",
     "ejs": "^2.7.1",
     "express": "^4.17.1",
     "express-session": "^1.17.0",

+ 2 - 2
views/merchantSetupPage/addIngredients.js

@@ -50,7 +50,7 @@ addIngredientsObj = {
     },
 
     submit: function(){
-        controller.data.ingredients = [];
+        controller.data.inventory = [];
 
         let tbody = document.querySelector("#ingredient-display tbody");
         let isValid = true;
@@ -59,7 +59,7 @@ addIngredientsObj = {
                 let quantity = row.children[3].children[0].value;
 
                 if(validator.ingredient.quantity(quantity)){
-                    controller.data.ingredients.push({
+                    controller.data.inventory.push({
                         ingredient: {
                             id: row.id,
                             name: row.children[1].innerText,

+ 19 - 12
views/merchantSetupPage/createIngredients.js

@@ -58,6 +58,7 @@ let createIngredientsObj = {
         let isValid = true;
 
         let newIngredients = [];
+        let axiosIngredients = [];
         
         for(let row of tbody.children){
             let name = row.children[0].children[0].value;
@@ -71,10 +72,17 @@ let createIngredientsObj = {
             let checkUnit = validator.ingredient.unit(unit);
 
             if(checkName && checkCategory && checkQuantity && checkUnit){
-                newIngredients.push({
+                let newIngredient = {
                     name: name,
                     category: category,
                     unit: unit
+                }
+
+                axiosIngredients.push(newIngredient);
+
+                newIngredients.push({
+                    ingredient: newIngredient,
+                    quantity: quantity
                 });
             }else{
                 isValid = false;
@@ -83,18 +91,17 @@ let createIngredientsObj = {
         }
 
         if(isValid){
-            axios.post("/ingredients/create", newIngredients)
+            axios.post("/ingredients/create", axiosIngredients)
                 .then((ingredients)=>{
-                    for(let ingredient of ingredients.data){
-                        controller.data.ingredients.push({
-                            ingredient: {
-                                id: ingredient._id,
-                                name: ingredient.name,
-                                category: ingredient.category,
-                                unit: ingredient.unit
-                            },
-                            quantity: quantity
-                        });
+                    for(let ingredient of newIngredients){
+                        for(let createdIngredient of ingredients.data){
+                            if(createdIngredient.name === ingredient.ingredient.name){
+                                ingredient.ingredient.id = createdIngredient._id;
+                                break;
+                            }
+                        }
+
+                        controller.data.inventory.push(ingredient);
                     }
 
                     banner.createNotification("All ingredients have been created and added to your inventory");

+ 2 - 2
views/merchantSetupPage/createRecipes.js

@@ -79,7 +79,7 @@ let createRecipesObj = {
 
             if(validator.ingredient.quantity(quantity)){
                 controller.data.recipes[this.recipeIndex].ingredients.push({
-                    id: row.children[0].children[0].value,
+                    ingredient: row.children[0].children[0].value,
                     quantity: quantity
                 });
             }else{
@@ -113,7 +113,7 @@ let createRecipesObj = {
         row.appendChild(ingTd);
 
         let ingName = document.createElement("select");
-        for(let ingredient of controller.data.ingredients){
+        for(let ingredient of controller.data.inventory){
             let newOption = document.createElement("option");
             newOption.innerText = ingredient.ingredient.name;
             newOption.value = ingredient.ingredient.id;