diff --git a/package.json b/package.json index 5b8547b..9c7daf0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "orvil", - "version": "0.5.1", + "version": "0.5.2", "description": "Messenger chatbot for books search and recommendation", "main": "processMessage.js", "scripts": { diff --git a/sendPromos.js b/sendPromos.js index 6f538a7..d213361 100644 --- a/sendPromos.js +++ b/sendPromos.js @@ -11,18 +11,29 @@ function errorEvent(error) { console.warn("error sending fb messages: ", error.response); } -exports.handler = (event, context, callback) => { - dynamoDB.getSubscribers((error, data) => { - if (error) { - console.warn('There was an error getting subscribers: ', errro); - } else { - lomadee.searchBestSellers() - .then((response) => { - const searchData = response.data; - - // console.log(`Lomadee searched by bestselles`, searchData); - const offers = searchData.offers.map(Offer.formatOffer); +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; @@ -33,10 +44,11 @@ exports.handler = (event, context, callback) => { console.log('Recommendations sent to :', user); }); - }) - .catch(err => console.warn('Was not possible get bestsellers: ', err)); - } + } - callback(error, data); - }); + callback(error, data); + }); + }) + .catch(err => console.warn('Was not possible get bestsellers: ', err) + ); }; \ No newline at end of file