Prettified Code!

This commit is contained in:
daniep01
2022-07-15 23:11:19 +00:00
committed by GitHub Action
parent 70aa167966
commit 0ea29e0b3e

View File

@@ -170,34 +170,30 @@ instance.prototype.action = function (action) {
opt.project + opt.project +
'"}}' '"}}'
} }
self.sendCommand('rest', cmd, body).then( self.sendCommand('rest', cmd, body).then((result) => {
result => { console.log('start: ' + JSON.stringify(result, null, 4))
console.log('start: ' + JSON.stringify(result, null, 4)) if (typeof result == 'object' && result.data !== null) {
if (typeof(result) == 'object' && result.data !== null) { console.log('new timer ' + result.data.id)
console.log('new timer ' + result.data.id) self.currentTimer = result.data.id
self.currentTimer = result.data.id } else {
} else { console.log('error starting timer')
console.log('error starting timer')
}
} }
) })
break break
} }
case 'stopCurrentTimer': { case 'stopCurrentTimer': {
console.log('stopping timer: ' + self.currentTimer) console.log('stopping timer: ' + self.currentTimer)
if (self.currentTimer != null && self.currentTimer != undefined) { if (self.currentTimer != null && self.currentTimer != undefined) {
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + self.currentTimer + '/stop' var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + self.currentTimer + '/stop'
self.sendCommand('rest_put', cmd).then( self.sendCommand('rest_put', cmd).then((result) => {
result => { // console.log('stop: ' + JSON.stringify(result, null, 4))
// console.log('stop: ' + JSON.stringify(result, null, 4)) if (typeof result == 'object' && result.data !== null) {
if (typeof(result) == 'object' && result.data !== null) { console.log('stopped ' + result.data.id + ' duration ' + result.data.duration)
console.log('stopped ' + result.data.id + ' duration ' + result.data.duration) self.currentTimer = null
self.currentTimer = null } else {
} else { console.log('error stopping timer')
console.log('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')
} }
@@ -222,7 +218,7 @@ instance.prototype.getWorkspace = function () {
// get workspace ID // get workspace ID
self.sendCommand('rest_get', cmd).then( self.sendCommand('rest_get', cmd).then(
result => { (result) => {
// console.log('result ' + JSON.stringify(result, null, 4)) // console.log('result ' + JSON.stringify(result, null, 4))
if (typeof result === 'object' && result !== null) { if (typeof result === 'object' && result !== null) {
console.log('Found ' + result.length + ' workspace') console.log('Found ' + result.length + ' workspace')
@@ -239,7 +235,7 @@ instance.prototype.getWorkspace = function () {
self.log('debug', 'No workspace') self.log('debug', 'No workspace')
} }
}, },
error => { (error) => {
console.log('error ' + error) console.log('error ' + error)
self.log('debug', 'Error getting workspace') self.log('debug', 'Error getting workspace')
} }
@@ -252,7 +248,7 @@ instance.prototype.getProjects = function () {
if (self.workspace !== null) { if (self.workspace !== null) {
var cmd = 'https://api.track.toggl.com/api/v8/workspaces/' + self.workspace + '/projects' var cmd = 'https://api.track.toggl.com/api/v8/workspaces/' + self.workspace + '/projects'
self.sendCommand('rest_get', cmd).then( self.sendCommand('rest_get', cmd).then(
result => { (result) => {
// console.log('result ' + JSON.stringify(result, null, 4)) // console.log('result ' + JSON.stringify(result, null, 4))
if (typeof result === 'object' && result !== null) { if (typeof result === 'object' && result !== null) {
// reset // reset
@@ -290,7 +286,7 @@ instance.prototype.getProjects = function () {
self.log('debug', 'No projects') self.log('debug', 'No projects')
} }
}, },
error => { (error) => {
console.log('error ' + error) console.log('error ' + error)
self.log('debug', 'Error getting projects') self.log('debug', 'Error getting projects')
} }
@@ -303,7 +299,7 @@ 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'
self.sendCommand('rest_get', cmd).then( self.sendCommand('rest_get', cmd).then(
result => { (result) => {
// console.log('result ' + JSON.stringify(result, null, 4)) // console.log('result ' + JSON.stringify(result, null, 4))
if (typeof result === 'object' && result.data !== null) { if (typeof result === 'object' && result.data !== null) {
// console.log('result ' + result.data) // console.log('result ' + result.data)
@@ -319,7 +315,7 @@ instance.prototype.getCurrentTimer = function () {
self.currentTimer = null self.currentTimer = null
} }
}, },
error => { (error) => {
console.log('error ' + error) console.log('error ' + error)
self.log('debug', 'Error getting current timer') self.log('debug', 'Error getting current timer')
} }
@@ -333,26 +329,37 @@ instance.prototype.sendCommand = function (mode, command, body = '') {
switch (mode) { switch (mode) {
case 'rest_get': { case 'rest_get': {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
self.system.emit(mode, command, (err, { data, error, response }) => { self.system.emit(
if (err) { mode,
reject(error) command,
return (err, { data, error, response }) => {
} if (err) {
resolve(data) reject(error)
}, self.header) return
}
resolve(data)
},
self.header
)
}) })
break break
} }
case 'rest': case 'rest':
case 'rest_put': { case 'rest_put': {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
self.system.emit(mode, command, body, (err, { data, error, response }) => { self.system.emit(
if (err) { mode,
reject(error) command,
return body,
} (err, { data, error, response }) => {
resolve(data) if (err) {
}, self.header) reject(error)
return
}
resolve(data)
},
self.header
)
}) })
break break
} }