Skip to content

Commit

Permalink
Merge pull request #10 from WBerredo/shuffle-promos
Browse files Browse the repository at this point in the history
Shuffle promos
  • Loading branch information
WBerredo authored Dec 13, 2017
2 parents ec20fd1 + a596c46 commit 64c30c4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
44 changes: 28 additions & 16 deletions sendPromos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
);
};

0 comments on commit 64c30c4

Please sign in to comment.