114 lines
3.7 KiB
JavaScript
114 lines
3.7 KiB
JavaScript
"use strict";
|
|
|
|
const { MAX_REAL_ESTATES_IN_EMAIL, APP_URL } = require("../config/appConfig");
|
|
const { AD_CATEGORY } = require("../common/enums");
|
|
|
|
const generateEmailFooter = searchRequestId => {
|
|
return `<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) => {
|
|
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 realEstateLinks = generateRealEstateLinks(realEstatesToShow);
|
|
const moreRealEstates = `<div>Kompletan spisak nekretnina možete pegledati na <a href="${allRealEstatesLink}">listi nekretnina</a><div>`;
|
|
const emailFooter = generateEmailFooter(searchRequestId);
|
|
|
|
return `<h3>Zdravo</h3>
|
|
<h4>Pronašli smo nekretnine koje odgovaraju Vašoj pretrazi</h4>
|
|
<div>
|
|
${realEstateLinks}
|
|
<div/>
|
|
${moreRealEstates}
|
|
</div>
|
|
<br/>
|
|
${emailFooter}`;
|
|
};
|
|
|
|
const generateNewSearchRequestEmail = (searchRequest, matchingRealEstates) => {
|
|
const realEstateType = AD_CATEGORY[searchRequest.realEstateType];
|
|
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);
|
|
|
|
return `<h3>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>Lokacija: </strong></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 `Kivi: ${singleRealEstateTitle}`;
|
|
}
|
|
|
|
const leastSignificantDigit = numberOfRealEstates % 10;
|
|
const numberWithoutLastDigit = Math.floor(numberOfRealEstates / 10);
|
|
const secondLeastSignificantDigit = numberWithoutLastDigit % 10;
|
|
|
|
if (leastSignificantDigit === 1 && secondLeastSignificantDigit !== 1) {
|
|
return `Kivi : ${numberOfRealEstates} nova nekretnina`;
|
|
}
|
|
|
|
if (
|
|
leastSignificantDigit >= 2 &&
|
|
leastSignificantDigit <= 4 &&
|
|
secondLeastSignificantDigit !== 1
|
|
) {
|
|
return `Kivi: ${numberOfRealEstates} nove nekretnine`;
|
|
}
|
|
|
|
return `Kivi: ${numberOfRealEstates} novih nekretnina`;
|
|
};
|
|
|
|
module.exports = {
|
|
generateNotificationEmail,
|
|
generateNewSearchRequestEmail,
|
|
generateEmailSubject
|
|
};
|