-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices_ActivityService.js.html
299 lines (258 loc) · 8.56 KB
/
services_ActivityService.js.html
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: services/ActivityService.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: services/ActivityService.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import { PouchService } from './PouchService'
import { TagService } from './TagService'
/**
* Provides access to the activities database through a standardized interface.
*
* @class
*/
export class ActivityService {
constructor () {
this.pouchService = new PouchService('activities')
this.pouchService.addIndex('endTime')
this.tagService = new TagService()
}
/**
* Creates a new activity in the database with default values.
*
* @return {string} The GUID of the newly created activity.
*
* @function
*/
async create () {
const document = {
description: '',
endTime: '',
pauseStartTime: '',
productivity: 0,
startTime: new Date().toISOString(),
tags: [],
totalPauseTime: 0
}
const id = await this.pouchService.create(document)
return id
}
/**
* Gets the activity with the specified ID from the database.
*
* @param {string} activityId - The ID of the desired activity.
* @return {Promise<Object>} A promise to the requested activity object.
*
* @function
*/
async getOne (activityId) {
return this.pouchService.getOne(activityId)
}
/**
* Gets all finished activities from the database.
*
* @return {Promise<Array<Object>>} A promise to an array containing all activity objects.
*
* @function
*/
async getAll () {
return this.pouchService.getByQuery({
selector: {
endTime: {
$exists: true,
$ne: ''
}
}
})
}
/**
* Gets all activities where endTime has not been set yet. Normally, the returned array should only contain one element.
*
* @return {Promise<Array<Object>>} A promise to an array containing all unfinished activities.
*
* @function
*/
async getUnfinished () {
return this.pouchService.getByQuery({
selector: {
endTime: ''
}
})
}
async getTags (activityId) {
let tags = []
const activity = await this.getOne(activityId)
for (const tagId of activity.tags) {
let tag = await this.tagService.getOne(tagId)
tags.push(tag)
}
return tags
}
/**
* Updates the startTime of the specified activity.
*
* @param {string} activityId - The ID of the activity to update the startTime for.
* @param {string} startTime - An ISO-8601-compliant string containing the new startTime.
*
* @function
*/
async updateStartTime (activityId, startTime) {
let document = await this.getOne(activityId)
document.startTime = new Date(startTime).toISOString()
this.pouchService.update(document)
}
/**
* Updates the endTime of the specified activity.
*
* @param {string} activityId - The ID of the activity to update the endTime for.
* @param {string} endTime - An ISO-8601-compliant string containing the new endTime.
*
* @function
*/
async updateEndTime (activityId, endTime = null) {
let document = await this.getOne(activityId)
if (endTime == null) {
document.endTime = new Date().toISOString()
} else {
document.endTime = new Date(endTime).toISOString()
}
this.pouchService.update(document)
}
/**
* Pauses the specified activity.
*
* @param {string} activityId - The ID of the activity to pause.
* @return {string} An ISO-8601-compliant string containing the time the activity was paused.
*
* @function
*/
async pause (activityId) {
let document = await this.getOne(activityId)
document.pauseStartTime = new Date().toISOString()
this.pouchService.update(document)
return document.pauseStartTime
}
/**
* Resumes the specified activity.
*
* @param {string} activityId - The ID of the activity to resume.
* @return {Number} The length of the pause in milliseconds.
*
* @function
*/
async resume (activityId) {
let document = await this.getOne(activityId)
const pauseTime = Date.parse(document.pauseStartTime)
const resumeTime = new Date().valueOf()
const pauseLength = resumeTime - pauseTime
document.totalPauseTime = document.totalPauseTime + pauseLength
document.pauseStartTime = ''
this.pouchService.update(document)
return pauseLength
}
/**
* Updates the description of the specified activity.
*
* @param {string} activityId - The ID of the activity to update the description for.
* @param {string} description - The new description.
*
* @function
*/
async updateDescription (activityId, description) {
let document = await this.getOne(activityId)
document.description = description
this.pouchService.update(document)
}
/**
* Updates the productivity of the specified activity.
*
* @param {string} activityId - The ID of the activity to update the productivity for.
* @param {Number} productivity - An integer value between 0 and 5 describing the productivity. 0 means nothing is set, 1 is the highest productivity, 5 the lowest.
*
* @function
*/
async updateProductivity (activityId, productivity) {
let document = await this.getOne(activityId)
document.productivity = productivity
this.pouchService.update(document)
}
/**
* Adds the tag with the specified ID to the specified activity.
*
* @param {string} activityId - The ID of the activity to add the tag to.
* @param {string} tagId - The ID of the tag to add to the activity.
*
* @function
*/
async addTag (activityId, tagId) {
let document = await this.getOne(activityId)
document.tags.push(tagId)
this.pouchService.update(document)
}
/**
* Removes the tag with the specified ID from the specified activity.
*
* @param {string} activityId - The ID of the activity to remove the tag from.
* @param {string} tagId - The ID of the tag to remove.
*
* @function
*/
async removeTag (activityId, tagId) {
let document = await this.getOne(activityId)
let index = document.tags.indexOf(tagId)
if (index !== -1) {
document.tags.splice(index, 1)
}
this.pouchService.update(document)
}
/**
* Removes all tags from the specified activity.
*
* @param {string} activityId - The ID of the activity to clear all tags from.
*
* @function
*/
async clearTags (activityId) {
let document = await this.getOne(activityId)
document.tags = []
this.pouchService.update(document)
}
/**
* Deletes the specified activity from the database.
*
* @param {string} activityId - The ID of the activity to delete.
*
* @function
*/
async delete (activityId) {
let document = await this.getOne(activityId)
this.pouchService.delete(document)
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ActivityService.html">ActivityService</a></li><li><a href="AreaService.html">AreaService</a></li><li><a href="PouchService.html">PouchService</a></li><li><a href="ProjectService.html">ProjectService</a></li><li><a href="TagService.html">TagService</a></li></ul><h3>Global</h3><ul><li><a href="global.html#compareArrayByName">compareArrayByName</a></li><li><a href="global.html#router">router</a></li><li><a href="global.html#routes">routes</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Sun Jan 24 2021 18:08:50 GMT+0000 (Coordinated Universal Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>