diff --git a/app/helpers/db/searchRequestMatch.js b/app/helpers/db/searchRequestMatch.js index 11d5cde..9170863 100644 --- a/app/helpers/db/searchRequestMatch.js +++ b/app/helpers/db/searchRequestMatch.js @@ -40,6 +40,16 @@ const findNotNotifiedMatches = async () => { return matchingRecords; }; +const findAllRequestsForCheckUp = async () => { + const query = { + subscribed: true + }; + const allRequestsForCheckUp = await db.SearchRequest.findAll({ + where: query + }); + + return allRequestsForCheckUp; +}; const addMatches = async matchingRecords => { return await db.SearchRequestMatch.bulkCreate(matchingRecords, { @@ -50,5 +60,6 @@ const addMatches = async matchingRecords => { module.exports = { findRealEstatesForSearchRequest, addMatches, - findNotNotifiedMatches + findNotNotifiedMatches, + findAllRequestsForCheckUp }; diff --git a/app/helpers/emailContentGenerator.js b/app/helpers/emailContentGenerator.js index 861bfb2..ee30ee6 100644 --- a/app/helpers/emailContentGenerator.js +++ b/app/helpers/emailContentGenerator.js @@ -120,8 +120,47 @@ const generateEmailSubject = (numberOfRealEstates, singleRealEstateTitle) => { return `Kivi: ${numberOfRealEstates} novih nekretnina`; }; +const generateCheckUpEmail = searchRequest => { + const realEstateType = AD_CATEGORY[searchRequest.realEstateType]; + const { + id, + gardenSizeMin, + gardenSizeMax, + sizeMin, + sizeMax, + priceMin, + priceMax + } = searchRequest; + + const gardenSize = realEstateType.hasGardenSize + ? `
Kvadratura okućnice: Od ${gardenSizeMin} do ${gardenSizeMax} m2
` + : ``; + + const allRealEstatesLink = `${APP_URL}/nekretnine/${id}`; + const moreRealEstates = `
Kompletan spisak do sada pronađenih nekretnina možete pogledati na listi nekretnina
`; + + const emailFooter = generateEmailFooter(id); + + return `

Zdravo

+
Kivi tim traži nekretnine za Vas i kada to ne vidite.
+
+
Vaša trenutno aktivna pretraga je:
+
+
+
Tip nekretnine: ${realEstateType.title}
+
Kvadratura nekretnine: Od ${sizeMin} do ${sizeMax} m2
+ ${gardenSize} +
Cijena: ${priceMin} do ${priceMax} KM
+
+
+ ${moreRealEstates} +
+ ${emailFooter}`; +}; + module.exports = { generateNotificationEmail, generateNewSearchRequestEmail, - generateEmailSubject + generateEmailSubject, + generateCheckUpEmail }; diff --git a/app/npmScripts/npmCheckUpNotify.js b/app/npmScripts/npmCheckUpNotify.js new file mode 100644 index 0000000..1becb3f --- /dev/null +++ b/app/npmScripts/npmCheckUpNotify.js @@ -0,0 +1,6 @@ +"use strict"; +const { checkUpNotifyAll } = require("../services/notificationService"); + +(async () => { + await checkUpNotifyAll(); +})(); diff --git a/app/services/notificationService.js b/app/services/notificationService.js index 7495e17..10a74df 100644 --- a/app/services/notificationService.js +++ b/app/services/notificationService.js @@ -6,9 +6,13 @@ const { const { generateNotificationEmail, generateNewSearchRequestEmail, - generateEmailSubject + generateEmailSubject, + generateCheckUpEmail } = require("../helpers/emailContentGenerator"); -const { findNotNotifiedMatches } = require("../helpers/db/searchRequestMatch"); +const { + findNotNotifiedMatches, + findAllRequestsForCheckUp +} = require("../helpers/db/searchRequestMatch"); const { sendEmail } = require("../services/emailService"); const notifyForNewRealEstates = async newRealEstates => { @@ -109,8 +113,26 @@ const notifyRequestsWithDailyOption = async () => { await notifyMatches(matches, true); }; +const checkUpNotifyAll = async () => { + const searchRequestsForCheckUp = await findAllRequestsForCheckUp(); + + const asyncSendEmailActions = []; + + for (const searchRequest of searchRequestsForCheckUp) { + const { email } = searchRequest.dataValues; + const emailSubject = `Kivi: Mi tražimo nekretnine za vas!`; + const emailContent = generateCheckUpEmail(searchRequest.dataValues); + + const sendEmailPromise = sendEmail(email, emailSubject, emailContent); + asyncSendEmailActions.push(sendEmailPromise); + sendEmailPromise.catch(err => console.log("[Email Sending Failed]", err)); + } + await Promise.all(asyncSendEmailActions); +}; + module.exports = { notifyForNewRealEstates, notifyForNewSearchRequest, - notifyRequestsWithDailyOption + notifyRequestsWithDailyOption, + checkUpNotifyAll }; diff --git a/package.json b/package.json index 75a7cc4..b4ce4a8 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "docker-stop": "docker stop pg_marketalerts", "crawl": "cd app/crawler && node npmCrawl.js", "daily-notify": "cd app/npmScripts && node npmDailyNotify.js", + "checkup-notify": "cd app/npmScripts && node npmCheckUpNotify.js", "test-search": "cd test && node searchTest.js", "test-olx-scraper": "cd test && node olxScrapeTest.js", "test-rental-scraper": "cd test && node rentalScrapeTest.js"