254 lines
8.1 KiB
JavaScript
254 lines
8.1 KiB
JavaScript
"use strict";
|
|
|
|
const {
|
|
MAX_REAL_ESTATES_IN_EMAIL,
|
|
APP_URL,
|
|
STAGING
|
|
} = require("../config/appConfig");
|
|
const { AD_CATEGORY, AD_TYPE, EMAIL_FREQUENCY } = require("../common/enums");
|
|
|
|
//Tag to recognize staging from development
|
|
const stagingTag = STAGING ? "[STAGING] " : "";
|
|
|
|
const generateEmailFooter = (searchRequestId, emailFrequencyTitle) => {
|
|
return ` <div>Trenutno ste prijavljeni da obavještenja o novim nekretninama primate <strong>${emailFrequencyTitle.toLowerCase()} </strong>.</div>
|
|
<div>Ako želite prestati dobijati obavještenja za ovu pretragu, <a href="${APP_URL}/odjava/${searchRequestId}">odjavite ovdje</a></div>
|
|
<div>Ako želite pogledati ili promijeniti uslove za ovu pretragu, <a href="${APP_URL}/pregled/${searchRequestId}">pogledajte ovdje</a></div>
|
|
<br/>
|
|
<strong>Vaš,<br/>Kivi tim</strong>`;
|
|
};
|
|
|
|
const generateRealEstateLinks = realEstates => {
|
|
let realEstateLinks = "";
|
|
for (const realEstate of realEstates) {
|
|
const { id: realEstateId, title } = realEstate;
|
|
|
|
realEstateLinks += `<li><a href="${APP_URL}/redirect/${realEstateId}">${title}</a></li>`;
|
|
}
|
|
return realEstateLinks;
|
|
};
|
|
|
|
const generateNotificationEmail = (
|
|
realEstates,
|
|
searchRequestId,
|
|
noAllRealEstates,
|
|
dailyNotification = false
|
|
) => {
|
|
const truncateList = realEstates.length > MAX_REAL_ESTATES_IN_EMAIL;
|
|
|
|
const realEstatesToShow = truncateList
|
|
? realEstates.slice(0, MAX_REAL_ESTATES_IN_EMAIL)
|
|
: realEstates;
|
|
|
|
const allRealEstatesLink = `${APP_URL}/nekretnine/${searchRequestId}`;
|
|
|
|
const emailFrequencyTitle = dailyNotification
|
|
? EMAIL_FREQUENCY.DAILY.title
|
|
: EMAIL_FREQUENCY.ASAP.title;
|
|
|
|
const realEstateLinks = generateRealEstateLinks(realEstatesToShow);
|
|
const moreRealEstates = `<div>Kompletan spisak nekretnina (${noAllRealEstates}) možete pogledati na <a href="${allRealEstatesLink}">listi nekretnina</a><div>`;
|
|
const emailFooter = generateEmailFooter(searchRequestId, emailFrequencyTitle);
|
|
const asapMessageBody =
|
|
realEstates.length > 1
|
|
? "Pronašli smo nekretnine koje odgovaraju Vašoj pretrazi"
|
|
: "Pronašli smo nekretninu koja odgovara Vašoj pretrazi";
|
|
|
|
const dailyMessageBody =
|
|
realEstates.length > 1
|
|
? "U posljednja 24h objavljene su sljedeće nekretnine koje odgovaraju uslovima Vaše pretrage"
|
|
: "U posljednja 24h objavljena je sljedeća nekretnina koja odgovara uslovima Vaše pretrage";
|
|
|
|
const messageBody = dailyNotification ? dailyMessageBody : asapMessageBody;
|
|
|
|
return `<h3>${stagingTag}Zdravo</h3>
|
|
<h4>${messageBody}</h4>
|
|
<div>
|
|
${realEstateLinks}
|
|
<div/>
|
|
${moreRealEstates}
|
|
</div>
|
|
<br/>
|
|
${emailFooter}`;
|
|
};
|
|
|
|
const generateNewSearchRequestEmail = (searchRequest, matchingRealEstates) => {
|
|
const realEstateType = AD_CATEGORY[searchRequest.realEstateType];
|
|
let adTypeTitle = "";
|
|
switch (searchRequest.adType) {
|
|
case AD_TYPE.AD_TYPE_SALE.stringId:
|
|
adTypeTitle = AD_TYPE.AD_TYPE_SALE.title;
|
|
break;
|
|
case AD_TYPE.AD_TYPE_RENT.stringId:
|
|
adTypeTitle = AD_TYPE.AD_TYPE_RENT.title;
|
|
break;
|
|
default:
|
|
adTypeTitle = "-";
|
|
break;
|
|
}
|
|
let emailFrequencyTitle;
|
|
switch (searchRequest.emailFrequency) {
|
|
case EMAIL_FREQUENCY.ASAP.stringId:
|
|
emailFrequencyTitle = EMAIL_FREQUENCY.ASAP.title;
|
|
break;
|
|
case EMAIL_FREQUENCY.DAILY.stringId:
|
|
emailFrequencyTitle = EMAIL_FREQUENCY.DAILY.title;
|
|
break;
|
|
}
|
|
|
|
const {
|
|
id,
|
|
gardenSizeMin,
|
|
gardenSizeMax,
|
|
sizeMin,
|
|
sizeMax,
|
|
priceMin,
|
|
priceMax
|
|
} = searchRequest;
|
|
|
|
const realEstateLinks = generateRealEstateLinks(matchingRealEstates);
|
|
|
|
const instantRealEstatesText = `<br/>
|
|
<div>
|
|
U međuvremenu pogledajte neke od nedavno objavljenih nekretnina koje odgovaraju Vašim uslovima pretrage :<br/>
|
|
${realEstateLinks}
|
|
</div>`;
|
|
|
|
const gardenSize = realEstateType.hasGardenSize
|
|
? `<div><strong>Kvadratura okućnice: Od ${gardenSizeMin} do ${gardenSizeMax} m2</strong></div>`
|
|
: ``;
|
|
|
|
const emailFooter = generateEmailFooter(id, emailFrequencyTitle);
|
|
|
|
return `<h3>${stagingTag}Zdravo</h3>
|
|
<div>Naručili ste da Vam javimo ako se nekretnina sa navedenim uslovima pojavi u oglasima:</div>
|
|
<br/>
|
|
<div>
|
|
<div><strong>Tip nekretnine: </strong>${realEstateType.title}</div>
|
|
<div><strong>Vrsta oglasa: </strong>${adTypeTitle}</div>
|
|
<div><strong>Kvadratura nekretnine:</strong> Od ${sizeMin} do ${sizeMax} m2</div>
|
|
${gardenSize}
|
|
<div><strong>Cijena:</strong> ${priceMin} do ${priceMax} KM</div>
|
|
</div>
|
|
${matchingRealEstates.length > 0 ? instantRealEstatesText : ""}
|
|
<br/>
|
|
${emailFooter}`;
|
|
};
|
|
|
|
const generateEmailSubject = (numberOfRealEstates, singleRealEstateTitle) => {
|
|
if (numberOfRealEstates === 1) {
|
|
return `${stagingTag}Kivi: ${singleRealEstateTitle}`;
|
|
}
|
|
|
|
const leastSignificantDigit = numberOfRealEstates % 10;
|
|
const numberWithoutLastDigit = Math.floor(numberOfRealEstates / 10);
|
|
const secondLeastSignificantDigit = numberWithoutLastDigit % 10;
|
|
|
|
if (leastSignificantDigit === 1 && secondLeastSignificantDigit !== 1) {
|
|
return `${stagingTag}Kivi : ${numberOfRealEstates} nova nekretnina`;
|
|
}
|
|
|
|
if (
|
|
leastSignificantDigit >= 2 &&
|
|
leastSignificantDigit <= 4 &&
|
|
secondLeastSignificantDigit !== 1
|
|
) {
|
|
return `${stagingTag}Kivi: ${numberOfRealEstates} nove nekretnine`;
|
|
}
|
|
|
|
return `${stagingTag}Kivi: ${numberOfRealEstates} novih nekretnina`;
|
|
};
|
|
|
|
const generateCheckUpEmail = searchRequest => {
|
|
const realEstateType = AD_CATEGORY[searchRequest.realEstateType];
|
|
const {
|
|
id,
|
|
gardenSizeMin,
|
|
gardenSizeMax,
|
|
sizeMin,
|
|
sizeMax,
|
|
priceMin,
|
|
priceMax
|
|
} = searchRequest;
|
|
|
|
let emailFrequencyTitle;
|
|
switch (searchRequest.emailFrequency) {
|
|
case EMAIL_FREQUENCY.ASAP.stringId:
|
|
emailFrequencyTitle = EMAIL_FREQUENCY.ASAP.title;
|
|
break;
|
|
case EMAIL_FREQUENCY.DAILY.stringId:
|
|
emailFrequencyTitle = EMAIL_FREQUENCY.DAILY.title;
|
|
break;
|
|
}
|
|
|
|
const gardenSize = realEstateType.hasGardenSize
|
|
? `<div><strong>Kvadratura okućnice: Od ${gardenSizeMin} do ${gardenSizeMax} m2</strong></div>`
|
|
: ``;
|
|
|
|
const emailFooter = generateEmailFooter(id, emailFrequencyTitle);
|
|
|
|
return `<h3>${stagingTag}Zdravo</h3>
|
|
<div><strong>Kivi tim traži nekretnine za Vas i kada to ne vidite.</strong></div>
|
|
<br />
|
|
<div>Vaša trenutno aktivna pretraga je:</div>
|
|
<br/>
|
|
<div>
|
|
<div><strong>Tip nekretnine: </strong>${realEstateType.title}</div>
|
|
<div><strong>Kvadratura nekretnine:</strong> Od ${sizeMin} do ${sizeMax} m2</div>
|
|
${gardenSize}
|
|
<div><strong>Cijena:</strong> ${priceMin} do ${priceMax} KM</div>
|
|
</div>
|
|
<br/>
|
|
${emailFooter}`;
|
|
};
|
|
|
|
const generateNewAdPublishEmail = (
|
|
realEstate,
|
|
kiviOriginal,
|
|
editingRealEstate,
|
|
numberOfMatchingRequests
|
|
) => {
|
|
let countingPrefix;
|
|
if (
|
|
numberOfMatchingRequests === 2 ||
|
|
numberOfMatchingRequests === 3 ||
|
|
numberOfMatchingRequests === 4
|
|
) {
|
|
countingPrefix = "postoje";
|
|
} else {
|
|
countingPrefix = "postoji";
|
|
}
|
|
let countingSufix;
|
|
if (numberOfMatchingRequests % 10 === 1 && numberOfMatchingRequests !== 11) {
|
|
countingSufix = "zahtjev";
|
|
} else {
|
|
countingSufix = "zahtjeva";
|
|
}
|
|
|
|
const successIntro = editingRealEstate
|
|
? `<div>Uspješno ste izmijenili oglas za Vašu nekretninu na <strong>Kivi.ba.</strong><div>`
|
|
: `<div>Uspješno ste objavili oglas za Vašu nekretninu na <strong>Kivi.ba.</strong><div>`;
|
|
|
|
return `<h3>${stagingTag}Zdravo</h3>
|
|
${successIntro}
|
|
<br/>
|
|
<div>U Kivi bazi trenutno ${countingPrefix} ${numberOfMatchingRequests} ${countingSufix} za nekretninom kao sto je Vaša.</div>
|
|
<br />
|
|
<div>Pregledajte Vaš oglas na sljedećem linku: <a href="${realEstate.url}" rel="noreferrer">${realEstate.title}</a><div>
|
|
<br/>
|
|
<div>Ako želite izmijeniti detalje oglasa, <a href="${APP_URL}/podacionekretnini/${kiviOriginal.kiviAdId}">izmjenite ovdje.</a></div>
|
|
<div>Ako želite izbrisati Vaš oglas iz Kivi baze, <a href="${APP_URL}/obrisioglas/${kiviOriginal.kiviAdId}">izbrišite ovdje.</a></div>
|
|
<br/>
|
|
<div>Hvala na ukazanom povjerenju!</div>
|
|
<br/>
|
|
<strong>Vaš,<br/>Kivi tim</strong>`;
|
|
};
|
|
|
|
module.exports = {
|
|
generateNotificationEmail,
|
|
generateNewSearchRequestEmail,
|
|
generateEmailSubject,
|
|
generateCheckUpEmail,
|
|
generateNewAdPublishEmail
|
|
};
|