-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathancillaryMethods.js
108 lines (95 loc) · 3.54 KB
/
ancillaryMethods.js
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
require('dotenv').config()
var log4js = require('log4js');
log4js.replaceConsole();
var logger = log4js.getLogger();
var request = require('sync-request');
const months = [
'Enero',
'Febrero',
'Marzo',
'Abril',
'Mayo',
'Junio',
'Julio',
'Agosto',
'Septiembre',
'Octubre',
'Noviembre',
'Diciembre'
];
module.exports = {
getDates: function () {
var result;
var currentMonth;
var twoMonthBefore;
var oneMonthBefore;
currentMonth = months[new Date().getMonth()] + " de " + new Date().getFullYear();
twoMonthBefore = new Date();
twoMonthBefore.setMonth(twoMonthBefore.getMonth() - 2);
twoMonthBefore = months[twoMonthBefore.getMonth()] + " de " + twoMonthBefore.getFullYear();
oneMonthBefore = new Date();
oneMonthBefore.setMonth(oneMonthBefore.getMonth() - 1);
oneMonthBefore = months[oneMonthBefore.getMonth()] + " de " + oneMonthBefore.getFullYear();
result = [currentMonth, twoMonthBefore, oneMonthBefore]
return result;
},
getNumEventsOfCurrentMonthForUserId: function (morpheuzID, fitbitID) {
var result = 0;
var res = request('GET', 'https://dreamstill-d507c.firebaseio.com/morpheuz/' + morpheuzID + '.json?auth=' + process.env.FIREBASE_SECRET + '&shallow=true', {
'headers': {
'Content-Type': ' application/json'
}
});
var resFitbit = request('GET', 'https://dreamstill-d507c.firebaseio.com/fitbit/' + fitbitID + '.json?auth=' + process.env.FIREBASE_SECRET + '&shallow=true', {
'headers': {
'Content-Type': ' application/json'
}
});
events = JSON.parse(res.getBody('utf8'));
eventsFitbit = JSON.parse(resFitbit.getBody('utf8'));
var daysOfMonth = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate();
console.log(daysOfMonth);
for (var _i = 1; _i <= daysOfMonth; _i++) {
var date = new Date(new Date().getFullYear(), new Date().getMonth(), _i)
var year = "" + date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
if (events[year + "-" + month + "-" + day] !== undefined) {
result++;
}
if (eventsFitbit[year + "-" + month + "-" + day] !== undefined) {
result++;
}
}
return result;
},
obscureEmail: function (email) {
var emailSplit = email.split('@');
var email = '';
for (i = 0; i < emailSplit[0].length; i++) {
if (i <= emailSplit.length / 2) {
email += emailSplit[0][i];
} else {
email += '*';
}
}
var result = email + '@' + emailSplit[1];
return result;
},
getSleepedHours: function (api, date) {
var result = 0;
var minutes = 0;
if (api === 'morpheuz' && date !== null) {
for (moment in date) {
if (date[moment]['Movements'] !== '-1' && date[moment]['Movements'] !== '-2' && date[moment]['Movements'] !== 'START' && date[moment]['Movements'] !== 'END' && date[moment]['Movements'] !== 'ALARM') {
minutes = minutes + 10;
}
}
}
if (api === 'fitbit' && date !== null) {
minutes = date.length;
}
result = Math.round((minutes / 60) * 100) / 100;
return result;
}
};