Files
old-web/app/lib/sendNotification.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-05-16 19:58:48 +02:00
const scrapTheItems = require("./scrapTheItems");
const convertToDate = require("./convertToDate");
2019-09-05 11:14:54 +02:00
const AWS = require("aws-sdk");
// AWS.config.update({region: 'eu-central-1'});
2019-03-20 05:59:23 +01:00
async function sendNotification(marketAlert) {
2019-05-16 19:58:48 +02:00
const { id, email, olx_url } = marketAlert;
let url =
"https://www.olx.ba/pretraga?" + olx_url + "&sort_order=desc&sort_po=datum";
let newItems = await scrapTheItems(url);
let lastDate = newItems.length && newItems[0].date;
let message =
newItems.length &&
newItems.reduce(
(mes, item) => mes + `<strong>${item.url} i ${item.price}</strong>`,
""
);
2019-03-20 05:59:23 +01:00
2019-05-16 19:58:48 +02:00
// Create sendEmail params
const params = {
2019-09-05 11:14:54 +02:00
Destination: {
/* required */
CcAddresses: [],
ToAddresses: [email]
2019-03-20 05:59:23 +01:00
},
2019-09-05 11:14:54 +02:00
Message: {
/* required */
Body: {
/* required */
2019-03-20 05:59:23 +01:00
Html: {
2019-09-05 11:14:54 +02:00
Charset: "UTF-8",
Data: message
2019-03-20 05:59:23 +01:00
},
Text: {
2019-09-05 11:14:54 +02:00
Charset: "UTF-8",
Data: message // TODO: convert to text
2019-03-20 05:59:23 +01:00
}
2019-09-05 11:14:54 +02:00
},
Subject: {
Charset: "UTF-8",
Data: "Javimi alert"
}
2019-03-20 05:59:23 +01:00
},
Source: 'Kivi.ba <info@saburly.com>' /* required */,
2019-09-05 11:14:54 +02:00
ReplyToAddresses: ["info@saburly.com"]
};
2019-03-20 05:59:23 +01:00
if (message) {
2019-09-05 11:14:54 +02:00
const sendPromise = new AWS.SES({ apiVersion: "2010-12-01" })
.sendEmail(params)
.promise();
2019-05-16 19:58:48 +02:00
await sendPromise;
return { id, date: String(convertToDate(lastDate)) };
}
}
2019-03-20 05:59:23 +01:00
module.exports = sendNotification;