forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasTimes.js
More file actions
80 lines (64 loc) · 2.34 KB
/
hasTimes.js
File metadata and controls
80 lines (64 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const _ = require('underscore');
const moment = require('moment');
const Base = require('./base');
const Time = require('./time');
const regex = /added (.*) of time spent/i;
const subRegex = /substracted (.*) of time spent/i;
/**
* base model for models that have times
*/
class hasTimes extends Base {
/**
* set stats
* @returns {Promise}
*/
stats() {
let promise = this.get(`projects/${this.data.project_id}/${this._type}/${this.iid}/time_stats`);
promise.then(response => this.stats = response.body);
return promise;
}
/**
* set notes
* @returns {Promise}
*/
notes() {
let promise = this.all(`projects/${this.data.project_id}/${this._type}/${this.iid}/notes`);
promise.then(notes => this.notes = notes);
return promise;
}
/**
* set times (call set notes first)
* @returns {Promise}
*/
times() {
let times = [];
let timeSpent = 0;
let timeUsers = {};
let promise = this.parallel(this.notes, (note, done) => {
let created = moment(note.created_at), match, subMatch;
if (
// filter out user notes
!note.system ||
// only include times by the configured user
(this.config.get('user') && this.config.get('user') !== note.author.username) ||
// filter out times that are not in the given time frame
!(created.isSameOrAfter(this.config.get('from')) && created.isSameOrBefore(this.config.get('to'))) ||
// filter out notes that are no time things
!(match = regex.exec(note.body)) && !(subMatch = subRegex.exec(note.body))
) return done();
if (!timeUsers[note.author.username]) timeUsers[note.author.username] = 0;
let time = new Time(match ? match[1] : `-${subMatch[1]}`, note, this, this.config);
timeSpent += time.seconds;
timeUsers[note.author.username] += time.seconds;
times.push(time);
done();
});
promise.then(() => {
_.each(timeUsers, (time, name) => this[`time_${name}`] = this.config.toHumanReadable(time));
this.timeSpent = timeSpent;
this.times = times
});
return promise;
}
}
module.exports = hasTimes;