Send notification email
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
|
||||
const dotenv = require('dotenv');
|
||||
dotenv.config();
|
||||
const dotenv = require('dotenv').config();
|
||||
const { getRealEstateTypeEnum } = require('./enums');
|
||||
const { getRegionName, getMunicipalityName } = require('./codes');
|
||||
const AWS = require('aws-sdk');
|
||||
var AWS = require('aws-sdk');
|
||||
const TEMPLATE_NAME = "MarketAlertTemplate"
|
||||
|
||||
AWS.config.update({
|
||||
region: process.env.AMAZON_REGION,
|
||||
credentials:
|
||||
@@ -77,13 +78,13 @@ Javimi tim.
|
||||
|
||||
const getEmaiTextVersion = (realestateRequest) => {
|
||||
const realEstateType = getRealEstateTypeEnum(realestateRequest.realEstateType);
|
||||
const gardenSize = realEstateType.hasGardenSize ? "Kvadratura okućnice od " + realestateRequest.gardenSizeMin + " do " + realestateRequest.gardenSizeMax : ""
|
||||
const gardenSize = realEstateType.hasGardenSize ? "Kvadratura okućnice od " + realestateRequest.gardenSizeMin + " do " + realestateRequest.gardenSizeMax : ""
|
||||
|
||||
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: "
|
||||
+ realestateRequest.realEstateType + "\n Područje" + getRegionName(realestateRequest.region) + "\n Mjesto " + getMunicipalityName(realestateRequest.region, realestateRequest.municipality)
|
||||
+ "\n Kvadratura nekretnine Od " + realestateRequest.sizeMin + " do " + realestateRequest.sizeMaX +
|
||||
+ gardenSize
|
||||
"\n Cijena od " + realestateRequest.priceMin + " do " + realestateRequest.priceMax +
|
||||
"\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"
|
||||
@@ -91,6 +92,94 @@ const getEmaiTextVersion = (realestateRequest) => {
|
||||
return text;
|
||||
}
|
||||
|
||||
const sendBulkEmail = async (marketAlerts) => {
|
||||
|
||||
try {
|
||||
// Create the promise and SES service object
|
||||
// const templatePromise = new AWS.SES({ apiVersion: '2010-12-01' }).getTemplate({ TemplateName: TEMPLATE_NAME }).promise();
|
||||
// const template = await templatePromise;
|
||||
// console.log(template);
|
||||
|
||||
destinations = []
|
||||
|
||||
marketAlerts.forEach(marketAlert => {
|
||||
destinations.push({
|
||||
Destination: {
|
||||
ToAddresses: [
|
||||
marketAlert.email
|
||||
]
|
||||
},
|
||||
ReplacementTemplateData: `{ "marketAlertUrl":"${marketAlert.url}", "favoriteanimal":"yak" }`
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
|
||||
// Create sendBulkTemplatedEmail params
|
||||
var params = {
|
||||
Destinations: /* required */
|
||||
// {
|
||||
// Destination: { /* required */
|
||||
// CcAddresses: [
|
||||
// 'EMAIL_ADDRESS',
|
||||
// /* more items */
|
||||
// ],
|
||||
// ToAddresses: [
|
||||
// 'EMAIL_ADDRESS',
|
||||
// 'EMAIL_ADDRESS'
|
||||
// /* more items */
|
||||
// ]
|
||||
// },
|
||||
// ReplacementTemplateData: '{ \"REPLACEMENT_TAG_NAME\":\"REPLACEMENT_VALUE\" }'
|
||||
// },
|
||||
// ],
|
||||
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();
|
||||
|
||||
await sendPromise;
|
||||
|
||||
|
||||
} catch (e) {
|
||||
console.log("Could not send bulk email", e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const createMarketAlertEmailTemplate = async () => {
|
||||
const marketAlertTemplate = {
|
||||
Template: {
|
||||
TemplateName: "MarketAlertTemplate",
|
||||
SubjectPart: "Greetings",
|
||||
TextPart: "Dear ,\r\nYour favorite animal is {{marketAlertUrl}}.",
|
||||
HtmlPart: "<h1>Hello </h1><p>Your favorite animal is {{marketAlertUrl}}.</p>"
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const templatePromise = new AWS.SES({ apiVersion: '2010-12-01' }).createTemplate(marketAlertTemplate).promise();
|
||||
await templatePromise
|
||||
|
||||
} catch (e) {
|
||||
console.log("Could not create MarketAlertEmailTemplate", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = {
|
||||
sendTemplatedEmail
|
||||
sendTemplatedEmail,
|
||||
sendBulkEmail,
|
||||
createMarketAlertEmailTemplate
|
||||
};
|
||||
|
||||
@@ -62,4 +62,3 @@ async function crawlAll() {
|
||||
};
|
||||
|
||||
crawlAll();
|
||||
|
||||
|
||||
@@ -2,18 +2,21 @@
|
||||
const Promise = require("bluebird");
|
||||
const db = require("../models/index");
|
||||
const { allMarketAlerts } = require('../helpers/db/dbHelper');
|
||||
const { createMarketAlertEmailTemplate, sendBulkEmail } = require('../helpers/awsEmail');
|
||||
|
||||
|
||||
async function processNotifications() {
|
||||
|
||||
try {
|
||||
const marketAlerts = await allMarketAlerts(false);
|
||||
console.log(marketAlerts);
|
||||
// await createMarketAlertEmailTemplate();
|
||||
await sendBulkEmail(marketAlerts);
|
||||
// console.log(marketAlerts);
|
||||
|
||||
process.exit();
|
||||
} catch (e) {
|
||||
console.log("NOTIFICATION SERVICE: could not send notifications reason: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
processNotifications();
|
||||
processNotifications();
|
||||
|
||||
Reference in New Issue
Block a user