diff --git a/models/vendor/index.js b/models/vendor/index.js index 65ef3af..f9bb8ff 100755 --- a/models/vendor/index.js +++ b/models/vendor/index.js @@ -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( @@ -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 => { @@ -19,11 +28,9 @@ export const createVendor = (user, vendor) => { } User.findByIdAndUpdate( - user._id, - { + user._id, { vendor: savedVendor - }, - { + }, { new: true }, (err2, updatedUser) => { @@ -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') @@ -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) => { @@ -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; }; diff --git a/routes/vendor/list.js b/routes/vendor/list.js index 054b68a..94b53bd 100755 --- a/routes/vendor/list.js +++ b/routes/vendor/list.js @@ -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.', @@ -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,