Skip to content

Commit 3a026da

Browse files
author
Bogdan Abaev
committed
adding auth from alexa user id
1 parent 98b3767 commit 3a026da

File tree

2 files changed

+81
-58
lines changed

2 files changed

+81
-58
lines changed

server/mongoUtils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
const { MongoClient} = require('mongodb');
33

4-
//'mongodb://127.0.0.1:27017';
54
const connectionUrl = 'mongodb://mongo:27017/';
65
//const connectionUrl = 'mongodb://127.0.0.1:27017';
76

server/timeTracker.js

Lines changed: 81 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@ const mongoUtils = require('./mongoUtils.js');
44
const fs = require('fs');
55

66

7-
const createProject = function (username, title) {
7+
const createProject = function (username, title) {
88
return mongoUtils.connectToDb(
9-
() => new Promise(function (resolve, reject) {
9+
() => new Promise(async function (resolve, reject) {
1010
if(!username){
1111
reject("User not signed in");
1212
}
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+
}
1620
if (usr.projectNames.includes(title)) {
1721
reject("Project title already taken");
22+
return;
1823
}
1924
db.collection('userData').updateOne({
2025
"user.username": 'username'
@@ -23,60 +28,58 @@ const createProject = function (username, title) {
2328
}).then((_) => {
2429
resolve("Project created!");
2530
});
26-
}).catch((e) => console.log(e));
2731
})
2832
);
2933
}
3034

3135
//
3236
const addLog = function (username, activity, projectName) {
3337
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) {
3839
assert(activity == 'start' || activity == 'end');
3940
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) {
4549

46-
if (activity == 'start') {
50+
if (activity == 'start') {
4751

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));
6061
} 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);
7563
}
7664
} 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+
}
7879
}
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+
}
8083
})
8184
);
8285
}
@@ -125,9 +128,9 @@ const nullifyCurrentProject = (username) => {
125128

126129
const removeProject = function (username, projectName) {
127130
return mongoUtils.connectToDb(
128-
() => new Promise(function (resolve, reject) {
131+
() => new Promise(async function (resolve, reject) {
129132
if(!username){
130-
reject("User not signed in");
133+
reject("User not found");
131134
}
132135
const db = mongoUtils.getDb();
133136
db.collection('userData').updateOne({
@@ -136,25 +139,32 @@ const removeProject = function (username, projectName) {
136139
$pull: {
137140
projectNames: projectName
138141
}
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+
})
140146
.catch((e) => reject(e))
141147
})
142148
);
143149
}
144150

145151
const listProjects = function (username) {
146-
console.log(username);
147152
return mongoUtils.connectToDb(
148153
() => new Promise(function (resolve, reject) {
149154
if(!username){
150-
reject("User not signed in");
155+
reject("User not provided");
151156
}
152157
const db = mongoUtils.getDb();
153158
db.collection('userData').findOne({ "user.username": username })
154-
.then((suc) => {
159+
.then(async function(suc) {
160+
var result = [];
155161
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);
158168
})
159169
.catch((e) => reject(e))
160170
})
@@ -207,16 +217,25 @@ const dateSeverDaysAgoLastMonth = function (month, currentDate) {
207217

208218
const report = function (username, days) {
209219
return mongoUtils.connectToDb(
210-
() => new Promise(function (resolve, reject) {
220+
() => new Promise(async function (resolve, reject) {
211221
if(!username){
212-
reject("User not signed in");
222+
reject("Username empty");
223+
return;
213224
}
214225
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+
215234
const { today, thisMonth, thisYear } = { today: timestamp('DD'), thisMonth: timestamp('MM'), thisYear: timestamp('YYYY') };
216235
var lastWeek = parseInt(today) - days;
217236
var lastMonth = thisMonth;
218237
var lastYear = thisYear;
219-
console.log(lastWeek);
238+
220239
if (lastWeek < 1) {
221240
console.log("AAAA");
222241
lastMonth--;
@@ -260,10 +279,15 @@ const getCurrentProjData = function (username) {
260279
return mongoUtils.connectToDb(
261280
() => new Promise(function (resolve, reject) {
262281
if(!username){
263-
reject("User not signed in");
282+
reject("Empty username");
264283
}
265284
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+
}
267291
if (usr.currentProject) {
268292
console.log("Current project: " + usr.currentProject);
269293
resolve("Current project: " + usr.currentProject);

0 commit comments

Comments
 (0)