From 3245c72d6b455d310e6922a1245a003b033362b6 Mon Sep 17 00:00:00 2001 From: Karim Bounekhla Date: Wed, 30 Dec 2020 23:22:35 -0700 Subject: [PATCH 1/6] Added support for filters on Mapathon page. Parameters: - sortReviews (sort by # of reviews) - sortDate (sort by date) - latitude - longitude - radius (find mapathons within certain radius of lat/long) - hideZeroReviews (hide mapathons with no reviews) --- src/routes/events/list-events.js | 35 ++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/routes/events/list-events.js b/src/routes/events/list-events.js index 9521d85..200faa2 100644 --- a/src/routes/events/list-events.js +++ b/src/routes/events/list-events.js @@ -41,7 +41,33 @@ module.exports = async (req, res, next) => { eventsQuery.startDate = { $lte: beforeDate }; } - let sortBy = queryParams.sortBy || '-startDate'; + const sortObj = {}; + if (queryParams.sortReviews) { + sortObj.reviewsAmount = Number(queryParams.sortReviews); + } + + if (queryParams.sortDate) { + sortObj.startDate = Number(queryParams.sortDate); + } else { + sortObj.startDate = -1; + } + + const EQUATORIAL_RADIUS = 3963.2; + if (queryParams.latitude && queryParams.longitude && queryParams.radius) { + eventsQuery.location = { + $geoWithin: { + $centerSphere: [ + [Number(queryParams.longitude), Number(queryParams.latitude)], + Number(queryParams.radius) / EQUATORIAL_RADIUS + ] + } + }; + } + + if (queryParams.hideZeroReviews) { + eventsQuery.reviewsAmount = { $gte: 1 }; + } + let page = queryParams.page ? queryParams.page - 1 : 0; const pageLimit = queryParams.pageLimit || 12; @@ -60,9 +86,10 @@ module.exports = async (req, res, next) => { poster: 1, reviewsAmount: 1, reviewsGoal: 1, - startDate: 1 + startDate: 1, + location: 1 }) - .sort(sortBy) + .sort(sortObj) .skip(page * pageLimit) .limit(pageLimit), Event.find(eventsQuery).count() @@ -89,7 +116,7 @@ module.exports = async (req, res, next) => { lastPage, pageLimit, total, - sortBy, + sortObj, results: events }); }; From c87b1bd227fa1453bb626142753a23b83c16e13f Mon Sep 17 00:00:00 2001 From: Karim Bounekhla Date: Wed, 30 Dec 2020 23:47:33 -0700 Subject: [PATCH 2/6] Condensed lat/long parameters into one location parameter and added validations --- src/routes/events/list-events.js | 11 +++--- src/routes/events/validations.js | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/routes/events/list-events.js b/src/routes/events/list-events.js index 200faa2..5747df5 100644 --- a/src/routes/events/list-events.js +++ b/src/routes/events/list-events.js @@ -43,22 +43,23 @@ module.exports = async (req, res, next) => { const sortObj = {}; if (queryParams.sortReviews) { - sortObj.reviewsAmount = Number(queryParams.sortReviews); + sortObj.reviewsAmount = parseFloat(queryParams.sortReviews); } if (queryParams.sortDate) { - sortObj.startDate = Number(queryParams.sortDate); + sortObj.startDate = parseFloat(queryParams.sortDate); } else { sortObj.startDate = -1; } const EQUATORIAL_RADIUS = 3963.2; - if (queryParams.latitude && queryParams.longitude && queryParams.radius) { + if (queryParams.location && queryParams.radius) { + const coordinates = queryParams.location.split(','); eventsQuery.location = { $geoWithin: { $centerSphere: [ - [Number(queryParams.longitude), Number(queryParams.latitude)], - Number(queryParams.radius) / EQUATORIAL_RADIUS + [coordinates[1], coordinates[0]], + parseFloat(queryParams.radius) / EQUATORIAL_RADIUS ] } }; diff --git a/src/routes/events/validations.js b/src/routes/events/validations.js index 2b15620..6ac6677 100644 --- a/src/routes/events/validations.js +++ b/src/routes/events/validations.js @@ -485,6 +485,72 @@ module.exports = { } } + if (queryParams.sortReviews) { + if (!isNumber(queryParams.sortReviews)) { + errors.sortReviews = 'Should be a number'; + } else if ( + parseFloat(queryParams.sortReviews) !== 1 && + parseFloat(queryParams.sortReviews) !== -1 + ) { + errors.sortReviews = 'Should be 1 (ascending) or -1 (descending)'; + } + } + + if (queryParams.sortDate) { + if (!isNumber(queryParams.sortDate)) { + errors.sortDate = 'Should be a number'; + } else if ( + parseFloat(queryParams.sortDate) !== 1 && + parseFloat(queryParams.sortDate) !== -1 + ) { + errors.sortDate = 'Should be 1 (ascending) or -1 (descending)'; + } + } + + if (!queryParams.location) { + errors.location = 'Is required'; + } else { + const location = queryParams.location.split(','); + + if (location.length !== 2) { + errors.location = 'Should have two coordinates'; + } else if (!location[0]) { + errors.location = 'Latitude is required'; + } else if (!isNumber(location[0])) { + errors.location = 'Latitude should be a number'; + } else if ( + parseFloat(location[0]) < -90 || + parseFloat(location[0]) > 90 + ) { + errors.location = 'Latitude value out of bounds'; + } else if (!location[1]) { + errors.location = 'Longitude is required'; + } else if (!isNumber(location[1])) { + errors.location = 'Longitude should be a number'; + } else if ( + parseFloat(location[1]) < -180 || + parseFloat(location[1]) > 180 + ) { + errors.location = 'Longitude value out of bounds'; + } + } + + if (queryParams.radius) { + if (!isNumber(queryParams.radius)) { + errors.radius = 'Should be a number'; + } else if (parseFloat(queryParams.radius) <= 0) { + errors.radius = 'Radius should be greater than 0 miles'; + } + } + + if (queryParams.hideZeroReviews) { + if (!isNumber(queryParams.hideZeroReviews)) { + errors.hideZeroReviews = 'Should be a number'; + } else if (parseFloat(queryParams.hideZeroReviews) !== 1) { + errors.hideZeroReviews = 'Should be 0 (false) or 1 (true)'; + } + } + return { errors, isValid: isEmpty(errors) }; } }; From db729e23d315f12cc48c1d3c394591c6a3f6f13a Mon Sep 17 00:00:00 2001 From: Karim Bounekhla Date: Wed, 30 Dec 2020 23:52:27 -0700 Subject: [PATCH 3/6] Fixed hideZeroReviews condition --- src/routes/events/list-events.js | 5 ++++- src/routes/events/validations.js | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/routes/events/list-events.js b/src/routes/events/list-events.js index 5747df5..c8677bd 100644 --- a/src/routes/events/list-events.js +++ b/src/routes/events/list-events.js @@ -65,7 +65,10 @@ module.exports = async (req, res, next) => { }; } - if (queryParams.hideZeroReviews) { + if ( + queryParams.hideZeroReviews && + parseFloat(queryParams.hideZeroReviews) === 1 + ) { eventsQuery.reviewsAmount = { $gte: 1 }; } diff --git a/src/routes/events/validations.js b/src/routes/events/validations.js index 6ac6677..58bbd1d 100644 --- a/src/routes/events/validations.js +++ b/src/routes/events/validations.js @@ -546,7 +546,10 @@ module.exports = { if (queryParams.hideZeroReviews) { if (!isNumber(queryParams.hideZeroReviews)) { errors.hideZeroReviews = 'Should be a number'; - } else if (parseFloat(queryParams.hideZeroReviews) !== 1) { + } else if ( + parseFloat(queryParams.hideZeroReviews) !== 1 && + parseFloat(queryParams.hideZeroReviews) !== 0 + ) { errors.hideZeroReviews = 'Should be 0 (false) or 1 (true)'; } } From 73979b6d3b1776a256c6a945d5038e7c3f6ee221 Mon Sep 17 00:00:00 2001 From: Karim Bounekhla Date: Sat, 2 Jan 2021 22:09:53 -0700 Subject: [PATCH 4/6] Added parsing of lat/long and updated validations to make location not required --- src/routes/events/list-events.js | 2 +- src/routes/events/validations.js | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/routes/events/list-events.js b/src/routes/events/list-events.js index c8677bd..606a0b6 100644 --- a/src/routes/events/list-events.js +++ b/src/routes/events/list-events.js @@ -58,7 +58,7 @@ module.exports = async (req, res, next) => { eventsQuery.location = { $geoWithin: { $centerSphere: [ - [coordinates[1], coordinates[0]], + [parseFloat(coordinates[1]), parseFloat(coordinates[0])], parseFloat(queryParams.radius) / EQUATORIAL_RADIUS ] } diff --git a/src/routes/events/validations.js b/src/routes/events/validations.js index 58bbd1d..c11a4c2 100644 --- a/src/routes/events/validations.js +++ b/src/routes/events/validations.js @@ -507,9 +507,7 @@ module.exports = { } } - if (!queryParams.location) { - errors.location = 'Is required'; - } else { + if (queryParams.location) { const location = queryParams.location.split(','); if (location.length !== 2) { From 54f2c77ee2760440f7e40897aba17df4d12a601c Mon Sep 17 00:00:00 2001 From: parsahonarmand Date: Mon, 1 Feb 2021 20:42:38 -0700 Subject: [PATCH 5/6] Hide inactive & searched location radius added --- src/routes/events/list-events.js | 52 ++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/src/routes/events/list-events.js b/src/routes/events/list-events.js index 606a0b6..c6ccd26 100644 --- a/src/routes/events/list-events.js +++ b/src/routes/events/list-events.js @@ -1,5 +1,5 @@ const moment = require('moment'); - +const axios = require('axios'); const { Event } = require('../../models/event'); const { validateListEvents } = require('./validations'); @@ -54,15 +54,39 @@ module.exports = async (req, res, next) => { const EQUATORIAL_RADIUS = 3963.2; if (queryParams.location && queryParams.radius) { - const coordinates = queryParams.location.split(','); - eventsQuery.location = { - $geoWithin: { - $centerSphere: [ - [parseFloat(coordinates[1]), parseFloat(coordinates[0])], - parseFloat(queryParams.radius) / EQUATORIAL_RADIUS - ] - } - }; + if (queryParams.keywords) { + axios + .get( + `https://maps.googleapis.com/maps/api/geocode/json?address=${ + queryParams.keywords + }&key=${process.env.PLACES_API_KEY}` + ) + .then(res => { + const locationObj = res.data.results[0].geometry.location; + const coordinates = [locationObj.latitude, locationObj.longitude]; + eventsQuery.location = { + $geoWithin: { + $centerSphere: [ + [parseFloat(coordinates[1]), parseFloat(coordinates[0])], + parseFloat(queryParams.radius) / EQUATORIAL_RADIUS + ] + } + }; + }) + .catch(error => { + console.log(error); + }); + } else { + const coordinates = queryParams.location.split(','); + eventsQuery.location = { + $geoWithin: { + $centerSphere: [ + [parseFloat(coordinates[1]), parseFloat(coordinates[0])], + parseFloat(queryParams.radius) / EQUATORIAL_RADIUS + ] + } + }; + } } if ( @@ -72,6 +96,14 @@ module.exports = async (req, res, next) => { eventsQuery.reviewsAmount = { $gte: 1 }; } + if ( + queryParams.hideInactiveMapathons && + parseFloat(queryParams.hideInactiveMapathons) === 1 + ) { + eventsQuery.startDate = { $lte: new Date() }; + eventsQuery.endDate = { $gte: new Date() }; + } + let page = queryParams.page ? queryParams.page - 1 : 0; const pageLimit = queryParams.pageLimit || 12; From 1cbc7960e76599f19e6db076524c548f2b740951 Mon Sep 17 00:00:00 2001 From: parsahonarmand Date: Tue, 2 Feb 2021 20:17:45 -0700 Subject: [PATCH 6/6] Fixed a bug for searched location query --- src/routes/events/list-events.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/events/list-events.js b/src/routes/events/list-events.js index c6ccd26..7450093 100644 --- a/src/routes/events/list-events.js +++ b/src/routes/events/list-events.js @@ -63,11 +63,11 @@ module.exports = async (req, res, next) => { ) .then(res => { const locationObj = res.data.results[0].geometry.location; - const coordinates = [locationObj.latitude, locationObj.longitude]; + const coordinates = [locationObj.lat, locationObj.lng]; eventsQuery.location = { $geoWithin: { $centerSphere: [ - [parseFloat(coordinates[1]), parseFloat(coordinates[0])], + [coordinates[1], coordinates[0]], parseFloat(queryParams.radius) / EQUATORIAL_RADIUS ] }