Email links now point to our URL and then redirect to the original property URL, ran prettier on the affected files
This commit is contained in:
16
app/controllers/redirect.js
Normal file
16
app/controllers/redirect.js
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
const { getMarketAlertById } = require("../helpers/db/dbHelper");
|
||||||
|
|
||||||
|
const redirect = async (req, res) => {
|
||||||
|
const id = req.params["id"];
|
||||||
|
const marketAlert = await getMarketAlertById(id);
|
||||||
|
if (marketAlert) {
|
||||||
|
res.redirect(marketAlert.url);
|
||||||
|
} else {
|
||||||
|
res.send("Not found");
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
redirect
|
||||||
|
};
|
||||||
@@ -1,32 +1,29 @@
|
|||||||
|
const dotenv = require("dotenv").config();
|
||||||
const dotenv = require('dotenv').config();
|
const { getRealEstateTypeEnum } = require("./enums");
|
||||||
const { getRealEstateTypeEnum } = require('./enums');
|
const { getRegionName, getMunicipalityName } = require("./codes");
|
||||||
const { getRegionName, getMunicipalityName } = require('./codes');
|
const { allRERequestByUiid } = require("./db/dbHelper");
|
||||||
const { allRERequestByUiid } = require('./db/dbHelper');
|
var AWS = require("aws-sdk");
|
||||||
var AWS = require('aws-sdk');
|
const TEMPLATE_NAME = "MarketAlertTemplate";
|
||||||
const TEMPLATE_NAME = "MarketAlertTemplate"
|
|
||||||
|
|
||||||
AWS.config.update({
|
AWS.config.update({
|
||||||
region: process.env.AMAZON_REGION,
|
region: process.env.AMAZON_REGION,
|
||||||
credentials:
|
credentials: {
|
||||||
{
|
|
||||||
accessKeyId: process.env.AMAZON_ACCES_KEY_ID,
|
accessKeyId: process.env.AMAZON_ACCES_KEY_ID,
|
||||||
secretAccessKey: process.env.AMAZON_SECRET_ACCESS_KEY
|
secretAccessKey: process.env.AMAZON_SECRET_ACCESS_KEY
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const sendTemplatedEmail = async (email, request) => {
|
const sendTemplatedEmail = async (email, request) => {
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
Destination: { /* required */
|
Destination: {
|
||||||
CcAddresses: [
|
/* required */
|
||||||
],
|
CcAddresses: [],
|
||||||
ToAddresses: [
|
ToAddresses: [email]
|
||||||
email
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
Message: { /* required */
|
Message: {
|
||||||
Body: { /* required */
|
/* required */
|
||||||
|
Body: {
|
||||||
|
/* required */
|
||||||
Html: {
|
Html: {
|
||||||
Charset: "UTF-8",
|
Charset: "UTF-8",
|
||||||
Data: getGreetingsEmailHTML(request)
|
Data: getGreetingsEmailHTML(request)
|
||||||
@@ -37,76 +34,124 @@ const sendTemplatedEmail = async (email, request) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Subject: {
|
Subject: {
|
||||||
Charset: 'UTF-8',
|
Charset: "UTF-8",
|
||||||
Data: `Javimi Potvrda: ${getSubject(request.realEstateType, request.region, request.municipality)}`
|
Data: `Javimi Potvrda: ${getSubject(
|
||||||
|
request.realEstateType,
|
||||||
|
request.region,
|
||||||
|
request.municipality
|
||||||
|
)}`
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Source: process.env.SOURCE_EMAIL, /* required */
|
Source: process.env.SOURCE_EMAIL /* required */,
|
||||||
ReplyToAddresses: [
|
ReplyToAddresses: [process.env.SOURCE_EMAIL]
|
||||||
process.env.SOURCE_EMAIL,
|
|
||||||
],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const sendEmailPromise = new AWS.SES({ apiVersion: '2010-12-01' }).sendEmail(params).promise();
|
const sendEmailPromise = new AWS.SES({ apiVersion: "2010-12-01" })
|
||||||
|
.sendEmail(params)
|
||||||
|
.promise();
|
||||||
await sendEmailPromise;
|
await sendEmailPromise;
|
||||||
}
|
};
|
||||||
|
|
||||||
const getGreetingsEmailHTML = (realestateRequest) => {
|
const getGreetingsEmailHTML = realestateRequest => {
|
||||||
const realEstateType = getRealEstateTypeEnum(realestateRequest.realEstateType);
|
const realEstateType = getRealEstateTypeEnum(
|
||||||
const gardenSize = realEstateType.hasGardenSize ? `<div><strong>Kvadratura okućnice: Od ${realestateRequest.gardenSizeMin} do ${realestateRequest.gardenSizeMax} m2 </strong></div>` : ``
|
realestateRequest.realEstateType
|
||||||
|
);
|
||||||
|
const gardenSize = realEstateType.hasGardenSize
|
||||||
|
? `<div><strong>Kvadratura okućnice: Od ${
|
||||||
|
realestateRequest.gardenSizeMin
|
||||||
|
} do ${realestateRequest.gardenSizeMax} m2 </strong></div>`
|
||||||
|
: ``;
|
||||||
|
|
||||||
return `<h1> Zdravo,
|
return `<h1> Zdravo,
|
||||||
Naručio/la si da ti javimo ako se nekretnina pojavi u oglasima. </h1>
|
Naručio/la si da ti javimo ako se nekretnina pojavi u oglasima. </h1>
|
||||||
<h2> Ovo je tražena nekretnina: </h2>
|
<h2> Ovo je tražena nekretnina: </h2>
|
||||||
<div>
|
<div>
|
||||||
<div> <strong>Tip nekretnine: ${realEstateType.title} </strong></div>
|
<div> <strong>Tip nekretnine: ${realEstateType.title} </strong></div>
|
||||||
<div><strong>Područje: ${getRegionName(realestateRequest.region)} </strong></div>
|
<div><strong>Područje: ${getRegionName(
|
||||||
<div><strong>Mjesto: ${getMunicipalityName(realestateRequest.region, realestateRequest.municipality)} </strong></div>
|
realestateRequest.region
|
||||||
<div><strong>Kvadratura nekretnine: Od ${realestateRequest.sizeMin} do ${realestateRequest.sizeMax} m2 </strong></div>
|
)} </strong></div>
|
||||||
|
<div><strong>Mjesto: ${getMunicipalityName(
|
||||||
|
realestateRequest.region,
|
||||||
|
realestateRequest.municipality
|
||||||
|
)} </strong></div>
|
||||||
|
<div><strong>Kvadratura nekretnine: Od ${realestateRequest.sizeMin} do ${
|
||||||
|
realestateRequest.sizeMax
|
||||||
|
} m2 </strong></div>
|
||||||
${gardenSize}
|
${gardenSize}
|
||||||
<div><strong>Cijena: ${realestateRequest.priceMin} do ${realestateRequest.priceMax} KM </strong></div>
|
<div><strong>Cijena: ${realestateRequest.priceMin} do ${
|
||||||
|
realestateRequest.priceMax
|
||||||
|
} KM </strong></div>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div><strong> Ako želis prestati dobijati obavještenja za ovu pretragu klikni ${process.env.APP_URL}/odjava/${realestateRequest.uniqueId} </strong></div>
|
<div><strong> Ako želis prestati dobijati obavještenja za ovu pretragu klikni ${
|
||||||
<div><strong>Ako želiš promijeniti uslove pretrage klikni ${process.env.APP_URL}/pregled/${realestateRequest.uniqueId} </strong></div>
|
process.env.APP_URL
|
||||||
|
}/odjava/${realestateRequest.uniqueId} </strong></div>
|
||||||
|
<div><strong>Ako želiš promijeniti uslove pretrage klikni ${
|
||||||
|
process.env.APP_URL
|
||||||
|
}/pregled/${realestateRequest.uniqueId} </strong></div>
|
||||||
<h4> Tvoj,
|
<h4> Tvoj,
|
||||||
Javimi tim.
|
Javimi tim.
|
||||||
</h4>`
|
</h4>`;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
const getGreetingsEmaiTextVersion = realestateRequest => {
|
||||||
|
const realEstateType = getRealEstateTypeEnum(
|
||||||
|
realestateRequest.realEstateType
|
||||||
|
);
|
||||||
|
const gardenSize = realEstateType.hasGardenSize
|
||||||
|
? "Kvadratura okućnice od " +
|
||||||
|
realestateRequest.gardenSizeMin +
|
||||||
|
" do " +
|
||||||
|
realestateRequest.gardenSizeMax
|
||||||
|
: "";
|
||||||
|
|
||||||
const getGreetingsEmaiTextVersion = (realestateRequest) => {
|
const text =
|
||||||
const realEstateType = getRealEstateTypeEnum(realestateRequest.realEstateType);
|
"Zdravo, \n Naručio/la si da ti javimo ako se nekretnina pojavi u oglasima \n Ovo je tražena nekretnina: \n , Tip nekretnine: " +
|
||||||
const gardenSize = realEstateType.hasGardenSize ? "Kvadratura okućnice od " + realestateRequest.gardenSizeMin + " do " + realestateRequest.gardenSizeMax : ""
|
realestateRequest.realEstateType +
|
||||||
|
"\n Područje" +
|
||||||
const text = "Zdravo, \n Naručio/la si da ti javimo ako se nekretnina pojavi u oglasima \n Ovo je tražena nekretnina: \n , Tip nekretnine: "
|
getRegionName(realestateRequest.region) +
|
||||||
+ realestateRequest.realEstateType + "\n Područje" + getRegionName(realestateRequest.region) + "\n Mjesto " + getMunicipalityName(realestateRequest.region, realestateRequest.municipality)
|
"\n Mjesto " +
|
||||||
+ "\n Kvadratura nekretnine Od " + realestateRequest.sizeMin + " do " + realestateRequest.sizeMaX +
|
getMunicipalityName(
|
||||||
+ gardenSize
|
realestateRequest.region,
|
||||||
"\n Cijena od " + realestateRequest.priceMin + " do " + realestateRequest.priceMax +
|
realestateRequest.municipality
|
||||||
"\n Ako želis prestati dobijati obavještenja za ovu pretragu klikni" + process.env.APP_URL + "/odjava/" + realestateRequest.uniqueId +
|
) +
|
||||||
"\n Ako želiš promijeniti uslove pretrage klikni " + process.env.APP_URL + "/odpregled/" + realestateRequest.uniqueId +
|
"\n Kvadratura nekretnine Od " +
|
||||||
"\n Tvoj,\n Javimi tim"
|
realestateRequest.sizeMin +
|
||||||
|
" do " +
|
||||||
|
realestateRequest.sizeMaX +
|
||||||
|
+gardenSize;
|
||||||
|
"\n Cijena od " +
|
||||||
|
realestateRequest.priceMin +
|
||||||
|
" do " +
|
||||||
|
realestateRequest.priceMax +
|
||||||
|
"\n Ako želis prestati dobijati obavještenja za ovu pretragu klikni" +
|
||||||
|
process.env.APP_URL +
|
||||||
|
"/odjava/" +
|
||||||
|
realestateRequest.uniqueId +
|
||||||
|
"\n Ako želiš promijeniti uslove pretrage klikni " +
|
||||||
|
process.env.APP_URL +
|
||||||
|
"/odpregled/" +
|
||||||
|
realestateRequest.uniqueId +
|
||||||
|
"\n Tvoj,\n Javimi tim";
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
};
|
||||||
|
|
||||||
const sendBulkEmail = async (marketAlerts) => {
|
|
||||||
|
|
||||||
|
const sendBulkEmail = async marketAlerts => {
|
||||||
try {
|
try {
|
||||||
|
destinations = [];
|
||||||
destinations = []
|
|
||||||
groupedRERequests = [];
|
groupedRERequests = [];
|
||||||
|
|
||||||
|
const RERequestUuidsMaped = marketAlerts.map(
|
||||||
const RERequestUuidsMaped = marketAlerts.map(marketAlert => marketAlert.request);
|
marketAlert => marketAlert.request
|
||||||
|
);
|
||||||
|
|
||||||
const RERequestUuidsArray = Array.from(new Set(RERequestUuidsMaped));
|
const RERequestUuidsArray = Array.from(new Set(RERequestUuidsMaped));
|
||||||
|
|
||||||
const RERequestUuids = RERequestUuidsArray.map(marketAlert => {
|
const RERequestUuids = RERequestUuidsArray.map(marketAlert => {
|
||||||
return { uniqueId: marketAlert }
|
return { uniqueId: marketAlert };
|
||||||
});
|
});
|
||||||
|
|
||||||
const RERequests = await allRERequestByUiid(RERequestUuids);
|
const RERequests = await allRERequestByUiid(RERequestUuids);
|
||||||
@@ -114,25 +159,24 @@ const sendBulkEmail = async (marketAlerts) => {
|
|||||||
|
|
||||||
RERequests.forEach(RERequest => {
|
RERequests.forEach(RERequest => {
|
||||||
var formatedRequest = {};
|
var formatedRequest = {};
|
||||||
formatedRequest[RERequest.uniqueId] =
|
formatedRequest[RERequest.uniqueId] = requestDataValues[
|
||||||
requestDataValues[RERequest.uniqueId] = {
|
RERequest.uniqueId
|
||||||
realEstateType: RERequest.realEstateType,
|
] = {
|
||||||
region: RERequest.region,
|
realEstateType: RERequest.realEstateType,
|
||||||
municipality: RERequest.municipality,
|
region: RERequest.region,
|
||||||
requestUrl : `${process.env.APP_URL}/nekretnine/${RERequest.uniqueId}`
|
municipality: RERequest.municipality,
|
||||||
};
|
requestUrl: `${process.env.APP_URL}/nekretnine/${RERequest.uniqueId}`
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
marketAlerts.forEach(marketAlert => {
|
marketAlerts.forEach(marketAlert => {
|
||||||
|
|
||||||
const requestObject = {
|
const requestObject = {
|
||||||
email: marketAlert.email,
|
email: marketAlert.email,
|
||||||
realEstateType: requestDataValues[marketAlert.request].realEstateType,
|
realEstateType: requestDataValues[marketAlert.request].realEstateType,
|
||||||
municipality: requestDataValues[marketAlert.request].municipality,
|
municipality: requestDataValues[marketAlert.request].municipality,
|
||||||
region: requestDataValues[marketAlert.request].region,
|
region: requestDataValues[marketAlert.request].region,
|
||||||
requestUrl: requestDataValues[marketAlert.request].requestUrl
|
requestUrl: requestDataValues[marketAlert.request].requestUrl
|
||||||
|
};
|
||||||
}
|
|
||||||
|
|
||||||
if (!groupedRERequests[marketAlert.request]) {
|
if (!groupedRERequests[marketAlert.request]) {
|
||||||
groupedRERequests[marketAlert.request] = {
|
groupedRERequests[marketAlert.request] = {
|
||||||
@@ -144,65 +188,66 @@ const sendBulkEmail = async (marketAlerts) => {
|
|||||||
groupedRERequests[marketAlert.request].marketAlertArray.push({
|
groupedRERequests[marketAlert.request].marketAlertArray.push({
|
||||||
url: marketAlert.url,
|
url: marketAlert.url,
|
||||||
title: marketAlert.title,
|
title: marketAlert.title,
|
||||||
|
id: marketAlert.id
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
for (request in groupedRERequests) {
|
for (request in groupedRERequests) {
|
||||||
|
|
||||||
const marketAlert = groupedRERequests[request];
|
const marketAlert = groupedRERequests[request];
|
||||||
let extractedData = toAWSArray(marketAlert.marketAlertArray);
|
let extractedData = toAWSArray(marketAlert.marketAlertArray);
|
||||||
const realEstateType = getRealEstateTypeEnum(marketAlert.requestObject.realEstateType).title;
|
const realEstateType = getRealEstateTypeEnum(
|
||||||
|
marketAlert.requestObject.realEstateType
|
||||||
|
).title;
|
||||||
const region = getRegionName(marketAlert.requestObject.region);
|
const region = getRegionName(marketAlert.requestObject.region);
|
||||||
const municipality = getMunicipalityName(marketAlert.requestObject.region, marketAlert.requestObject.municipality);
|
const municipality = getMunicipalityName(
|
||||||
const requestUrl = marketAlert.requestObject.requestUrl
|
marketAlert.requestObject.region,
|
||||||
|
marketAlert.requestObject.municipality
|
||||||
|
);
|
||||||
|
const requestUrl = marketAlert.requestObject.requestUrl;
|
||||||
|
|
||||||
let repData = `{ "marketAlertUrl":[${extractedData}], "realestateType":"${realEstateType}", "region":"${region}", "municipality":"${municipality}", "requestUrl":"${requestUrl}" }`
|
let repData = `{ "marketAlertUrl":[${extractedData}], "realestateType":"${realEstateType}", "region":"${region}", "municipality":"${municipality}", "requestUrl":"${requestUrl}" }`;
|
||||||
|
|
||||||
destinations.push({
|
destinations.push({
|
||||||
Destination: {
|
Destination: {
|
||||||
ToAddresses: [
|
ToAddresses: [marketAlert.requestObject.email]
|
||||||
marketAlert.requestObject.email
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
ReplacementTemplateData: repData
|
ReplacementTemplateData: repData
|
||||||
})
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
console.log("AWS EMAIL : Bulk email replacement data:");
|
console.log("AWS EMAIL : Bulk email replacement data:");
|
||||||
console.log(destinations);
|
console.log(destinations);
|
||||||
|
|
||||||
var params = {
|
var params = {
|
||||||
Destinations:
|
Destinations: destinations,
|
||||||
destinations,
|
Source: process.env.SOURCE_EMAIL /* required */,
|
||||||
Source: process.env.SOURCE_EMAIL, /* required */
|
Template: TEMPLATE_NAME /* required */,
|
||||||
Template: TEMPLATE_NAME, /* required */
|
DefaultTemplateData: '{ "REPLACEMENT_TAG_NAME":"REPLACEMENT_VALUE" }',
|
||||||
DefaultTemplateData: '{ \"REPLACEMENT_TAG_NAME\":\"REPLACEMENT_VALUE\" }',
|
ReplyToAddresses: [process.env.SOURCE_EMAIL]
|
||||||
ReplyToAddresses: [
|
|
||||||
process.env.SOURCE_EMAIL,
|
|
||||||
]
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create the promise and SES service object
|
// Create the promise and SES service object
|
||||||
const sendPromise = new AWS.SES({ apiVersion: '2010-12-01' }).sendBulkTemplatedEmail(params).promise();
|
const sendPromise = new AWS.SES({ apiVersion: "2010-12-01" })
|
||||||
|
.sendBulkTemplatedEmail(params)
|
||||||
|
.promise();
|
||||||
const awsResult = await sendPromise;
|
const awsResult = await sendPromise;
|
||||||
console.log("AWS SES bulk email response");
|
console.log("AWS SES bulk email response");
|
||||||
console.log(awsResult);
|
console.log(awsResult);
|
||||||
|
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Could not send bulk email", e)
|
console.log("Could not send bulk email", e);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
const toAWSArray = (urlArray) => {
|
const redirectUrl = marketAlertId =>
|
||||||
|
`${process.env.APP_URL}/redirect/${marketAlertId}`;
|
||||||
let arrayString = ""
|
const toAWSArray = urlArray => {
|
||||||
|
let arrayString = "";
|
||||||
urlArray.forEach(element => {
|
urlArray.forEach(element => {
|
||||||
const formatetdTitle = element.title.replace(/"/g, "");
|
const formatetdTitle = element.title.replace(/"/g, "");
|
||||||
arrayString = arrayString + `{"url":"${element.url.trim()}" , "title":"${formatetdTitle}"},`
|
arrayString =
|
||||||
|
arrayString +
|
||||||
|
`{"url":"${redirectUrl(element.id)}" , "title":"${formatetdTitle}"},`;
|
||||||
});
|
});
|
||||||
return arrayString.slice(0, -1);
|
return arrayString.slice(0, -1);
|
||||||
|
};
|
||||||
}
|
|
||||||
|
|
||||||
const getNotificationEmailHtml = () => {
|
const getNotificationEmailHtml = () => {
|
||||||
return `<h2> Zdravo,
|
return `<h2> Zdravo,
|
||||||
@@ -212,36 +257,40 @@ const getNotificationEmailHtml = () => {
|
|||||||
<div>{{#each marketAlertUrl}}<li><a href="{{url}}">{{title}}</a></li><br />{{/each}}<div/>
|
<div>{{#each marketAlertUrl}}<li><a href="{{url}}">{{title}}</a></li><br />{{/each}}<div/>
|
||||||
<div/>
|
<div/>
|
||||||
<div>Kompletan spisak nekretnina možete pegledati ovdije: <a href="{{requestUrl}}">Nekretnine</a> <div>
|
<div>Kompletan spisak nekretnina možete pegledati ovdije: <a href="{{requestUrl}}">Nekretnine</a> <div>
|
||||||
</div>`
|
</div>`;
|
||||||
}
|
};
|
||||||
|
|
||||||
const getNotificationEmailText = () => {
|
const getNotificationEmailText = () => {
|
||||||
return ` Zdravo,
|
return ` Zdravo,
|
||||||
Pronašli smo nekretninu koju ste tražili. Ovo su tražene nekretnine: {{#each marketAlertUrl}} {{url}} {{title}} {{/each}} , Kompletan spisan nekretnina mozete pegledati ovdije: {{requestUrl}}`
|
Pronašli smo nekretninu koju ste tražili. Ovo su tražene nekretnine: {{#each marketAlertUrl}} {{url}} {{title}} {{/each}} , Kompletan spisan nekretnina mozete pegledati ovdije: {{requestUrl}}`;
|
||||||
}
|
};
|
||||||
|
|
||||||
const createMarketAlertEmailTemplate = async () => {
|
const createMarketAlertEmailTemplate = async () => {
|
||||||
const marketAlertTemplate = {
|
const marketAlertTemplate = {
|
||||||
Template: {
|
Template: {
|
||||||
TemplateName: TEMPLATE_NAME,
|
TemplateName: TEMPLATE_NAME,
|
||||||
SubjectPart: "Javi mi obavijest: {{realestateType}}, {{region}}, {{municipality}}",
|
SubjectPart:
|
||||||
|
"Javi mi obavijest: {{realestateType}}, {{region}}, {{municipality}}",
|
||||||
TextPart: getNotificationEmailText(),
|
TextPart: getNotificationEmailText(),
|
||||||
HtmlPart: getNotificationEmailHtml()
|
HtmlPart: getNotificationEmailHtml()
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const templatePromise = new AWS.SES({ apiVersion: '2010-12-01' }).updateTemplate(marketAlertTemplate).promise();
|
const templatePromise = new AWS.SES({ apiVersion: "2010-12-01" })
|
||||||
await templatePromise
|
.updateTemplate(marketAlertTemplate)
|
||||||
|
.promise();
|
||||||
|
await templatePromise;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Could not create MarketAlertEmailTemplate", e);
|
console.log("Could not create MarketAlertEmailTemplate", e);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
const getSubject = (realEstateType, region, municipality) => {
|
const getSubject = (realEstateType, region, municipality) => {
|
||||||
return `${getRealEstateTypeEnum(realEstateType).title} ${getRegionName(region)}, ${getMunicipalityName(region, municipality)}`
|
return `${getRealEstateTypeEnum(realEstateType).title} ${getRegionName(
|
||||||
}
|
region
|
||||||
|
)}, ${getMunicipalityName(region, municipality)}`;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
sendTemplatedEmail,
|
sendTemplatedEmail,
|
||||||
|
|||||||
@@ -1,29 +1,28 @@
|
|||||||
const db = require('../../models/index');
|
const db = require("../../models/index");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find all subscribed RealEstateRequests
|
* Find all subscribed RealEstateRequests
|
||||||
*/
|
*/
|
||||||
const allRERequest = async () => {
|
const allRERequest = async () => {
|
||||||
return await db.RealEstateRequest.findAll({
|
return await db.RealEstateRequest.findAll({
|
||||||
where: {
|
where: {
|
||||||
subscribed: true
|
subscribed: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find all subscribed RealEstateRequests by UUID
|
* Find all subscribed RealEstateRequests by UUID
|
||||||
*/
|
*/
|
||||||
const allRERequestByUiid = async (requestArray) => {
|
const allRERequestByUiid = async requestArray => {
|
||||||
|
|
||||||
const Op = db.Sequelize.Op;
|
const Op = db.Sequelize.Op;
|
||||||
return await db.RealEstateRequest.findAll({
|
return await db.RealEstateRequest.findAll({
|
||||||
where: {
|
where: {
|
||||||
subscribed: true,
|
subscribed: true,
|
||||||
[Op.or]: requestArray
|
[Op.or]: requestArray
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find all , or all depending on notified bolean marketalerts, that the hasLocation is true, and order them by email
|
* Find all , or all depending on notified bolean marketalerts, that the hasLocation is true, and order them by email
|
||||||
@@ -34,41 +33,53 @@ const allRERequestByUiid = async (requestArray) => {
|
|||||||
* @returns array of MarketAlerts
|
* @returns array of MarketAlerts
|
||||||
*/
|
*/
|
||||||
const allMarketAlerts = async (fetchAll, notified) => {
|
const allMarketAlerts = async (fetchAll, notified) => {
|
||||||
|
let queryObject = {
|
||||||
|
order: [["email", "DESC"]]
|
||||||
|
};
|
||||||
|
|
||||||
let queryObject = {
|
if (!fetchAll) {
|
||||||
order: [
|
queryObject.where = {
|
||||||
['email', 'DESC'],
|
notified: notified,
|
||||||
]
|
hasLocation: true
|
||||||
}
|
};
|
||||||
|
|
||||||
if (!fetchAll){
|
|
||||||
queryObject.where = {
|
|
||||||
notified: notified,
|
|
||||||
hasLocation: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return await db.MarketAlert.findAll(queryObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return await db.MarketAlert.findAll(queryObject);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
* Find all , MarketAlerts depending on request
|
* Find all , MarketAlerts depending on request
|
||||||
*
|
*
|
||||||
* @param request string
|
* @param request string
|
||||||
*
|
*
|
||||||
* @returns array of MarketAlerts
|
* @returns array of MarketAlerts
|
||||||
*/
|
*/
|
||||||
const allMarketAlertsByRequest = async (request) => {
|
const allMarketAlertsByRequest = async request => {
|
||||||
|
|
||||||
let queryObject = {
|
let queryObject = {
|
||||||
where : {
|
where: {
|
||||||
request: request
|
request: request
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
return await db.MarketAlert.findAll(queryObject);
|
return await db.MarketAlert.findAll(queryObject);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find MarketAlerts by id
|
||||||
|
*
|
||||||
|
* @param id number
|
||||||
|
*
|
||||||
|
* @returns single MarketAlert
|
||||||
|
*/
|
||||||
|
const getMarketAlertById = async id => {
|
||||||
|
let queryObject = {
|
||||||
|
where: {
|
||||||
|
id: id
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return await db.MarketAlert.findOne(queryObject);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find all unnotified marketalerts
|
* Find all unnotified marketalerts
|
||||||
@@ -78,13 +89,18 @@ const allMarketAlertsByRequest = async (request) => {
|
|||||||
* @returns array of MarketAlerts
|
* @returns array of MarketAlerts
|
||||||
*/
|
*/
|
||||||
const findPointInsideBoundingBox = async (latLng, email, uniqueId) => {
|
const findPointInsideBoundingBox = async (latLng, email, uniqueId) => {
|
||||||
return await db.sequelize.query(`SELECT * FROM "RealEstateRequests" WHERE email = '${email}' AND "uniqueId" = '${uniqueId}' AND subscribed = true AND ST_Contains("RealEstateRequests".bounding_box, ST_GEOMFROMTEXT('POINT (${latLng[0]} ${latLng[1]})'))`);
|
return await db.sequelize.query(
|
||||||
}
|
`SELECT * FROM "RealEstateRequests" WHERE email = '${email}' AND "uniqueId" = '${uniqueId}' AND subscribed = true AND ST_Contains("RealEstateRequests".bounding_box, ST_GEOMFROMTEXT('POINT (${
|
||||||
|
latLng[0]
|
||||||
|
} ${latLng[1]})'))`
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
allRERequest,
|
allRERequest,
|
||||||
allMarketAlerts,
|
allMarketAlerts,
|
||||||
allRERequestByUiid,
|
allRERequestByUiid,
|
||||||
findPointInsideBoundingBox,
|
findPointInsideBoundingBox,
|
||||||
allMarketAlertsByRequest
|
allMarketAlertsByRequest,
|
||||||
|
getMarketAlertById
|
||||||
};
|
};
|
||||||
|
|||||||
156
index.js
156
index.js
@@ -1,19 +1,38 @@
|
|||||||
const welcome = require('./app/controllers/welcome').getWelcome;
|
const welcome = require("./app/controllers/welcome").getWelcome;
|
||||||
const { getRealEstateTypes, postRealEstateTypes } = require('./app/controllers/realEstateTypes');
|
const {
|
||||||
const { getRegion, postRegion } = require('./app/controllers/regions');
|
getRealEstateTypes,
|
||||||
const { getMunicipality, postMunicipality } = require('./app/controllers/municipalities');
|
postRealEstateTypes
|
||||||
const { getSize, postSize } = require('./app/controllers/sizes');
|
} = require("./app/controllers/realEstateTypes");
|
||||||
const { getGardenSize, postGardenSize } = require('./app/controllers/gardenSizes');
|
const { getRegion, postRegion } = require("./app/controllers/regions");
|
||||||
const { getPrice, postPrice } = require('./app/controllers/prices');
|
const {
|
||||||
const { getQueryReview, postQueryReview } = require('./app/controllers/queryReview');
|
getMunicipality,
|
||||||
const { getQuerySubmit, postQuerySubmit } = require('./app/controllers/querySubmit');
|
postMunicipality
|
||||||
const { getGoAgain } = require('./app/controllers/goAgain');
|
} = require("./app/controllers/municipalities");
|
||||||
const { getNeighborhood, postNeighborhood } = require('./app/controllers/neighborhoodMap');
|
const { getSize, postSize } = require("./app/controllers/sizes");
|
||||||
const { getUnsubscribe } = require('./app/controllers/unsubscribe');
|
const {
|
||||||
const { getRealEstates} = require('./app/controllers/realEstates');
|
getGardenSize,
|
||||||
const schedule = require('node-schedule');
|
postGardenSize
|
||||||
const crawlAll = require('./app/services/crawlerService')
|
} = require("./app/controllers/gardenSizes");
|
||||||
const processNotifications = require('./app/services/notificationService')
|
const { getPrice, postPrice } = require("./app/controllers/prices");
|
||||||
|
const {
|
||||||
|
getQueryReview,
|
||||||
|
postQueryReview
|
||||||
|
} = require("./app/controllers/queryReview");
|
||||||
|
const {
|
||||||
|
getQuerySubmit,
|
||||||
|
postQuerySubmit
|
||||||
|
} = require("./app/controllers/querySubmit");
|
||||||
|
const { getGoAgain } = require("./app/controllers/goAgain");
|
||||||
|
const {
|
||||||
|
getNeighborhood,
|
||||||
|
postNeighborhood
|
||||||
|
} = require("./app/controllers/neighborhoodMap");
|
||||||
|
const { getUnsubscribe } = require("./app/controllers/unsubscribe");
|
||||||
|
const { getRealEstates } = require("./app/controllers/realEstates");
|
||||||
|
const { redirect } = require("./app/controllers/redirect");
|
||||||
|
const schedule = require("node-schedule");
|
||||||
|
const crawlAll = require("./app/services/crawlerService");
|
||||||
|
const processNotifications = require("./app/services/notificationService");
|
||||||
|
|
||||||
let express = require("express");
|
let express = require("express");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
@@ -23,7 +42,7 @@ const sendNotification = require("./app/lib/sendNotification");
|
|||||||
const scrapTheItems = require("./app/lib/scrapTheItems");
|
const scrapTheItems = require("./app/lib/scrapTheItems");
|
||||||
const sequelize = require("./app/models/index").sequelize;
|
const sequelize = require("./app/models/index").sequelize;
|
||||||
const Twocheckout = require("2checkout-node");
|
const Twocheckout = require("2checkout-node");
|
||||||
const layout = require('express-layout');
|
const layout = require("express-layout");
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
@@ -31,11 +50,11 @@ app.use(bodyParser.urlencoded({ extended: true }));
|
|||||||
|
|
||||||
const port = process.env.PORT || 5000;
|
const port = process.env.PORT || 5000;
|
||||||
|
|
||||||
app.set('views', path.join(__dirname, '/app/views'));
|
app.set("views", path.join(__dirname, "/app/views"));
|
||||||
app.set('view engine', 'ejs');
|
app.set("view engine", "ejs");
|
||||||
app.use(layout());
|
app.use(layout());
|
||||||
|
|
||||||
const compression = require('compression');
|
const compression = require("compression");
|
||||||
app.use(compression());
|
app.use(compression());
|
||||||
|
|
||||||
app.get("/api/sendnotifications", async (req, res) => {
|
app.get("/api/sendnotifications", async (req, res) => {
|
||||||
@@ -111,7 +130,7 @@ app.post("/api/payforalert", (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
tco.checkout.authorize(params, function (error, data) {
|
tco.checkout.authorize(params, function(error, data) {
|
||||||
if (error) {
|
if (error) {
|
||||||
res.send(error.message);
|
res.send(error.message);
|
||||||
} else {
|
} else {
|
||||||
@@ -120,64 +139,73 @@ app.post("/api/payforalert", (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/', welcome);
|
app.get("/", welcome);
|
||||||
app.get('/vrstanekretnine/:request_id', getRealEstateTypes);
|
app.get("/vrstanekretnine/:request_id", getRealEstateTypes);
|
||||||
app.get('/vrstanekretnine', getRealEstateTypes);
|
app.get("/vrstanekretnine", getRealEstateTypes);
|
||||||
|
|
||||||
app.post('/vrstanekretnine/:request_id', postRealEstateTypes);
|
app.post("/vrstanekretnine/:request_id", postRealEstateTypes);
|
||||||
app.post('/vrstanekretnine', postRealEstateTypes);
|
app.post("/vrstanekretnine", postRealEstateTypes);
|
||||||
|
|
||||||
app.get('/grad/:request_id', getRegion);
|
app.get("/grad/:request_id", getRegion);
|
||||||
app.post('/grad/:request_id', postRegion);
|
app.post("/grad/:request_id", postRegion);
|
||||||
|
|
||||||
app.get('/mjesto/:request_id', getMunicipality);
|
app.get("/mjesto/:request_id", getMunicipality);
|
||||||
app.post('/mjesto/:request_id', postMunicipality);
|
app.post("/mjesto/:request_id", postMunicipality);
|
||||||
|
|
||||||
app.get('/naselje/:request_id/:municipality', getNeighborhood);
|
app.get("/naselje/:request_id/:municipality", getNeighborhood);
|
||||||
app.post('/naselje/:request_id/:municipality', postNeighborhood);
|
app.post("/naselje/:request_id/:municipality", postNeighborhood);
|
||||||
|
|
||||||
app.get('/povrsina/:request_id', getSize);
|
app.get("/povrsina/:request_id", getSize);
|
||||||
app.post('/povrsina/:request_id', postSize);
|
app.post("/povrsina/:request_id", postSize);
|
||||||
|
|
||||||
app.get('/okucnica/:request_id', getGardenSize);
|
app.get("/okucnica/:request_id", getGardenSize);
|
||||||
app.post('/okucnica/:request_id', postGardenSize);
|
app.post("/okucnica/:request_id", postGardenSize);
|
||||||
|
|
||||||
app.get('/cijena/:request_id', getPrice);
|
app.get("/cijena/:request_id", getPrice);
|
||||||
app.post('/cijena/:request_id', postPrice);
|
app.post("/cijena/:request_id", postPrice);
|
||||||
|
|
||||||
app.get('/pregled/:request_id', getQueryReview);
|
app.get("/pregled/:request_id", getQueryReview);
|
||||||
app.post('/pregled/:request_id', postQueryReview);
|
app.post("/pregled/:request_id", postQueryReview);
|
||||||
|
|
||||||
app.get('/posalji/:request_id', getQuerySubmit);
|
app.get("/posalji/:request_id", getQuerySubmit);
|
||||||
app.post('/posalji/:request_id', postQuerySubmit);
|
app.post("/posalji/:request_id", postQuerySubmit);
|
||||||
|
|
||||||
app.get('/odjava/:request_id', getUnsubscribe);
|
app.get("/odjava/:request_id", getUnsubscribe);
|
||||||
|
|
||||||
app.get('/ponovo', getGoAgain);
|
app.get("/ponovo", getGoAgain);
|
||||||
|
|
||||||
app.get('/nekretnine/:request_id', getRealEstates);
|
app.get("/nekretnine/:request_id", getRealEstates);
|
||||||
|
|
||||||
app.use('/assets', express.static('./app/public'));
|
app.get("/redirect/:id", redirect);
|
||||||
|
|
||||||
|
app.use("/assets", express.static("./app/public"));
|
||||||
|
|
||||||
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
|
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
|
||||||
|
|
||||||
var rule = new schedule.RecurrenceRule();
|
var rule = new schedule.RecurrenceRule();
|
||||||
rule.seccond = 1;
|
rule.seccond = 1;
|
||||||
schedule.scheduleJob(rule, async function () {
|
schedule.scheduleJob(rule, async function() {
|
||||||
console.log(new Date(), 'Crawler service started');
|
console.log(new Date(), "Crawler service started");
|
||||||
await crawlAll();
|
await crawlAll();
|
||||||
console.log(new Date(), 'Crawler service finished, starting Notification service');
|
console.log(
|
||||||
await processNotifications();
|
new Date(),
|
||||||
console.log(new Date(), 'Notification service finished');
|
"Crawler service finished, starting Notification service"
|
||||||
});
|
);
|
||||||
|
await processNotifications();
|
||||||
/**
|
console.log(new Date(), "Notification service finished");
|
||||||
* Add flat method to Array
|
});
|
||||||
*/
|
|
||||||
Object.defineProperty(Array.prototype, 'flat', {
|
/**
|
||||||
value: function(depth = 1) {
|
* Add flat method to Array
|
||||||
return this.reduce(function (flat, toFlatten) {
|
*/
|
||||||
return flat.concat((Array.isArray(toFlatten) && (depth>1)) ? toFlatten.flat(depth-1) : toFlatten);
|
Object.defineProperty(Array.prototype, "flat", {
|
||||||
}, []);
|
value: function(depth = 1) {
|
||||||
}
|
return this.reduce(function(flat, toFlatten) {
|
||||||
|
return flat.concat(
|
||||||
|
Array.isArray(toFlatten) && depth > 1
|
||||||
|
? toFlatten.flat(depth - 1)
|
||||||
|
: toFlatten
|
||||||
|
);
|
||||||
|
}, []);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user