forked from danibram/time-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.js
More file actions
239 lines (199 loc) · 6.51 KB
/
Copy pathTask.js
File metadata and controls
239 lines (199 loc) · 6.51 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import moment from 'moment'
import { STARTED, PAUSED, IN_PROGRESS, FINISHED } from './constants'
import { recognizeModifierTiming, inPeriod, getSeconds } from './utils'
import { cliError } from './output'
import { EINPROGRESS } from 'constants'
export default class Task {
constructor(task) {
this.task = task
? task
: {
description: '',
timings: []
}
this.filtered = false
}
_modifyTiming(time, operation) {
let timing = this.task.timings[this.task.timings.length - 1]
if (!timing) {
throw `This task is not started, start it.`
}
let newStop = moment(timing.stop)
newStop = newStop[operation](time.value, time.momentKey)
if (operation === 'subtract' && newStop.isBefore(timing.start)) {
throw 'You cant subtract more time'
return
}
this.task.timings[this.task.timings.length - 1].stop = newStop
}
start(description) {
return new Promise((resolve, reject) => {
let started = false
let timing = this.task.timings[this.task.timings.length - 1]
if (timing && timing.start && !timing.stop) {
reject(this.error(`This task already started.`))
}
this.task.timings.push({
start: moment().toDate()
})
this.setDescription(description)
this.setStatus(IN_PROGRESS)
this.log('start')
resolve()
})
}
pause() {
return new Promise((resolve, reject) => {
let timing = this.task.timings[this.task.timings.length - 1]
if (!timing) {
reject(this.error(`This task is not started, start it.`))
}
if (timing && timing.start && timing.stop) {
reject(this.error(`This task are ended/paused, unpaused it.`))
}
this.task.timings[
this.task.timings.length - 1
].stop = moment().toDate()
this.setStatus(PAUSED)
this.log('pause')
resolve()
})
}
unpause() {
return new Promise((resolve, reject) => {
let timing = this.task.timings[this.task.timings.length - 1]
if (!timing) {
reject(this.error(`This task is not started, start it`))
}
if (timing && timing.start && !timing.stop) {
reject(this.error(`This task is started, pause/stop first`))
}
this.task.timings.push({
start: moment().toDate()
})
this.setStatus(IN_PROGRESS)
this.log('unpause')
resolve()
})
}
stop(description) {
return new Promise((resolve, reject) => {
let timing = this.task.timings[this.task.timings.length - 1]
if (!timing || (timing && !timing.start)) {
reject(this.error(`This task is not started, start it`))
}
if (timing.start && timing.stop) {
reject(this.error(`This task already ended, start/unpause it.`))
}
this.task.timings[
this.task.timings.length - 1
].stop = moment().toDate()
this.setDescription(description)
this.setStatus(FINISHED)
this.log('stop')
resolve()
})
}
log(operation) {
if (!this.task.log) {
this.task.log = []
}
this.task.log.push(`${operation}#${moment().toISOString()}`)
}
description() {
return this.task.description
}
setDescription(text) {
this.task.description = text
? text
: this.task.description ? this.task.description : ''
}
setStatus(status) {
this.task.status = status
}
makeOperationOverTime(operation, stringTime) {
return new Promise((resolve, reject) => {
let parsed = recognizeModifierTiming(stringTime)
try {
parsed.map(t => {
this._modifyTiming(t, operation)
})
} catch (err) {
console.error(err)
reject(this.error(`Error trying to ${operation} time to task`))
}
this.log(`${operation}:${stringTime}`)
resolve()
})
}
get() {
return this.task
}
filterByDates(start, end) {
if (this.task.timings) {
if (start && end) {
this.task.timings = this._filterTimingsByDate(start, end)
} else if (start) {
end = moment().format('YYYY/MM/DD')
this.task.timings = this._filterTimingsByDate(start, end)
} else if (end) {
start = moment(0).format('YYYY/MM/DD')
this.task.timings = this._filterTimingsByDate(start, end)
}
if (this.task.timings.length === 0) {
this.filtered = true
}
} else {
let t = inPeriod(this.task.start, this.task.stop, start, end)
if (t.isBetween) {
this.task.start = t.start
this.task.stop = t.stop
} else {
this.filtered = true
}
}
return this
}
_filterTimingsByDate(start, end) {
return this.task.timings.reduce((acc, val) => {
let t = inPeriod(val.start, val.stop, start, end)
if (t.isBetween) {
acc.push({
start: t.start,
stop: t.stop
})
}
return acc
}, [])
}
getSeconds() {
let duration = 0
if (this.task.timings) {
this.task.timings.forEach(timing => {
duration += getSeconds(timing)
})
} else {
duration = getSeconds(this.task)
}
return duration
}
getStartDate() {
if (this.task.timings && this.task.timings[0]) {
return this.task.timings[0].start
} else {
return this.task.start
}
}
getEndDate() {
if (this.task.timings) {
return this.task.timings[this.task.timings.length - 1].stop
} else if (this.task.stop) {
return this.task.stop
} else {
return moment().toDate()
}
}
error(err) {
return err //+ ` \n Task: ${JSON.stringify(this.task)}`
}
}