-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
77 lines (65 loc) · 2.87 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require("express");
const Amadeus = require('amadeus');
const bodyParser = require('body-parser');
const amadeus = new Amadeus({
clientId: 'PbF84xvmAkGEGJbYOHoBj4lHAQzjeU5K',
clientSecret: 'ZWRmCAivSVUGwgH4'
});
// Set up the express app
const app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.post('/inspirationSearch', (req, res) => {
const origin = req.body.origin; //true
amadeus.referenceData.locations.get({
keyword : origin,
subType : Amadeus.location.city
}).then(function (response) {
console.log(response.data);
res.status(200).send(response.data)
}).catch(function (responseError) {
console.log(responseError);
res.status(400).send(responseError)
});
});
getCityCode = async (cityKeyword) => {
let cities = await amadeus.referenceData.locations.get({
keyword : cityKeyword,
subType : Amadeus.location.city
})
console.log(cities.data[0].address.cityCode)
return {cityCode: cities.data[0].address.cityCode, location: cities.data[0].geoCode}
};
app.post('/book', async (req, res) => {
try {
let {cityCode: originCityCode, location: originLocation} = await getCityCode(req.body.originCityCode)
let {cityCode: destCityCode, location: destLocation} = await getCityCode(req.body.destCityCode)
console.log(originCityCode)
let airports = await amadeus.referenceData.locations.airports.get({
longitude : parseFloat(destLocation.longitude),
latitude : parseFloat(destLocation.latitude)
})
let flightOffers = await amadeus.shopping.flightOffers.get({
origin : originCityCode,
destination : destCityCode,
departureDate : req.body.startDate
})
flightOffers.data.filter(flight => {
return flight.offerItems[0].price < req.body.flightBudget
})
let hotels = await amadeus.shopping.hotelOffers.get({
cityCode : destCityCode
})
hotels.result.data.filter(h => {return (h.hotel.rating > req.body.minStars) && (h.offers[0].price.total<req.body.budget)}).sort((a,b)=> {
if(a.hotel.hotelDistance > b.hotel.hotelDistance) return 1; if (b.hotel.hotelDistance > a.hotel.hotelDistance) return -1; return 0;
});
res.status(200).send({flights: flightOffers.data, hotels: hotels.result.data})
} catch(responseError) {
console.log(responseError);
res.status(400).send(responseError)
}
});
const PORT = 5000;
app.listen(PORT, () => {
console.log(`server running on port ${PORT}`)
});