Files
old-web/backend/utils/sendnotification.js

34 lines
1013 B
JavaScript
Raw Normal View History

const scrapTheItems = require("./scraptheitems");
const convertToDate = require("./convertToDate");
const sgMail = require("@sendgrid/mail");
// should be process.env.SENDGRID_API_KEY
sgMail.setApiKey(
"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"
);
async function sendNotification(marketAlert) {
const { id, email, olx_url, last_date } = 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>`,
""
);
const msg = {
to: email,
from: "test@example.com",
subject: "Market Alert",
text: "New items on olx",
html: message
};
if (message) {
await sgMail.send(msg);
return { id, date: String(convertToDate(lastDate)) };
}
}
module.exports = sendNotification;