Added exceptions

This commit is contained in:
Dobie Wollert
2015-12-30 04:41:59 -08:00
parent f097af33f5
commit 26111f4697
2 changed files with 82 additions and 1 deletions

View File

@ -82,6 +82,23 @@ function findUserSpans(user, day) {
.exec();
}
function findUserSpansForWeek(user, day) {
var startOfDay = day.clone().startOf('week').toDate();
var endOfDay = day.clone().endOf('week').toDate();
var query = {
start: {
'$gte': startOfDay,
'$lte': endOfDay
},
user: user
};
return TimeClockSpan
.find(query)
.exec();
}
function findUserWorkorders(user, day) {
var startOfDay = day.clone().startOf('day').toDate();
var endOfDay = day.clone().endOf('day').toDate();
@ -414,6 +431,64 @@ function handleClockInExceptions(user, workorder, spans, now) {
}
}
function handleClockOutExceptions(currentSpan, user, spans, now) {
if (currentSpan.type !== 'workday') {
return;
}
var dayOfWeek = now.day();
if (dayOfWeek != 3 && dayOfWeek != 4) {
return;
}
var workdaySpans = filterSpans(spans, {type: 'workday'});
var secondsWorked = 0;
workdaySpans.forEach(span => secondsWorked += span.status === 'closed' ? span.duration : 0);
secondsWorked += currentSpan.duration;
console.log("Seconds Worked: " + secondsWorked);
if (dayOfWeek == 3 && secondsWorked < 20 * 60 * 60) {
new TimeClockException({
user: user._id,
date: new Date(),
reason: 'less_than_twenty_hours_worked'
}).save();
reportException({
user: user,
reason: 'Less than 20 hours worked by end of day Wednesday.'
});
}
if (dayOfWeek == 4 && secondsWorked < 30 * 60 * 60) {
new TimeClockException({
user: user._id,
date: new Date(),
reason: 'less_than_thirty_hours_worked'
}).save();
reportException({
user: user,
reason: 'Less than 30 hours worked by end of day Thursday.'
});
}
if (dayOfWeek == 4 && secondsWorked > 40 * 60 * 60) {
new TimeClockException({
user: user._id,
date: new Date(),
reason: 'greater_than_forty_hours_worked'
}).save();
reportException({
user: user,
reason: 'Greater than 40 hours worked by end of day Thursday.'
});
}
}
function reportException(exception) {
const message = {
@ -480,6 +555,9 @@ function handleClockOutRequest(params, user, spans, workorders, now) {
span.duration = moment(span.end).diff(span.start, 'seconds');
span.notes = params.notes;
findUserSpansForWeek(user, now)
.then(weeklySpans => handleClockOutExceptions(span, user, weeklySpans, now));
return span.save().then(spanToResponse);
}

View File

@ -7,7 +7,10 @@ var ObjectId = Schema.ObjectId;
const EXCEPTION_REASONS = [
'late_to_first_workorder',
'too_little_travel',
'too_much_travel'
'too_much_travel',
'less_than_twenty_hours_worked',
'less_than_thirty_hours_worked',
'greater_than_forty_hours_worked'
];
var schema = new Schema({