This repository was archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
123 lines (102 loc) · 3.88 KB
/
index.js
File metadata and controls
123 lines (102 loc) · 3.88 KB
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
'use strict';
/* * * * * */
/* CHEF POINT - DAEMON SYNC */
/* * */
/* * */
/* IMPORTS */
const config = require('config');
const database = require('./services/database');
const logger = require('./services/logger');
const syncAPI = require('./services/syncAPI');
const Store = require('./models/Store');
/* * */
/* At program initiation all stores are retrieved from the database */
/* and, for each store, orders are retrieved from Square API. */
/* Each order is formated into a Transaction and saved to the database. */
(async () => {
// Store start time for logging purposes
const startTime = process.hrtime();
logger('****************************************');
logger(new Date().toISOString());
logger('****************************************');
logger();
logger('Starting...');
await database.connect();
// Get all store locations from the database
const stores = await Store.find({});
// For each store, sync it's transactions
for (const store of stores) {
logger();
logger('----------------------------------------');
logger('Syncing [' + store.name + ']...');
await syncStoreTransactions(store);
logger('----------------------------------------');
}
logger();
logger('- - - - - - - - - - - - - - - - - - - -');
logger('Shutting down...');
await database.disconnect();
logger('Operation took ' + getDuration(startTime) / 1000 + ' seconds.');
logger('- - - - - - - - - - - - - - - - - - - -');
logger();
})();
/* * */
/* The caller provides the store object containing squareLocationID and lastSyncTime. */
/* Two operations are performed in this function: */
/* First, orders are retrieved from Square, formated into transactions */
/* and saved to the database. */
/* Second, for the most recent transaction, it's closed_at date value */
/* is saved as the lastSyncTime value for the store. */
/* This is what keeps track of which transactions were synced and which were not. */
const syncStoreTransactions = async (store) => {
// First, get orders from Square
const orders = await syncAPI.getOrdersFromSquare(store.squareLocationID, store.lastSyncTime);
// If response is empty, return no new orders to sync
if (!orders) return logger('No new orders to sync.');
else logger('Syncing ' + orders.length + ' orders...');
// If response is not empty:
// For each order,
for (const [index, order] of orders.entries()) {
// Clear console output
process.stdout.write(' \r');
// Check the validity of the order
// 1) If a sale has no items:
if (typeof order.line_items == 'undefined') {
logger('Invalid order.');
logger('Order has no items.');
logger('Order ID: ' + order.id);
continue;
}
if (config.get('general.verbose-logging')) {
process.stdout.write('Syncing order ' + index + ' of ' + orders.length);
process.stdout.write(' [' + order.id + ']\r');
}
// Format and save it to the database
await syncAPI.formatOrderIntoTransaction(order, store);
// Check if it's date is more recent than this store's last sync time,
// and update store location with the latest time orders were synced.
await store
.set({
lastSyncTime: syncAPI.compareSyncDates(order.closed_at, store.lastSyncTime),
})
.save();
}
// Clean verbose loggin artifacts
if (config.get('general.verbose-logging')) {
process.stdout.write(' \n');
}
// Log successful operation.
logger('Done. ' + orders.length + ' orders synced.');
logger('Last transaction was at ' + store.lastSyncTime);
};
/* * */
/* Returns a time interval for a provided start time. */
const getDuration = (startTime) => {
const interval = process.hrtime(startTime);
return parseInt(
// seconds -> miliseconds +
interval[0] * 1000 +
// + nanoseconds -> miliseconds
interval[1] / 1000000
);
};