Basically I'm coding a timeclock app for our place. It works well UNLESS somebody forgets to clock out from the day before. That causes a crash in my code when my code attempts to determine how long they've been clocked in for a given day in total. One of the "pairs" (which book end a shift worked) can't be found because it's not a 'today' event!
[CODE lang="javascript" title="excerpt"]db.query('SELECT FIRST 10 * FROM EVENTS WHERE ID = ' + req.params.id + ' AND DATETIME >= \'' + moment().startOf('day').format('YYYY-MM-DD HH:mm:ss') + '\' ORDER BY DATETIME DESC', function(err, today_rows) {
today_rows.push({
DATETIME: moment().format('YYYY-MM-DD HH:mm:ss')
});
splitRows = today_rows.reduce(function(result, value, index, array) {
if (index % 2 === 0)
result.push(array.slice(index, index + 2));
return result;
}, []);
splitRows.forEach(pair => {
totalMinutes += Math.round(moment.duration(moment(pair[1].DATETIME).diff(moment(pair[0].DATETIME))).asMinutes());
});
}[/CODE]
If I change
SELECT FIRST 10 * FROM EVENTS WHERE ID = ' + req.params.id + ' AND DATETIME >= \'' + moment().startOf('day').format('YYYY-MM-DD HH:mm:ss') + '\' ORDER BY DATETIME DESC
to
SELECT FIRST 10 * FROM EVENTS WHERE ID = ' + req.params.id + ' AND DATETIME >= \'' + moment().startOf('week').format('YYYY-MM-DD HH:mm:ss') + '\' ORDER BY DATETIME DESC
for example, the problem goes away, but then, I lose my calculation for total number of minutes worked TODAY (which I need)
I've asked elsewhere online but I just get responses like 'use a cron job to terminate any open shifts at midnight'...but that just leads to other problems like a) the member of staff not realising they had forgotten to clock out so just doing it again next time and b) the shift being recorded as far longer than it actually was and nobody "knowing" and correcting it to ensure correct pay.
What I want to do is code for, say:-
if the first row found on a given day is an eventtype of '1' then that indicates a "forgot to clock out" event so EXCLUDE that row from the subsequent calculations that then occur.
Full code is https://pastebin.com/UAni9exg here IF needed.
many thanks
[CODE lang="javascript" title="excerpt"]db.query('SELECT FIRST 10 * FROM EVENTS WHERE ID = ' + req.params.id + ' AND DATETIME >= \'' + moment().startOf('day').format('YYYY-MM-DD HH:mm:ss') + '\' ORDER BY DATETIME DESC', function(err, today_rows) {
today_rows.push({
DATETIME: moment().format('YYYY-MM-DD HH:mm:ss')
});
splitRows = today_rows.reduce(function(result, value, index, array) {
if (index % 2 === 0)
result.push(array.slice(index, index + 2));
return result;
}, []);
splitRows.forEach(pair => {
totalMinutes += Math.round(moment.duration(moment(pair[1].DATETIME).diff(moment(pair[0].DATETIME))).asMinutes());
});
}[/CODE]
If I change
SELECT FIRST 10 * FROM EVENTS WHERE ID = ' + req.params.id + ' AND DATETIME >= \'' + moment().startOf('day').format('YYYY-MM-DD HH:mm:ss') + '\' ORDER BY DATETIME DESC
to
SELECT FIRST 10 * FROM EVENTS WHERE ID = ' + req.params.id + ' AND DATETIME >= \'' + moment().startOf('week').format('YYYY-MM-DD HH:mm:ss') + '\' ORDER BY DATETIME DESC
for example, the problem goes away, but then, I lose my calculation for total number of minutes worked TODAY (which I need)
I've asked elsewhere online but I just get responses like 'use a cron job to terminate any open shifts at midnight'...but that just leads to other problems like a) the member of staff not realising they had forgotten to clock out so just doing it again next time and b) the shift being recorded as far longer than it actually was and nobody "knowing" and correcting it to ensure correct pay.
What I want to do is code for, say:-
if the first row found on a given day is an eventtype of '1' then that indicates a "forgot to clock out" event so EXCLUDE that row from the subsequent calculations that then occur.
Full code is https://pastebin.com/UAni9exg here IF needed.
many thanks