-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabaseDiff.txt
More file actions
178 lines (172 loc) · 7.76 KB
/
databaseDiff.txt
File metadata and controls
178 lines (172 loc) · 7.76 KB
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
diff --git a/app/lib/Database.js b/app/lib/Database.js
index ddb3eb2..b5d564a 100644
--- a/app/lib/Database.js
+++ b/app/lib/Database.js
@@ -55,17 +55,74 @@ class Database {
*/
static unsubscribeUser(userCurrentExperimentID) {
return new Promise( (success, fail) => {
-
//Update this experiment's active user count
let dailyReactionPath = "/experiment/" + userCurrentExperimentID;
success(firebase.database().ref(dailyReactionPath).child('active_user_count').transaction(function (currentValue) {
return (currentValue || 0) - 1
+ }).then(() => {
+ this.archiveUserData(userCurrentExperimentID)
}));
-
});
+ }
+
+ /**
+ * Archive the user's data
+ * @param userCurrentExperimentID
+ * @returns {firebase.Promise<any>|!firebase.Promise.<void>}
+ */
+ static archiveUserData(userCurrentExperimentID) {
+ return new Promise( (success, fail) => {
+ firebase.auth().onAuthStateChanged((user) => {
+ if (user) {
+ var id = user.uid;
+ var userPath = "/user/"+id;
+
+ //extract the user's satisfaction rating
+ firebase.database().ref(userPath).on('value', (data) => {
+
+ console.log("ARCHIVING DATA");
+ //Check to see if the user has a satisfaction for the experiment their subscribed to, if not, don't update their archive.
+ if(data.val().satisfaction){
+ var userSatisfaction = data.val().satisfaction;
+
+ console.log("With userSatisfaction: "+userSatisfaction);
+ let archiveDataPath = "/user/" + id + "/archive_data/"+userCurrentExperimentID;
+ success( firebase.database().ref(archiveDataPath).update({
+ satisfaction: userSatisfaction
+ }).then(()=>{
+ this.clearActiveExperimentData()
+ }))
+ } else {
+ success();
+ }
+ });
+ }
+ });
+ });
}
+ /**
+ * Archive the user's data
+ * @param userCurrentExperimentID
+ * @returns {firebase.Promise<any>|!firebase.Promise.<void>}
+ */
+ static clearActiveExperimentData() {
+ return new Promise((success, fail) => {
+ firebase.auth().onAuthStateChanged((user) => {
+ if (user) {
+ var id = user.uid;
+ var userPath = "/user/" + id;
+
+ success(firebase.database().ref(userPath).update({
+ reactions: null,
+ satisfaction: null
+ }))
+
+ }
+ });
+ });
+ }
/**
* Sets a users reaction for the day
@@ -104,7 +161,6 @@ class Database {
if(user) {
var id = user.uid;
let userPath = "/user/" + id ;
-
//get the user's reactions -> then average them
this.getMyExperimentData().then((userExperimentData) => {
var reactions = userExperimentData.reactions;
@@ -114,7 +170,6 @@ class Database {
count += 1;
});
satisfaction = sum / count;
-
//post user satisfaction
success( firebase.database().ref(userPath).update({
satisfaction: satisfaction
@@ -143,14 +198,17 @@ class Database {
//get the user's reactions -> then average them&&&&&&&&&&&&&&
this.getAllUserExperimentData(experiment_id).then((userExperimentSatisfactionData) => {
-
//calculate the overall satisfaction
let sum = 0, count = 0;
userExperimentSatisfactionData.map((userSatisfaction) => {
sum += userSatisfaction;
count += 1;
});
- var experimentSatisfaction = sum / count;
+ var experimentSatisfaction;
+ if(count == 0)
+ experimentSatisfaction = null; //delete this field for now
+ else
+ experimentSatisfaction = sum / count;
//post it in the experiment
success( firebase.database().ref(experimentPath).update({
@@ -166,19 +224,36 @@ class Database {
* Gets all users satisfaction ratings for a given experiment
* @returns {firebase.Promise<any>|!firebase.Promise.<void>}
*/
- static getAllUserExperimentData(experiment_id) {
+ static
+ getAllUserExperimentData(experiment_id) {
return new Promise( (success, fail) => {
let userPath = "/user/" ;
return firebase.database().ref(userPath).on('value', (data) => {
//convert data to an array
var userList =[]
+ var userArchiveList = []
data.forEach(function(data) {
-
//Filter out all but given experiment id - also filter out any nulls - this would indicate that a user has subscribed to an experiment, but hasn't posted any reactions yet
- if(data.val().experiment_id == experiment_id && data.val().satisfaction){
+ if(data.val().satisfaction && data.val().experiment_id == experiment_id){
userList.push(data.val().satisfaction);
}
+ //Snatch all fromthe archives as well ;)
+ // if(data.val().archive_data) {
+ // // Object.keys(data.val().archive_data).forEach((key) => {
+ // // if(key) {
+ // // //console.log('KEEEEEEEY' + key);
+ // // if(key == experiment_id){
+ // // if(data.val().archive_data[key]){
+ // // console.log(data.val().archive_data[key]);
+ // // userArchiveList.push(data.val().archive_data[key].satisfaction)
+ // // }
+ // //
+ // // }
+ // // }
+ // // });
+ // }
+ // console.log("ARCHIVE USER RATINGS: "+ JSON.stringify(userArchiveList));
});
success(userList);
});
@@ -197,12 +272,10 @@ class Database {
static getMyExperimentData() {
return new Promise( (success, fail) => {
firebase.auth().onAuthStateChanged((user) => { //Retrieve current users id
-
if(user) {
var id = user.uid;
let experimentPath = "/user/" + id ;
return firebase.database().ref(experimentPath).on('value', (snapshot) => {
-
success(snapshot.val());
});
}
@@ -251,7 +324,6 @@ class Database {
let experimentPath = "/experiment/" + experiment_id;
return firebase.database().ref(experimentPath).on('value', (snapshot) => {
var experimentInfo = JSON.stringify(snapshot.val());
- console.log("experimentInfo: "+experimentInfo);
success(snapshot.val());
});
}