-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathznyzhka-function.js
55 lines (54 loc) · 1.47 KB
/
znyzhka-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
const axios = require('axios');
const _ = require('lodash');
const fli = require('fli-webtask');
const loader = fli.lib.loader;
/**
* @param context {WebtaskContext}
*/
module.exports = async function(context, cb) {
try {
if(context.secrets.token !== context.query.token) {
throw 'No token.';
}
const page = context.query.page || 1;
const response = await axios.get(`${context.secrets.catalog}${page}`);
let deals = _.get(response, 'data.data.data', []).map((offer) => {
return {
promoText: offer.title,
promoImg: offer.image,
promoType: 'Знижка',
promoStart: offer.period_start,
promoExpired: offer.period_end,
promoDescription: offer.description,
slug: offer.slug,
url: _.trim(`${context.secrets.link}${offer.slug}`),
notification: 'minified',
info: {
labels: ['Знижка', 'Акція']
}
};
});
if(context.query.max) {
deals = deals.slice(0, context.query.max);
}
if(context.query.publish) {
deals.map((deal) => {
if(deal && deal.url && deal.promoText) {
loader({
method: 'post',
url: context.secrets.storeFunction,
qs: {
token: context.secrets.token
},
json: deal
}, () => {});
}
return deal;
});
}
cb(null, deals);
}
catch(e) {
cb(null, _.toString(e));
}
};