-
Notifications
You must be signed in to change notification settings - Fork 0
/
promo-function.js
108 lines (106 loc) · 3.47 KB
/
promo-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
const fli = require('fli-webtask');
const express = require('express');
const moment = require('moment');
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`];
var marketTag = req.webtaskContext.secrets[`${market}-tag`];
if(!(market && marketDB && marketTag)) {
const errMsgMarket = 'No market provided.';
responseHandler(errMsgMarket, res);
return next(errMsgMarket);
}
req.market = market;
req.marketDB = marketDB;
req.marketTag = marketTag;
return next();
};
router
.all('/', function (req, res) {
console.log('- promo');
as.waterfall([
(next) => fli.npm.request({
gzip: true,
headers: {
'Accept-Charset': 'utf-8',
'Accept-Encoding': 'gzip',
},
encoding: 'utf8',
url: `${req.webtaskContext.secrets.promoFunction}`
}, (err, response, data) => next(null, err || data)),
(data, next) => {
let promos;
try {
promos = _.get(JSON.parse(data), req.webtaskContext.secrets.promo, []);
} catch (e) {
promos = [];
}
if(!promos.length) {
return next('No promo');
}
return next(null, promos.map((p) => {
let title = _.get(p, 'title', 'prize');
let image = _.get(p, 'priceImageUrl', '');
let expare = moment().add(req.webtaskContext.secrets.expire, 'ms');
let discount = _.get(p, 'promotionPercentOff', '');
let promoText = `${req.webtaskContext.secrets.promoText}: ${title}`;
let promoDescription = '';
if(!!discount) {
promoDescription = `<table><tr><td>${req.webtaskContext.secrets.promoText} and Save ${discount}% off on ${title}<\/td><\/tr><tr><td>Expires ${expare.format('MMM DD, YYYY')}<\/td><\/tr><\/td><\/tr><\/table>`
} else {
promoDescription = `<table><tr><td>${req.webtaskContext.secrets.promoText}: ${title}<\/td><\/tr><tr><td>Expires ${expare.format('MMM DD, YYYY')}<\/td><\/tr><\/td><\/tr><\/table>`
}
return {
promoText: promoText,
promoImg: image,
promoExpired: expare.unix(),
promoDescription: promoDescription,
url: [
req.webtaskContext.secrets.link,
_.get(p, 'id', ''),
`?tag=${req.marketTag}`
].join(''),
info: {
title: title,
image: image,
labels: _.chain(req.webtaskContext.secrets).get('labels', '').split(',').compact().value()
}
};
}));
},
(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, promo) => {
responseHandler(err, res, promo);
});
});
app
.use(bodyParser.json())
.use('/:market', validateMiddleware, router);
module.exports = wt.fromExpress(app);