-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
95 lines (86 loc) · 3.22 KB
/
index.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
/*
Requiring necessary helpers.
*/
var express = require('express');
var request = require('request');
var fs = require('fs');
var app = express();
var Appbase = require('appbase-js');
var helper = require("./helper.js");
/*
Including appbase credentials stored in json file.
*/
var appbaseCredentials = require('./appbase_credentials.json');
var sendgrid_api_key = "ENTER_YOUR_SENDGRID_API_KEY_HERE"
/*
Appbase Credentials. Just make new account at appbase and configure it according to your account.
Creating object of appbase, passing appbaseCredentials.
*/
var appbase = new Appbase(appbaseCredentials);
/* This is to access any file withn folder, no routing required for these files. */
app.use('/', express.static(__dirname + '/'));
/* This route is for returning product details of particular product. */
app.get('/product', function(req, res) {
helper.getProductDetails(req.param('productId'), function(data) {
var details = {
'productId': req.param('productId'),
'price': data.productBaseInfoV1.flipkartSpecialPrice.amount,
'name': data.productBaseInfoV1.title,
'imageurls': data.productBaseInfoV1.imageUrls
}
res.send(details);
});
});
/* Price alert routing. The Client side makes the ajax call to this route with
params [productId,email,price]. This route has 2 tasks, first one is to start
polling of this product in order to save the updated price of product into appbase
database and another is start the search for the condition mentioned by the user
and send the mail as soon as the condition is matched.
*/
app.get('/alert', function(req, res) {
/* Starting polling for the requested product */
var mailBody = "You have set the price alert for flipkart product {{{name}}}. Your condition has been matched and Price has reached to {{{price}}}";
var requestObject = {
type: appbaseCredentials.type,
body: {
"query": {
"filtered": {
"query": {
"match": { "productId": req.param('productId') }
},
"filter": {
"range": {
"price": {
"lt": req.param('lte'),
"gte": req.param('gte')
}
}
}
}
}
}
}
var webhookObject = {
'method': 'POST',
'url': 'https://api.sendgrid.com/api/mail.send.json',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + sendgrid_api_key
},
"count": 1,
'string_body': 'to=' + req.param('email') + '&subject=Your Flipkart product price Alert&text=' + mailBody + '&from=yash@appbase.io'
}
/* Starting stream search for the user condition */
appbase.searchStreamToURL(requestObject, webhookObject).on('data', function(response) {
console.log("Webhook has been configured : ", response);
}).on('error', function(error) {
console.log("searchStreamToURL() failed with: ", error)
})
helper.indexProduct(req.param('productId'));
});
/* It will start the server. */
var server = app.listen(process.env.PORT || 8081, '0.0.0.0', function() {
var host = server.address().address;
var port = server.address().port;
console.log('Flipkart extension back-end app listening at http://%s:%s', host, port);
});