Bladeren bron

Replace code for creating new square merchant that was deleted by git.

Lee Morgan 5 jaren geleden
bovenliggende
commit
1461ea84c3

+ 25 - 30
controllers/squareData.js

@@ -155,29 +155,25 @@ module.exports = {
     },
 
     updateRecipes: function(req, res){
-        let merchant = {};
         let merchantRecipes = [];
         let newRecipes = [];
+
+        let populate = res.locals.merchant.populate("recipes").execPopulate();
+
+        let squareRecipes = axios.post(`${process.env.SQUARE_ADDRESS}/v2/catalog/search`, {
+                object_types: ["ITEM"]
+            }, {
+                headers: {
+                    Authorization: `Bearer ${res.locals.owner.square.accessToken}`
+                }
+            });
     
-        res.locals.merchant
-            .populate("recipes")
-            .execPopulate()
-            .then((fetchedMerchant)=>{
-                merchant = fetchedMerchant;
-                return axios.post(`${process.env.SQUARE_ADDRESS}/v2/catalog/search`, {
-                    object_types: ["ITEM"]
-                }, {
-                    headers: {
-                        Authorization: `Bearer ${merchant.square.accessToken}`
-                    }
-                });
-            })
+        Promise.all([populate, squareRecipes])
             .then((response)=>{
-                merchantRecipes = merchant.recipes.slice();
+                merchantRecipes = res.locals.merchant.recipes.slice();
     
-                
-                for(let i = 0; i < response.data.objects.length; i++){
-                    let itemData = response.data.objects[i].item_data;
+                for(let i = 0; i < response[1].data.objects.length; i++){
+                    let itemData = response[1].data.objects[i].item_data;
                     for(let j = 0; j < itemData.variations.length; j++){
                         let isFound = false;
     
@@ -193,7 +189,7 @@ module.exports = {
                         if(!isFound){
                             let newRecipe = new Recipe({
                                 posId: itemData.variations[j].id,
-                                merchant: merchant._id,
+                                merchant: res.locals.merchant._id,
                                 name: "",
                                 price: itemData.variations[j].item_variation_data.price_money.amount,
                                 ingredients: []
@@ -206,7 +202,7 @@ module.exports = {
                             }
     
                             newRecipes.push(newRecipe);
-                            merchant.recipes.push(newRecipe);
+                            res.locals.merchant.recipes.push(newRecipe);
                         }
                     }
                 }
@@ -214,9 +210,9 @@ module.exports = {
                 let ids = [];
                 for(let i = 0; i < merchantRecipes.length; i++){
                     ids.push(merchantRecipes[i]._id);
-                    for(let j = 0; j < merchant.recipes.length; j++){
-                        if(merchantRecipes[i]._id.toString() === merchant.recipes[j]._id.toString()){
-                            merchant.recipes.splice(j, 1);
+                    for(let j = 0; j < res.locals.merchant.recipes.length; j++){
+                        if(merchantRecipes[i]._id.toString() === res.locals.merchant.recipes[j]._id.toString()){
+                            res.locals.merchant.recipes.splice(j, 1);
                             j--;
                             break;
                         }
@@ -226,18 +222,14 @@ module.exports = {
                 if(newRecipes.length > 0) Recipe.create(newRecipes);
                 if(merchantRecipes.length > 0) Recipe.deleteMany({_id: {$in: ids}});    
 
-                return merchant.save();
+                return res.locals.merchant.save();
             })
             .then((merchant)=>{
                 return res.json({new: newRecipes, removed: merchantRecipes});
             })
             .catch((err)=>{
-                if(typeof(err) === "string"){
-                    return res.json(err);
-                }
-                if(err.name === "ValidationError"){
-                    return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
-                }
+                if(typeof(err) === "string") return res.json(err);
+                if(err.name === "ValidationError") return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
                 return res.json("ERROR: UNABLE TO RETRIEVE RECIPE DATA FROM SQUARE");
             });
     },
@@ -286,9 +278,11 @@ module.exports = {
 
     /*
     GET: create new merchant from square location and add to owner
+    req.params.location = location.id
     response = [Owner, Merchant]
     */
     addMerchant: function(req, res){
+        console.log(req.params.location);
         let merchant = new Merchant({
             owner: res.locals.owner._id,
             pos: "square",
@@ -339,6 +333,7 @@ module.exports = {
                 helper.getAllMerchantTransactions(merchant, res.locals.owner.square.accessToken);
             })
             .catch((err)=>{
+                console.log(err.response.data);
                 if(err.name === "ValidationError") return req.session.err = err.errors[Object.keys(err.errors)[0]].properties.message;
                 return res.json("ERROR: UNABLE TO CREATE NEW MERCHANT");
             });

+ 18 - 1
views/dashboardPage/ejs/strands/account.ejs

@@ -19,7 +19,24 @@
             <div class="settingsSectionHeader">
                 <h2>YOUR LOCATIONS</h2>
 
-                <button id="settingsAddMerchant" class="button">NEW LOCATION</button>
+                <div>
+                    <button id="newMerchantSquareButton" type="button">
+                        <svg aria-hidden="true" viewBox="0 0 44 44" width="24" height="24">
+                            <path fill="currentColor" d="M36.65 0H7.354A7.354 7.354 0 0 0 0 7.354V36.65a7.354 7.354 0 0 0 7.354 7.354H36.65a7.353 7.353 0 0 0 7.354-7.354V7.354A7.352 7.352 0 0 0 36.65 0zm-.646 33.685a2.32 2.32 0 0 1-2.32 2.32H10.325a2.32 2.32 0 0 1-2.321-2.32v-23.36a2.32 2.32 0 0 1 2.321-2.321h23.359a2.32 2.32 0 0 1 2.32 2.321v23.36z"></path>
+                            <path fill="currentColor" d="M17.333 28.003c-.736 0-1.332-.6-1.332-1.339V17.34c0-.739.596-1.339 1.332-1.339h9.338c.738 0 1.332.6 1.332 1.339v9.324c0 .739-.594 1.339-1.332 1.339h-9.338z"></path>
+                        </svg>
+                        <svg aria-hidden="true" viewBox="0 0 66 22" width="75" height="24">
+                            <path d="M11.2 10c-.7-.2-1.3-.4-1.8-.6C8.4 9 8 8.4 8 7.6c0-1.5 1.5-2.2 3-2.2 1.4 0 2.6.6 3.5 1.6l.1.1 1.2-.9-.1-.1c-1.1-1.4-2.7-2.2-4.6-2.2-1.2 0-2.4.3-3.2.9-1 .8-1.5 1.7-1.5 2.9 0 2.7 2.6 3.4 4.6 3.9 2.1.6 3.4 1 3.4 2.6s-1.3 2.6-3.2 2.6c-1 0-2.8-.3-3.9-2l-.1-.1-1.2.9v.1c1.1 1.6 2.9 2.5 5.1 2.5 2.9 0 4.9-1.6 4.9-4.1 0-2.8-2.7-3.5-4.8-4.1z" fill="currentColor"></path>
+                            <path d="M25.5 9.5V8h1.4v14h-1.4v-5.5c-.8 1.1-2 1.7-3.4 1.7-2.7 0-4.5-2.1-4.5-5.2s1.9-5.2 4.5-5.2c1.4-.1 2.6.6 3.4 1.7zM19.1 13c0 2.9 1.6 3.9 3.2 3.9 2 0 3.3-1.5 3.3-3.9s-1.3-3.9-3.3-3.9c-2.4 0-3.2 2-3.2 3.9z" fill-rule="evenodd" clip-rule="evenodd" fill="currentColor"></path>
+                            <path d="M36.2 8v5.5c0 1.9-1.3 3.3-3.2 3.3-1.5 0-2.3-.9-2.3-2.8V8h-1.4v6.4c0 2.4 1.3 3.8 3.5 3.8 1.4 0 2.5-.6 3.3-1.6V18h1.4V8h-1.3z" fill="currentColor"></path>
+                            <path d="M40.3 9c1-.8 2.4-1.3 3.9-1.3 2.2 0 3.6 1.1 3.6 3V18h-1.4v-1.1c-.7.9-1.7 1.3-3.1 1.3-2.2 0-3.5-1.2-3.5-3.1 0-2.5 2.3-2.8 3.3-3 .2 0 .3-.1.5-.1 1.4-.2 2.7-.4 2.7-1.6 0-1.3-1.7-1.4-2.2-1.4-.9 0-2.2.3-3.1 1v.2L40.3 9zm.9 6.1c0 1.6 1.5 1.8 2.2 1.8 1.4 0 2.9-.8 2.9-2.9v-1.4c-.7.4-1.7.6-2.5.8l-.4.1c-1.5.1-2.2.5-2.2 1.6z" fill-rule="evenodd" clip-rule="evenodd" fill="currentColor"></path>
+                            <path d="M55.9 8.2c-.3-.2-.9-.4-1.4-.4-1.1 0-2.2.6-2.8 1.5V8h-1.4v10h1.4v-5.4c0-2.4 1.3-3.4 2.7-3.4.4 0 .8.1 1.1.2l.1.1.3-1.3z" fill="currentColor"></path>
+                            <path d="M56.3 13c0-3.1 1.9-5.3 4.7-5.3 2.7 0 4.5 1.9 4.5 4.7V13.2h-7.7c0 2.2 1.4 3.6 3.4 3.6 1.2 0 2.2-.5 2.8-1.3l.1-.1 1 .9-.1.1c-.7.8-1.9 1.8-3.9 1.8-2.9 0-4.8-2.1-4.8-5.2zm4.6-3.9c-1.7 0-2.9 1.1-3.1 2.9H64c-.1-1.4-1-2.9-3.1-2.9z" fill-rule="evenodd" clip-rule="evenodd" fill="currentColor"></path>
+                        </svg>
+                    </button>
+
+                    <button id="settingsAddMerchant" class="button">NEW LOCATION</button>
+                </div>
             </div>
 
             <div id="settingsMerchants"></div>

+ 48 - 0
views/dashboardPage/js/modal.js

@@ -93,6 +93,54 @@ let modal = {
             .finally(()=>{
                 loader.style.display = "none";
             });
+    },
+
+    squareLocations: function(locations){
+        document.getElementById("modalSquareLocations").style.display = "flex";
+        document.getElementById("squareLocationsCancel").onclick = ()=>{controller.closeModal()};
+        let container = document.getElementById("squareLocationsButtons");
+        
+        for(let i = 0; i < locations.length; i++){
+            let button = document.createElement("button");
+            button.innerText = locations[i].name;
+            button.classList.add("button");
+            button.onclick = ()=>{this.addSquareMerchant(locations[i].id)};
+            container.appendChild(button);
+        }
+    },
+
+    addSquareMerchant: function(id){
+        let loader = document.getElementById("loaderContainer");
+        loader.style.display = "none";
+
+        fetch(`/square/add/${id}`)
+            .then(response => response.json())
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    controller.createBanner(response, "error");
+                }else{
+                    window.merchant = new Merchant(
+                        response[1].name,
+                        response[1].pos,
+                        response[1].inventory,
+                        response[1].recipes,
+                        [],
+                        response[0]
+                    );
+
+                    state.updateMerchant();
+                    controller.closeModal();
+                    controller.openStrand("home");
+                    controller.createBanner(`NEW MERCHANT, "${response[1].name}", CREATED`, "success");
+                }
+            })
+            .catch((err)=>{
+                console.log(err);
+                controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
+            })
+            .finally(()=>{
+                loader.style.display = "none";
+            });
     }
 };
 

+ 23 - 0
views/dashboardPage/js/strands/account.js

@@ -41,6 +41,7 @@ let account = {
         };
 
         document.getElementById("changePasswordButton").onclick = ()=>{this.updatePassword()};
+        document.getElementById("newMerchantSquareButton").onclick = ()=>{this.getSquareLocations()};
     },
 
     updateData: function(){
@@ -176,6 +177,28 @@ let account = {
             .finally(()=>{
                 loader.style.display = "none";
             });
+    },
+
+    getSquareLocations: function(){
+        let loader = document.getElementById("loaderContainer");
+        loader.style.display = "flex";
+
+        fetch("/square/locations")
+            .then(response => response.json())
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    controller.createBanner(response, "error");
+                }else{
+                    controller.openModal("squareLocations", response);
+                }
+            })
+            .catch((err)=>{
+                console.log(err);
+                controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
+            })
+            .finally(()=>{
+                loader.style.display = "none";
+            });
     }
 }
 

+ 1 - 3
views/dashboardPage/js/strands/recipeBook.js

@@ -8,9 +8,7 @@ let recipeBook = {
         if(!this.isPopulated){
             this.populateRecipes();
 
-            if(merchant.pos !== "none"){
-                document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate()};
-            }
+            if(merchant.pos === "square") document.getElementById("posUpdateRecipe").onclick = ()=>{this.posUpdate()};
             
             document.getElementById("recipeSearch").oninput = ()=>{this.search()};
 

+ 1 - 18
views/dashboardPage/modal.ejs

@@ -34,24 +34,7 @@
                     <input type="text" id="addMerchantName" required>
                 </label>
 
-                <h3>CREATE WITH:</h3>
-
-                <button id="newMerchantSquareButton" type="button">
-                    <svg aria-hidden="true" viewBox="0 0 44 44" width="24" height="24">
-                        <path fill="currentColor" d="M36.65 0H7.354A7.354 7.354 0 0 0 0 7.354V36.65a7.354 7.354 0 0 0 7.354 7.354H36.65a7.353 7.353 0 0 0 7.354-7.354V7.354A7.352 7.352 0 0 0 36.65 0zm-.646 33.685a2.32 2.32 0 0 1-2.32 2.32H10.325a2.32 2.32 0 0 1-2.321-2.32v-23.36a2.32 2.32 0 0 1 2.321-2.321h23.359a2.32 2.32 0 0 1 2.32 2.321v23.36z"></path>
-                        <path fill="currentColor" d="M17.333 28.003c-.736 0-1.332-.6-1.332-1.339V17.34c0-.739.596-1.339 1.332-1.339h9.338c.738 0 1.332.6 1.332 1.339v9.324c0 .739-.594 1.339-1.332 1.339h-9.338z"></path>
-                    </svg>
-                    <svg aria-hidden="true" viewBox="0 0 66 22" width="75" height="24">
-                        <path d="M11.2 10c-.7-.2-1.3-.4-1.8-.6C8.4 9 8 8.4 8 7.6c0-1.5 1.5-2.2 3-2.2 1.4 0 2.6.6 3.5 1.6l.1.1 1.2-.9-.1-.1c-1.1-1.4-2.7-2.2-4.6-2.2-1.2 0-2.4.3-3.2.9-1 .8-1.5 1.7-1.5 2.9 0 2.7 2.6 3.4 4.6 3.9 2.1.6 3.4 1 3.4 2.6s-1.3 2.6-3.2 2.6c-1 0-2.8-.3-3.9-2l-.1-.1-1.2.9v.1c1.1 1.6 2.9 2.5 5.1 2.5 2.9 0 4.9-1.6 4.9-4.1 0-2.8-2.7-3.5-4.8-4.1z" fill="currentColor"></path>
-                        <path d="M25.5 9.5V8h1.4v14h-1.4v-5.5c-.8 1.1-2 1.7-3.4 1.7-2.7 0-4.5-2.1-4.5-5.2s1.9-5.2 4.5-5.2c1.4-.1 2.6.6 3.4 1.7zM19.1 13c0 2.9 1.6 3.9 3.2 3.9 2 0 3.3-1.5 3.3-3.9s-1.3-3.9-3.3-3.9c-2.4 0-3.2 2-3.2 3.9z" fill-rule="evenodd" clip-rule="evenodd" fill="currentColor"></path>
-                        <path d="M36.2 8v5.5c0 1.9-1.3 3.3-3.2 3.3-1.5 0-2.3-.9-2.3-2.8V8h-1.4v6.4c0 2.4 1.3 3.8 3.5 3.8 1.4 0 2.5-.6 3.3-1.6V18h1.4V8h-1.3z" fill="currentColor"></path>
-                        <path d="M40.3 9c1-.8 2.4-1.3 3.9-1.3 2.2 0 3.6 1.1 3.6 3V18h-1.4v-1.1c-.7.9-1.7 1.3-3.1 1.3-2.2 0-3.5-1.2-3.5-3.1 0-2.5 2.3-2.8 3.3-3 .2 0 .3-.1.5-.1 1.4-.2 2.7-.4 2.7-1.6 0-1.3-1.7-1.4-2.2-1.4-.9 0-2.2.3-3.1 1v.2L40.3 9zm.9 6.1c0 1.6 1.5 1.8 2.2 1.8 1.4 0 2.9-.8 2.9-2.9v-1.4c-.7.4-1.7.6-2.5.8l-.4.1c-1.5.1-2.2.5-2.2 1.6z" fill-rule="evenodd" clip-rule="evenodd" fill="currentColor"></path>
-                        <path d="M55.9 8.2c-.3-.2-.9-.4-1.4-.4-1.1 0-2.2.6-2.8 1.5V8h-1.4v10h1.4v-5.4c0-2.4 1.3-3.4 2.7-3.4.4 0 .8.1 1.1.2l.1.1.3-1.3z" fill="currentColor"></path>
-                        <path d="M56.3 13c0-3.1 1.9-5.3 4.7-5.3 2.7 0 4.5 1.9 4.5 4.7V13.2h-7.7c0 2.2 1.4 3.6 3.4 3.6 1.2 0 2.2-.5 2.8-1.3l.1-.1 1 .9-.1.1c-.7.8-1.9 1.8-3.9 1.8-2.9 0-4.8-2.1-4.8-5.2zm4.6-3.9c-1.7 0-2.9 1.1-3.1 2.9H64c-.1-1.4-1-2.9-3.1-2.9z" fill-rule="evenodd" clip-rule="evenodd" fill="currentColor"></path>
-                    </svg>
-                </button>
-
-                <input type="submit" class="button" value="NO POS">
+                <input type="submit" class="button" value="SUBMIT">
             </form>
 
             <div id="modalConfirm" style="display:none;">