-
Notifications
You must be signed in to change notification settings - Fork 0
/
deals-function.js
116 lines (114 loc) · 3.29 KB
/
deals-function.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
const fli = require('fli-webtask');
const express = require('express');
const wt = require('webtask-tools');
const bodyParser = require('body-parser');
const request = fli.npm.request;
const as = fli.npm.async;
const _ = fli.npm.lodash;
const loader = fli.lib.loader;
const responseHandler = fli.lib.responseHandler;
const app = express();
const router = express.Router();
const validateMiddleware = (req, res, next) => {
if(req.webtaskContext.secrets.token !== req.query.token) {
const errMsgToken = 'No token.';
responseHandler(errMsgToken, res);
return next(errMsgToken);
}
var market = _.get(req, 'params.market', '').toUpperCase();
var marketDB = req.webtaskContext.secrets[`${market}-db`];
if(!(market && marketDB)) {
const errMsgMarket = 'No market provided.';
responseHandler(errMsgMarket, res);
return next(errMsgMarket);
}
req.market = market;
req.marketDB = marketDB;
return next();
};
router
.all('/clean', function (req, res) {
console.log('- cleanup deals');
var now = new Date();
var tenDaysAgo = (new Date(now.getFullYear(), now.getMonth(), now.getDate() - 10)).getTime();
loader({
method: 'post',
url: `${req.marketDB}/clean`,
qs: {
token: req.webtaskContext.secrets.token
},
json: {
"timestamp" : {"$lte": tenDaysAgo}
}
}, (err, result) => {
responseHandler(err, res, result);
});
})
.all('/today', function (req, res) {
console.log('- get today deals');
var now = new Date();
var startOfDay = (new Date(now.getFullYear(), now.getMonth(), now.getDate())).getTime();
loader({
method: 'post',
url: `${req.marketDB}/find`,
qs: {
token: req.webtaskContext.secrets.token
},
json: req.query.active ? {
"payload.promoExpired": {"$gte": `${new Date(startOfDay).toISOString().split('T')[0]} 00:00:00`}
} : {
"timestamp" : {"$gte": startOfDay}
}
}, (err, deals) => {
responseHandler(err, res, deals);
});
})
.all('/', function (req, res) {
console.log('- deals');
as.waterfall([
(next) => loader({
url: `${req.webtaskContext.secrets.goldboxFunction}/${req.market}`,
qs: {
token: req.webtaskContext.secrets.token,
max: req.query.max
}
}, (err, data) => next(null, err || data)),
(data, next) => {
const deals = _.get(data, 'deals', []);
if(!deals.length && req.query.alarm) {
// Trigger alarm
return loader({
method: 'post',
url: `${req.webtaskContext.secrets.alarmFunction}/${req.market}/trigger`,
qs: {
token: req.webtaskContext.secrets.token
},
json: {
msg: `Alarm: No new deals for: Market - ${req.market}. DB - ${req.marketDB}. Fix me.`
}
}, () => next('No deals'));
}
return next(null, deals);
},
(deals, next) => as.mapSeries(deals,
(deal, next) => {
if(deal && deal.url && deal.promoText) {
loader({
method: 'post',
url: req.marketDB,
qs: {
token: req.webtaskContext.secrets.token
},
json: deal
}, () => {});
}
return next(null, deal);
}, next)
], (err, goldbox) => {
responseHandler(err, res, goldbox);
});
});
app
.use(bodyParser.json())
.use('/:market', validateMiddleware, router);
module.exports = wt.fromExpress(app);