Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added API support for filters on Mapathon page. #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 68 additions & 5 deletions src/routes/events/list-events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const moment = require('moment');

const axios = require('axios');
const { Event } = require('../../models/event');

const { validateListEvents } = require('./validations');
Expand Down Expand Up @@ -41,7 +41,69 @@ module.exports = async (req, res, next) => {
eventsQuery.startDate = { $lte: beforeDate };
}

let sortBy = queryParams.sortBy || '-startDate';
const sortObj = {};
if (queryParams.sortReviews) {
sortObj.reviewsAmount = parseFloat(queryParams.sortReviews);
}

if (queryParams.sortDate) {
sortObj.startDate = parseFloat(queryParams.sortDate);
} else {
sortObj.startDate = -1;
}

const EQUATORIAL_RADIUS = 3963.2;
if (queryParams.location && queryParams.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.lat, locationObj.lng];
eventsQuery.location = {
$geoWithin: {
$centerSphere: [
[coordinates[1], 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 (
queryParams.hideZeroReviews &&
parseFloat(queryParams.hideZeroReviews) === 1
) {
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;

Expand All @@ -60,9 +122,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()
Expand All @@ -89,7 +152,7 @@ module.exports = async (req, res, next) => {
lastPage,
pageLimit,
total,
sortBy,
sortObj,
results: events
});
};
67 changes: 67 additions & 0 deletions src/routes/events/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,73 @@ 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) {
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 &&
parseFloat(queryParams.hideZeroReviews) !== 0
) {
errors.hideZeroReviews = 'Should be 0 (false) or 1 (true)';
}
}

return { errors, isValid: isEmpty(errors) };
}
};