var startlogging = new Array();
startlogging.checkIfIsTheSameHour = function(date1, date2, callback){
if(date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDay() == date2.getDay() && date1.getHours() == date2.getHours()){
callback(true);
} else {
callback(false);
}
}
startlogging.checkIfIsTheSameDay = function(date1, date2, callback){
if(date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDay() == date2.getDay()){
callback(true);
} else {
callback(false);
}
};
startlogging.checkIfIsTheSameMonth = function(date1, date2, callback){
if(date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth()){
callback(true);
} else {
callback(false);
}
};
startlogging.checkIfDayExists = function(date, Day, token, eachAsync, callback){
Day.find({'worker.token': token}, function(err, days){
if(err) return console.error(err);
if(typeof days !== 'undefined' && days.length > 0){
eachAsync(days, function(item, index, done){
startlogging.checkIfIsTheSameDay(item.date, date, function(same){
if(same == true){
callback(true);
} else if(same == false){
callback(false);
}
});
done();
}, function(error){
});
} else {
callback(false);
}
});
};
startlogging.checkIfMonthExists = function(date, Month, token, eachAsync, callback){
Month.find({'worker.token': token}, function(err, months){
if (err) return console.error(err);
if(typeof months !== 'undefined' && months.length > 0){
eachAsync(months, function(item, index, done){
startlogging.checkIfIsTheSameMonth(item.date, date, function(same){
if(same == true){
callback(true);
} else if(same == false){
callback(false);
}
})
});
} else {
callback(false);
}
});
};
startlogging.createMonth = function(date, Month, user, token, eachAsync, randomstring, log, callback){
var create = function(callback){
var newMonth = new Month ({
id: randomstring({length: 18}),
worker: {
token: user.token,
username: user.username
},
date: date,
worked: true,
hours: {
legit: 0,
extra: 0,
total: 0
}
});
newMonth.save(function(err, day){
if (err) return console.error(err);
log.db('Created new Month.');
if(callback){
callback(true);
}
});
};
create(function(success){
if(success == true){
callback(true);
}
});
};
startlogging.createDay = function(date, Day, Month, user, token, eachAsync, randomstring, log, callback){
var create = function(callback){
var newDay = new Day({
id: randomstring({length: 18}),
worker: {
token: user.token,
username: user.username
},
date: date,
worked: true,
hours: {
legit: 0,
extra: 0,
total: 0
}
});
newDay.save(function(err, day){
if (err) return console.error(err);
log.db('Created new Day.');
if(callback){
callback(true);
}
});
};
startlogging.checkIfMonthExists(date, Month, token, eachAsync, function(exists){
if(exists == true){
log.db('Month already exists.');
log.db('Starting Day creation...');
create();
} else if(exists == false){
startlogging.createMonth(date, Month, user, token, eachAsync, randomstring, log, function(success){
if(success == true){
create();
}
});
}
});
callback(true);
};
startlogging.createHour = function(hourDate, Hour, Day, Month, User, user, token, eachAsync, randomstring, log, callback){
log.db('Creating Hour...');
var create = function(callback){
var newHour = new Hour({
id: randomstring({length: 18}),
worker: {
token: user.token,
username: user.username
},
start: hourDate,
end: null
});
newHour.save(function(err, hour){
if (err) return console.error(err);
log.db('Created new Hour.');
if(callback){
callback(true);
}
});
};
log.db('Checking if day exists...');
startlogging.checkIfDayExists(hourDate, Day, token, eachAsync, function(exists){
if(exists == true){
log.db('Day already exists.');
log.db('Starting Hour creation...');
create();
} else if(exists == false){
startlogging.createDay(hourDate, Day, Month, user, token, eachAsync, randomstring, log, function(success){
log.db('Day created.');
log.db('Starting Hour creation...');
create();
});
}
})
callback(true);
};
startlogging.sendSuccessResponse = function(res, log){
res.json({
error: false,
message: 'Successfully started logging!'
});
log.http('Successfully started logging!');
};
startlogging.start = function(req, res, log, User, Month, Day, Hour, eachAsync, randomstring){
var token = req.body.token;
var currentDate = new Date();
log.server('Getting current date...');
if (token){
log.db('Searching for user...');
User.findOne({token: token}, function(err, user){
if (err) return console.error(err);
if(user){
log.db('User found.');
log.db('Searching for Hours...');
Hour.find({'worker.token': token}, function(err, hours){
if (err) return console.error(err);
log.db('Hours loaded.');
if(typeof hours !== 'undefined' && hours.length > 0){
log.db('Searching an Hour in Hours...');
var oneIsTheSame = false;
eachAsync(hours, function(item, index, done){
startlogging.checkIfIsTheSameHour(item.start, currentDate, function(same){
if(same == true){
oneIsTheSame = true;
}
});
done();
}, function(error){
if (error) return console.error(error);
if(oneIsTheSame == true){
var message = 'The Hour you want to add already exists. \n You are already logging your work!';
res.json({
error: true,
message: message
});
log.hours('The hour tried to add already exists.');
} else if(oneIsTheSame == false){
startlogging.createHour(currentDate, Hour, Day, Month, User, user, token, eachAsync, randomstring, log, function(success){
if(log.verbose == true) log.db('Created new hour.');
startlogging.sendSuccessResponse(res, log);
User.update({ token: user.token }, { $set: { 'info.working': true }}, function(err, user){
if (err) return console.error(err);
log.db('Set working to true...');
});
});
}
});
} else {
startlogging.createHour(currentDate, Hour, Day, Month, User, user, token, eachAsync, randomstring, log, function(success){
if(log.verbose == true) log.db('Created new hour.');
startlogging.sendSuccessResponse(res, log);
User.update({ token: user.token }, { $set: { 'info.working': true }}, function(err, user){
if (err) return console.error(err);
log.db('Set working to true...');
});
});
}
});
} else {
res.json({
error: true,
message: 'User does not exists.'
});
log.hours('ERROR! User does not exists!');
}
});
} else {
res.json({
error: true,
message: 'No token provided...'
});
log.hours('ERROR! No token provided!');
}
};
module.exports = startlogging;