-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendPromos.js
54 lines (43 loc) · 1.49 KB
/
sendPromos.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
console.log('Loading function');
const dynamoDB = require('./lib/DynamoDBHandler');
const lomadee = require('./lib/LomadeeHandler');
const MessageSender = require('./lib/MessageSender');
const Message = require('./model/Message');
const Offer = require('./model/Offer');
function errorEvent(error) {
console.warn("error sending fb messages: ", error.response);
}
function randomSort() {
return 0.5 - Math.random();
}
exports.handler = (event, context, callback) => {
const promoTotalSize = 100;
const promoToSendSize = 5;
lomadee.searchBestSellers(0, promoTotalSize)
.then((response) => {
const searchData = response.data;
// console.log(`Lomadee searched by bestsellers`, searchData);
const offers = searchData.offers
.sort(randomSort)
.slice(0, promoToSendSize)
.map(Offer.formatOffer)
;
dynamoDB.getSubscribers((error, data) => {
if (error) {
console.warn('There was an error getting subscribers: ', error);
} else {
data.Items.forEach((item) => {
const user = item.attrs;
MessageSender.sendTextMessage(user.fbId, Message.RECOMMEND)
.catch(errorEvent);
MessageSender.sendOfferTemplateMessage(user.fbId, offers)
.catch(errorEvent);
console.log('Recommendations sent to :', user);
});
}
callback(error, data);
});
})
.catch(err => console.warn('Was not possible get bestsellers: ', err)
);
};