Skip to content

Commit 8a1dcd3

Browse files
author
Bogdan Abaev
committed
fixing bug in remove_project command
1 parent 0df80dc commit 8a1dcd3

File tree

6 files changed

+244
-205
lines changed

6 files changed

+244
-205
lines changed

.DS_Store

0 Bytes
Binary file not shown.

alexa_lambda.js

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// This function is ran on AWS Lambda. It is the endpoint
22
// where all Alexa commands are sent. Then, it sends requests
33
// to the node app from server folder to process them.
4+
45
const Alexa = require('ask-sdk-core');
56
const request = require('request');
6-
const host = "http://18.233.168.151:80";
7+
8+
const host = "http://internal-alexaAppELB-464974694.us-east-1.elb.amazonaws.com";
79

810
const sendQuery = function(query, handlerInput) {
9-
return new Promise((resolve,reject) => {
11+
return new Promise(function(resolve,reject) {
1012
const username = handlerInput.requestEnvelope.session.user.userId;
1113
request.post({
1214
headers: {
@@ -36,10 +38,11 @@ const sendQuery = function(query, handlerInput) {
3638
} else if (handlerInput.requestEnvelope.request.intent.name == "report"){
3739
const days = handlerInput.requestEnvelope.request.intent.slots.days.value;
3840
var temp = `Here is the breakdown of your activities for the last ${days} days:\n`;
41+
console.log(output);
3942
output.forEach((prj) => {
40-
temp += `You spent total of ${prj.totalTime} minutes on ${prj._id}`;
43+
temp += `You spent total of ${prj.totalTime} minutes on ${prj._id}. Among those: `;
4144
prj.brief.forEach((session)=>{
42-
temp += `${session.timeSpent} minutes spent on day ${session.day} `
45+
temp += `${session.timeSpent} minutes were spent on day ${session.day}, `
4346
});
4447
});
4548
output = temp;
@@ -51,9 +54,9 @@ const sendQuery = function(query, handlerInput) {
5154
.withSimpleCard('Got it!!!', output)
5255
.getResponse());
5356
}
54-
})
55-
});
56-
}
57+
});
58+
});
59+
};
5760

5861

5962

@@ -64,7 +67,7 @@ const LaunchRequestHandler = {
6467
},
6568
handle(handlerInput) {
6669
const speechText = 'Describe time tracker';
67-
70+
6871
return handlerInput.responseBuilder
6972
.speak(speechText)
7073
.reprompt(speechText)
@@ -161,15 +164,34 @@ const startProjectIntentHandler = {
161164
},
162165
handle(handlerInput) {
163166
console.log("Start project");
167+
const token = handlerInput.requestEnvelope.context.System.apiAccessToken;
168+
const deviceId = handlerInput.requestEnvelope.context.System.device.deviceId;
164169
const project = handlerInput.requestEnvelope.request.intent.slots.projectName.value;
170+
return new Promise((resolve,reject) => {
171+
request.get({
172+
headers: {
173+
"Authorization": `Bearer ${token}`
174+
},
175+
url: `https://api.amazonalexa.com/v2/devices/${deviceId}/settings/System.timeZone`
176+
}, function(err,res) {
177+
if (err){
178+
resolve(handlerInput.responseBuilder
179+
.speak("Something went wrong!")
180+
.withSimpleCard('Oops...', "Something broken on our part")
181+
.getResponse());
182+
} else {
165183
var query = `{
166-
time(username: "username"){
184+
time(username: "username", timeZone: ${res.body}){
167185
start(projectName: "${project}")
168186
}
169-
}`;
170-
return sendQuery(query,handlerInput);
171-
}
172-
};
187+
}`;
188+
resolve(sendQuery(query,handlerInput));
189+
}
190+
});
191+
})
192+
193+
}
194+
};
173195

174196

175197
const finishProjectIntentHandler = {
@@ -214,7 +236,7 @@ const lsIntentHandler = {
214236
},
215237
handle(handlerInput) {
216238
console.log("Ls");
217-
var query = `{
239+
var query = `{
218240
time(username: "username"){
219241
ls
220242
}
@@ -230,7 +252,7 @@ const currentIntentHandler = {
230252
},
231253
handle(handlerInput) {
232254
console.log("Current project");
233-
var query = `{
255+
var query = `{
234256
time(username: "username"){
235257
current
236258
}
@@ -247,18 +269,18 @@ const reportIntentHandler = {
247269
},
248270
handle(handlerInput) {
249271
var query=`{
250-
time(username: ${username} ){
251-
report(days:${days}){
252-
_id
253-
totalTime
254-
brief{
255-
timeSpent
256-
day
257-
month
272+
time(username: "username"){
273+
report(days: 4){
274+
_id
275+
totalTime
276+
brief{
277+
timeSpent
278+
day
279+
month
280+
}
258281
}
259-
}
260-
}
261-
}`;
282+
}
283+
}`;
262284
return sendQuery(query,handlerInput);
263285
}
264286
};

client/CLI.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
const yargs = require('yargs')
55
const request = require('request')
66
const fs = require('fs');
7-
//host = "http://localhost:3000";
8-
host = "http://54.237.77.73:80";
7+
host = "http://localhost:3000";
98
path = "/Users/bogdanabaev/RandomProgramming/node/notes/client/";
109

1110

@@ -221,19 +220,7 @@ yargs.command({
221220
});
222221

223222

224-
yargs.command({
225-
'command': 'signup <username>',
226-
'describe': 'signup',
227-
handler: function (argv) {
228-
var query = `{
229-
signup(username: "${argv.username}")
230-
}`;
231-
const res = sendQuery(query, argv.username, true);
232-
if (res) {
233-
fs.writeFileSync(path + '.username.txt', argv.username);
234-
}
235-
}
236-
});
223+
237224

238225

239226

server/helpers.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// File with helper utility functions used in time tracker implementation.
2+
3+
const mongoUtils = require('./mongoUtils.js');
4+
const moment = require('moment-timezone');
5+
6+
const subtractDates = function (date, timeZone) {
7+
return Math.round((computeTime(timeZone) - date) / 60000);
8+
}
9+
10+
const getLowerBound = function (timeZone, days) {
11+
var d = computeTime(timeZone);
12+
d.setDate(d.getDate() - days);
13+
const daysAgo = new Date(d);
14+
console.log(daysAgo);
15+
return daysAgo;
16+
}
17+
18+
19+
const updateLogInDb = (log) => {
20+
const db = mongoUtils.getDb();
21+
22+
db.collection('logs').updateOne({
23+
_id: log.id
24+
}, {
25+
$set: {
26+
finish: log.finish,
27+
duration: Number(log.duration)
28+
}
29+
}).then((suc) => console.log(suc))
30+
.catch((e) => console.log(e));
31+
}
32+
33+
34+
const nullifyCurrentProject = (username) => {
35+
const db = mongoUtils.getDb();
36+
db.collection('userData').updateOne({
37+
"user.username": username
38+
}, {
39+
$set: {
40+
currentProject: null
41+
}
42+
}).then((suc) => console.log("current project nulled"))
43+
.catch((e) => console.log(e))
44+
}
45+
46+
const setTimeZone = (username, timeZone) => {
47+
const db = mongoUtils.getDb();
48+
db.collection('userData').updateOne({
49+
"user.username": username
50+
}, {
51+
$set: {
52+
timeZone: timeZone
53+
}
54+
}).then((suc) => console.log("current project nulled"))
55+
.catch((e) => console.log(e))
56+
}
57+
58+
const insertLogToDb = (log) => {
59+
const db = mongoUtils.getDb();
60+
const addToCollection = db.collection('logs').insertOne(log);
61+
62+
addToCollection.then((suc) => {
63+
}).catch((e) => console.log(e));
64+
65+
}
66+
67+
const computeTime = function (timeZone) {
68+
const now = new Date();
69+
var mom = moment.tz(now, timeZone);
70+
const date = mom.format().substring(0, 19) + "Z";
71+
return new Date(date);
72+
}
73+
74+
module.exports = {
75+
subtractDates:subtractDates,
76+
getLowerBound:getLowerBound,
77+
updateLogInDb:updateLogInDb,
78+
nullifyCurrentProject:nullifyCurrentProject,
79+
setTimeZone:setTimeZone,
80+
insertLogToDb:insertLogToDb,
81+
computeTime:computeTime
82+
}

server/server.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,7 @@ class TimeTracker{
132132
});
133133
}
134134
}
135-
signup=function(username){
136-
var outcome = notes.signup(username);
137-
return outcome.then((result) => {
138-
return(result);
139-
}).catch((e) => {
140-
return(e)
141-
});
142-
}
135+
143136

144137
hello=function(){
145138
return "Hello!";

0 commit comments

Comments
 (0)