ソースを参照

Creating new merchant now working correctly.

Lee Morgan 5 年 前
コミット
0391896269
2 ファイル変更24 行追加22 行削除
  1. 8 8
      controllers/helper.js
  2. 16 14
      controllers/renderer.js

+ 8 - 8
controllers/helper.js

@@ -3,11 +3,9 @@ const axios = require("axios");
 const Transaction = require("../models/transaction.js");
 
 module.exports = {
-    getSquareData: function(merchant){
-        let now = new Date().toISOString();
-        now = `${now.substring(0, now.length - 1)}+00:00`;
-        let before = new Date(merchant.lastUpdatedTime).toISOString();
-        before = `${before.substring(0, before.length - 1)}+00:00`;
+    getSquareData: function(merchant, time){
+        time.setMilliseconds(time.getMilliseconds() + 1000);
+        let now = new Date();
 
         let ingredients = {};
 
@@ -17,7 +15,7 @@ module.exports = {
                 filter: {
                     date_time_filter: {
                         closed_at: {
-                            start_at: before,
+                            start_at: time,
                             end_at: now
                         }
                     },
@@ -41,7 +39,7 @@ module.exports = {
                 if(response.data.orders){
                     for(let i = 0; i < response.data.orders.length; i++){
                         let transaction = new Transaction({
-                            merchant: merchant,
+                            merchant: merchant._id,
                             date: response.data.orders[i].created_at,
                             posId: response.data.orders[i].id,
                             recipes: []
@@ -70,9 +68,11 @@ module.exports = {
 
                         transactions.push(transaction);
                     }
+
+                    return Transaction.create(transactions);
                 }
 
-                return Transaction.create(transactions);
+                return [];
             })
             .then((transactions)=>{
                 const keys = Object.keys(ingredients);

+ 16 - 14
controllers/renderer.js

@@ -34,9 +34,6 @@ module.exports = {
             return res.redirect(`/verify/email/${res.locals.merchant._id}`);
         }
 
-        // if(res.locals.merchant.square !== undefined){
-        //     await helper.getSquareData(res.locals.merchant);
-        // }
         res.locals.merchant
             .populate("inventory.ingredient")
             .populate("recipes")
@@ -58,16 +55,22 @@ module.exports = {
                 ]);      
             })
             .then(async (transactions)=>{
-                let latest = {};
-                if(transactions.length === 0){
-                    latest = await Transaction.find({merchant: res.locals.merchant._id}).sort({date: -1}).limit(1)[0].date;
-                    latest = new Date(latest);
-                }else{
-                    latest = new Date(transactions[0].date);
-                }
+                if(res.locals.pos !== "none"){
+                    let latest = null;
+                    if(transactions.length === 0){
+                        let latestTransaction = await Transaction.find({merchant: res.locals.merchant._id}).sort({date: -1}).limit(1);
+                        if(latestTransaction.length > 0) latest = new Date(latest[0].date);
+                    }else{
+                        latest = new Date(transactions[0].date);
+                    }
 
-                let newRecipes = await helper.getSquareData(res.locals.merchant, latest);
-                newRecipes.push(transactions);
+                    let newRecipes = {};
+                    if(latest !== null){
+                        newRecipes = await helper.getSquareData(res.locals.merchant, latest);
+                        newRecipes = newRecipes.concat(transactions);                        
+                        transactions = newRecipes;
+                    }
+                }
 
                 res.locals.merchant._id = undefined;
                 res.locals.password = undefined;
@@ -75,10 +78,9 @@ module.exports = {
                 res.locals.square = undefined;
                 res.locals.session = undefined;
 
-                return res.render("dashboardPage/dashboard", {merchant: res.locals.merchant, transactions: newRecipes});
+                return res.render("dashboardPage/dashboard", {merchant: res.locals.merchant, transactions: transactions});
             })
             .catch((err)=>{
-                console.log(err);
                 req.session.error = "ERROR: UNABLE TO RETRIEVE DATA";
                 return res.redirect("/");
             });