Skip to content

Commit

Permalink
#2: isOpenNow filter when set return open restaurants
Browse files Browse the repository at this point in the history
  • Loading branch information
NiteshKumar07091993 committed Jan 14, 2019
1 parent 0a50b28 commit 6f27143
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 91 deletions.
97 changes: 54 additions & 43 deletions models/vendor/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// @flow
import $q from 'q';
import { extend } from 'alfred/services/util';
import { ServerError, ResourceNotFoundError } from 'alfred/core/errors';
import {
extend
} from 'alfred/services/util';
import {
ServerError,
ResourceNotFoundError
} from 'alfred/core/errors';
import User from '../../models/user';

const Vendor = require('../../services/mongo').registerModel(
Expand All @@ -10,7 +15,11 @@ const Vendor = require('../../services/mongo').registerModel(
);

export const createVendor = (user, vendor) => {
const { promise, resolve, reject } = $q.defer();
const {
promise,
resolve,
reject
} = $q.defer();

const savedVendor = new Vendor(vendor);
savedVendor.save(err => {
Expand All @@ -19,11 +28,9 @@ export const createVendor = (user, vendor) => {
}

User.findByIdAndUpdate(
user._id,
{
user._id, {
vendor: savedVendor
},
{
}, {
new: true
},
(err2, updatedUser) => {
Expand All @@ -42,7 +49,11 @@ export const createVendor = (user, vendor) => {
};

export const findVendorById = vendorId => {
const { promise, resolve, reject } = $q.defer();
const {
promise,
resolve,
reject
} = $q.defer();

Vendor.findById(vendorId)
.populate('menu')
Expand All @@ -60,14 +71,16 @@ export const findVendorById = vendorId => {
};

export const updateVendor = (vendorId, params) => {
const { promise, resolve, reject } = $q.defer();
const {
promise,
resolve,
reject
} = $q.defer();

Vendor.findByIdAndUpdate(
vendorId,
{
vendorId, {
$set: params
},
{
}, {
new: true
},
(err, updatedVendor) => {
Expand All @@ -88,37 +101,35 @@ export const listVendors = (params: any) => {
console.log(params);
const { promise, resolve, reject } = $q.defer();

Vendor.find(params)
.populate('menu')
.exec((err, vendors) => {
if (err) {
return reject(new ServerError(err));
}
Vendor.aggregate(params).exec((err, vendors) => {
if (err) {
return reject(new ServerError(err));
}

// if(vendors.length !== 0) {
// console.log(currentDayAndHour);
// if(currentDayAndHour !== {}) {
// console.log("Enter");
// for(let i = 0; i < vendors.length; i++) {
// for(let j = 0; j < vendors[i].hours.length; j++) {
// if(vendors[i].hours[j].dayOfWeek === currentDayAndHour.day &&
// currentDayAndHour.hour >= vendors[i].hours[j].openTimeHour &&
// currentDayAndHour.hour <= vendors[i].hours[j].closeTimeHour
// ) {
// console.log("Found!!!");
// }
// }
// }
//
// // console.log(newVendors);
// //
// // return resolve(newVendors);
// }
// return resolve(vendors);
// }

return resolve(vendors);
});
// if(vendors.length !== 0) {
// console.log(currentDayAndHour);
// if(currentDayAndHour !== {}) {
// console.log("Enter");
// for(let i = 0; i < vendors.length; i++) {
// for(let j = 0; j < vendors[i].hours.length; j++) {
// if(vendors[i].hours[j].dayOfWeek === currentDayAndHour.day &&
// currentDayAndHour.hour >= vendors[i].hours[j].openTimeHour &&
// currentDayAndHour.hour <= vendors[i].hours[j].closeTimeHour
// ) {
// console.log("Found!!!");
// }
// }
// }
//
// // console.log(newVendors);
// //
// // return resolve(newVendors);
// }
// return resolve(vendors);
// }

return resolve(vendors);
});

return promise;
};
Expand Down
181 changes: 133 additions & 48 deletions routes/vendor/list.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
// @flow
import type { $Request, $Response } from 'express';
import { debug } from 'alfred/services/logger';
import { ServerError } from 'alfred/core/errors';
import { listVendors } from '../../models/vendor';
import type, {
$Request,
$Response
} from 'express';
import {
debug
} from 'alfred/services/logger';
import {
ServerError
} from 'alfred/core/errors';
import {
listVendors
} from '../../models/vendor';

module.exports = {
description: 'List all vendors.',
Expand Down Expand Up @@ -33,63 +42,139 @@ module.exports = {
},
async run(req: $Request, res: $Response) {
try {
const params = {};
const currentDayAndHour = {};
const { name, distance, longitude, latitude, activeFilters, pricing } = req.query;
const {
name,
distance,
longitude,
latitude,
activeFilters,
pricing
} = req.query;
const aggQuery = [];

if (latitude && longitude) {
const geo = {
$geoNear: {
near: {
type: 'point',
coordinates: [parseFloat(latitude), parseFloat(longitude)]
},
maxDistance: distance || 50 * 1.60934 * 1000,
spherical: true
}
};
aggQuery.push(geo);
}

const lookUp = {
$lookup: {
from: 'menus',
localField: 'menu',
foreignField: '_id',
as: 'menu'
}
};
aggQuery.push(lookUp);
const matchQuery = {
$match: {}
};
if (name) {
params.name = {
$regex: new RegExp(name.toLowerCase()),
matchQuery.$match.name = {
$regex: name,
$options: 'i'
};
}

if (longitude && latitude) {
const coordinates = [longitude, latitude];
const maxDistance = parseInt(distance || (50 * 1.60934 * 1000));

params['location.coordinates'] = {
$near: {
$geometry: {
type: 'Point',
coordinates
const activeFiltersArray =
activeFilters === '' || !activeFilters
? false
: activeFilters.split(',');
if (activeFiltersArray && activeFiltersArray.indexOf('openNow') !== -1) {
matchQuery.$match.hours = {
$elemMatch: {
dayOfWeek: {
$eq: new Date().getDay()
},
$maxDistance: maxDistance
$or: [
{
openTimeHour: {
$lte: new Date().getHours()
}
},
{
$and: [
{
openTimeHour: {
$eq: new Date().getHours()
}
},
{
openTimeMinutes: {
$gt: new Date().getMinutes()
}
}
]
}
],
$or: [
{
closeTimeHour: {
$gt: new Date().getHours()
}
},
{
$and: [
{
closeTimeHour: {
$eq: new Date().getHours()
}
},
{
closeTimeMinutes: {
$gt: new Date().getMinutes()
}
}
]
}
]
}
};
}

if(activeFilters && activeFilters !== '') {
const array = activeFilters.split(',');
for(let index in array) {
if(array[index] === 'openNow') {
const date = new Date();
params['hours'] = {
$elemMatch: {
dayOfWeek: date.getDay(),
openTimeHour: { $lte: date.getHours() },
closeTimeHour: { $gt: date.getHours() }
}
};
const index = array.indexOf('openNow');
console.log(index);
if(index !== -1) {
array.splice(index, 1);
}
console.log(array);
}
if(array[index] === 'price' && pricing) {
params.pricing = parseInt(pricing);
}
}
if(array.length !== 0) {
params.filters = { $all: array };
}
if (
activeFiltersArray &&
activeFiltersArray.indexOf('price') !== -1 &&
pricing
) {
matchQuery.$match.pricing = parseInt(pricing, 10);
}
aggQuery.push(matchQuery);
aggQuery.push({
$unwind: '$menu'
});
// if (longitude && latitude) {
// const coordinates = [longitude, latitude];
// const maxDistance = parseInt(distance || (50 * 1.60934 * 1000));

// debug('Params: ', JSON.stringify(params), '');
// params['location.coordinates'] = {
// $near: {
// $geometry: {
// type: 'Point',
// coordinates
// },
// $maxDistance: maxDistance
// }
// };
// }

const vendors = await listVendors(params, currentDayAndHour);
// if (activeFilters && activeFilters !== '') {
// // need more clarity
// // if (array.length !== 0) {
// // params.filters = {
// // $all: array
// // };
// // }
// }
const vendors = await listVendors(aggQuery);

res.set({
res_code: 200,
Expand Down

0 comments on commit 6f27143

Please sign in to comment.