2019-09-30 09:51:10 +02:00
"use strict" ;
const { MAX _REAL _ESTATES _IN _EMAIL , APP _URL } = require ( "../config/appConfig" ) ;
2020-01-13 14:58:09 +01:00
const { AD _CATEGORY , AD _TYPE , EMAIL _FREQUENCY } = require ( "../common/enums" ) ;
2019-09-30 09:51:10 +02:00
2020-01-13 14:58:09 +01:00
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 > < / d i v >
2019-09-30 12:06:35 +02:00
< div > Ako želite pogledati ili promijeniti uslove za ovu pretragu , < a href = "${APP_URL}/pregled/${searchRequestId}" > pogledajte ovdje < / a > < / d i v >
2019-09-30 09:51:10 +02:00
< br / >
2019-10-11 23:07:45 +02:00
< strong > Vaš , < br / > Kivi tim < / s t r o n g > ` ;
2019-09-30 09:51:10 +02:00
} ;
2019-10-07 20:49:54 +02:00
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 ;
} ;
2019-11-04 14:28:11 +01:00
const generateNotificationEmail = (
realEstates ,
searchRequestId ,
2020-01-06 23:59:56 +01:00
noAllRealEstates ,
2019-11-04 14:28:11 +01:00
dailyNotification = false
) => {
2019-09-30 09:51:10 +02:00
const truncateList = realEstates . length > MAX _REAL _ESTATES _IN _EMAIL ;
2020-01-06 23:59:56 +01:00
2019-09-30 09:51:10 +02:00
const realEstatesToShow = truncateList
? realEstates . slice ( 0 , MAX _REAL _ESTATES _IN _EMAIL )
: realEstates ;
const allRealEstatesLink = ` ${ APP _URL } /nekretnine/ ${ searchRequestId } ` ;
2020-01-06 23:59:56 +01:00
2020-01-13 14:58:09 +01:00
const emailFrequencyTitle = dailyNotification
? EMAIL _FREQUENCY . DAILY . title
: EMAIL _FREQUENCY . ASAP . title ;
2019-10-07 20:49:54 +02:00
const realEstateLinks = generateRealEstateLinks ( realEstatesToShow ) ;
2020-01-06 23:59:56 +01:00
const moreRealEstates = ` <div>Kompletan spisak nekretnina ( ${ noAllRealEstates } ) možete pogledati na <a href=" ${ allRealEstatesLink } ">listi nekretnina</a><div> ` ;
2020-01-13 14:58:09 +01:00
const emailFooter = generateEmailFooter ( searchRequestId , emailFrequencyTitle ) ;
2019-11-04 14:28:11 +01:00
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 ;
2019-09-30 09:51:10 +02:00
2019-09-30 12:06:35 +02:00
return ` <h3>Zdravo</h3>
2019-11-04 14:28:11 +01:00
< h4 > $ { messageBody } < / h 4 >
2019-09-30 09:51:10 +02:00
< div >
$ { realEstateLinks }
< div / >
$ { moreRealEstates }
< / d i v >
< br / >
$ { emailFooter } ` ;
} ;
2019-10-07 20:49:54 +02:00
const generateNewSearchRequestEmail = ( searchRequest , matchingRealEstates ) => {
2019-09-30 09:51:10 +02:00
const realEstateType = AD _CATEGORY [ searchRequest . realEstateType ] ;
2020-01-13 14:58:09 +01:00
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 ;
}
2019-09-30 09:51:10 +02:00
const {
id ,
gardenSizeMin ,
gardenSizeMax ,
sizeMin ,
sizeMax ,
priceMin ,
priceMax
} = searchRequest ;
2019-10-07 20:49:54 +02:00
const realEstateLinks = generateRealEstateLinks ( matchingRealEstates ) ;
2020-01-06 23:59:56 +01:00
2019-10-07 20:49:54 +02:00
const instantRealEstatesText = ` <br/>
< div >
U međuvremenu pogledajte neke od nedavno objavljenih nekretnina koje odgovaraju Vašim uslovima pretrage : < br / >
$ { realEstateLinks }
< / d i v > ` ;
2019-09-30 09:51:10 +02:00
const gardenSize = realEstateType . hasGardenSize
2019-10-07 20:49:54 +02:00
? ` <div><strong>Kvadratura okućnice: Od ${ gardenSizeMin } do ${ gardenSizeMax } m2</strong></div> `
2019-09-30 09:51:10 +02:00
: ` ` ;
2020-01-13 14:58:09 +01:00
const emailFooter = generateEmailFooter ( id , emailFrequencyTitle ) ;
2019-09-30 09:51:10 +02:00
2019-09-30 12:06:35 +02:00
return ` <h3>Zdravo</h3>
< div > Naručili ste da Vam javimo ako se nekretnina sa navedenim uslovima pojavi u oglasima : < / d i v >
< br / >
2019-09-30 09:51:10 +02:00
< div >
< div > < strong > Tip nekretnine : < / s t r o n g > $ { r e a l E s t a t e T y p e . t i t l e } < / d i v >
2020-01-13 14:58:09 +01:00
< div > < strong > Vrsta oglasa : < / s t r o n g > $ { a d T y p e T i t l e } < / d i v >
2019-09-30 09:51:10 +02:00
< div > < strong > Kvadratura nekretnine : < / s t r o n g > O d $ { s i z e M i n } d o $ { s i z e M a x } m 2 < / d i v >
$ { gardenSize }
< div > < strong > Cijena : < / s t r o n g > $ { p r i c e M i n } d o $ { p r i c e M a x } K M < / d i v >
< / d i v >
2019-10-07 20:49:54 +02:00
$ { matchingRealEstates . length > 0 ? instantRealEstatesText : "" }
2019-09-30 09:51:10 +02:00
< br / >
$ { emailFooter } ` ;
} ;
2019-10-11 23:07:45 +02:00
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 ` ;
} ;
2019-09-30 09:51:10 +02:00
module . exports = {
generateNotificationEmail ,
2019-10-11 23:07:45 +02:00
generateNewSearchRequestEmail ,
generateEmailSubject
2019-09-30 09:51:10 +02:00
} ;