This repository was archived by the owner on Jun 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgetAllUsers.ts
More file actions
166 lines (153 loc) · 4.26 KB
/
getAllUsers.ts
File metadata and controls
166 lines (153 loc) · 4.26 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
import app from 'firebase/app';
import Firebase from '../components/Firebase';
export function getAllFirebaseUsers(): Promise<app.firestore.QuerySnapshot> {
const firebase = new Firebase();
return firebase
.users()
.get()
.then(function(doc) {
return doc;
})
.catch(function(error) {
console.log('Error getting document:', error);
return error;
});
}
export function updateUserList(userList: any): Promise<any[]> {
const out = userList.map((user: any) => {
return getHeartbeatInfo(user.email).then(heartbeatInfo => {
const heartbeatInfoData = heartbeatInfo.data();
if (heartbeatInfoData) {
const lastActive = heartbeatInfoData.lastActive;
user.lastActive = lastActive;
return user;
}
return user;
});
});
// we need to make this function return a promise (not an array of promises)
// so, we construct a Promise.all around it
return Promise.all(out);
}
export function getFirebaseUser(email: String): Promise<app.firestore.QuerySnapshot> {
const firebase = new Firebase();
return firebase
.user(email)
.get()
.then(function(doc) {
return doc;
})
.catch(function(error) {
console.log('Error getting document:', error);
return error;
});
}
export function getHeartbeatInfo(uid: String): Promise<app.firestore.DocumentSnapshot> {
const firebase = new Firebase();
return firebase
.user(uid)
.collection('studies')
.doc('heartbeat')
.get()
.then(function(doc) {
return doc;
})
.catch(function(error) {
console.log('Error getting document:', error);
return error;
});
}
export function getUserFromUID(uid: string): Promise<app.firestore.QueryDocumentSnapshot> {
const firebase = new Firebase();
return firebase.db
.collection('registered-patients')
.where('userID', '==', uid)
.get()
.then(querySnapshot => {
// return the first since it should be unique
return querySnapshot.docs[0];
})
.catch(function(error) {
console.log('Error getting document:', error);
return error;
});
}
// export function getMedicationsFromUID(uid: String): Promise<app.firestore.QuerySnapshot>{
// const firebase = new Firebase();
// return getUserFromUID(uid).then((userDoc) => {
// return userDoc.ref.collection('medications').get().then((querySnapshot) => {
// console.log(querySnapshot);
// return querySnapshot;
// })
// });
// }
export function saveNewMedications(userEmail: string, newMedications: any) {
const firebase = new Firebase();
firebase.db
.collection('registered-patients')
.doc(userEmail)
.update({
medications: newMedications,
})
.then(() => {
alert('Medications saved.');
})
.catch(error => {
// The document probably doesn't exist.
alert('ERROR: Medications were not saved. ' + error);
});
}
export function deleteFirebaseUser(userID: string) {
getUserFromUID(userID).then(docSnapshot => {
docSnapshot.ref
.delete()
.then(() => {
console.log('Document successfully deleted!');
})
.catch((error: any) => {
console.error('Error removing document: ', error);
});
});
}
export function getSurveys(email: String, uid: String): Promise<app.firestore.QuerySnapshot> {
const firebase = new Firebase();
return firebase
.surveys(email, uid)
.get()
.then(function(doc) {
return doc;
})
.catch(function(error) {
console.log('Error getting document:', error);
return error;
});
}
/*
import { UserDetails } from './user';
interface APIUserSummary {
ID: number;
eID: string;
lastActive: string;
lastWalktest: string;
}
export function getAllUsers(authToken: string): Promise<UserDetails[]> {
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: authToken,
Accept: 'application/json',
'X-DeviceFingerprint': 'tempfingerprint',
},
};
return request(`${BASE_URL}/api/v1/user/list`, options).then((users: APIUserSummary[]) =>
users.map(user => {
return {
ID: user.ID,
eID: user.eID,
lastActive: new Date(user.lastActive),
lastWalktest: new Date(user.lastWalktest),
};
})
);
}*/