forked from endocrimes/MondoSquared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoursquare.js
146 lines (112 loc) · 4.32 KB
/
foursquare.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const request = require('request');
var foursquareUserToken = process.env["FOURSQUARE_USER_TOKEN"];
var foursquareConfig = {
'secrets' : {
'clientId' : process.env["FOURSQUARE_CLIENT_ID"],
'clientSecret' : process.env["FOURSQUARE_CLIENT_SECRET"],
'redirectUrl' : process.env["FOURSQUARE_REDIRECT_URI"]
}
};
var foursquareApi = require('node-foursquare')(foursquareConfig);
var response = {
message: "(none)"
};
exports.postToSlack = function(response, callback) {
if (!response.report) {
callback(response);
return;
}
const url = process.env["SLACK_WEBHOOK_URL"];
if (!url) {
callback(response)
return
}
request.post(
{
headers : { 'Content-type' : 'application/json' },
url,
form : {payload: JSON.stringify({text: response.merchant+': '+response.message})}
},
(error, res, body) => callback(response)
);
};
exports.checkTransaction = function(body, callback) {
var type = body.type;
if (type != "transaction.created") {
callback({"message":"Unsupported event type:" + type, report: false});
return;
}
var tx = body.data;
var myAccountId = process.env["MONZO_ACCOUNT_ID"];
if (!myAccountId || (tx.account_id !== myAccountId)) {
callback({"message": "Monzo Account ID does not match", report: true});
return;
}
var merchant = tx.merchant;
if (merchant.online) {
callback({"message":"Unsupported transaction: online", report: false});
return;
}
response.merchant = merchant.name
// First we need a foursquare id
if (merchant.metadata.foursquare_id) {
console.log('getDetails for ' + merchant.metadata.foursquare_id);
foursquareApi.Venues.getDetails(merchant.metadata.foursquare_id, {}, foursquareUserToken, function(error, fsqVenue) {
console.log("Transaction detected at: " + merchant.name);
/*
// Foursquare response contains this block:
beenHere: {
count: 20,
unconfirmedCount: 0,
marked: true,
lastVisitedAt: 1476107751,
lastCheckinExpiredAt: 1476118551
}
*/
var beenHere = fsqVenue.venue.beenHere;
// check been here before BUT not today / lastCheckinExpired
if (beenHere.count > 0) {
today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
// check the tx time is today
var txTime = new Date(Date.parse(tx.created));
if (txTime.getTime() >= today.getTime()) {
var lastVisit = new Date(beenHere.lastVisitedAt * 1000);
if (lastVisit.getTime() < today.getTime()) {
// attempt checkin
foursquareApi.Checkins.add(merchant.metadata.foursquare_id, {}, foursquareUserToken, function(error, checkinResponse) {
if (error) {
response.message = "Error posting to Swarm";
response.error = error;
response.report = true;
callback(response);
} else {
response.message = "Checked-in 👍";
response.report = true;
callback(response);
}
});
} else {
response.message = 'Already checked in here today, abort!';
response.report = false;
callback(response);
}
} else {
response.message = 'Transaction was not for today, abort!';
response.report = false;
callback(response);
}
} else {
response.message = 'No previous checkins here, abort!';
response.report = false;
callback(response);
}
});
} else {
response.message = 'No foursquare ID available, abort!';
response.report = false;
callback(response);
}
};