123 lines
3.4 KiB
JavaScript
123 lines
3.4 KiB
JavaScript
const dobrodosli = require('./app/controllers/dobrodosli').getDobrodosli;
|
|
const { getVrstaNekretnine, postVrstaNekretnine} = require('./app/controllers/vrsta_nekretnine');
|
|
const { getGrad, postGrad } = require('./app/controllers/grad');
|
|
|
|
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 sequelize = require("./app/models/index").sequelize;
|
|
const Twocheckout = require("2checkout-node");
|
|
const layout = require('express-layout');
|
|
|
|
const app = express();
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
const port = process.env.PORT || 5000;
|
|
|
|
app.set('views', path.join(__dirname, '/app/views'));
|
|
app.set('view engine', 'ejs');
|
|
app.use(layout());
|
|
|
|
const compression = require('compression');
|
|
app.use(compression());
|
|
|
|
app.get("/api/sendnotifications", async function(req, res) {
|
|
let marketAlerts = await MarketAlert.findAll();
|
|
|
|
let lastDateUpdate = await Promise.all(
|
|
marketAlerts
|
|
.map(marketAlert => {
|
|
const { id, email, olx_url, last_date } = marketAlert.dataValues;
|
|
return { id, email, olx_url, last_date };
|
|
})
|
|
.map(sendNotification)
|
|
);
|
|
lastDateUpdate = lastDateUpdate.filter(Boolean(dateUpdate));
|
|
lastDateUpdate.length &&
|
|
lastDateUpdate.forEach(dateUpdate =>
|
|
MarketAlert.update(
|
|
{ last_date: dateUpdate.date },
|
|
{ where: { id: dateUpdate.id } }
|
|
)
|
|
);
|
|
});
|
|
|
|
app.get("/api/items/:url", async (req, res) => {
|
|
let url =
|
|
"https://www.olx.ba/pretraga?" +
|
|
req.params.url +
|
|
"&sort_order=desc&sort_po=datum";
|
|
let appts = await scrapTheItems(url);
|
|
res.json({
|
|
last_date: appts[0] && appts[0].date,
|
|
items: appts
|
|
});
|
|
});
|
|
|
|
app.post("/api/marketalerts", function(req, res) {
|
|
const { email, last_date, olx_url } = req.body;
|
|
console.log(email, last_date, olx_url);
|
|
sequelize.sync().then(() => {
|
|
MarketAlert.create({
|
|
olx_url,
|
|
last_date,
|
|
email
|
|
})
|
|
.then(() => {
|
|
res.json({ message: "Market Alert Created!" });
|
|
})
|
|
.catch(e => console.error(e));
|
|
});
|
|
});
|
|
|
|
app.post("/api/payforalert", function(request, response) {
|
|
let tco = new Twocheckout({
|
|
sellerId: "901402692",
|
|
privateKey: "A28DCE5F-9292-405C-8161-F84D8BB83AFC",
|
|
sandbox: true
|
|
});
|
|
|
|
let params = {
|
|
merchantOrderId: "123",
|
|
token: request.body.token,
|
|
currency: "USD",
|
|
total: "2.00",
|
|
billingAddr: {
|
|
name: "Testing Tester",
|
|
addrLine1: "123 Test St",
|
|
city: "Sarajevo",
|
|
state: "BiH",
|
|
zipCode: "71000",
|
|
country: "BiH",
|
|
email: request.body.email,
|
|
phoneNumber: "5555555555"
|
|
}
|
|
};
|
|
|
|
tco.checkout.authorize(params, function(error, data) {
|
|
if (error) {
|
|
response.send(error.message);
|
|
} else {
|
|
response.send(data.response.responseMsg);
|
|
}
|
|
});
|
|
});
|
|
|
|
app.get('/', dobrodosli);
|
|
app.get('/vrstanekretnine/:request_id', getVrstaNekretnine);
|
|
app.get('/vrstanekretnine', getVrstaNekretnine);
|
|
|
|
app.post('/vrstanekretnine/:request_id', postVrstaNekretnine);
|
|
app.post('/vrstanekretnine', postVrstaNekretnine);
|
|
|
|
app.get('/grad/:request_id', getGrad);
|
|
app.post('/grad/:request_id', postGrad);
|
|
|
|
app.use('/assets', express.static('./app/public'))
|
|
|
|
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
|