Prettified Code!

This commit is contained in:
daniep01
2022-07-16 11:53:27 +00:00
committed by GitHub Action
parent d2f7dea478
commit aeeddfc283

View File

@@ -35,9 +35,8 @@ instance.prototype.init = function () {
self.auth() self.auth()
self.getWorkspace() self.getWorkspace()
self.getCurrentTimer().then((result) => { self.getCurrentTimer().then((result) => {
self.log('debug', 'Current timer id ' + result) self.log('debug', 'Current timer id ' + result)
} })
)
self.actions() self.actions()
} }
@@ -197,41 +196,39 @@ instance.prototype.action = function (action) {
} }
self.sendCommand('rest', cmd, body).then((result) => { self.sendCommand('rest', cmd, body).then((result) => {
if (typeof result == 'object' && result.data !== null && result.data !== undefined) { if (typeof result == 'object' && result.data !== null && result.data !== undefined) {
self.log('debug','New timer started ' + result.data.id) self.log('debug', 'New timer started ' + result.data.id)
} else { } else {
self.log('warn','Error starting timer') self.log('warn', 'Error starting timer')
} }
}) })
} else { } else {
self.log('debug','A timer is already running') self.log('debug', 'A timer is already running')
} }
}) })
break break
} }
case 'stopCurrentTimer': { case 'stopCurrentTimer': {
self.getCurrentTimer().then((timerId) => { self.getCurrentTimer().then((timerId) => {
self.log('debug', 'Current timer id ' + timerId) self.log('debug', 'Current timer id ' + timerId)
if (timerId !== null && timerId !== undefined) { if (timerId !== null && timerId !== undefined) {
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + timerId + '/stop' var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + timerId + '/stop'
self.sendCommand('rest_put', cmd).then((result) => { self.sendCommand('rest_put', cmd).then((result) => {
if (typeof result == 'object' && result.data !== null && result.data !== undefined) { if (typeof result == 'object' && result.data !== null && result.data !== undefined) {
self.log('debug', 'Stopped ' + result.data.id + ', duration ' + result.data.duration) self.log('debug', 'Stopped ' + result.data.id + ', duration ' + result.data.duration)
} else { } else {
self.log('warn', 'Error stopping timer') self.log('warn', 'Error stopping timer')
} }
}) })
} else { } else {
self.log('warn', 'No running timer to stop or running timer id unknown') self.log('warn', 'No running timer to stop or running timer id unknown')
}
} }
) })
break break
} }
case 'getCurrentTimer': { case 'getCurrentTimer': {
self.getCurrentTimer().then((result) => { self.getCurrentTimer().then((result) => {
self.log('debug', 'Current timer id ' + result) self.log('debug', 'Current timer id ' + result)
} })
)
break break
} }
case 'refreshProjects': { case 'refreshProjects': {
@@ -334,28 +331,27 @@ instance.prototype.getCurrentTimer = function () {
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/current' var cmd = 'https://api.track.toggl.com/api/v8/time_entries/current'
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
self.sendCommand('rest_get', cmd).then( self.sendCommand('rest_get', cmd).then(
(result) => { (result) => {
if (typeof result === 'object' && result.data !== null && result.data !== undefined) { if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
if ('id' in result.data) { if ('id' in result.data) {
console.log('current timer: ' + result.data.id) console.log('current timer: ' + result.data.id)
resolve(result.data.id) resolve(result.data.id)
} else {
console.log('getCurrentTimer: No timer id found')
resolve(null)
}
} else { } else {
console.log('getCurrentTimer: No timer running') console.log('getCurrentTimer: No timer id found')
resolve(null) resolve(null)
} }
}, } else {
(error) => { console.log('getCurrentTimer: No timer running')
console.log('error ' + error) resolve(null)
self.log('debug', 'Error getting current timer')
} }
) },
} (error) => {
) console.log('error ' + error)
self.log('debug', 'Error getting current timer')
}
)
})
} }
instance.prototype.sendCommand = function (mode, command, body = '') { instance.prototype.sendCommand = function (mode, command, body = '') {