This repository has been archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
593 lines (528 loc) · 20 KB
/
routes.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
var router = require('express').Router()
const Business = require('./models/business');
const googleBusiness = require('./models/googleBusiness');
const Message = require('./models/locations.js');
const User = require('./models/users');
const adminSetting = require('./models/AdminSetting');
const userEstablishmentRating = require('./models/userEstablishmentRating');
const axios = require('axios');
var mongoose = require('mongoose');
var cloudinary = require('cloudinary').v2;
const data = require('./jsonData/nightClubs');
const barData = require('./jsonData/bar');
const restaurantData = require('./jsonData/restaurant');
const mixedData = require('./jsonData/mixedThree');
var nodemailer = require("nodemailer");
var crypto = require('crypto');
var schedule = require('node-schedule');
var uniqid = require('uniqid');
const moment = require('moment');
const { findById } = require('./models/users');
cloudinary.config({
cloud_name: 'developer-inn',
api_key: '141611599995991',
api_secret: '-FP9u7Z7cyB3TVM7x5dGpXcfDco'
});
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "[email protected]",
pass: "abubakar032449746"
}
});
var rand,mailOptions,host,link;
router.get('/countAllBusinesses', async function(req, res){
var totalCount = 0;
var businesses = await googleBusiness.find({});
for (let b of businesses){
if( b.opening_hours.weekday_text.length ){
totalCount = totalCount + 1;
}
}
console.log("all businesses", totalCount);
})
router.post('/setDefaultSettings', async function(req, res){
let body = req.body;
let { fun, crowd, ratioInput, difficultyGettingIn, difficultyGettingDrink } = body.rating;
try{
let {rating, ratingChangeTime} = await adminSetting.findOne({ _id: mongoose.Types.ObjectId("600ad7fa8dc42b13b2c0e2da")})
const filter = { _id: mongoose.Types.ObjectId("600ad7fa8dc42b13b2c0e2da") };
if( parseInt(fun) === rating.fun && parseInt(crowd) === rating.crowd && parseInt(difficultyGettingIn) === rating.difficultyGettingIn
&& parseInt(ratioInput) === rating.ratioInput && parseInt(difficultyGettingDrink) === rating.difficultyGettingDrink
){
body.rating = rating;
body.ratingChangeTime = ratingChangeTime;
}
console.log("the body in getting backedn", body);
let updatedDoc = await adminSetting.findOneAndUpdate(filter, body, {
new: true
});
console.log("the updated doc", updatedDoc)
res.send({ data: updatedDoc })
}catch(error){
res.status(400).json({ error: error.toString() });
}
})
router.post('/setScheduleEvent', async function(req, res){
const body = req.body;
body.startJobId = uniqid();
body.endJobId = uniqid();
body.isScheduleApply = true
try{
const filter = { _id: mongoose.Types.ObjectId("600ad7fa8dc42b13b2c0e2da") };
let updatedDoc = await adminSetting.findOneAndUpdate(filter, body, {
new: true
});
let [hours, minutes] = body.scheduleStartTime.split(':')
console.log("the hours", hours)
if(updatedDoc){
let rule = new schedule.RecurrenceRule();
rule.second = 0;
rule.minute = parseInt(minutes);
rule.hour = parseInt(hours);
rule.dayOfWeek = body.noOfscheduleEventInAWeek;
rule.month = [0,11]
rule.tz = moment.tz.guess();
console.log("the rule", rule)
schedule.scheduleJob(body.startJobId, rule, async function () {
console.log("in start")
const filter = { _id: mongoose.Types.ObjectId("600ad7fa8dc42b13b2c0e2da") };
await adminSetting.findOneAndUpdate(filter, { isRunning: true }, {
new: true
});
})
let endRule = new schedule.RecurrenceRule();
hours = body.scheduleEndTime.split(':')[0]
minutes = body.scheduleEndTime.split(':')[1]
endRule.second = 0;
endRule.minute = minutes;
endRule.hour = hours;
endRule.dayOfWeek = body.noOfscheduleEventInAWeek;
endRule.month = [0,11]
endRule.tz = moment.tz.guess();
schedule.scheduleJob(body.endJobId, endRule, async function () {
console.log("in close")
const filter = { _id: mongoose.Types.ObjectId("600ad7fa8dc42b13b2c0e2da") };
await adminSetting.findOneAndUpdate(filter, { isRunning: false }, {
new: true
});
})
res.send({ data: updatedDoc })
}
}catch(error){
res.status(400).json({ error: error.toString() });
}
})
router.post('/stopScheduleEvent', async function(req, res){
try{
const filter = { _id: mongoose.Types.ObjectId("600ad7fa8dc42b13b2c0e2da") };
const updatedSchedule = await adminSetting.findOneAndUpdate(filter, { isRunning:false, isScheduleApply : false }, {
new: true
});
console.log("the updated schedule", updatedSchedule)
let startingCurrentJob = schedule.scheduledJobs[updatedSchedule.startJobId]
let endingCurrentJob = schedule.scheduledJobs[updatedSchedule.endJobId]
startingCurrentJob.cancel();
endingCurrentJob.cancel();
res.send({ data: updatedSchedule })
}catch(error){
console.log("on stoping error", error)
res.status(400).json({ error: error.toString() });
}
})
router.post("/createSettings", async function(req, res){
const userSetting = {
scheduleStartTime: "13:10",
scheduleEndTime: "16:00",
startJobId: "231",
endJobId: "4623",
rating: {
fun: 2,
crowd: 2,
ratioInput: 3,
difficultyGettingIn: 2,
difficultyGettingDrink : 2
},
noOfUsersUntilShowDefault: 3,
noOfscheduleEventInAWeek: [1, 2]
}
const getSettings = new adminSetting(userSetting)
const userCreated = await getSettings.save();
res.send({ settings: userCreated })
})
router.get('/getdefaultSettings', async function(req, res){
const getAdminSettings = await adminSetting.find();
console.log("the get admin sett", getAdminSettings)
res.send({ settings: getAdminSettings[0] })
})
router.post('/updateMapPinColor', async function(req, res){
const filter = { _id: mongoose.Types.ObjectId("600ad7fa8dc42b13b2c0e2da") };
console.log("here comess... the data", req.body.vibeCategories)
const updatedMapPinsData = await adminSetting.findOneAndUpdate(filter, { isRunning:true, vibeCategoryPinsColor: req.body.vibeCategories}, {
new: true
});
console.log("here the updateMap", updatedMapPinsData)
res.send({ settings: updatedMapPinsData })
})
router.post('/sendEmailVerification',async function(req, res){
var mykey = crypto.createCipher('aes-128-cbc', 'Halting');
let randomString = Math.random().toString(36).substring(7);
console.log("random", req.body);
var mystr = mykey.update(randomString, 'utf8', 'hex')
mystr += mykey.final('hex');
const { userId } = req.body;
console.log("the request body", userId)
const user = await User.findById(mongoose.Types.ObjectId(userId))
console.log("the user", user)
if(!user)
throw new Error("User Not Found")
const update = {
isVerified: {
encodeData: randomString,
verify: false
}
}
const filter = { _id: mongoose.Types.ObjectId(user._id) };
try{
let updatedDoc = await User.findOneAndUpdate( filter ,update, {
new: true
});
if(updatedDoc){
host=req.get('host');
link="http://"+req.get('host')+"/verify?id="+mystr+`&email=${user.email}`;
mailOptions={
to : user.email,
subject : "Please confirm your Email account",
html : `Hello ${user.firstName},<br> Please Click on the link to verify your email.<br><a href="${link}">Click here to verify</a>`
}
smtpTransport.sendMail(mailOptions, function(error, response){
console.log("the error", error)
if(error)
res.send({ message: "Email Could not send Successfully", status: 500 });
else
res.send({ message: "Email send Successfully", status: 200 });
})
}
else
res.send( { message: "Email Could not send Successfullyss", status: 500 })
}catch(err){
console.log("the error", err)
}
});
router.get('/verify', async function(req,res){
console.log(req.protocol+":/"+req.get('host'));
var mykey = crypto.createDecipher('aes-128-cbc', 'Halting');
const { id: hashedId, email } = req.query;
var mystr = mykey.update(hashedId, 'hex', 'utf8')
mystr += mykey.final('utf8');
const user = await User.findOne({ email })
if(!user)
res.end("Unauthorized User")
console.log("the user", user)
const { isVerified } = user;
if((req.protocol+"://"+req.get('host'))==("http://"+host)){
console.log("Domain is matched. Information is from Authentic email");
console.log(`the random var ${mystr} and query value id is ${email} `)
if( isVerified.encodeData === mystr){
const update = {
isVerified: {
encodeData: null,
verify: true
}
}
const filter = { _id: mongoose.Types.ObjectId(user._id) };
try{
let updatedDoc = await User.findOneAndUpdate( filter ,update, {
new: true
});
if(updatedDoc){
console.log("email is verified");
res.end(`Your ${updatedDoc.email} is verified`)
}
}catch(err){
res.end("Email Not verified")
}
}
else
res.end("<h1>Bad Request</h1>");
}
else
res.end("<h1>Request is from unknown source");
});
router.post('/uploadAllBusinessPhotos', async function(req, res) {
const getAllBusinesses = await Business.find({}).populate('googleBusiness').skip(2000).limit(479)
const totalNames = []
getAllBusinesses.map(async(business)=>{
if(business.googleBusiness.photos && business.googleBusiness.photos.length > 1){
const allPhotos = business.googleBusiness.photos;
totalNames.push(business.placeId)
const uploadAll = await uploadAllPhotos(allPhotos);
console.log("the upload all", uploadAll)
const filter = { placeId: business.placeId };
const update = {
uploadedPhotos: uploadAll
}
let updatedDoc = await Business.findOneAndUpdate(filter, update, {
new: true
});
console.log("the updated doc", updatedDoc);
}
})
res.send({
totalNames: totalNames
})
})
router.get('/updateBusiness', async function(req, res){
let businessSelectedCategories = [];
const { place_id } = req.query
const { category_id } = req.query
businessSelectedCategories.push(mongoose.Types.ObjectId(category_id));
const filter = { placeId : place_id};
const update = {
category: businessSelectedCategories,
}
let updatedDoc = await Business.findOneAndUpdate(filter, update, {
new: true
});
console.log("the updated doc", updatedDoc);
res.send(updatedDoc)
})
router.post('/storeData', function(req, res) {
let totalNames = [];
let totalResults = [];
let totalCountResults = 0;
mixedData.data.map(async(business) => {
let businessData = await googleBusiness.findOne({ place_id: business.placeId })
if(!businessData){
const businessDetails = await getDetailsOfBusiness(business.placeId);
console.log("the business detais", businessDetails)
if(businessDetails){
console.log("the business Detaials", businessDetails)
totalNames.push(businessDetails.name)
// console.log("the total Name", totalNames)
totalResults.push(businessDetails)
totalCountResults = totalCountResults + 1;
const { location } = businessDetails.geometry;
let googleData = new googleBusiness(businessDetails)
const getGoogleData = await googleData.save();
let uploadPhotoResult = {};
if(getGoogleData.photos && getGoogleData.photos.length > 0 )
uploadPhotoResult = await uploadAllPhotos(getGoogleData.photos)
console.log("the upload photoresult", uploadPhotoResult)
let businessLocal = new Business( {
placeId: getGoogleData.place_id,
category: [],
name: googleData.name,
googleBusiness : mongoose.Types.ObjectId(googleData._id),
location: {
type: "Point",
coordinates: [location.lng, location.lat ] //[longitude, latitude]
},
uploadedPhotos : uploadPhotoResult
});
await businessLocal.save();
}
}
console.log("the totoal names", totalNames)
})
res.send({
totalNames,
totalResults,
totalCountResults
});
});
router.get('/getBusiness', async (req, res)=>{
const getBusiness = await Business.findById(mongoose.Types.ObjectId('5fd83e7674710e0b68528ae4')).populate('googleBusiness')
console.log("get business", getBusiness)
res.send(getBusiness);
})
router.get('/uploadPhoto', async(req, res) => {
// const photoReference = 'ATtYBwK6BOJcWTN_bZVK7xryLg1caQHSb5dkU8059rWW6c4HNBg7Daq-3KizjLJTSloAtjejn3aAXn_eMh_EuUZtzM8ZUtXx0u3i0gqGxgvs8yyuMOzVuAFYgMdb1ZycwZi7kS3mGzMyUdAPaktiyB8GlzoRm-sI3Fog7-KZ8fhtl4g8Xpia'
const finalString = `https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=${photoReference}&key=AIzaSyAx3VG1J8LBgf02sGaIQOoEVgf71m4XP2w`;
console.log(`the final string:: ${finalString}`)
try{
const getUrl = await cloudinary.uploader.upload(finalString);
console.log("the get url::", getUrl);
}
catch(err){
console.log("the err", err)
}
})
router.get('/getGoogleMapsResults', async function (req, res, next) {
const latitude = req.query.lat;
const longitude = req.query.lon;
const radius = req.query.radius;
const {business_type} = req.query;
const { search_text } = req.query;
let detailCall = 1;
let totalNames = [];
let totalResults = [];
let totalCountResults = 0
let noOfCalls = 50000;
let pageToken = true;
let first = true;
while(first || (pageToken && noOfCalls !== 0) ){
// console.log("the page token", pageToken)
let { data, nextPageToken } = await getAnother(latitude, longitude, radius, business_type, pageToken, first)
// let { data, nextPageToken } = await getBySearchText( search_text, pageToken, first, business_type)
if (first)
first = false;
data.map(async(business) => {
let businessData = await googleBusiness.findOne({ place_id: business.place_id })
if(!businessData){
const businessDetails = await getDetailsOfBusiness(business.place_id);
totalNames.push(businessDetails.name)
console.log("the total Name", totalNames)
totalResults.push(businessDetails)
totalCountResults = totalCountResults + 1;
const { location } = businessDetails.geometry;
let googleData = new googleBusiness(businessDetails)
const getGoogleData = await googleData.save();
let uploadPhotoResult = {};
if(getGoogleData.photos && getGoogleData.photos.length > 0 )
uploadPhotoResult = await uploadPhoto(getGoogleData.photos[0].photo_reference)
let businessLocal = new Business( {
placeId: getGoogleData.place_id,
category: [],
name: googleData.name,
googleBusiness : mongoose.Types.ObjectId(googleData._id),
location: {
type: "Point",
coordinates: [location.lng, location.lat ] //[longitude, latitude]
},
uploadedPhotos : [uploadPhotoResult]
});
await businessLocal.save();
}
})
await delay();
if(!nextPageToken)
pageToken = null
else
pageToken = nextPageToken;
if(noOfCalls === 0)
pageToken = false;
noOfCalls = noOfCalls - 1;
}
res.send({
totalNames,
totalResults,
totalCountResults
});
})
const uploadPhoto = async(photoReference) => {
const finalString = `https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=${photoReference}&key=AIzaSyAx3VG1J8LBgf02sGaIQOoEVgf71m4XP2w`;
try{
const result = await cloudinary.uploader.upload(finalString);
return result;
}
catch(err){
console.log("the err", err)
}
}
const uploadAllPhotos = async(photos) => {
const totalphotosUrl = [];
let i = 1
for(let photo of photos){
const finalString = `https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=${photo.photo_reference}&key=AIzaSyAx3VG1J8LBgf02sGaIQOoEVgf71m4XP2w`;
try{
if(i !== 0){
const result = await cloudinary.uploader.upload(finalString);
totalphotosUrl.push(result)
}
i = i-1;
}
catch(err){
console.log("the err", err)
}
}
return totalphotosUrl;
}
const getDetailsOfBusiness = async (placeId)=>{
console.log("the place id", placeId)
const details = await axios.get(`https://maps.googleapis.com/maps/api/place/details/json?place_id=${placeId}&key=AIzaSyAx3VG1J8LBgf02sGaIQOoEVgf71m4XP2w`);
return details.data.result
}
const delay = (args) => new Promise((resolve) => {
setTimeout(()=>{ resolve('ok') }, 1500);
});
const getAnother = async(latitude, longitude, radius, business_type, pageToken, first) => {
console.log(`the latitude ${latitude} the longitude: ${longitude} the radius: ${radius} and businesType: ${business_type} `)
let getBusiness = '';
if(!pageToken || first )
getBusiness = await axios.get(`https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${latitude},${longitude}&radius=${radius}&type=${business_type}&key=AIzaSyAx3VG1J8LBgf02sGaIQOoEVgf71m4XP2w`);
else{
// console.log("in comingg")
getBusiness = await axios.get(`https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${latitude},${longitude}&radius=${radius}&type=${business_type}&key=AIzaSyAx3VG1J8LBgf02sGaIQOoEVgf71m4XP2w&pagetoken=${pageToken}`);
}
console.log("the page token fetched", getBusiness.data.next_page_token);
return { data: getBusiness.data.results,
nextPageToken: getBusiness.data.next_page_token
}
}
const getBySearchText = async(searchText, pageToken, first, type) => {
console.log(`the type ${type} and search text ${searchText} `)
let getBusiness = '';
if(!pageToken || first )
getBusiness = await axios.get(`https://maps.googleapis.com/maps/api/place/textsearch/json?query=${searchText}&key=AIzaSyAx3VG1J8LBgf02sGaIQOoEVgf71m4XP2w`);
else{
console.log("in comingg")
getBusiness = await axios.get(`https://maps.googleapis.com/maps/api/place/textsearch/json?query=${searchText}&key=AIzaSyAx3VG1J8LBgf02sGaIQOoEVgf71m4XP2w&pagetoken=${pageToken}`);
}
console.log("the page toke token next", getBusiness.data.next_page_token);
return { data: getBusiness.data.results,
nextPageToken: getBusiness.data.next_page_token
}
}
router.get('/getSinglePlaceResult', async function (req, res, next) {
const { place_id } = req.query
console.log("the params in backend", req.query.place_id);
const getData = await axios.get(`https://maps.googleapis.com/maps/api/place/details/json?place_id=${req.query.place_id}&key=AIzaSyAx3VG1J8LBgf02sGaIQOoEVgf71m4XP2w`);
console.log("the resultss", getData.data.result)
res.send(getData.data.result);
})
router.post('/createMessage', async function(req, res, next){
var message = new Message({
username: "nayyar",
text: "mcdonalds",
location: {
type: "Point",
coordinates: [-109.110492, 35.021112 ] //[longitude, latitude]
},
});
message.save((err, message) => {
if (err) console.log(err);
console.log(message);
res.send(message)
});
})
router.get('/getNearLocations', async function(req, res, next){
const latitude = req.query.lat;
const longitude = req.query.lon;
const radius = req.query.radius;
const getData = await Business.find({
location: {
$near: {
$maxDistance: radius,
$geometry: {
type: "Point",
coordinates: [longitude, latitude ]
}
}
}
}).find().populate('googleBusiness').limit(60)
console.log("the getdata", getData);
res.send( { totalCount: getData});
})
router.get('/getSearchTextResult', async function (req, res, next) {
const { search_text } = req.query
console.log("the params in backend", search_text);
try{
const getData = await axios.get(`https://maps.googleapis.com/maps/api/place/textsearch/json?query=${search_text}&key=AIzaSyD9CLs9poEBtI_4CHd5Y8cSHklQPoCi6NM`);
console.log("the resultss", getData)
res.send(getData.data.results);
}catch(err){
console.log("the error", err.response);
}
})
module.exports = router