Selaa lähdekoodia

Getting square transactions when signing in now will get as many as there are and not just 500.
Update middleware to handle statuses.
Update middleware to refresh access token when needed.

Lee Morgan 5 vuotta sitten
vanhempi
sitoutus
e75972f82e
2 muutettua tiedostoa jossa 39 lisäystä ja 34 poistoa
  1. 3 27
      controllers/helper.js
  2. 36 7
      controllers/renderer.js

+ 3 - 27
controllers/helper.js

@@ -3,35 +3,11 @@ const axios = require("axios");
 const Transaction = require("../models/transaction.js");
 
 module.exports = {
-    getSquareData: function(merchant, time){
-        time.setMilliseconds(time.getMilliseconds() + 1000);
-        let now = new Date();
-
+    getSquareData: function(merchant, data){
         let ingredients = {};
 
-        return axios.post(`${process.env.SQUARE_ADDRESS}/v2/orders/search`, {
-            location_ids: [merchant.square.location],
-            query: {
-                filter: {
-                    date_time_filter: {
-                        closed_at: {
-                            start_at: time,
-                            end_at: now
-                        }
-                    },
-                    state_filter: {
-                        states: ["COMPLETED"]
-                    }
-                },
-                sort: {
-                    sort_field: "CLOSED_AT",
-                    sort_order: "DESC"
-                }
-            }
-        }, {
-            headers: {
-                Authorization: `Bearer ${merchant.square.accessToken}`
-            }
+        return axios.post(`${process.env.SQUARE_ADDRESS}/v2/orders/search`, data, {
+            headers: {Authorization: `Bearer ${merchant.square.accessToken}`}
         })
             .then((response)=>{
                 let transactions = [];

+ 36 - 7
controllers/renderer.js

@@ -65,13 +65,41 @@ module.exports = {
                     }
 
                     if(latest !== null){
-                        let newOrders = await helper.getSquareData(res.locals.merchant, latest);
-                        for(let i = 0; i < newOrders.length; i++){
-                           for(let j = 0; j < newOrders[i].recipes.length; j++){
-                               newOrders[i].recipes[j].recipe = newOrders[i].recipes[j].recipe._id;
-                           }
-                        }
-                        transactions = newOrders.concat(transactions);
+                        latest.setMilliseconds(latest.getMilliseconds() + 1000);
+                        let now = new Date();
+
+                        let postData = {
+                            location_ids: [res.locals.merchant.square.location],
+                            query: {
+                                filter: {
+                                    date_time_filter: {
+                                        closed_at: {
+                                            start_at: latest,
+                                            end_at: now
+                                        }
+                                    },
+                                    state_filter: {
+                                        states: ["COMPLETED"]
+                                    }
+                                },
+                                sort: {
+                                    sort_field: "CLOSED_AT",
+                                    sort_order: "DESC"
+                                }
+                            },
+                            limit: 10000
+                        };
+
+                        do{
+                            let newOrders = await helper.getSquareData(res.locals.merchant, postData);
+                            postData.cursor = newOrders.cursor;
+                            for(let i = 0; i < newOrders.length; i++){
+                                for(let j = 0; j < newOrders[i].recipes.length; j++){
+                                    newOrders[i].recipes[j].recipe = newOrders[i].recipes[j].recipe._id;
+                                }
+                            }
+                            transactions = newOrders.concat(transactions);
+                        }while(postData.cursor !== undefined);
                     }
                 }
 
@@ -84,6 +112,7 @@ module.exports = {
                 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("/");
             });