|
1 | 1 | const config = require('./config')
|
2 | 2 | const util = require('./util')
|
3 | 3 |
|
4 |
| -const jsdom = require('jsdom') |
5 |
| -const {JSDOM} = jsdom |
6 | 4 | const format = require('dateformat')
|
| 5 | +const request = require('request-promise') |
| 6 | +const baseUrl = `${config.desktime.protocol}://${config.desktime.host}` |
7 | 7 |
|
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 | +}; |
9 | 18 |
|
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'; |
| 21 | + |
| 22 | +function _parseTimeValue(value) { |
| 23 | + value = (value && (value = parseInt(value)))? value/60 : null; |
| 24 | + |
| 25 | + if(!value){ |
| 26 | + console.log('Warning: project time value is not valid!'); |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + return parseInt(value); //float to int |
| 31 | +} |
| 32 | + |
| 33 | +function _parseProjectTitleValue(value) { |
| 34 | + if(!value || value.trim().length === 0){ |
| 35 | + console.log('Warning: project title value is empty!'); |
| 36 | + return null; |
| 37 | + } |
12 | 38 |
|
13 |
| -const req = request.defaults({baseUrl, jar: config.__jar}) |
| 39 | + return value.trim(); |
| 40 | +} |
14 | 41 |
|
15 |
| -function _parseValue(content) { |
16 |
| - content = content.trim() |
| 42 | +function _validateValue(obj, propName) { |
| 43 | + if(!obj.hasOwnProperty(propName)){ |
| 44 | + console.log('Warning: Projects data property "' + propName + '" not isset!'); |
| 45 | + return null; |
| 46 | + } |
17 | 47 |
|
18 |
| - return content === '-' ? null : content |
| 48 | + return obj[propName]; |
19 | 49 | }
|
20 | 50 |
|
21 |
| -function _parseTime(html) { |
22 |
| - const {document} = new JSDOM(html).window |
23 |
| - const data = document.querySelectorAll('.table-projects > tbody > tr') |
24 |
| - |
25 |
| - 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) |
31 |
| - const id = util.hash(project + task + time) |
32 |
| - result.push({ |
| 51 | +function _parseTime(data) { |
| 52 | + const result = []; |
| 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)); |
| 60 | + const id = util.hash(project + task + time); |
| 61 | + project && time && result.push({ |
33 | 62 | project,
|
34 | 63 | task,
|
35 | 64 | time,
|
36 | 65 | id
|
37 |
| - }) |
38 |
| - }) |
| 66 | + }); |
| 67 | + }); |
39 | 68 |
|
40 |
| - return result |
| 69 | + return result; |
41 | 70 | }
|
42 | 71 |
|
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 | + }); |
52 | 78 | }
|
53 | 79 |
|
54 | 80 | /**
|
55 |
| - * @param {Date} date |
56 | 81 | * @param {callback} fn
|
57 |
| - * @param {String} scope |
58 | 82 | */
|
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)); |
68 | 86 | })
|
69 | 87 | }
|
70 | 88 |
|
|
0 commit comments