add emailService to handle email sending
This commit is contained in:
@@ -1,246 +0,0 @@
|
||||
const { APP_URL } = require("../config/appConfig");
|
||||
const { AD_CATEGORY } = require("../common/enums");
|
||||
const { getRegionName, getMunicipalityName } = require("./codes");
|
||||
const { allRERequestByUiid } = require("./db/dbHelper");
|
||||
let AWS = require("aws-sdk");
|
||||
const TEMPLATE_NAME = "MarketAlertTemplate";
|
||||
|
||||
AWS.config.update({
|
||||
region: process.env.AMAZON_REGION,
|
||||
credentials: {
|
||||
accessKeyId: process.env.AMAZON_ACCES_KEY_ID,
|
||||
secretAccessKey: process.env.AMAZON_SECRET_ACCESS_KEY
|
||||
}
|
||||
});
|
||||
|
||||
const sendTemplatedEmail = async (email, request) => {
|
||||
const params = {
|
||||
Destination: {
|
||||
/* required */
|
||||
CcAddresses: [],
|
||||
ToAddresses: [email]
|
||||
},
|
||||
Message: {
|
||||
/* required */
|
||||
Body: {
|
||||
/* required */
|
||||
Html: {
|
||||
Charset: "UTF-8",
|
||||
Data: getGreetingsEmailHTML(request)
|
||||
},
|
||||
Text: {
|
||||
Charset: "UTF-8",
|
||||
Data: getGreetingsEmailTextVersion(request)
|
||||
}
|
||||
},
|
||||
Subject: {
|
||||
Charset: "UTF-8",
|
||||
Data: `Javimi Potvrda: ${getSubject(request.realEstateType)}`
|
||||
}
|
||||
},
|
||||
Source: process.env.SOURCE_EMAIL /* required */,
|
||||
ReplyToAddresses: [process.env.SOURCE_EMAIL]
|
||||
};
|
||||
|
||||
const sendEmailPromise = new AWS.SES({ apiVersion: "2010-12-01" })
|
||||
.sendEmail(params)
|
||||
.promise();
|
||||
await sendEmailPromise;
|
||||
};
|
||||
|
||||
const getGreetingsEmailHTML = realEstateRequest => {
|
||||
const realEstateType = AD_CATEGORY[realEstateRequest.realEstateType];
|
||||
|
||||
const gardenSize = realEstateType.hasGardenSize
|
||||
? `<div><strong>Kvadratura okućnice: Od ${realEstateRequest.gardenSizeMin} do ${realEstateRequest.gardenSizeMax} m2 </strong></div>`
|
||||
: ``;
|
||||
|
||||
return `<h1> Zdravo,
|
||||
Naručio/la si da ti javimo ako se nekretnina pojavi u oglasima. </h1>
|
||||
<h2> Ovo je tražena nekretnina: </h2>
|
||||
<div>
|
||||
<div> <strong>Tip nekretnine: ${realEstateType.title} </strong></div>
|
||||
<div><strong>Lokacija: </strong></div>
|
||||
<div><strong>Kvadratura nekretnine: Od ${realEstateRequest.sizeMin} do ${realEstateRequest.sizeMax} m2 </strong></div>
|
||||
${gardenSize}
|
||||
<div><strong>Cijena: ${realEstateRequest.priceMin} do ${realEstateRequest.priceMax} KM </strong></div>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
</div>
|
||||
<div><strong> Ako želis prestati dobijati obavještenja za ovu pretragu klikni ${APP_URL}/odjava/${realEstateRequest.id} </strong></div>
|
||||
<div><strong>Ako želiš promijeniti uslove pretrage klikni ${APP_URL}/pregled/${realEstateRequest.id} </strong></div>
|
||||
<h4> Tvoj,
|
||||
Javimi tim.
|
||||
</h4>`;
|
||||
};
|
||||
|
||||
const getGreetingsEmailTextVersion = realEstateRequest => {
|
||||
const realEstateType = AD_CATEGORY[realEstateRequest.realEstateType];
|
||||
const gardenSize = realEstateType.hasGardenSize
|
||||
? `Kvadratura okućnice od ${realEstateRequest.gardenSizeMin} do ${realEstateRequest.gardenSizeMax}`
|
||||
: "";
|
||||
|
||||
return `Zdravo\nNaručio/la si da ti javimo ako se nekretnina pojavi u oglasima\n
|
||||
Ovo je tražena nekretnina:\nTip nekretnine: ${realEstateRequest.realEstateType}\n
|
||||
Lokacija nekretnine :\n
|
||||
Kvadratura nekretnine Od ${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
|
||||
${APP_URL}/odjava/${realEstateRequest.id}\n
|
||||
Ako želiš promijeniti uslove pretrage klikni
|
||||
${APP_URL}/odpregled/${realEstateRequest.id}\n
|
||||
Tvoj,\n Javimi tim`;
|
||||
};
|
||||
|
||||
const sendBulkEmail = async marketAlerts => {
|
||||
try {
|
||||
destinations = [];
|
||||
groupedRERequests = [];
|
||||
|
||||
const RERequestUuidsMaped = marketAlerts.map(
|
||||
marketAlert => marketAlert.request
|
||||
);
|
||||
|
||||
const RERequestUuidsArray = Array.from(new Set(RERequestUuidsMaped));
|
||||
|
||||
const RERequestUuids = RERequestUuidsArray.map(marketAlert => {
|
||||
return { id: marketAlert };
|
||||
});
|
||||
|
||||
const RERequests = await allRERequestByUiid(RERequestUuids);
|
||||
const requestDataValues = [];
|
||||
|
||||
RERequests.forEach(RERequest => {
|
||||
var formatedRequest = {};
|
||||
formatedRequest[RERequest.id] = requestDataValues[RERequest.id] = {
|
||||
realEstateType: RERequest.realEstateType,
|
||||
region: RERequest.region,
|
||||
municipality: RERequest.municipality,
|
||||
requestUrl: `${APP_URL}/nekretnine/${RERequest.id}`
|
||||
};
|
||||
});
|
||||
|
||||
marketAlerts.forEach(marketAlert => {
|
||||
const requestObject = {
|
||||
email: marketAlert.email,
|
||||
realEstateType: requestDataValues[marketAlert.request].realEstateType,
|
||||
municipality: requestDataValues[marketAlert.request].municipality,
|
||||
region: requestDataValues[marketAlert.request].region,
|
||||
requestUrl: requestDataValues[marketAlert.request].requestUrl
|
||||
};
|
||||
|
||||
if (!groupedRERequests[marketAlert.request]) {
|
||||
groupedRERequests[marketAlert.request] = {
|
||||
requestObject: requestObject,
|
||||
marketAlertArray: []
|
||||
};
|
||||
}
|
||||
|
||||
if (groupedRERequests[marketAlert.request].marketAlertArray.length < 10) {
|
||||
groupedRERequests[marketAlert.request].marketAlertArray.push({
|
||||
url: marketAlert.url,
|
||||
title: marketAlert.title,
|
||||
id: marketAlert.id
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
for (request in groupedRERequests) {
|
||||
const marketAlert = groupedRERequests[request];
|
||||
let extractedData = toAWSArray(marketAlert.marketAlertArray);
|
||||
const realEstateType =
|
||||
AD_CATEGORY[marketAlert.requestObject.realEstateType].title || "-";
|
||||
const region = getRegionName(marketAlert.requestObject.region);
|
||||
const municipality = getMunicipalityName(
|
||||
marketAlert.requestObject.region,
|
||||
marketAlert.requestObject.municipality
|
||||
);
|
||||
const requestUrl = marketAlert.requestObject.requestUrl;
|
||||
|
||||
let repData = `{ "marketAlertUrl":[${extractedData}], "realestateType":"${realEstateType}", "region":"${region}", "municipality":"${municipality}", "requestUrl":"${requestUrl}" }`;
|
||||
|
||||
destinations.push({
|
||||
Destination: {
|
||||
ToAddresses: [marketAlert.requestObject.email]
|
||||
},
|
||||
ReplacementTemplateData: repData
|
||||
});
|
||||
}
|
||||
|
||||
var params = {
|
||||
Destinations: destinations,
|
||||
Source: process.env.SOURCE_EMAIL /* required */,
|
||||
Template: TEMPLATE_NAME /* required */,
|
||||
DefaultTemplateData: '{ "REPLACEMENT_TAG_NAME":"REPLACEMENT_VALUE" }',
|
||||
ReplyToAddresses: [process.env.SOURCE_EMAIL]
|
||||
};
|
||||
|
||||
// Create the promise and SES service object
|
||||
const sendPromise = new AWS.SES({ apiVersion: "2010-12-01" })
|
||||
.sendBulkTemplatedEmail(params)
|
||||
.promise();
|
||||
const awsResult = await sendPromise;
|
||||
} catch (e) {
|
||||
console.log("Could not send bulk email", e);
|
||||
}
|
||||
};
|
||||
|
||||
const redirectUrl = marketAlertId => `${APP_URL}/redirect/${marketAlertId}`;
|
||||
const toAWSArray = urlArray => {
|
||||
let arrayString = "";
|
||||
urlArray.forEach(element => {
|
||||
const formatetdTitle = element.title.replace(/"/g, "");
|
||||
arrayString =
|
||||
arrayString +
|
||||
`{"url":"${redirectUrl(element.id)}" , "title":"${formatetdTitle}"},`;
|
||||
});
|
||||
return arrayString.slice(0, -1);
|
||||
};
|
||||
|
||||
const getNotificationEmailHtml = () => {
|
||||
return `<h2> Zdravo,
|
||||
Pronašli smo nekretninu koju ste tražili. </h2>
|
||||
<h3> Ovo su tražene nekretnine: </h3>
|
||||
<div>
|
||||
<div>{{#each marketAlertUrl}}<li><a href="{{url}}">{{title}}</a></li><br />{{/each}}<div/>
|
||||
<div/>
|
||||
<div>Kompletan spisak nekretnina možete pegledati ovdije: <a href="{{requestUrl}}">Nekretnine</a> <div>
|
||||
</div>`;
|
||||
};
|
||||
|
||||
const getNotificationEmailText = () => {
|
||||
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}}`;
|
||||
};
|
||||
|
||||
const createMarketAlertEmailTemplate = async () => {
|
||||
const marketAlertTemplate = {
|
||||
Template: {
|
||||
TemplateName: TEMPLATE_NAME,
|
||||
SubjectPart: "Javi mi obavijest: {{realestateType}}",
|
||||
TextPart: getNotificationEmailText(),
|
||||
HtmlPart: getNotificationEmailHtml()
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
const templatePromise = new AWS.SES({ apiVersion: "2010-12-01" })
|
||||
.updateTemplate(marketAlertTemplate)
|
||||
.promise();
|
||||
await templatePromise;
|
||||
} catch (e) {
|
||||
console.log("Could not create MarketAlertEmailTemplate", e);
|
||||
}
|
||||
};
|
||||
|
||||
const getSubject = realEstateType => {
|
||||
return AD_CATEGORY[realEstateType].title || "-";
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
sendTemplatedEmail,
|
||||
sendBulkEmail,
|
||||
createMarketAlertEmailTemplate
|
||||
};
|
||||
56
app/services/emailService.js
Normal file
56
app/services/emailService.js
Normal file
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
|
||||
let AWS = require("aws-sdk");
|
||||
const htmlToText = require("html-to-text");
|
||||
|
||||
const { AWS_EMAIL_CONFIG } = require("../config/appConfig");
|
||||
|
||||
AWS.config.update({
|
||||
region: AWS_EMAIL_CONFIG.REGION,
|
||||
credentials: {
|
||||
accessKeyId: AWS_EMAIL_CONFIG.CREDENTIALS.ACCESS_KEY_ID,
|
||||
secretAccessKey: AWS_EMAIL_CONFIG.CREDENTIALS.SECRET_ACCESS_KEY
|
||||
}
|
||||
});
|
||||
|
||||
const awsMailer = new AWS.SES({ apiVersion: "2010-12-01" });
|
||||
|
||||
const sendEmail = async (to, subject, message, from) => {
|
||||
const params = {
|
||||
Destination: {
|
||||
ToAddresses: [to]
|
||||
},
|
||||
Message: {
|
||||
Subject: {
|
||||
Charset: "UTF-8",
|
||||
Data: subject
|
||||
},
|
||||
Body: {
|
||||
Html: {
|
||||
Charset: "UTF-8",
|
||||
Data: message
|
||||
},
|
||||
Text: {
|
||||
Charset: "UTF-8",
|
||||
Data: htmlToText.fromString(message)
|
||||
}
|
||||
}
|
||||
},
|
||||
ReturnPath: from ? from : AWS_EMAIL_CONFIG.SOURCE_EMAIL,
|
||||
Source: from ? from : AWS_EMAIL_CONFIG.SOURCE_EMAIL
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
awsMailer.sendEmail(params, (error, data) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
sendEmail
|
||||
};
|
||||
Reference in New Issue
Block a user