Skip to content

Commit

Permalink
Block double start records
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigaVukcevicDev committed Feb 17, 2017
1 parent ba360dd commit f89df37
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,48 @@ controller.on('slash_command', function (slashCommand, message) {
if (message.text === 'start') {
timeStart = moment();

Time.create({
time: timeStart, // NOTE: date will be written as ISO format in database
type: 'start',
userId: message.user_id
}, function(err, time) {
// Checking if start time for current day already exists
var todayStart = new Date();
todayStart.setHours(0, 0, 0, 0);

var todayEnd = new Date();
todayEnd.setHours(23, 59, 59, 999);

Time.find(
{'time': {
'$gte': todayStart,
'$lte': todayEnd
}}
).exec(function(err, times) {
if (err) {
// TODO: output proper error
console.log('I got some error!');
}
else {
// Refusing inserting time started
else if (times.length) {
slashCommand.replyPublic(message,
'Started working time: *' + moment(timeStart).format('dddd, DD.MM.YYYY [at] HH:mm') + '*.\n' +
'Time to get work done, we need to make some money.'
'This is odd, haven\'t you already started to work today?'
);
}
// Inserting time started
else {
Time.create({
time: timeStart, // NOTE: date will be written as ISO format in database
type: 'start',
userId: message.user_id
}, function(err, time) {
if (err) {
// TODO: output proper error
console.log('I got some error!');
}
else {
slashCommand.replyPublic(message,
'Started working time: *' + moment(timeStart).format('dddd, DD.MM.YYYY [at] HH:mm') + '*.\n' +
'Time to get work done, we need to make some money.'
);
}
});
}
});
}

Expand Down

0 comments on commit f89df37

Please sign in to comment.