Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Database query returning a result I don't want

boom

New Coder
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 :)
 
I looked at your pastebin; the code looks longer then needed and too complicated, so I did not study it.
Here is just a thought: At clock in check that user has clocked out. if not throw an error on previous days time.
Or check for authorized over time after shift end and terminate hours and throw a mark if none.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom