Răsfoiți Sursa

Add saving of newly created merchant to frontend.
Then calls function to update the frontend, function does not yet exist.

Lee Morgan 5 ani în urmă
părinte
comite
2174a946f6

+ 1 - 0
controllers/helper.js

@@ -21,6 +21,7 @@ module.exports = {
                             recipes: []
                         });
 
+                        if(response.data.orders[i].line_items === undefined) continue;
                         for(let j = 0; j < response.data.orders[i].line_items.length; j++){
                             for(let k = 0; k < merchant.recipes.length; k++){
                                 if(response.data.orders[i].line_items[j].catalog_object_id === merchant.recipes[k].posId){

+ 12 - 4
controllers/merchantData.js

@@ -72,7 +72,6 @@ module.exports = {
                 return res.redirect(`/verify/email/${response[0]._id}`);
             })
             .catch((err)=>{
-                console.log(err);
                 if(typeof(err) === "string"){
                     req.session.error = err;
                 }else if(err.name === "ValidationError"){
@@ -90,6 +89,7 @@ module.exports = {
     req.body = {
         name: String
     }
+    response = [Owner, Merchant]
     */
     addMerchantNone: function(req, res){
         let merchant = new Merchant({
@@ -101,11 +101,19 @@ module.exports = {
             recipes: []
         });
 
-        res.locals.owner.push(merchant._id);
+        res.locals.owner.merchants.push(merchant._id);
 
-        Promise.all([owner.save(), merchant.save()])
+        let populate = res.locals.owner.populate("merchants", "name").execPopulate();
+
+        Promise.all([res.locals.owner.save(), merchant.save(), populate])
             .then((response)=>{
-                return res.json(response);
+                let owner = {
+                    _id: response[0]._id,
+                    email: response[0].email,
+                    merchants: response[0].merchants
+                }
+                
+                return res.json([owner, response[1]]);
             })
             .catch((err)=>{
                 if(err.name === "ValidationError"){

+ 5 - 2
controllers/renderer.js

@@ -54,7 +54,7 @@ module.exports = {
                     }}
                 ]);
                 
-                let merchants = res.locals.owner.populate("merchants", "name");
+                let merchants = res.locals.owner.populate("merchants", "name").execPopulate();
 
                 return Promise.all([transactions, merchants]);
             })
@@ -119,7 +119,10 @@ module.exports = {
                 res.locals.owner.session = undefined;
 
                 for(let i = 0; i < res.locals.owner.merchants.length; i++){
-                    if(res.locals.owner.merchants[i]._id.toString() === res.locals.merchant._id.toString()) res.locals.owner.merchants.splice(i, 1);
+                    if(res.locals.owner.merchants[i]._id.toString() === res.locals.merchant._id.toString()){
+                        res.locals.owner.merchants.splice(i, 1);
+                        break;
+                    } 
                 }
 
                 return res.render("dashboardPage/dashboard", {owner: res.locals.owner, merchant: res.locals.merchant, transactions: transactions});

+ 17 - 2
views/dashboardPage/js/modal.js

@@ -1,4 +1,5 @@
-const merchant = require("../../../models/merchant");
+const merchant = require("../../../models/merchant.js");
+const Merchant = require("./classes/Merchant.js");
 
 let modal = {
     feedback: function(){
@@ -65,11 +66,25 @@ let modal = {
             },
             body: JSON.stringify(data)
         })
+            .then(response => response.json())
             .then((response)=>{
                 if(typeof(response) === "string"){
                     controller.createBanner(response, "error");
                 }else{
-                    console.log(response);
+                    let newMerchant = new Merchant(
+                        response[1].name,
+                        response[0].email,
+                        response[1].pos,
+                        response[1].inventory,
+                        response[1].recipes,
+                        [],
+                        response[0]
+                    );
+
+                    window.merchant = newMerchant;
+
+                    state.updateMerchant();
+                    controller.openStrand("home");
                 }
             })
             .catch((err)=>{