Skip to content

Commit 6a7050c

Browse files
author
Vladislav Repka
committed
Fetching data from api
1 parent a99eca2 commit 6a7050c

File tree

3 files changed

+61
-42
lines changed

3 files changed

+61
-42
lines changed

lib/desktime.js

+56-38
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,64 @@
11
const config = require('./config')
22
const util = require('./util')
33

4-
const jsdom = require('jsdom')
5-
const {JSDOM} = jsdom
64
const format = require('dateformat')
5+
const request = require('request-promise')
6+
const baseUrl = `${config.desktime.protocol}://${config.desktime.host}`
77

8-
const request = require('request')
8+
const options = {
9+
method: 'GET',
10+
uri: baseUrl + '/api/v2/json/employee/projects',
11+
qs: {
12+
apiKey: config.desktime.APIkey,
13+
id: config.desktime.EmployeeId,
14+
date: `${format(new Date(), 'yyyy-mm-dd')}`
15+
},
16+
json: true
17+
}
918

10-
const baseUrl = `${config.desktime.protocol}://${config.desktime.host}`
11-
const urlLogin = '/app/?session=' + config.desktime.APIkey
19+
const projectTitlePropName = 'project_title'
20+
const timePropName = 'duration'
1221

13-
const req = request.defaults({baseUrl, jar: config.__jar})
22+
function _parseTimeValue(value) {
23+
value = (value && (value = parseInt(value))) ? (value / 60) : null
1424

15-
function _parseValue(content) {
16-
content = content.trim()
25+
if(!value){
26+
console.log('Warning: project time value is not valid!')
27+
return null
28+
}
1729

18-
return content === '-' ? null : content
30+
return parseInt(value) // float to int
1931
}
2032

21-
function _parseTime(html) {
22-
const {document} = new JSDOM(html).window
23-
const data = document.querySelectorAll('.table-projects > tbody > tr')
33+
function _parseProjectTitleValue(value) {
34+
if (!value || value.trim().length === 0) {
35+
console.log('Warning: project title value is empty!')
36+
return null
37+
}
38+
39+
return value.trim()
40+
}
41+
42+
function _validateValue(obj, propName) {
43+
if (!obj.hasOwnProperty(propName)) {
44+
console.log('Warning: Projects data property "' + propName + '" not isset!')
45+
return null
46+
}
2447

48+
return obj[propName]
49+
}
50+
51+
function _parseTime(data) {
2552
const result = []
26-
data.forEach(function(item) {
27-
const cells = item.cells
28-
const project = _parseValue(cells.item(0).textContent)
29-
const task = _parseValue(cells.item(1).textContent)
30-
const time = _parseValue(cells.item(2).textContent)
53+
data = data || {}
54+
data.projects = data.projects || []
55+
56+
data.projects.forEach(function(item) {
57+
const project = _parseProjectTitleValue(_validateValue(item, projectTitlePropName))
58+
const task = ''
59+
const time = _parseTimeValue(_validateValue(item, timePropName))
3160
const id = util.hash(project + task + time)
32-
result.push({
61+
project && time && result.push({
3362
project,
3463
task,
3564
time,
@@ -40,31 +69,20 @@ function _parseTime(html) {
4069
return result
4170
}
4271

43-
function _desktimeRequest(url, fn) {
44-
if (config.__cookie.isEmpty() || config.__cookie.isExpired()) {
45-
req(urlLogin, function(error) {
46-
error && console.log(error)
47-
req(url, fn)
48-
})
49-
} else {
50-
req(url, fn)
51-
}
72+
function _desktimeRequest(fn) {
73+
request(options).then(function(response) {
74+
fn(response)
75+
}).catch(function(err) {
76+
console.log(err)
77+
})
5278
}
5379

5480
/**
55-
* @param {Date} date
5681
* @param {callback} fn
57-
* @param {String} scope
5882
*/
59-
function fetchMyTime(date, fn, scope = 'day') {
60-
fn = fn || date
61-
date = typeof date === 'object' ? date : new Date()
62-
63-
const url = `/app/my/${format(date, 'yyyy-mm-dd')}/${scope}`
64-
65-
_desktimeRequest(url, function(error, response, body) {
66-
error && console.log(error)
67-
fn(_parseTime(body))
83+
function fetchMyTime(fn) {
84+
_desktimeRequest(function(response) {
85+
fn(_parseTime(response))
6886
})
6987
}
7088

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
"test": "standart"
1717
},
1818
"dependencies": {
19-
"assign-deep": "^0.4.7",
19+
"assign-deep": "^1.0.1",
2020
"betterc": "^1.3.0",
2121
"dateformat": "^3.0.3",
2222
"jira-connector": "^2.7.0",
23-
"jsdom": "^11.6.2",
24-
"request": "^2.85.0",
23+
"request": "^2.88.2",
24+
"request-promise": "^4.2.5",
2525
"tough-cookie-file-store": "^1.2.0"
2626
},
2727
"devDependencies": {

readme.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Create file `~/.integrator/config`:
2222
}
2323
},
2424
"desktime": {
25-
"APIkey": "%key%"
25+
"APIkey": "%key%",
26+
"EmployeeId": "%employeeId%"
2627
}
2728
}
2829
```

0 commit comments

Comments
 (0)