forked from cnative100/eddie
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadsquare.js
126 lines (102 loc) · 2.83 KB
/
readsquare.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import dotenv from 'dotenv';
import { Client, Environment } from 'square'
/**
* If a token has not already been assigned as an env var (via CLI, etc),
* dotenv will look for a .env file, which can be added to root and will be
* ignored by git.
* @see https://nodejs.dev/learn/how-to-read-environment-variables-from-nodejs
*/
if (!process.env.SQUARE_ACCESS_TOKEN) {
dotenv.config();
}
const client = new Client({
environment: Environment.Production,
accessToken: process.env.SQUARE_ACCESS_TOKEN,
})
const ordersApi = client.ordersApi;
var map = {};
var totalPurchaseValue = 0.0;
async function searchOrders () {
var resultsArray = new Array();
var cursorPresent = '';
do{
try {
const response = await client.ordersApi.searchOrders({
locationIds: [
'AB4HBYP24XMZW'
],
query: {
filter: {
stateFilter: {
states: [
'OPEN'
]
},
dateTimeFilter: {
createdAt: {
startAt: '2021-10-24T00:00:00+00:00'
}
}
}
},
limit: 500,
returnEntries: false,
cursor: cursorPresent
});
if(response.result.cursor){
cursorPresent = response.result.cursor;
}
else{
cursorPresent = '';
}
resultsArray.push(response.result);
} catch(error) {
console.log(error);
}
} while (cursorPresent != '');
return resultsArray;
};
function addLineItemsToMap(order){
try {
const lineItems = order.lineItems;
lineItems.forEach(function(lineItem) {
if(!lineItem.name.toUpperCase().includes("MUTUAL") &&
!lineItem.name.toUpperCase().includes("WHOLESALE") &&
!lineItem.name.toUpperCase().includes("CASE") &&
!lineItem.name.toUpperCase().includes("PER LB") &&
!lineItem.name.toUpperCase().includes("PRIMAL") &&
!lineItem.name.toUpperCase().includes("DELIVERY") &&
!lineItem.name.toUpperCase().includes("HALF HOG")){
// Add purchase value to the total
totalPurchaseValue += (parseFloat(lineItem.totalMoney.amount) / 100.00);
if(lineItem.name in map){
map[lineItem.name] = map[lineItem.name]+parseInt(lineItem.quantity);
}
else{
map[lineItem.name] = parseInt(lineItem.quantity);
}
}
});
} catch (error) {
console.log(error);
}
}
export default async function readSquare() {
const ordersResultsArray = await searchOrders();
for(let h = 0; h < ordersResultsArray.length; h++){
var ordersResult = ordersResultsArray[h];
if(ordersResult.orders){
for (let i = 0; i < ordersResult.orders.length; i++){
var order = ordersResult.orders[i];
addLineItemsToMap(order);
}
}
}
return sortObjectByKeys(map);
}
function sortObjectByKeys(o) {
return Object.keys(o).sort().reduce((r, k) => (r[k] = o[k], r), {});
}
export function readSquareTotal() {
return totalPurchaseValue;
}