diff --git a/app/config/appConfig.js b/app/config/appConfig.js index 7a7887b..d269e6f 100644 --- a/app/config/appConfig.js +++ b/app/config/appConfig.js @@ -14,6 +14,8 @@ const DEFAULT_TIMEZONE = "Europe/Sarajevo"; const CRAWLER_INTERVAL = parseInt(process.env.CRAWLER_INTERVAL) || 60; const STOP_CRAWLER = !!parseInt(process.env.STOP_CRAWLER); +const CHECK_UP_DAYS = parseInt(process.env.CHECK_UP_DAYS) || 10; + const AWS_EMAIL_CONFIG = { REGION: process.env.AWS_REGION || "", CREDENTIALS: { @@ -42,5 +44,6 @@ module.exports = { MAX_REAL_ESTATES_IN_EMAIL, MAX_REAL_ESTATES_IN_FIRST_EMAIL, PRINT_CRAWLER_DEBUG, - API_MAP_KEY + API_MAP_KEY, + CHECK_UP_DAYS }; diff --git a/app/helpers/db/searchRequestMatch.js b/app/helpers/db/searchRequestMatch.js index 9170863..6dfa986 100644 --- a/app/helpers/db/searchRequestMatch.js +++ b/app/helpers/db/searchRequestMatch.js @@ -1,5 +1,6 @@ "use strict"; const db = require("../../models/index"); +const { CHECK_UP_DAYS } = require("../../config/appConfig"); const findRealEstatesForSearchRequest = async searchRequestId => { const query = { @@ -41,8 +42,27 @@ const findNotNotifiedMatches = async () => { return matchingRecords; }; const findAllRequestsForCheckUp = async () => { + //First we find IDs of search request that don't need to be emailed for check up - to EXCLUDE + //The ones that received notification for real estate CHECK_UP_DAYS days from now + const date = new Date(); + const checkUpDate = date.getDate() - CHECK_UP_DAYS; + date.setDate(checkUpDate); + const dateQuery = { + createdAt: { + [Op.gte]: date + } + }; + + const excludedRequests = await db.SearchRequestMatch.findAll({ + attributes: ["searchRequestId"], + where: dateQuery + }); + const query = { - subscribed: true + subscribed: true, + id: { + [Op.notIn]: excludedRequests + } }; const allRequestsForCheckUp = await db.SearchRequest.findAll({ where: query diff --git a/app/npmScripts/npmCheckUpNotify.js b/app/npmScripts/npmCheckUpNotify.js index 1becb3f..b69cbf6 100644 --- a/app/npmScripts/npmCheckUpNotify.js +++ b/app/npmScripts/npmCheckUpNotify.js @@ -1,6 +1,6 @@ "use strict"; -const { checkUpNotifyAll } = require("../services/notificationService"); - +const { checkUpNotify } = require("../services/notificationService"); +//For testing pursposes (async () => { - await checkUpNotifyAll(); + await checkUpNotify(); })(); diff --git a/app/services/notificationService.js b/app/services/notificationService.js index 10a74df..d0f382e 100644 --- a/app/services/notificationService.js +++ b/app/services/notificationService.js @@ -113,7 +113,7 @@ const notifyRequestsWithDailyOption = async () => { await notifyMatches(matches, true); }; -const checkUpNotifyAll = async () => { +const checkUpNotify = async () => { const searchRequestsForCheckUp = await findAllRequestsForCheckUp(); const asyncSendEmailActions = []; @@ -134,5 +134,5 @@ module.exports = { notifyForNewRealEstates, notifyForNewSearchRequest, notifyRequestsWithDailyOption, - checkUpNotifyAll + checkUpNotify }; diff --git a/development.env b/development.env index 89f0a1e..f8f3d52 100644 --- a/development.env +++ b/development.env @@ -11,6 +11,7 @@ APP_BASE_URL=base url for the app MAX_REAL_ESTATES_IN_EMAIL=Max number of real estates that will be shown in email, others will be truncated and URL with full list will be shwon MAX_REAL_ESTATES_IN_FIRST_EMAIL=Max number of real estates that will be shown in first (welcome) email +CHECK_UP_DAYS=Check up email is sent after this number of days without notification #=============== GOOGLE ANALYTICS =============# GA_ID=Google Analytics ID diff --git a/index.js b/index.js index 074099b..abcd14b 100644 --- a/index.js +++ b/index.js @@ -8,10 +8,12 @@ const forceSSL = require("./app/helpers/forceSSL"); const { APP_PORT, CRAWLER_INTERVAL, - STOP_CRAWLER + STOP_CRAWLER, + CHECK_UP_DAYS } = require("./app/config/appConfig"); const routes = require("./app/routes"); const { crawlAll } = require("./app/crawler/crawl"); +const { checkUpNotify } = require("./app/services/notificationService"); const { notifyForNewRealEstates } = require("./app/services/notificationService"); @@ -45,4 +47,7 @@ const crawl = () => { }); } }; + setInterval(crawl, CRAWLER_INTERVAL * 1000); + +setInterval(checkUpNotify, CHECK_UP_DAYS * 24 * 60 * 60 * 1000);