clean code

This commit is contained in:
Bilal Catic
2019-05-16 19:58:48 +02:00
parent c505062770
commit 1542310a81
9 changed files with 52 additions and 54 deletions

View File

@@ -1,25 +1,24 @@
const db = require('../models/index');
const { currentRERequest } = require('../helpers/url');
const { currentRERequest } = require('../helpers/url');
const { places } = require('../helpers/codes');
const getNeighborhood = async (req,res) => {
let request = await currentRERequest(req);
const neighborhoods = places(request.city);
const nextStep = req.query.nextStep || '/';
res.render('neighborhood', {
nextStep,
neighborhoods
res.render('neighborhood', {
nextStep,
neighborhoods
});
}
};
const postgNeighborhood = async (req, res) => {
let request = await currentRERequest(req);
request.neighborhood = req.body.neighborhood;
await request.save();
res.send("Result is " + JSON.stringify(request));
}
res.send("Result is " + JSON.stringify(request));
};
module.exports = {
module.exports = {
getNeighborhood,
postgNeighborhood
};

View File

@@ -6,7 +6,6 @@ const realEstateTypes = [
{ ime: "Vikendica", id: "vikendica" }
];
const getRealEstateTypes = (req,res) => {
const nextStep = req.query.nextStep;
res.render('realEstateType', {

View File

@@ -1,7 +1,7 @@
const getWelcome = (req,res) => {
res.render('welcome', { nextStep: '/vrstanekretnine' } );
}
};
module.exports = {
module.exports = {
getWelcome
};

View File

@@ -1,10 +1,8 @@
const geographies = [
{
"ime":" Sarajevo",
"id":"sarajevo",
"olxid": "9",
"olxid": "9",
"mjesta":[
{
"ime":"Hadžići",
@@ -56,7 +54,7 @@ const geographies = [
{
"ime":" Unsko-sanski",
"id":"unskosanski",
"olxid": "9",
"olxid": "9",
"mjesta":[
{
"ime":"Bihać",
@@ -103,7 +101,7 @@ const geographies = [
{
"ime":" Posavski",
"id":"posavski",
"olxid": "15",
"olxid": "15",
"mjesta":[
{
"ime":"Domaljevac",
@@ -130,7 +128,7 @@ const geographies = [
{
"ime":" Tuzlanski",
"id":"tuzlanski",
"olxid": "15",
"olxid": "15",
"mjesta":[
{
"ime":"Banovići",
@@ -202,7 +200,7 @@ const geographies = [
{
"ime":" Zeničko-dobojski",
"id":"zenickodobojski",
"olxid": "15",
"olxid": "15",
"mjesta":[
{
"ime":"Breza",
@@ -269,7 +267,7 @@ const geographies = [
{
"ime":" Bosansko-podrinjski",
"id":"bosanskopodrinjski",
"olxid": "15",
"olxid": "15",
"mjesta":[
{
"ime":"Foča",
@@ -291,7 +289,7 @@ const geographies = [
{
"ime":" Srednjobosanski",
"id":"srednjobosanski",
"olxid": "6",
"olxid": "6",
"mjesta":[
{
"ime":"Bugojno",
@@ -358,7 +356,7 @@ const geographies = [
{
"ime":" Hercegovačko-neretvanski",
"id":"hercegovackoneretvanski",
"olxid": "7",
"olxid": "7",
"mjesta":[
{
"ime":"Grad Mostar",
@@ -410,7 +408,7 @@ const geographies = [
{
"ime":" Zapadno-hercegovački",
"id":"zapadnohercegovacki",
"olxid": "8",
"olxid": "8",
"mjesta":[
{
"ime":"Grude",
@@ -437,7 +435,7 @@ const geographies = [
{
"ime":" Livanjski",
"id":"livanjski",
"olxid": "10",
"olxid": "10",
"mjesta":[
{
"ime":"Bosansko Grahovo",
@@ -474,7 +472,7 @@ const geographies = [
{
"ime":" Banjalučka",
"id":"banjalučka",
"olxid": "14",
"olxid": "14",
"mjesta":[
{
"ime":"Banja Luka",
@@ -586,7 +584,7 @@ const geographies = [
{
"ime":" Dobojsko-Bijeljinska",
"id":"dobojskobijeljinska",
"olxid": "15",
"olxid": "15",
"mjesta":[
{
"ime":"Bijeljina",
@@ -682,8 +680,8 @@ const geographies = [
},
{
"ime":" Sarajevsko-Zvornička",
"id":"sarajevskozvornicka",
"olxid": "16",
"id":"sarajevskozvornicka",
"olxid": "16",
"mjesta":[
{
"ime":"Bratunac",
@@ -805,7 +803,7 @@ const geographies = [
{
"ime":" Trebinjsko-Fočanska",
"id":"trebinjskofocanska",
"olxid": "17",
"olxid": "17",
"mjesta":[
{
"ime":"Berkovići",
@@ -862,7 +860,7 @@ const geographies = [
{
"ime":"Distrikt Brčko",
"id":"distriktbrcko",
"olxid": "12",
"olxid": "12",
"mjesta":[
{
"ime":"Brčko",
@@ -872,22 +870,22 @@ const geographies = [
]
}
]
];
const regions = () => {
return geographies.map( (g) => ({ ime: g.ime, id: g.id, olxid: g.olxid }) );
}
return geographies.map( (g) => ({ ime: g.ime, id: g.id, olxid: g.olxid }) );
};
const places = (regionId) => {
for (geo of geographies) {
for (geo of geographies) {
if(geo.id === regionId) {
return geo.mjesta;
}
}
return null;
}
return null;
};
module.exports = {
regions,
places
}
};

View File

@@ -6,8 +6,8 @@ const currentRERequest = async (req) => {
const request = await db.RealEstateRequest.findOne({ where: {uniqueId} });
return request;
}
};
module.exports = {
currentRERequest
}
};

View File

@@ -1,8 +1,10 @@
const convertToDate = require("./convertToDate");
function areThereAnyNewItems(lastItemDate, controlDate) {
if (!lastItemDate) {
return true;
}
return new Date(controlDate) < convertToDate(lastItemDate);
}
module.exports = areThereAnyNewItems;

View File

@@ -1,6 +1,6 @@
let fetch = require("node-fetch");
let cheerio = require("cheerio");
const areThereAnyNewItems = require("./arethereanynewitems");
const areThereAnyNewItems = require("./areThereAnyNewItems");
async function scrapTheItems(url, controlDate, noNewItems = false) {
let items = [];

View File

@@ -1,11 +1,11 @@
const scrapTheItems = require("./scraptheitems");
const scrapTheItems = require("./scrapTheItems");
const convertToDate = require("./convertToDate");
const AWS = require('aws-sdk');
AWS.config.update({region: 'eu-central-1'});
async function sendNotification(marketAlert) {
const { id, email, olx_url, last_date } = marketAlert;
const { id, email, olx_url } = marketAlert;
let url =
"https://www.olx.ba/pretraga?" + olx_url + "&sort_order=desc&sort_po=datum";
let newItems = await scrapTheItems(url);
@@ -17,8 +17,8 @@ async function sendNotification(marketAlert) {
""
);
// Create sendEmail params
var params = {
// Create sendEmail params
const params = {
Destination: { /* required */
CcAddresses: [
],
@@ -50,7 +50,7 @@ async function sendNotification(marketAlert) {
if (message) {
const sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
await sendPromise;
await sendPromise;
return { id, date: String(convertToDate(lastDate)) };
}
}

View File

@@ -7,8 +7,8 @@ let express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const MarketAlert = require("./app/models/marketalert");
const sendNotification = require("./app/lib/sendnotification");
const scrapTheItems = require("./app/lib/scraptheitems");
const sendNotification = require("./app/lib/sendNotification");
const scrapTheItems = require("./app/lib/scrapTheItems");
const sequelize = require("./app/models/index").sequelize;
const Twocheckout = require("2checkout-node");
const layout = require('express-layout');
@@ -26,7 +26,7 @@ app.use(layout());
const compression = require('compression');
app.use(compression());
app.get("/api/sendnotifications", async function(req, res) {
app.get("/api/sendnotifications", async (req, res) => {
let marketAlerts = await MarketAlert.findAll();
let lastDateUpdate = await Promise.all(
@@ -59,7 +59,7 @@ app.get("/api/items/:url", async (req, res) => {
});
});
app.post("/api/marketalerts", function(req, res) {
app.post("/api/marketalerts", (req, res) => {
const { email, last_date, olx_url } = req.body;
console.log(email, last_date, olx_url);
sequelize.sync().then(() => {
@@ -75,7 +75,7 @@ app.post("/api/marketalerts", function(req, res) {
});
});
app.post("/api/payforalert", function(request, response) {
app.post("/api/payforalert", (req, res) => {
let tco = new Twocheckout({
sellerId: "901402692",
privateKey: "A28DCE5F-9292-405C-8161-F84D8BB83AFC",
@@ -84,7 +84,7 @@ app.post("/api/payforalert", function(request, response) {
let params = {
merchantOrderId: "123",
token: request.body.token,
token: req.body.token,
currency: "USD",
total: "2.00",
billingAddr: {
@@ -94,16 +94,16 @@ app.post("/api/payforalert", function(request, response) {
state: "BiH",
zipCode: "71000",
country: "BiH",
email: request.body.email,
email: req.body.email,
phoneNumber: "5555555555"
}
};
tco.checkout.authorize(params, function(error, data) {
if (error) {
response.send(error.message);
res.send(error.message);
} else {
response.send(data.response.responseMsg);
res.send(data.response.responseMsg);
}
});
});