Geocoding restricttions, added values for range finders, added confirmation email, and .env file #14

Merged
nedimu merged 3 commits from confirmation-email into master 2019-06-11 10:42:07 +02:00
15 changed files with 328 additions and 125 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
node_modules/
.env

View File

@@ -2,6 +2,8 @@ const { currentRERequest } = require('../helpers/url');
const { getRealEstateTypeEnum } = require('../helpers/enums');
const getGardenSize = (req,res) => {
const unit = " m2"
const rangeFrom = {
min : 10,
max : 3000,
@@ -15,7 +17,8 @@ const getGardenSize = (req,res) => {
value : 100,
step : 10
}
res.render('gardenSize', { rangeFrom, rangeTo });
res.render('gardenSize', { rangeFrom, rangeTo, unit });
};
const postGardenSize = async (req, res) => {

View File

@@ -1,6 +1,8 @@
const { currentRERequest } = require('../helpers/url');
const getPrice = (req,res) => {
const unit = " KM"
const rangeFrom = {
min : 1000,
max : 250000,
@@ -16,7 +18,7 @@ const getPrice = (req,res) => {
}
res.render('price', {rangeFrom, rangeTo });
res.render('price', {rangeFrom, rangeTo, unit });
};
const postPrice = async (req, res) => {

View File

@@ -1,5 +1,6 @@
const { currentRERequest } = require('../helpers/url');
const { isValidEmail } = require('../helpers/email');
const { sendTemplatedEmail} = require('../helpers/awsEmail');
const getQuerySubmit = async (req, res) => {
const nextStep = req.query.nextStep;
@@ -38,7 +39,9 @@ const postQuerySubmit = async (req, res) => {
}
request.email = req.body.email;
request.subscribed = true;
await request.save();
sendTemplatedEmail(req.body.email, request);
res.redirect(nextStep);
};

View File

@@ -2,6 +2,8 @@ const { currentRERequest } = require('../helpers/url');
const { sizes, getRealEstateTypeEnum } = require('../helpers/enums');
const getSize = (req,res) => {
const unit = " m2"
const rangeFrom = {
min : 10,
max : 250,
@@ -16,7 +18,7 @@ const getSize = (req,res) => {
step : 10
}
res.render('size', { rangeFrom, rangeTo });
res.render('size', { rangeFrom, rangeTo, unit });
};
const postSize = async (req, res) => {

View File

@@ -0,0 +1,15 @@
const { currentRERequest } = require('../helpers/url');
const getUnsubscribe = async (req, res) => {
const request = await currentRERequest(req);
request.subscribed = false;
await request.save();
res.render('unsubscribe', { nextStep: '/vrstanekretnine' });
};
module.exports = {
getUnsubscribe
};

96
app/helpers/awsEmail.js Normal file
View File

@@ -0,0 +1,96 @@
const dotenv = require('dotenv');
dotenv.config();
const { getRealEstateTypeEnum } = require('./enums');
const { getRegionName, getMunicipalityName } = require('./codes');
const AWS = require('aws-sdk');
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: getEmailHTML(request)
},
Text: {
Charset: "UTF-8",
Data: getEmaiTextVersion(request)
}
},
Subject: {
Charset: 'UTF-8',
Data: `Javimi Potvrda: ${request.realEstateType} ${getRegionName(request.region)}, ${getMunicipalityName(request.region, request.municipality)}`
}
},
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 getEmailHTML = (realestateRequest) => {
const realEstateType = getRealEstateTypeEnum(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>Područje: ${getRegionName(realestateRequest.region)} </strong></div>
<div><strong>Mjesto: ${getMunicipalityName(realestateRequest.region, realestateRequest.municipality)} </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 ${process.env.APP_URL}/odjava/${realestateRequest.uniqueId} </strong></div>
<div><strong>Ako želiš promijeniti uslove pretrage klikni ${process.env.APP_URL}/pregled/${realestateRequest.uniqueId} </strong></div>
<h4> Tvoj,
Javimi tim.
</h4>`
}
const getEmaiTextVersion = (realestateRequest) => {
const realEstateType = getRealEstateTypeEnum(realestateRequest.realEstateType);
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 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"
return text;
}
module.exports = {
sendTemplatedEmail
};

View File

@@ -1,7 +1,7 @@
const scrapTheItems = require("./scrapTheItems");
const convertToDate = require("./convertToDate");
const AWS = require('aws-sdk');
AWS.config.update({region: 'eu-central-1'});
// AWS.config.update({region: 'eu-central-1'});
async function sendNotification(marketAlert) {

View File

@@ -0,0 +1,18 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn(
'RealEstateRequests',
'subscribed',
Sequelize.BOOLEAN
);
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn(
'RealEstateRequests',
'subscribed'
);
}
};

View File

@@ -18,7 +18,8 @@ module.exports = (sequelize, DataTypes) => {
gardenSizeMax: DataTypes.INTEGER,
priceMin: DataTypes.INTEGER,
priceMax: DataTypes.INTEGER,
bounding_box: DataTypes.GEOMETRY('POINT', 4326)
bounding_box: DataTypes.GEOMETRY('POINT', 4326),
subscribed: DataTypes.BOOLEAN
}, {});
RealEstateRequest.associate = function(models) {
// associations can be defined here

View File

@@ -2,89 +2,124 @@
<h2>U kojem naselju tražite nekretninu?</h2>
</div>
<div class="row center-align" >
<div class="row center-align">
<div id="floating-panel">
<input id="address" type="textbox" value="">
<input id="submit" type="button" value="Trazi">
</div>
<div id="map"></div>
</div>
<form method="POST" id="form-map-output">
<div class="row center-align">
<div class="col s6 push-s3">
<a id="btnsubmit" href="#" class="welcome-center-button waves-effect waves-light btn">
Dalje
</a>
</div>
<form method="POST" id="form-map-output">
<div class="row center-align">
<div class="col s6 push-s3">
<a id="btnsubmit" href="#" class="welcome-center-button waves-effect waves-light btn">
Dalje
</a>
</div>
<input type="hidden" name="north" id=north />
<input type="hidden" name="south" id=south />
<input type="hidden" name="east" id=east />
<input type="hidden" name="west" id=west />
</form>
<script>
</div>
<input type="hidden" name="north" id=north />
<input type="hidden" name="south" id=south />
<input type="hidden" name="east" id=east />
<input type="hidden" name="west" id=west />
</form>
<script>
var map;
var municipality = "<%= municipality%>";
var defaultAddress = document.getElementById('address');
var map;
var municipality = "<%= municipality%>";
var defaultAddress = document.getElementById('address');
var latLngRestrictions = [];
var BOSNIA_BOUNDS = {
north: 45.70,
south: 41.69,
west: 15.55,
east: 20.77,
};
var BOSNIA_BOUNDS = {
north: 45.70,
south: 41.69,
west: 15.55,
east: 20.77,
};
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: -34.397, lng: 150.644 },
zoom: 11,
restriction: {
latLngBounds: BOSNIA_BOUNDS,
strictBounds: false,
},
});
var geocoder = new google.maps.Geocoder();
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: -34.397, lng: 150.644 },
zoom: 11,
restriction: {
latLngBounds: BOSNIA_BOUNDS,
strictBounds: false,
},
});
var geocoder = new google.maps.Geocoder();
document.getElementById('submit').addEventListener('click', function () {
geocodeAddress(geocoder, map);
});
document.getElementById('submit').addEventListener('click', function () {
geocodeAddress(geocoder, map, false, latLngRestrictions);
});
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
function geocodeAddress(geocoder, resultsMap, isInit, geocoderRestrictions) {
var address = document.getElementById('address').value;
let geocoderOptions = geocoderRestrictions
? { 'address': address, 'bounds': geocoderRestrictions }
: { 'address': address }
geocoder.geocode(geocoderOptions, function (results, status) {
if (status === 'OK') {
var bounds = results[0].geometry.bounds;
var resultBounds = new google.maps.LatLngBounds(
results[0].geometry.viewport.getSouthWest(),
results[0].geometry.viewport.getNorthEast()
);
// map.fitBounds(resultBounds);
resultsMap.setCenter(results[0].geometry.location);
if (isInit) {
latLngRestrictions = new google.maps.LatLngBounds(
new google.maps.LatLng(bounds.getSouthWest().lat(), bounds.getSouthWest().lng()),
new google.maps.LatLng(bounds.getNorthEast().lng(), bounds.getNorthEast().lng()));
let latLngRestrictionsa = {
west: bounds.getSouthWest().lng(),
east: bounds.getNorthEast().lng(),
north: bounds.getNorthEast().lat(),
south: bounds.getSouthWest().lat()
}
map.setOptions({
restriction: {
latLngBounds: latLngRestrictionsa,
strictBounds: false,
}
})
} else {
alert('Geocode was not successful for the following reason: ' + status);
resultsMap.setZoom(17);
}
});
}
defaultAddress.value = municipality;
geocodeAddress(geocoder, map);
$(document).ready(() => {
$("#btnsubmit").click(() => {
var bounds = map.getBounds();
$("#north").val(map.getBounds().getNorthEast().lat());
$("#south").val(map.getBounds().getSouthWest().lat());
$("#east").val(map.getBounds().getNorthEast().lng());
$("#west").val(map.getBounds().getSouthWest().lng());
$("#form-map-output").submit();
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAna8ohfV2HBMcxGk_29vqxU5Z_bDickqg&callback=initMap"
async defer></script>
defaultAddress.value = municipality;
geocodeAddress(geocoder, map, true);
$(document).ready(() => {
$("#btnsubmit").click(() => {
var bounds = map.getBounds();
$("#north").val(map.getBounds().getNorthEast().lat());
$("#south").val(map.getBounds().getSouthWest().lat());
$("#east").val(map.getBounds().getNorthEast().lng());
$("#west").val(map.getBounds().getSouthWest().lng());
$("#form-map-output").submit();
});
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAna8ohfV2HBMcxGk_29vqxU5Z_bDickqg&callback=initMap" async
defer></script>
</div>

View File

@@ -1,56 +1,61 @@
<form method="POST" id="form-range">
<div>
<div class="row center-align">
<h6>Od</h6>
</div>
<p class="range-field">
<input
name="from"
id="from"
type="range"
value="<%= rangeFrom.value %>"
min="<%= rangeFrom.min %>"
max="<%= rangeFrom.max %>"
step="<%= rangeFrom.step %>"/>
</p>
<div class="row center-align">
<h6>Do</h6>
</div>
<p class="range-field">
<input
name="to"
id="to"
type="range"
value="<%= rangeTo.value %>"
min="<%= rangeTo.min %>"
max="<%= rangeTo.max %>"
step="<%= rangeTo.step %>"/>
</p>
<div>
<div class="row center-align">
<h6>Od</h6>
</div>
<div class="col s6 push-s3">
<a id="btnsubmit" href="#" class="welcome-center-button waves-effect waves-light btn">
Dalje
</a>
<div class="row center-align">
<h6 id="from-value"><%= rangeFrom.value %> <%= unit %> </h12>
</div>
</form>
<script>
$(document).ready(() => {
$("#btnsubmit").click(() => {
var from = $("#from").val();
var to = $("#to").val();
console.log("From " + from + " " + to);
if (parseInt(from, 10) >= parseInt(to, 10)) {
alert("\"Od\" vrijednost ne smije biti veca od \"Do\" vrijednosti ")
return;
}
$("#form-range").submit();
});
<p class="range-field">
<input name="from" id="from" type="range" value="<%= rangeFrom.value %>" min="<%= rangeFrom.min %>"
max="<%= rangeFrom.max %>" step="<%= rangeFrom.step %>" />
</p>
<div class="row center-align">
<h6>Do</h6>
</div>
<div class="row center-align">
<h6 id="to-value"><%= rangeTo.value %> <%= unit %></h12>
</div>
<p class="range-field">
<input name="to" id="to" type="range" value="<%= rangeTo.value %>" min="<%= rangeTo.min %>"
max="<%= rangeTo.max %>" step="<%= rangeTo.step %>" />
</p>
</div>
<div class="col s6 push-s3">
<a id="btnsubmit" href="#" class="welcome-center-button waves-effect waves-light btn">
Dalje
</a>
</div>
</form>
<script>
$(document).ready(() => {
$("#from").change(function () {
$('#from-value').text($("#from").val() + "<%= unit %>");
});
</script>
$("#to").change(function () {
$('#to-value').text($("#to").val() + "<%= unit %>" );
});
$("#btnsubmit").click(() => {
var from = $("#from").val();
var to = $("#to").val();
$("#foo").on("click", function () {
alert($(this).text());
});
if (parseInt(from, 10) >= parseInt(to, 10)) {
alert("\"Od\" vrijednost ne smije biti veca od \"Do\" vrijednosti ")
return;
}
$("#form-range").submit();
});
});
</script>

14
app/views/unsubscribe.ejs Normal file
View File

@@ -0,0 +1,14 @@
<div class="row center-align">
<span class="welcome-big-logo">🤙</span>
</div>
<div class="row center-align">
<div>Uspješno ste se odjavili</div>
</div>
<div class="row">
<div class="col s6 push-s3">
<a href="<%= nextStep %>" class="welcome-center-button waves-effect waves-light btn">
Nova pretraga
</a>
</div>
</div>

5
development.env Normal file
View File

@@ -0,0 +1,5 @@
AMAZON_ACCES_KEY_ID=(your-key-here)
AMAZON_SECRET_ACCESS_KEY=(your-key-here)
AMAZON_REGION=eu-west-1
APP_URL=http://localhost:3001
SOURCE_EMAIL=info@saburly.com

View File

@@ -1,5 +1,5 @@
const welcome = require('./app/controllers/welcome').getWelcome;
const { getRealEstateTypes, postRealEstateTypes} = require('./app/controllers/realEstateTypes');
const { getRealEstateTypes, postRealEstateTypes } = require('./app/controllers/realEstateTypes');
const { getRegion, postRegion } = require('./app/controllers/regions');
const { getMunicipality, postMunicipality } = require('./app/controllers/municipalities');
const { getSize, postSize } = require('./app/controllers/sizes');
@@ -9,6 +9,7 @@ const { getQueryReview, postQueryReview } = require('./app/controllers/queryRevi
const { getQuerySubmit, postQuerySubmit } = require('./app/controllers/querySubmit');
const { getGoAgain } = require('./app/controllers/goAgain');
const { getNeighborhood, postNeighborhood } = require('./app/controllers/neighborhoodMap');
const { getUnsubscribe } = require('./app/controllers/unsubscribe');
let express = require("express");
const path = require("path");
@@ -106,7 +107,7 @@ app.post("/api/payforalert", (req, res) => {
}
};
tco.checkout.authorize(params, function(error, data) {
tco.checkout.authorize(params, function (error, data) {
if (error) {
res.send(error.message);
} else {
@@ -146,6 +147,8 @@ app.post('/pregled/:request_id', postQueryReview);
app.get('/posalji/:request_id', getQuerySubmit);
app.post('/posalji/:request_id', postQuerySubmit);
app.get('/odjava/:request_id', getUnsubscribe);
app.get('/ponovo', getGoAgain);
app.use('/assets', express.static('./app/public'));