-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
256 lines (214 loc) · 7.34 KB
/
index.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
var path = require('path');
var mongodb = require('mongodb');
var mongourl = 'mongodb+srv://mongoaccessaccount:[email protected]/test?retryWrites=true&w=majority';
const mongoClient = mongodb.MongoClient(mongourl);
var listOfLists = [];
// Called once to setup the mongo client
async function dbSetup()
{
await mongoClient.connect();
// Setting these statically because they're not gonna change usually
// If they do, we need to restart the server
setListOfDefaultLists();
}
async function setListOfDefaultLists()
{
mongoClient.db('users').collection("lists").find(
{email: "[email protected]"}
).toArray(function (err, result)
{
if (err)
throw err;
var lists = result[0].lists;
// Go through the results and assign the lists
for (var i = 0; i < lists.length; ++ i)
{
var items = lists[i].items;
var itemList = [];
for (var j = 0; j < items.length; ++ j)
{
itemList.push ({
name: items[j],
checked: false
});
}
listOfLists.push({name: lists[i].name, items: itemList})
}
});
}
async function getoutletdata(countryname)
{
mongoClient.db('countries').collection("outletlookup").find(
{Country: countryname}
).toArray(function (err, result)
{
if (err)
throw err;
console.log(result)
return(result)
});
}
dbSetup();
app.use('/frontEnd', express.static(__dirname + '/frontEnd'));
app.get('/', function (req, res)
{ // When server requests '/' page
res.sendFile(path.join(__dirname + '/frontEnd/html/index.html'));
});
app.get('/newTrip.html', function (req, res)
{
res.sendFile(path.join(__dirname + '/frontEnd/html/newTrip.html'));
});
app.get('/tripInfo.html', function (req, res)
{ // This one's gonna need some information about the trip too
res.sendFile(path.join(__dirname + '/frontEnd/html/tripInfo.html'));
});
app.get('/default-lists', function (req, res)
{ // let 5 days be default duration of journey
var numDays = 5;
if (req.headers.numdays)
{
numDays = parseInt(req.headers.numdays);
}
res.json(getDefaultLists(numDays));
});
function getDefaultLists(numDays)
{
if (numDays == 5)
return listOfLists;
var toReturn = JSON.parse(JSON.stringify(listOfLists));
var normalizedDays = Math.min(numDays, 10);
// Do some number of days specific stuff here for the lists
toReturn[1].items[0].name = normalizedDays + " " + toReturn[1].items[0].name;
toReturn[1].items[1].name = normalizedDays + " " + toReturn[1].items[1].name
toReturn[1].items[2].name = (normalizedDays + 1) + " " + toReturn[1].items[2].name
toReturn[1].items[3].name = normalizedDays + " " + toReturn[1].items[3].name
toReturn[1].items[4].name = Math.ceil(normalizedDays / 2) + " " + toReturn[1].items[4].name
return toReturn;
}
function normalizeTripbase(tripbase)
{
var keys = Object.keys(tripbase)
for (var i = 0; i < keys.length; ++i)
{
if(keys[i] != "lists")
tripbase[keys[i]] = JSON.stringify(tripbase[keys[i]]).replace(/\"/g, "")
}
return tripbase
}
app.post('/newtripdata', function (req, res)
{
var tripbase = normalizeTripbase(req.body)
if (tripbase.email == "")
res.send("No email :(")
mongoClient.db('users').collection('tripbase').updateOne(
{
email: tripbase.email,
tripid: tripbase.tripid
},
{
$set: {
start_date: tripbase.start_date,
end_date: tripbase.end_date,
departure_city: tripbase.departure_city,
departure_countryCode: tripbase.departure_countryCode,
arrival_city: tripbase.arrival_city,
arrival_countryCode: tripbase.arrival_countryCode,
lists: tripbase.lists
}
},
{upsert: true}
);
// mongoClient.db('users').collection('tripbase').find(
// {
// email: JSON.stringify(tripbase.email).replace(/\"/g, ""),
// start_date: JSON.stringify(tripbase.start_date).replace(/\"/g, ""),
// end_date: JSON.stringify(tripbase.end_date).replace(/\"/g, ""),
// arrival_countryCode: JSON.stringify(tripbase.arrival_countryCode).replace(/\"/g, ""),
// arrival_city: JSON.stringify(tripbase.arrival_city).replace(/\"/g, ""),
// departure_city: JSON.stringify(tripbase.departure_city).replace(/\"/g, ""),
// departure_countryCode: JSON.stringify(tripbase.departure_countryCode).replace(/\"/g, "")
// }
// ).toArray(function (err, check)
// {
// if (check[0] == undefined)
// {
// mongoClient.db('users').collection('tripbase').find(
// {
// email: JSON.stringify(tripbase.email).replace(/\"/g, "")
// }
// ).sort(
// {trip_id: -1}
// ).limit(1).toArray(function (err, result)
// {
// var tripidnew = 0
// if (err)
// throw err;
// if (result[0] == undefined)
// {
// trip_idnew = 1
// }
// else
// {
// trip_idnew = parseInt(result[0].trip_id) + 1
// } tripbase["trip_id"] = trip_idnew.toString()
// mongoClient.db('users').collection('tripbase').insertOne(tripbase)
// });
// }
// });
// }
res.send("success")
});
app.get('/outletdata', function (req, res)
{
var outletdataholder = {}
mongoClient.db('countries').collection("outletlookup").find(
{
Country: JSON.stringify(req.headers.country).replace(/\"/g, "")
}
).toArray(function (err, result1)
{
if (err)
throw err;
if (result1[0] != undefined)
{
res.json(result1)
}
else
{
mongoClient.db('countries').collection("outletlookup").find(
{
Country: JSON.stringify(req.headers.country_alt).replace(/\"/g, "")
}
).toArray(function (err, result2)
{
if (err)
throw err;
res.json(result2)
});
}
});
});
app.get('/old-trips', function (req, res)
{
// let 5 days be default duration of journey
var useremail = JSON.stringify(req.headers.email).replace(/\"/g, "");
if (useremail == "")
{
res.send("[]");
}
mongoClient.db("users").collection("tripbase").find({email: useremail}).toArray(function(err, result)
{
if (err) throw err;
res.json(result);
});
});
let port = process.env.PORT;
if (port == null || port == "")
{
port = 3000;
}
app.listen(port);