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

Create backend for clover merchant creation. Entire flow for creating merchants is complete.

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

+ 43 - 30
controllers/home.js

@@ -5,10 +5,6 @@ const Merchant = require("../models/merchant");
 const Ingredient = require("../models/ingredient");
 const Recipe = require("../models/recipe");
 
-// const merchantId = "HCVKASXH94531";
-// const token = "f1c88a69-e3e4-059a-da06-8858d0636e82";
-
-const merchantId = "YHVPCQMVB1P81";
 const token = "b48068eb-411a-918e-ea64-52007147e42c";
 
 module.exports = {
@@ -73,9 +69,11 @@ module.exports = {
     //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/${merchantId}/items?access_token=${token}`)
+                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});
                     })
@@ -206,38 +204,53 @@ module.exports = {
     createMerchantClover: function(req, res){
         let data = JSON.parse(req.body.data);
 
-        axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
-            .then((merchant)=>{
-                let newMerchant = new Merchant({
-                    name: merchant.data.name,
-                    posId: merchant.data.id,
-                    lastUpdatedTime: Date.now(),
-                    inventory: [],
-                    recipes: []
+        axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}?access_token=${token}`)
+            .then((cloverMerchant)=>{
+                req.session.posId = undefined;
+
+                let salt = bcrypt.genSaltSync(10);
+                let hash = bcrypt.hashSync(data.password, salt);
+
+                let merchant = new Merchant({
+                    name: cloverMerchant.data.name,
+                    email: data.email,
+                    password: hash,
+                    pos: "clover",
+                    posId: cloverMerchant.data.id
                 });
 
-                for(let ingredient of data.ingredients){
-                    let newIngredient = {
-                        ingredient: ingredient.id,
-                        quantity: parseInt(ingredient.quantity)
-                    }
-                    newMerchant.inventory.push(newIngredient);
+                for(let item of data.inventory){
+                    merchant.inventory.push({
+                        ingredient: item.ingredient.id,
+                        quantity: item.quantity
+                    });
                 }
 
-                let newRecipes = []
                 for(let recipe of data.recipes){
-                    let newRecipe = {
-                        posId: recipe.posId,
-                        merchant: newMerchant._id,
-                        name: recipe.name,
-                        ingredients: []
-                    };
-                    for(let ingredient of recipe.ingredients){
-                        newRecipe.ingredients.push(ingredient);
-                    }
-                    newRecipes.push(newRecipe);
+                    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");
+                    });
+
                 Recipe.create(newRecipes)
                     .then((recipes)=>{
                         for(let recipe of recipes){

+ 5 - 4
views/merchantSetupPage/basicInfo.js

@@ -2,6 +2,10 @@ basicInfoObj = {
     display: function(){
         controller.clearScreen();
         controller.basicInfoStrand.style.display = "flex";
+
+        if(!recipes){
+            document.querySelector("#nameLabel").style.display = "block";
+        }
     },
 
     submit: function(){
@@ -12,10 +16,7 @@ basicInfoObj = {
         let password = document.querySelector("#regPass").value;
         let confirmPassword = document.querySelector("#regConfirmPass").value;
 
-        let nameCheck = validator.merchant.name(name);
-        let passCheck = validator.merchant.password(password, confirmPassword);
-
-        if(nameCheck && passCheck){
+        if(validator.merchant.password(password, confirmPassword)){
             controller.data.name = name;
             controller.data.email = email;
             controller.data.password = password;

+ 9 - 1
views/merchantSetupPage/createRecipes.js

@@ -6,7 +6,15 @@ let createRecipesObj = {
         controller.createRecipesStrand.style.display = "flex";
 
         if(recipes){
-            controller.data.recipes = recipes;
+            controller.data.recipes = [];
+
+            for(let recipe of recipes.elements){
+                controller.data.recipes.push({
+                    name: recipe.name,
+                    posId: recipe.id,
+                    ingredients: [],
+                });
+            }
         }
 
         this.showRecipe();

+ 4 - 0
views/merchantSetupPage/merchantSetup.css

@@ -69,6 +69,10 @@ table{
         flex-direction: column;
     }
 
+    #nameLabel{
+        display: none;
+    }
+
 #addIngredientsStrand{
     display: none;
 }

+ 4 - 6
views/merchantSetupPage/merchantSetup.ejs

@@ -14,11 +14,9 @@
             <h1>Basic Information</h1>
 
             <form onsubmit="basicInfoObj.submit()">
-                <% if(!locals.recipe){ %>
-                    <label>Name
-                        <input id="regName" type="text">
-                    </label>
-                <% } %>
+                <label id="nameLabel">Name
+                    <input id="regName" type="text">
+                </label>
 
                 <label>Email
                     <input id="regEmail" type="email" required>
@@ -32,7 +30,7 @@
                     <input id="regConfirmPass" type="password" required>
                 </label>
 
-                <input type="Submit" value="Create Account">
+                <input type="Submit" value="Next">
             </form>
         </div>
 

+ 0 - 11
views/shared/validation.js

@@ -54,17 +54,6 @@ let validator = {
     },
 
     merchant: {
-        name: function(name, createBanner = true){
-            if(name.length < 3){
-                if(createBanner){
-                    banner.createError("Your name is too short");
-                }
-                return false;
-            }
-
-            return true;
-        },
-
         password: function(pass, confirmPass, createBanner = true){
             if(pass !== confirmPass){
                 if(createBanner){