This repository has been archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
543 lines (505 loc) · 17.6 KB
/
db.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// const mongoose = require('mongoose');
const mongoose = require('mongoose').set('debug', true);
require('mongoose-double')(mongoose);
// mongoose.Promise = require('bluebird');
// mongoose.Promise = global.Promise;
const email = require('./send_email');
const https = require('https');
const qs = require('qs');
const key = require('./key');
// The database part
// const dbName = 'ggirls';
// use test database first
const dbName = 'test';
const url = key.url;
console.log(url);
mongoose.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("we're connected!");
});
const SchemaTypes = mongoose.Schema.Types;
const userSchema = new mongoose.Schema({
email: String,
home: {type:SchemaTypes.ObjectId, ref:'places'},
work: {type:SchemaTypes.ObjectId, ref:'places'},
fav_places : [{fav_place:{type:SchemaTypes.ObjectId, ref:'places'}}],
vis_places:[{
vis_place:{type:SchemaTypes.ObjectId, ref:'places'},
vis_date:{type: Date, default: Date.now}
}],
contact_email: String
})
const placeSchema = new mongoose.Schema({
mapID:String,
status:String,
mapName:String,
geometry:{
lat: SchemaTypes.Double,
lng: SchemaTypes.Double
},
flag: {type:Boolean, default:false},
cases: [{case:{type:SchemaTypes.ObjectId, ref:'cases'}}],
types: [{ type: String }]
})
const caseSchema = new mongoose.Schema({
case_id: String,
place_and_date:[{
place: {type:SchemaTypes.ObjectId, ref:'places'},
start_date: { type: Date, default: Date.now },
end_date: { type: Date, default: Date.now }
}]
})
const User = mongoose.model('users',userSchema);
const Place = mongoose.model('places',placeSchema);
const Case = mongoose.model('cases',caseSchema)
mongoose.set('useFindAndModify', false);
async function getUser(email){
return new Promise((resolve,reject) => {
let user = new User({email:email,contact_email:email});
let userID = null;
// find user
User.findOne({email:email}).exec((err,docs)=>{
if(err) reject(err);
//If user doesn't exist
if(docs===null || docs.length===0){
user.save((err)=>{
if (err) reject(err);
});
userID = user;
} else {
userID = docs;
}
resolve(userID);
});
});
}
async function setEmail(userID, email){
return await User.findOneAndUpdate({_id:userID},{"contact_email":email},{new:true}).exec();
}
function getPlaceByID(id){
return new Promise( (resolve,reject) => {
Place.findOne({_id: id}).exec((err, data) => {
if (err) reject(err);
resolve(data);
});
});
}
function getUserByID(id){
return new Promise((resolve,reject) => {
User.findOne({_id: id}).exec((err, data) => {
if (err) reject(err);
resolve(data);
});
});
}
function getCaseByID(id){
return new Promise((resolve,reject) => {
Case.findOne({_id: id}).exec((err, data) => {
if (err) reject(err);
resolve(data);
});
});
}
async function searchPlace(input){
var query = {
key:key.placekey,
input:input,
inputtype:"textquery",
fields:"geometry,name,place_id,types"
}
var options = {
host: 'maps.googleapis.com',
path:'/maps/api/place/findplacefromtext/json?'+qs.stringify(query),
method: 'GET'
}
var body = null;
var json = {};
return new Promise((resolve, reject) => {
var req = https.get(options, function(res) {
// console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
// Buffer the body entirely for processing as a whole.
var bodyChunks = [];
res.on('data', function(chunk) {
// You can process streamed parts here...
bodyChunks.push(chunk);
}).on('end', function() {
body = Buffer.concat(bodyChunks);
var tmp = body.toString('utf8');
var tmp = JSON.parse(tmp);
if(tmp['status']==='ZERO_RESULTS'){
json["status"] = 'ZERO_RESULTS';
}else{
json["status"] = tmp['status'];
tmp = tmp["candidates"][0];
json["mapName"] = tmp["name"];
json["mapID"] = tmp["place_id"];
json["geometry"] = tmp["geometry"]["location"];
json["types"] = tmp["types"];
}
console.log(json);
resolve(json);
})
});
req.on('error', function(e) {
reject(e);
});
});
}
// Search all places by text search API (like wellcome, blabla).
function textSearch(input){
var query = {
key:key.placekey,
query:input+" in Hong Kong",
// region:"hk",
}
var options = {
host: 'maps.googleapis.com',
path:'/maps/api/place/textsearch/json?'+qs.stringify(query),
method: 'GET'
}
var body = null;
https.get(options, function(res) {
// console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
// Buffer the body entirely for processing as a whole.
var bodyChunks = [];
res.on('data', function(chunk) {
// You can process streamed parts here...
bodyChunks.push(chunk);
}).on('end', function() {
body = Buffer.concat(bodyChunks);
var tmp = body.toString('utf8');
tmp = JSON.parse(tmp);
for (var item in tmp["results"]){
// console.log(tmp["results"][item]);
var json = {};
json["mapID"] = tmp["results"][item]["place_id"];
json["mapName"] = tmp["results"][item]["name"];
json["status"] = tmp["status"];
json["geometry"] = tmp["results"][item]["geometry"]["location"];
json["types"] = tmp["results"][item]["types"];
savePlace(json);
}
});
});
}
function findPlace(input){
return new Promise(async (resolve,reject) => {
let place = 'None';
await Place.find({mapName:input}).exec(async (err,docs) => {
if(err) reject(err);
place = docs;
if (place.length===0) {
place = await searchPlace(input);
let place_res = await savePlace(place);
console.log("Find place in API");
console.log("x2 is", place);
console.log("place_res is: ",place_res)
let new_place = await Place.find({mapName:place_res["mapName"]});
resolve([place_res]);
} else {
resolve(place);
}
});
});
};
function savePlace(place){
return new Promise(async (resolve,reject) =>{
let place_res = null;
await Place.findOne({mapID: place["mapID"]}).exec(async (err,docs)=>{
if(err) reject(err);
if(docs===null || docs.length===0){
place_res = new Place(place);
place_res.save(err=>{
if (err) reject(err);
});
} else {
place_res = docs;
}
resolve(place_res);
});
});
}
// type
// 1 -> home
// 2 -> work
// 3 -> fav places
// 4 -> visited places (have day)
function setPlace(userID,placeID,type,date=null){
return new Promise(async (resolve,reject) =>{
let result = {
n:0,
nModified:0
};
if(type===1){
const res = await User.updateOne({_id:userID},{home:placeID});
result["n"] = res.n;
result["nModified"] = res.nModified;
resolve(result);
}
if(type===2){
const res = await User.updateOne({_id:userID},{work:placeID});
result["n"] = res.n;
result["nModified"] = res.nModified;
resolve(result);
}
if(type===3){
let item = {fav_place:placeID};
User.findOne({_id:userID,"fav_places.fav_place":placeID},async (err,docs)=>{
if(docs===null || docs.length==0){
const res = await User.updateOne({_id:userID},{$addToSet:{fav_places:item}});
result["n"] = res.n;
result["nModified"] = res.nModified;
} else {
result["n"] = 1;
result["nModified"] = 0;
}
resolve(result);
});
}
if(type===4){
let item = {vis_place:placeID,vis_date:date};
User.findOne({_id:userID,"vis_places.vis_place":placeID,"vis_places.vis_date":date},async (err,docs)=>{
if(docs===null || docs.length===0){
const res = await User.updateOne({_id:userID},{$addToSet:{vis_places:item}});
result["n"] = res.n;
result["nModified"] = res.nModified;
} else {
result["n"] = 1;
result["nModified"] = 0;
}
resolve(result);
});
}
});
}
//type is 3 or 4
function deletePlace(userID,placeID,type,date=null){
return new Promise(async (resolve,reject) =>{
let result = {
n:0,
nModified:0
};
if(type===3){
const res = await User.updateOne({_id:userID},{$pull:{fav_places:{fav_place:{$in:placeID}}}},null);
result["n"] = res.n;
result["nModified"] = res.nModified;
}
if(type===4){
const res = await User.updateOne({_id:userID},{$pull:{vis_places:{$and:[{vis_place:{$in:placeID}},{vis_date:{$in:date}}]}}},null);
result["n"] = res.n;
result["nModified"] = res.nModified;
}
resolve(result);
});
}
function findPlaceType(type){
return new Promise(async (resolve,reject) =>{
await Place.find({types:type},(err,docs)=>{
if(err) reject(err);
resolve(docs);
});
});
}
function addCase(new_case){
return new Promise(async (resolve,reject)=>{
let flag = false;
let place_name = new_case["place_and_date"]["place"];
console.log(place_name);
let place = await findPlace(place_name);
if(place.length===1 && place[0]!==null && place[0]["status"] === "OK"){
new_case["place_and_date"]["place"] = place[0]["_id"];
} else {
reject("The place name may incorrect: ",place_name);
}
let newcaseID = null;
let res = await Case.findOne({"case_id":new_case["case_id"]}).exec();
if(res === null){
let newcase = new Case(new_case);
console.log(newcase);
newcaseID = newcase["_id"];
await newcase.save(err=>{
reject(err);
});
} else {
newcaseID = res["_id"];
let rres = await Case.findOne({"case_id":new_case["case_id"],
"place_and_date.place":new_case["place_and_date"]["place"],
"place_and_date.start_date":new_case["place_and_date"]["start_date"],
"place_and_date.end_date":new_case["place_and_date"]["end_date"]}).exec();
if(rres === null){
const ures = await Case.updateOne({"case_id":new_case["case_id"]},{$addToSet:{place_and_date:new_case["place_and_date"]}});
console.log("Case updated result: ",ures.n, ures.nModified);
}
}
console.log(newcaseID);
// Update place info
let item = {case:newcaseID};
console.log(item);
const pres = await Place.updateOne({_id:place[0]["_id"]},{
flag:true,
$addToSet:{cases:item}
});
console.log("Place updated result: ",pres.n, pres.nModified);
resolve(true);
});
}
//Todo: if flag is true (add date check)
async function findCaseByGeo1(geo,flag){
let lat = geo["lat"];
let lng = geo["lng"];
let new_lat = 500 * 0.0000089;
let new_lng = new_lat / Math.cos(lat * 0.018);
let cases = [];
console.log(new_lat,new_lng);
return new Promise(async (resolve,reject) => {
Place.find({
"flag":"true",
"geometry.lat":{$lt:lat+new_lat,$gt:lat-new_lat},
"geometry.lng":{$lt:lng+new_lng,$gt:lat-new_lng}
},(err,docs)=>{
resolve(docs);
})
});
}
//if flag = true: add date check
async function findCaseByGeo(geo,flag){
let lat = geo["lat"];
let lng = geo["lng"];
let new_lat = 500 * 0.0000089;
let new_lng = new_lat / Math.cos(lat * 0.018);
let cases = [];
console.log(new_lat,new_lng);
return new Promise(async (resolve,reject) => {
let docs = await Place.find({
"flag":"true",
"geometry.lat":{$lt:lat+new_lat,$gt:lat-new_lat},
"geometry.lng":{$lt:lng+new_lng,$gt:lat-new_lng}
}).exec();
for(const place in docs){
cases[place] = {}
cases[place]["mapName"] = docs[place]["mapName"];
cases[place]["cases"] = [];
let tmp = {};
const doc_cases = docs[place]["cases"];
for(const item in doc_cases){
let x = await getCaseByID(doc_cases[item]["_id"]);
cases[place]["cases"].push(x);
}
}
resolve(cases);
});
}
// send emails
async function sendEmail(user){
console.log(user);
if(user["contact_email"]!=null && user["contact_email"]!="null"){
let email_res = {};
console.log(email_res);
email_res["email"] = user["contact_email"];
// Check Home
const home = await getPlaceByID(user["home"]);
console.log ("home is: ",home);
if(home !== null){
email_res["home"] = home["mapName"];
email_res["home_case"] = await findCaseByGeo(home["geometry"],false);
}
//Check Work
const work = await getPlaceByID(user["work"]);
// console.log("work is: ",work);
if(work!== null) {
email_res["work"] = work["mapName"];
email_res["work_case"] = await findCaseByGeo(work["geometry"], false);
}
//Check Fav
email_res["fav_places"] = {}
for (const place in user["fav_places"]){
const fav = await getPlaceByID(user["fav_places"][place]["fav_place"]);
if(fav["flag"]===true){
email_res["fav_places"][place] = {};
email_res["fav_places"][place]["mapName"] = fav["mapName"];
email_res["fav_places"][place]["flag"] = fav["flag"];
email_res["fav_places"][place]["cases"] = fav["cases"];
}
}
email_res["vis_places"] = {}
for (const place in user["vis_places"]){
const vis = await getPlaceByID(user["vis_places"][place]["vis_place"]);
if(vis["flag"]===true){
const p = vis["cases"];
const d = user["vis_places"][place]["vis_date"];
console.log(p,d);
email_res["vis_places"][place] = {};
email_res["vis_places"][place]["mapName"] = vis["mapName"];
email_res["vis_places"][place]["flag"] = vis["flag"];
email_res["vis_places"][place]["cases"] = vis["cases"];
}
}
// console.log(email_res);
email.emailSending(email_res);
console.log("Email Debug: ");
console.log(email_res["email"]);
console.log(email_res["home"]);
console.log(email_res["home_case"]);
if(email_res["home_case"]){
for(const items in email_res["home_case"]){
console.log(email_res["home_case"][items]);
if(email_res["home_case"][items]===null) continue;
for(const cases in email_res["home_case"][items]["cases"]){
console.log(cases);
}
}
}
}
}
// check place and send emails
async function checkPlace(){
// for (const user of User.find()) {
// await sendEmail(user);
// }
const userres = await User.find().exec();
console.log(userres);
for (const user of userres) {
await sendEmail(user);
}
}
// type
// 1 -> nearby cases
// 2 -> nearby restaurants
async function searchNearby(placeID,type){
console.log("Place ID is: ",placeID);
console.log("Type is: ",type);
return new Promise(async (resolve,reject) => {
await getPlaceByID(placeID).then(place=>{
console.log(">>>>?",place);
const geo = place["geometry"]
if(type == 1){
findCaseByGeo1(geo,true).then(x=>resolve(x));
} else if (type == 2){
} else {
resolve(false);
}
})
});
}
exports.getUser = getUser;
// exports.getInfo = getInfo;
exports.setPlace = setPlace;
exports.findPlace = findPlace;
exports.findPlaceType = findPlaceType;
exports.searchNearby = searchNearby;
// private
exports.textSearch = textSearch;
exports.addCase = addCase;
exports.checkPlace = checkPlace;
exports.sendEmail = sendEmail;
//getByID
exports.getPlaceByID = getPlaceByID;
exports.getUserByID = getUserByID;
exports.getCaseByID = getCaseByID;
//settings
exports.setEmail = setEmail;
exports.deletePlace = deletePlace;