@@ -4,17 +4,22 @@ const mongoUtils = require('./mongoUtils.js');
4
4
const fs = require ( 'fs' ) ;
5
5
6
6
7
- const createProject = function ( username , title ) {
7
+ const createProject = function ( username , title ) {
8
8
return mongoUtils . connectToDb (
9
- ( ) => new Promise ( function ( resolve , reject ) {
9
+ ( ) => new Promise ( async function ( resolve , reject ) {
10
10
if ( ! username ) {
11
11
reject ( "User not signed in" ) ;
12
12
}
13
- const db = mongoUtils . getDb ( ) ;
14
- const getUser = db . collection ( 'userData' ) . findOne ( { "user.username" : username } ) ;
15
- getUser . then ( ( usr ) => {
13
+ const db = await mongoUtils . getDb ( ) ;
14
+ var usr = await db . collection ( 'userData' ) . findOne ( { "user.username" : username } ) ;
15
+
16
+ if ( ! usr ) {
17
+ await db . collection ( 'userData' ) . insertOne ( { user : { username :username } , projectNames : [ ] , currentProject : null } ) ;
18
+ usr = { user : { username :username } , projectNames : [ ] , currentProject : null }
19
+ }
16
20
if ( usr . projectNames . includes ( title ) ) {
17
21
reject ( "Project title already taken" ) ;
22
+ return ;
18
23
}
19
24
db . collection ( 'userData' ) . updateOne ( {
20
25
"user.username" : 'username'
@@ -23,60 +28,58 @@ const createProject = function (username, title) {
23
28
} ) . then ( ( _ ) => {
24
29
resolve ( "Project created!" ) ;
25
30
} ) ;
26
- } ) . catch ( ( e ) => console . log ( e ) ) ;
27
31
} )
28
32
) ;
29
33
}
30
34
31
35
//
32
36
const addLog = function ( username , activity , projectName ) {
33
37
return mongoUtils . connectToDb (
34
- ( ) => new Promise ( function ( resolve , reject ) {
35
- if ( ! username ) {
36
- reject ( "User not signed in" ) ;
37
- }
38
+ ( ) => new Promise ( async function ( resolve , reject ) {
38
39
assert ( activity == 'start' || activity == 'end' ) ;
39
40
const db = mongoUtils . getDb ( ) ;
40
- const searchUser = db . collection ( 'userData' ) . findOne ( { "user.username" : username } ) ;
41
- searchUser . then ( ( result ) => {
42
- console . log ( result ) ;
43
- console . log ( projectName ) ;
44
- if ( result . projectNames . includes ( projectName ) || result . currentProject ) {
41
+ const result = await db . collection ( 'userData' ) . findOne ( { "user.username" : username } ) ;
42
+ if ( ! result ) {
43
+ reject ( "Username not found" ) ;
44
+ return ;
45
+ }
46
+ console . log ( result ) ;
47
+ console . log ( projectName ) ;
48
+ if ( result . projectNames . includes ( projectName ) || result . currentProject ) {
45
49
46
- if ( activity == 'start' ) {
50
+ if ( activity == 'start' ) {
47
51
48
- if ( ! result . currentProject ) {
49
- log = { user : username , projectName : projectName , start : { year : Number ( timestamp ( 'YYYY' ) ) , month : Number ( timestamp ( 'MM' ) ) , day : Number ( timestamp ( 'DD' ) ) , time : timestamp ( 'HH:mm:ss' ) } , finish : null , duration : null } ;
50
- insertLogToDb ( log ) ;
51
- db . collection ( 'userData' ) . updateOne ( { "user.username" : username } ,
52
- {
53
- $set : {
54
- currentProject : projectName
55
- }
56
- } ) . then ( ( suc ) => resolve ( "Work on " + projectName + " started!" ) ) . catch ( ( e ) => console . log ( e ) ) ;
57
- } else {
58
- reject ( "You are already working on " + result . currentProject ) ;
59
- }
52
+ if ( ! result . currentProject ) {
53
+ log = { user : username , projectName : projectName , start : { year : Number ( timestamp ( 'YYYY' ) ) , month : Number ( timestamp ( 'MM' ) ) , day : Number ( timestamp ( 'DD' ) ) , time : timestamp ( 'HH:mm:ss' ) } , finish : null , duration : null } ;
54
+ insertLogToDb ( log ) ;
55
+ db . collection ( 'userData' ) . updateOne ( { "user.username" : username } ,
56
+ {
57
+ $set : {
58
+ currentProject : projectName
59
+ }
60
+ } ) . then ( ( suc ) => resolve ( "Work on " + projectName + " started!" ) ) . catch ( ( e ) => console . log ( e ) ) ;
60
61
} else {
61
- if ( result . currentProject ) {
62
- db . collection ( 'userData' ) . findOne ( { "user.username" : username } ) . then ( ( result ) => {
63
- db . collection ( 'logs' ) . findOne ( { projectName : result . currentProject , finish : null } )
64
- . then ( ( log ) => {
65
- const fin = { year : Number ( timestamp ( 'YYYY' ) ) , month : Number ( timestamp ( 'MM' ) ) , day : Number ( timestamp ( 'DD' ) ) , time : timestamp ( 'HH:mm:ss' ) } ;
66
- updateLog = { projectName : result . currentProject , finish : fin , id : log . _id , duration : durationInMins ( subtractTimeStamps ( fin , log . start ) ) }
67
- updateLogInDb ( updateLog ) ;
68
- nullifyCurrentProject ( username ) ;
69
- resolve ( "Work on " + result . currentProject + " complete!" ) ;
70
- } ) . catch ( ( e ) => console . log ( e ) ) ;
71
- } ) . catch ( ( e ) => console . log ( e ) ) ;
72
- } else {
73
- reject ( "Work on this project has already been finished" ) ;
74
- }
62
+ reject ( "You are already working on " + result . currentProject ) ;
75
63
}
76
64
} else {
77
- reject ( "Either your work is complete or you are trying to work on non existing project" ) ;
65
+ if ( result . currentProject ) {
66
+ db . collection ( 'userData' ) . findOne ( { "user.username" : username } ) . then ( ( result ) => {
67
+ db . collection ( 'logs' ) . findOne ( { projectName : result . currentProject , finish : null } )
68
+ . then ( ( log ) => {
69
+ const fin = { year : Number ( timestamp ( 'YYYY' ) ) , month : Number ( timestamp ( 'MM' ) ) , day : Number ( timestamp ( 'DD' ) ) , time : timestamp ( 'HH:mm:ss' ) } ;
70
+ updateLog = { projectName : result . currentProject , finish : fin , id : log . _id , duration : durationInMins ( subtractTimeStamps ( fin , log . start ) ) }
71
+ updateLogInDb ( updateLog ) ;
72
+ nullifyCurrentProject ( username ) ;
73
+ resolve ( "Work on " + result . currentProject + " complete!" ) ;
74
+ } ) . catch ( ( e ) => console . log ( e ) ) ;
75
+ } ) . catch ( ( e ) => console . log ( e ) ) ;
76
+ } else {
77
+ reject ( "Work on this project has already been finished" ) ;
78
+ }
78
79
}
79
- } ) . catch ( ( e ) => reject ( e ) ) ;
80
+ } else {
81
+ reject ( "Either your work is complete or you are trying to work on non existing project" ) ;
82
+ }
80
83
} )
81
84
) ;
82
85
}
@@ -125,9 +128,9 @@ const nullifyCurrentProject = (username) => {
125
128
126
129
const removeProject = function ( username , projectName ) {
127
130
return mongoUtils . connectToDb (
128
- ( ) => new Promise ( function ( resolve , reject ) {
131
+ ( ) => new Promise ( async function ( resolve , reject ) {
129
132
if ( ! username ) {
130
- reject ( "User not signed in " ) ;
133
+ reject ( "User not found " ) ;
131
134
}
132
135
const db = mongoUtils . getDb ( ) ;
133
136
db . collection ( 'userData' ) . updateOne ( {
@@ -136,25 +139,32 @@ const removeProject = function (username, projectName) {
136
139
$pull : {
137
140
projectNames : projectName
138
141
}
139
- } ) . then ( ( suc ) => resolve ( "Project removed" ) )
142
+ } ) . then ( ( suc ) => {
143
+ suc . matchedCount == 0 ? reject ( "You are new to time tracker. Create a project first" ) :
144
+ suc . modifiedCount > 0 ? resolve ( "Project removed" ) : reject ( "Project does not exist" ) ;
145
+ } )
140
146
. catch ( ( e ) => reject ( e ) )
141
147
} )
142
148
) ;
143
149
}
144
150
145
151
const listProjects = function ( username ) {
146
- console . log ( username ) ;
147
152
return mongoUtils . connectToDb (
148
153
( ) => new Promise ( function ( resolve , reject ) {
149
154
if ( ! username ) {
150
- reject ( "User not signed in " ) ;
155
+ reject ( "User not provided " ) ;
151
156
}
152
157
const db = mongoUtils . getDb ( ) ;
153
158
db . collection ( 'userData' ) . findOne ( { "user.username" : username } )
154
- . then ( ( suc ) => {
159
+ . then ( async function ( suc ) {
160
+ var result = [ ] ;
155
161
console . log ( suc ) ;
156
- suc . projectNames . forEach ( ( name ) => console . log ( name + '\t' ) ) ;
157
- resolve ( suc . projectNames ) ;
162
+ if ( ! suc ) {
163
+ await db . collection ( 'userData' ) . insertOne ( { user : { username :username } , projectNames : [ ] , currentProject : null } ) ;
164
+ } else {
165
+ result = suc . projectNames ;
166
+ }
167
+ resolve ( result ) ;
158
168
} )
159
169
. catch ( ( e ) => reject ( e ) )
160
170
} )
@@ -207,16 +217,25 @@ const dateSeverDaysAgoLastMonth = function (month, currentDate) {
207
217
208
218
const report = function ( username , days ) {
209
219
return mongoUtils . connectToDb (
210
- ( ) => new Promise ( function ( resolve , reject ) {
220
+ ( ) => new Promise ( async function ( resolve , reject ) {
211
221
if ( ! username ) {
212
- reject ( "User not signed in" ) ;
222
+ reject ( "Username empty" ) ;
223
+ return ;
213
224
}
214
225
const db = mongoUtils . getDb ( ) ;
226
+ var usr = await db . collection ( 'userData' ) . findOne ( { "user.username" : username } ) ;
227
+
228
+ if ( ! usr ) {
229
+ await db . collection ( 'userData' ) . insertOne ( { user : { username :username } , projectNames : [ ] , currentProject : null } ) ;
230
+ usr = { user : { username :username } , projectNames : [ ] , currentProject : null }
231
+ resolve ( [ "No recent projects" ] ) ;
232
+ }
233
+
215
234
const { today, thisMonth, thisYear } = { today : timestamp ( 'DD' ) , thisMonth : timestamp ( 'MM' ) , thisYear : timestamp ( 'YYYY' ) } ;
216
235
var lastWeek = parseInt ( today ) - days ;
217
236
var lastMonth = thisMonth ;
218
237
var lastYear = thisYear ;
219
- console . log ( lastWeek ) ;
238
+
220
239
if ( lastWeek < 1 ) {
221
240
console . log ( "AAAA" ) ;
222
241
lastMonth -- ;
@@ -260,10 +279,15 @@ const getCurrentProjData = function (username) {
260
279
return mongoUtils . connectToDb (
261
280
( ) => new Promise ( function ( resolve , reject ) {
262
281
if ( ! username ) {
263
- reject ( "User not signed in " ) ;
282
+ reject ( "Empty username " ) ;
264
283
}
265
284
const db = mongoUtils . getDb ( ) ;
266
- db . collection ( 'userData' ) . findOne ( { "user.username" : username } ) . then ( ( usr ) => {
285
+ db . collection ( 'userData' ) . findOne ( { "user.username" : username } ) . then ( async function ( usr ) {
286
+ if ( ! usr ) {
287
+ await db . collection ( 'userData' ) . insertOne ( { user : { username :username } , projectNames : [ ] , currentProject : null } ) ;
288
+ resolve ( "No current project" ) ;
289
+ return ;
290
+ }
267
291
if ( usr . currentProject ) {
268
292
console . log ( "Current project: " + usr . currentProject ) ;
269
293
resolve ( "Current project: " + usr . currentProject ) ;
0 commit comments