From 06f80296f371729265a3e9981d797e47e1a8c0d8 Mon Sep 17 00:00:00 2001 From: Naida Vatric Date: Sat, 15 Feb 2020 02:30:02 +0100 Subject: [PATCH 1/3] Changed checkup email logic. --- app/helpers/db/searchRequest.js | 21 +++++++++- app/helpers/db/searchRequestMatch.js | 40 +------------------ ...6-add-column-notified-to-searchrequests.js | 14 +++++++ app/models/searchRequest.js | 6 ++- app/services/notificationService.js | 17 ++++++-- index.js | 4 +- 6 files changed, 56 insertions(+), 46 deletions(-) create mode 100644 app/migrations/20200215002846-add-column-notified-to-searchrequests.js diff --git a/app/helpers/db/searchRequest.js b/app/helpers/db/searchRequest.js index e2633b4..b40bda5 100644 --- a/app/helpers/db/searchRequest.js +++ b/app/helpers/db/searchRequest.js @@ -3,6 +3,7 @@ const db = require("../../models/index"); const sequelize = require("sequelize"); const Op = sequelize.Op; const { AD_CATEGORY } = require("../../common/enums"); +const { CHECK_UP_DAYS } = require("../../config/appConfig"); const getSearchRequest = async searchRequestId => { try { @@ -16,6 +17,23 @@ const getSearchRequest = async searchRequestId => { const createSearchRequest = async (searchRequestFields = {}) => { return await db.SearchRequest.create(searchRequestFields); }; +const findAllRequestsForCheckUp = async () => { + const date = new Date(); + const checkUpDate = date.getDate() - CHECK_UP_DAYS; + date.setDate(checkUpDate); + + const dateQuery = { + notifiedAt: { + [Op.lte]: date + } + }; + + const allRequestsForCheckUp = await db.SearchRequest.findAll({ + where: dateQuery + }); + + return allRequestsForCheckUp; +}; const findSearchRequestsForRealEstate = async realEstate => { const { @@ -438,5 +456,6 @@ const findSearchRequestsForRealEstate = async realEstate => { module.exports = { getSearchRequest, createSearchRequest, - findSearchRequestsForRealEstate + findSearchRequestsForRealEstate, + findAllRequestsForCheckUp }; diff --git a/app/helpers/db/searchRequestMatch.js b/app/helpers/db/searchRequestMatch.js index 794711b..38fdda5 100644 --- a/app/helpers/db/searchRequestMatch.js +++ b/app/helpers/db/searchRequestMatch.js @@ -2,7 +2,6 @@ const db = require("../../models/index"); const sequelize = require("sequelize"); const Op = sequelize.Op; -const { CHECK_UP_DAYS } = require("../../config/appConfig"); const findRealEstatesForSearchRequest = async searchRequestId => { const query = { @@ -43,42 +42,6 @@ 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 excludedMatches = await db.SearchRequestMatch.findAll({ - attributes: ["searchRequestId"], - where: dateQuery, - order: [["searchRequestId", "ASC"]] - }); - - const excludedRequestsAll = excludedMatches.map(match => { - return match.dataValues.searchRequestId; - }); - //Removing duplicate search request id-s for optimization - const excludedRequests = [...new Set(excludedRequestsAll)]; - - const query = { - subscribed: true, - id: { - [Op.notIn]: excludedRequests - } - }; - const allRequestsForCheckUp = await db.SearchRequest.findAll({ - where: query - }); - - return allRequestsForCheckUp; -}; const addMatches = async matchingRecords => { return await db.SearchRequestMatch.bulkCreate(matchingRecords, { @@ -89,6 +52,5 @@ const addMatches = async matchingRecords => { module.exports = { findRealEstatesForSearchRequest, addMatches, - findNotNotifiedMatches, - findAllRequestsForCheckUp + findNotNotifiedMatches }; diff --git a/app/migrations/20200215002846-add-column-notified-to-searchrequests.js b/app/migrations/20200215002846-add-column-notified-to-searchrequests.js new file mode 100644 index 0000000..f5456c9 --- /dev/null +++ b/app/migrations/20200215002846-add-column-notified-to-searchrequests.js @@ -0,0 +1,14 @@ +"use strict"; + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.addColumn("SearchRequests", "notifiedAt", { + type: Sequelize.DATE, + defaultValue: new Date() + }); + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.removeColumn("SearchRequests", "notifiedAt"); + } +}; diff --git a/app/models/searchRequest.js b/app/models/searchRequest.js index 0d4997f..39bcd2e 100644 --- a/app/models/searchRequest.js +++ b/app/models/searchRequest.js @@ -90,7 +90,11 @@ module.exports = (sequelize, DataTypes) => { floorMin: DataTypes.INTEGER, floorMax: DataTypes.INTEGER, accessRoadType: DataTypes.TEXT, - heatingType: DataTypes.TEXT + heatingType: DataTypes.TEXT, + notifiedAt: { + type: DataTypes.DATE, + defaultValue: new Date() + } }); return SearchRequest; diff --git a/app/services/notificationService.js b/app/services/notificationService.js index a005f84..08138ca 100644 --- a/app/services/notificationService.js +++ b/app/services/notificationService.js @@ -15,9 +15,10 @@ const { } = require("../helpers/emailContentGenerator"); const { findNotNotifiedMatches, - findAllRequestsForCheckUp, findRealEstatesForSearchRequest } = require("../helpers/db/searchRequestMatch"); +const { findAllRequestsForCheckUp } = require("../helpers/db/searchRequest"); + const { sendEmail } = require("../services/emailService"); const notifyForNewRealEstates = async newRealEstates => { @@ -35,7 +36,7 @@ const notifyForNewSearchRequest = async searchRequest => { matchingRealEstates ); const { email } = searchRequest; - + //In case of the new search req, notifiedAt column is populated with default value - now (moment of creation) await sendEmail( email, `${stagingTag} Kivi - novi zahtjev za pretragu`, @@ -76,6 +77,10 @@ const notifyMatches = async (matches, dailyNotification = false) => { sendEmailPromise.catch(err => console.log("[Email Sending Failed]", err) ); + + //Change time of notified At for searchReq + searchRequest.notifiedAt = new Date(); + searchRequest.save(); } } } @@ -131,7 +136,7 @@ const notifyRequestsWithDailyOption = async () => { }; const checkUpNotify = async () => { - /* const searchRequestsForCheckUp = await findAllRequestsForCheckUp(); + const searchRequestsForCheckUp = await findAllRequestsForCheckUp(); const asyncSendEmailActions = []; @@ -143,8 +148,12 @@ const checkUpNotify = async () => { const sendEmailPromise = sendEmail(email, emailSubject, emailContent); asyncSendEmailActions.push(sendEmailPromise); sendEmailPromise.catch(err => console.log("[Email Sending Failed]", err)); + + //Change time of notified At for searchReq + searchRequest.notifiedAt = new Date(); + searchRequest.save(); } - await Promise.all(asyncSendEmailActions); */ + await Promise.all(asyncSendEmailActions); }; module.exports = { diff --git a/index.js b/index.js index 0669e7b..45f9a5e 100644 --- a/index.js +++ b/index.js @@ -49,4 +49,6 @@ const crawl = () => { setInterval(crawl, CRAWLER_INTERVAL * 1000); -setInterval(checkUpNotify, 1000 * 60 * 60 * 24); +//setInterval(checkUpNotify, 1000 * 60 * 60 * 24); + +setInterval(checkUpNotify, 1000 * 60 * 2); -- 2.47.3 From 22bffc126d29782451309854dde1e0353a405a63 Mon Sep 17 00:00:00 2001 From: Naida Vatric Date: Sat, 15 Feb 2020 02:39:38 +0100 Subject: [PATCH 2/3] For staging - checkup fix. --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 45f9a5e..0669e7b 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,4 @@ const crawl = () => { setInterval(crawl, CRAWLER_INTERVAL * 1000); -//setInterval(checkUpNotify, 1000 * 60 * 60 * 24); - -setInterval(checkUpNotify, 1000 * 60 * 2); +setInterval(checkUpNotify, 1000 * 60 * 60 * 24); -- 2.47.3 From bc7ce9d7085a13a2a71816daec213aeac61b53b5 Mon Sep 17 00:00:00 2001 From: Naida Vatric Date: Mon, 17 Feb 2020 21:22:45 +0100 Subject: [PATCH 3/3] Changed checkup query to miliseconds. --- app/helpers/db/searchRequest.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/helpers/db/searchRequest.js b/app/helpers/db/searchRequest.js index b40bda5..f845981 100644 --- a/app/helpers/db/searchRequest.js +++ b/app/helpers/db/searchRequest.js @@ -18,16 +18,15 @@ const createSearchRequest = async (searchRequestFields = {}) => { return await db.SearchRequest.create(searchRequestFields); }; const findAllRequestsForCheckUp = async () => { - const date = new Date(); - const checkUpDate = date.getDate() - CHECK_UP_DAYS; - date.setDate(checkUpDate); + const checkUpOffset = 24 * 60 * 60 * 1000 * CHECK_UP_DAYS; //in miliseconds + const checkupDate = new Date(); + checkupDate.setTime(checkupDate.getTime() - checkUpOffset); const dateQuery = { notifiedAt: { - [Op.lte]: date + [Op.lte]: checkupDate } }; - const allRequestsForCheckUp = await db.SearchRequest.findAll({ where: dateQuery }); -- 2.47.3