Compare commits

..

5 Commits

Author SHA1 Message Date
Naida Vatric
a508f72d7c Merge branch 'master' into 'checkup-email-bug-fix'
# Conflicts:
#   app/services/notificationService.js
2020-02-25 15:12:47 +00:00
Naida Vatric
08ad9edfe1 Merge branch 'scraper-api-support' into 'master'
Added Scraper API option.

See merge request saburly/marketalarm/web!100
2020-02-25 15:10:39 +00:00
Naida Vatric
bc7ce9d708 Changed checkup query to miliseconds. 2020-02-17 21:22:45 +01:00
Naida Vatric
22bffc126d For staging - checkup fix. 2020-02-15 02:39:38 +01:00
Naida Vatric
06f80296f3 Changed checkup email logic. 2020-02-15 02:30:02 +01:00
5 changed files with 52 additions and 45 deletions

View File

@@ -3,6 +3,7 @@ const db = require("../../models/index");
const sequelize = require("sequelize"); const sequelize = require("sequelize");
const Op = sequelize.Op; const Op = sequelize.Op;
const { AD_CATEGORY } = require("../../common/enums"); const { AD_CATEGORY } = require("../../common/enums");
const { CHECK_UP_DAYS } = require("../../config/appConfig");
const getSearchRequest = async searchRequestId => { const getSearchRequest = async searchRequestId => {
try { try {
@@ -16,6 +17,22 @@ const getSearchRequest = async searchRequestId => {
const createSearchRequest = async (searchRequestFields = {}) => { const createSearchRequest = async (searchRequestFields = {}) => {
return await db.SearchRequest.create(searchRequestFields); return await db.SearchRequest.create(searchRequestFields);
}; };
const findAllRequestsForCheckUp = async () => {
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]: checkupDate
}
};
const allRequestsForCheckUp = await db.SearchRequest.findAll({
where: dateQuery
});
return allRequestsForCheckUp;
};
const findSearchRequestsForRealEstate = async realEstate => { const findSearchRequestsForRealEstate = async realEstate => {
const { const {
@@ -459,5 +476,6 @@ const findSearchRequestsForRealEstate = async realEstate => {
module.exports = { module.exports = {
getSearchRequest, getSearchRequest,
createSearchRequest, createSearchRequest,
findSearchRequestsForRealEstate findSearchRequestsForRealEstate,
findAllRequestsForCheckUp
}; };

View File

@@ -2,7 +2,6 @@
const db = require("../../models/index"); const db = require("../../models/index");
const sequelize = require("sequelize"); const sequelize = require("sequelize");
const Op = sequelize.Op; const Op = sequelize.Op;
const { CHECK_UP_DAYS } = require("../../config/appConfig");
const findRealEstatesForSearchRequest = async searchRequestId => { const findRealEstatesForSearchRequest = async searchRequestId => {
const query = { const query = {
@@ -43,42 +42,6 @@ const findNotNotifiedMatches = async () => {
return matchingRecords; 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 => { const addMatches = async matchingRecords => {
return await db.SearchRequestMatch.bulkCreate(matchingRecords, { return await db.SearchRequestMatch.bulkCreate(matchingRecords, {
@@ -89,6 +52,5 @@ const addMatches = async matchingRecords => {
module.exports = { module.exports = {
findRealEstatesForSearchRequest, findRealEstatesForSearchRequest,
addMatches, addMatches,
findNotNotifiedMatches, findNotNotifiedMatches
findAllRequestsForCheckUp
}; };

View File

@@ -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");
}
};

View File

@@ -82,7 +82,11 @@ module.exports = (sequelize, DataTypes) => {
floorMin: DataTypes.INTEGER, floorMin: DataTypes.INTEGER,
floorMax: DataTypes.INTEGER, floorMax: DataTypes.INTEGER,
accessRoadType: DataTypes.TEXT, accessRoadType: DataTypes.TEXT,
heatingType: DataTypes.TEXT heatingType: DataTypes.TEXT,
notifiedAt: {
type: DataTypes.DATE,
defaultValue: new Date()
}
}); });
return SearchRequest; return SearchRequest;

View File

@@ -15,9 +15,10 @@ const {
} = require("../helpers/emailContentGenerator"); } = require("../helpers/emailContentGenerator");
const { const {
findNotNotifiedMatches, findNotNotifiedMatches,
findAllRequestsForCheckUp,
findRealEstatesForSearchRequest findRealEstatesForSearchRequest
} = require("../helpers/db/searchRequestMatch"); } = require("../helpers/db/searchRequestMatch");
const { findAllRequestsForCheckUp } = require("../helpers/db/searchRequest");
const { sendEmail } = require("../services/emailService"); const { sendEmail } = require("../services/emailService");
const notifyForNewRealEstates = async newRealEstates => { const notifyForNewRealEstates = async newRealEstates => {
@@ -35,7 +36,7 @@ const notifyForNewSearchRequest = async searchRequest => {
matchingRealEstates matchingRealEstates
); );
const { email } = searchRequest; const { email } = searchRequest;
//In case of the new search req, notifiedAt column is populated with default value - now (moment of creation)
await sendEmail( await sendEmail(
email, email,
`${stagingTag} Kivi - novi zahtjev za pretragu`, `${stagingTag} Kivi - novi zahtjev za pretragu`,
@@ -76,6 +77,10 @@ const notifyMatches = async (matches, dailyNotification = false) => {
sendEmailPromise.catch(err => sendEmailPromise.catch(err =>
console.log("[Email Sending Failed]", 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 checkUpNotify = async () => {
/* const searchRequestsForCheckUp = await findAllRequestsForCheckUp(); const searchRequestsForCheckUp = await findAllRequestsForCheckUp();
const asyncSendEmailActions = []; const asyncSendEmailActions = [];
@@ -143,8 +148,12 @@ const checkUpNotify = async () => {
const sendEmailPromise = sendEmail(email, emailSubject, emailContent); const sendEmailPromise = sendEmail(email, emailSubject, emailContent);
asyncSendEmailActions.push(sendEmailPromise); asyncSendEmailActions.push(sendEmailPromise);
sendEmailPromise.catch(err => console.log("[Email Sending Failed]", err)); 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 = { module.exports = {