Skip to content

Instantly share code, notes, and snippets.

@gonzalomelov
Created October 25, 2016 14:44
Show Gist options
  • Select an option

  • Save gonzalomelov/660520b6e8109fceaa16d382cffa558b to your computer and use it in GitHub Desktop.

Select an option

Save gonzalomelov/660520b6e8109fceaa16d382cffa558b to your computer and use it in GitHub Desktop.
var moment = require('moment');
var fs = require("fs");
var mongoose = require( 'mongoose' );
//read conf json file
var conf = JSON.parse(fs.readFileSync('./config.json', 'utf8'))[process.env.NODE_ENV || 'development' ];
console.log(conf);
global.__base = __dirname + '/';
global.kona = {};
global.kona.model = require('./lib/konaModel');
global.kona.conf = conf;
global.kona.util = {};
global.kona.util.parseInt = function(obj) {
if (obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
obj[key] = parseInt(obj[key]);
}
}
}
return obj;
};
var options = {
server: { poolSize: 5, socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
config: { autoIndex: false }
};
mongoose.connect(conf.dataBase.url, options);
mongoose.connection.on('error', function (err) {
console.log(err);
});
mongoose.connection.on('connected', function () {
console.log('Mongoose default connection open');
});
var Event = kona.model.open('event');
Event
.where({ repeat: true , peopleGoing : {$gt : 0} })
.populate('country')
.find()
.exec(function(err, events) {
//all repeated events
console.log('removeEventPeople');
console.log(events.length);
events.forEach(function(event) {
var dow = parseInt(moment.tz(new Date(), event.country.tz || 'America/Montevideo').day());
var hour = parseInt(moment.tz(new Date(), event.country.tz || 'America/Montevideo').format('HH'));
var todayIndex;
switch (dow) {
case 0:
todayIndex = 6;
break;
case 1:
todayIndex = 0;
break;
case 2:
todayIndex = 1;
break;
case 3:
todayIndex = 2;
break;
case 4:
todayIndex = 3;
break;
case 5:
todayIndex = 4;
break;
case 6:
todayIndex = 5;
break;
}
if (event.days[todayIndex].repeat) {
//the event runs today
var startHour = parseInt(event.startTime.split(":")[0]);
var endHour = parseInt(event.endTime.split(":")[0]);
if (hour > endHour) {
//remove the people and save event
// Event.update({ _id: event._id }, { people: [], peoplePreview: [], peopleGoing: 0 }, function(err, result) {
// });
console.log("Remove: " + event._id);
} else {
if (endHour > startHour) {
//the event ends tomorrow
if (hour > startHour) {
//remove the people and save event
// Event.update({ _id: event._id }, { people: [], peoplePreview: [], peopleGoing: 0 }, function(err, result) {
// });
console.log("Remove ends tomorrow: " + event._id);
}
}
}
}
});
mongoose.connection.close(function () {
console.log('Mongoose default connection disconnected through app termination');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment