Skip to content

Instantly share code, notes, and snippets.

@ykabbara
Created October 23, 2018 18:05
Show Gist options
  • Save ykabbara/028827251640bd818bc05de82ecc0df6 to your computer and use it in GitHub Desktop.
Save ykabbara/028827251640bd818bc05de82ecc0df6 to your computer and use it in GitHub Desktop.
const Tour = require('../models/tour.model.js');
exports.searchPackages = async (req, res)=>{
const searchParams = req.query;
const kids= searchParams.kids || "2";
const solo =searchParams.solo || "2";
let startTime = searchParams.startTime || 0;
let endTime = searchParams.endTime || 0;
let day = (endTime - startTime) / (24*3600*1000) - 1; // divide by one day in seconds
const city = searchParams.city || 'istanbul';
const country = searchParams.country || 'turkey';
const packageObjects = [];
startTime = parseInt(startTime, 10);
endTime = parseInt(endTime, 10);
//TODO add first day and last day to tours while creating a package.
//TODO add free day if tours less than days number.
const searchQuery = {
kids:{$in: [kids, "2"]},
solo:{$in: [solo, "2"]},
city,
country,
};
if (day < 1) {
day = 1;
}
const editorsChoice = await Tour.find({
...searchQuery,
})
.sort({editorRate:-1})
.limit(day);
packageObjects.push({
name: 'Editor\'s Choice',
tours: editorsChoice,
startTime,
endTime,
});
const relaxingTours = await Tour.find({
...searchQuery,
category: {$all :'Relaxing'}
}).limit(day);
if(relaxingTours.length !== 0) {
packageObjects.push({
name: 'Relaxing',
tours: relaxingTours,
startTime,
endTime,
});
}
const shopingPackages = await Tour.find({
...searchQuery,
category: {$all :'Shopping'}
}).limit(day);
if(shopingPackages.length !== 0) {
packageObjects.push({
name: 'Shopping',
tours: shopingPackages,
startTime,
endTime,
});
}
const sightSeeingPackages = await Tour.find({
...searchQuery,
category: {$all :'Sightseeing'}
}).limit(day);
if(sightSeeingPackages.length !== 0) {
packageObjects.push({
name: 'Sightseeing',
tours: sightSeeingPackages,
startTime,
endTime,
});
}
const activitesPackage = await Tour.find({
...searchQuery,
category: {$all :'Activities'}
}).limit(day);
if(activitesPackage.length !== 0) {
packageObjects.push({
name: 'Activities',
tours: activitesPackage,
startTime,
endTime,
});
}
res.send(packageObjects);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment