-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex_noti.js
413 lines (367 loc) · 11.6 KB
/
index_noti.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
var firebase = require("firebase-admin");
var http = require("http");
const { result } = require("lodash");
const express = require('express');
const path = require('path');
var serviceAccount = require("./bluetrace-lums-sproj-firebase-adminsdk-nrue3-cf47aba60c.json");
if (!firebase.apps.length) {
firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount),
});
}
const db = firebase.firestore();
var tokenList = {};
function noti(tok) {
//add name variable later
var message = {
notification: {
title: "Covid Contact Alert",
body:
"Someone you've recently met has been tested Covid Positive, Please have yourself tested and quranteened.",
},
token: tok,
};
// Send a message to the device corresponding to the provided
// registration token.
firebase
.messaging()
.send(message)
.then((response) => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
})
.catch((error) => {
console.log("Error sending message:", error);
});
}
async function addToRecentlyNotified(Uuid, From) {
const notifyDB = firebase.firestore();
const usersDb = notifyDB.collection("recentlyNotified");
const liam = usersDb.doc();
await liam.set({
// name: Name,
uuid: Uuid,
from: From,
});
}
const distinct = (value, index, self) => {
return self.indexOf(value) == index;
};
async function getAllDataFromFirebase(db) {
const covidPosRef = db.collection("covidPositive");
const snapshot = await covidPosRef.get();
// Get covid pos user name
var cpUser = [];
var cpUuid = [];
// Get user close contact table
const closeContactRef = db.collection("userCloseContact");
const closeTable = await closeContactRef.get();
// Get user close contact name
var uccName = [];
var timestamp = [];
var location = [];
var uccUuid1 = [];
var uccUuid2 = [];
// get recently notified name
var recNot = [];
var recNotTemp = [];
// const recentlyNotifiedRef = db.collection('recentlyNotified');
const recentlyNotifiedRef = db.collection("recentlyNotified");
const recentlyNotifiedTable = await recentlyNotifiedRef.get();
recentlyNotifiedTable.forEach((doc) => {
recNotTemp.push(doc.data().uuid);
});
// reomve duplicates
recNot = recNotTemp.filter(distinct);
// test(cpUser,cpUuid,uccName,uccUuid1,uccUuid2,recNot)
// check for updates in recently notified
const observer = db
.collection("recentlyNotified")
.onSnapshot((querySnapshot) => {
var recNotTemp2 = recNotTemp;
querySnapshot.docChanges().forEach((change) => {
if (change.type === "added") {
recNotTemp2.push(change.doc.data().uuid);
if (tokenList[change.doc.data().uuid] === undefined) {
console.log('My object: ', tokenList);
console.log("user with invalid token");
} else {
lst = tokenList[change.doc.data().uuid];
for (var tks in lst) {
//console.log(tks, " ", lst[tks]);
noti(lst[tks]);
}
}
}
if (change.type === "removed") {
// console.log("REMOVEDEDDE", change.doc.data().from ,"UUID" , change.doc.data().uuid)
const index = recNotTemp2.indexOf(change.doc.data().uuid);
if (index > -1) {
recNotTemp2.splice(index, 1);
}
}
});
recNot = recNotTemp2.filter(distinct);
// console.log("SENDING REC NOT TABLE UPDATE");
// send data to main func
test(cpUser, cpUuid, uccName, uccUuid1, uccUuid2, recNot);
});
// cehck for updates in covid pos
const observer2 = db
.collection("covidPositive")
.onSnapshot((querySnapshot) => {
querySnapshot.docChanges().forEach((change) => {
if (change.type === "added") {
cpUuid.push(change.doc.data().uuid);
var uuid_covid = change.doc.data().uuid;
cpUser.push(change.doc.data().user);
var user_covid = change.doc.data().user;
db.collection("contactData")
.where("sndUserUUID", "==", uuid_covid)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
// doc.data() is never undefined for query doc snapshots
//console.log(doc.id, " => ", doc.data());
db.collection("contactData")
.doc(doc.id)
.set({ covidStatus: true }, { merge: true });
});
});
}
// if (change.type === 'removed') {
// const indexUuid = cpUuid.indexOf(change.doc.data().uuid)
// const indexUser = cpUser.indexOf(change.doc.data().user)
// if (index>-1){
// cpUuid.splice(indexUuid,1)
// cpUser.splice(indexUser,1)
// }
// }
});
//console.log("SENDING COVID POS TABLE UPDATE");
test(cpUser, cpUuid, uccName, uccUuid1, uccUuid2, recNot);
});
// check for updates in user close contacts
const observer3 = db
.collection("userCloseContact")
.onSnapshot((querySnapshot) => {
querySnapshot.docChanges().forEach((change) => {
if (change.type === "added") {
uccName.push(change.doc.data().name);
var currUUID = change.doc.data().uuid1;
uccUuid1.push(change.doc.data().uuid1);
uccUuid2.push(change.doc.data().uuid2);
var my_uuid2 = change.doc.data().uuid2;
timestamp.push(change.doc.data().timestamp);
var times = change.doc.data().timestamp;
location.push(change.doc.data().location);
var loc = change.doc.data().location;
//console.log("detected");
//console.log(change.doc.data().name);
db.collection("users")
.where("uuid", "==", my_uuid2)//change.doc.data().my_uuid2)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
// doc.data() is never undefined for query doc snapshots
//console.log(doc.id, " => ", doc.data());
db.collection("contactData").add({
uuid: currUUID,
sndUserUUID: my_uuid2,
sndUserName: doc.data().name,
covidStatus: doc.data().covidStatus,
location: loc,
timestamp: times,
});
});
});
}
if (change.type === "removed") {
const indexName = cpUuid.indexOf(change.doc.data().name);
const indexUuid1 = cpUser.indexOf(change.doc.data().uuid1);
const indexUuid2 = cpUser.indexOf(change.doc.data().uuid2);
if (index > -1) {
uccName.splice(indexName, 1);
uccUuid1.splice(indexUuid1, 1);
uccUuid2.splice(indexUuid2, 1);
}
}
});
// (doc) => {
// db.collection('contactData').add({uuid:uccUuid1[0],sndUserName:doc.data().name,covidStatus:doc.data().covidStatus,location:location[0],timestamp:timestamp[0]});
// }
// ).catch((error) => {
// console.log("Error getting document:", error);
// });
//console.log("SENDING USER CLOSE CONTACT TABLE UPDATE");
// send data to main function
test(cpUser, cpUuid, uccName, uccUuid1, uccUuid2, recNot);
});
}
function test(cpUser, cpUuid, uccName, uccUuid1, uccUuid2, recNot) {
/*console.log("COVID pos user name", cpUser, cpUser);
console.log("COVID pos user uuid", cpUuid);
console.log("User close contact user name", uccName);
console.log("User close contact user ID 1", uccUuid1);
console.log("User close contact user ID 2", uccUuid2);
console.log("recently notified", recNot);*/
var notifyUsersID = [];
var notifyFrom = [];
var notifyName = [];
for (i in cpUuid) {
for (k in uccUuid1) {
if (cpUuid[i] == uccUuid1[k] && !recNot.includes(uccUuid2[k])) {
// console.log(cpUuid[i], "has been tested pos, plz inform", uccUuid2[k]);
if (
notifyUsersID.includes(uccUuid2[k]) &&
notifyFrom.includes(cpUuid[i])
) {
} else {
// notifyName.push(uccName[k])
notifyUsersID.push(uccUuid2[k]);
notifyFrom.push(cpUuid[i]);
}
}
}
for (m in uccUuid2) {
if (cpUuid[i] == uccUuid2[m] && !recNot.includes(uccUuid1[m])) {
//console.log(cpUuid[i], "has been tested pos, plz inform", uccUuid1[m]);
if (
notifyUsersID.includes(uccUuid1[m]) &&
notifyFrom.includes(cpUuid[i])
) {
} else {
notifyUsersID.push(uccUuid1[m]);
notifyFrom.push(cpUuid[i]);
}
}
}
}
if (cpUser.length != 0 && uccName.length != 0) {
for (i in notifyUsersID) {
addToRecentlyNotified(notifyUsersID[i], notifyFrom[i]);
}
}
}
async function addToCovidPos(db, Timestampo, User, Uuid) {
await db.collection("covidPositive").add({
timestamp: Timestamp.fromMillisecondsSinceEpoch(parseInt(Timestampo)),
user: User,
uuid: Uuid,
});
db.collection("users")
.where("uuid", "==", Uuid)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
// doc.data() is never undefined for query doc snapshots
//console.log(doc.id, " => ", doc.data());
db.collection("users")
.doc(doc.id)
.set({ covidStatus: true }, { merge: true });
});
});
}
module.exports = function (req, res,next) {
console.log("middleware launch");
getAllDataFromFirebase(db);
var tokken, guid;
if (req.method === "POST") {
console.log("Posting posted");
tokenList = {};
//console.log(req.body);
type = req.body.callType;
tokken = req.body.token;
guid = req.body.uuid;
//console.log("CALLED");
//console.log(type);
//noti(tokken);
if (tokenList[guid] === undefined) {
tokenList[guid] = [tokken];
} else if (tokenList[guid] != tokken) {
tokenList[guid].push(tokken);
}
}
else{
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Server Up and Running");
}
next();
};
//Notification Implementation:
//module.exports.admin = firebase
// http.createServer(function (req, res) {
// res.writeHead(200, { "Content-Type": "text/plain" });
// res.end("Server Up and Running");
// }).listen(process.env.PORT ||8080);
// var port = process.env.PORT || 3000;
// var app = express();
// //var bodyParser = require("body-parser");
// app.use('/json', express.json());
// // app.use('/user/:id', function (req, res, next) {
// // console.log('Request Type:', req.method);
// // next();
// // });
// app.post('/', function (req, res) {
// var type, tokken, guid;
// var body = "";
// var obj;
// type = req.body.callType;
// tokken = req.body.token;
// guid = req.body.uuid;
// console.log("CALLED");
// console.log(type);
// //noti(tokken);
// if (tokenList[guid] === undefined) {
// tokenList[guid] = [tokken];
// } else if (tokenList[guid] != tokken) {
// tokenList[guid].push(tokken);
// }
// res.send("POST REQUEST PROCESSED");
// });
// //module.exports = router;
// var server = app.listen(port, function () {
// // console.log("authondication checker process");
// // getAllDataFromFirebase(db);
// console.log(`Node server is running..`);
// });
// var tokken, guid;
// if (req.method === "POST") {
// console.log("Posting posted");
// var body = "";
// var obj;
// req.on('error', (err) => {
// // This prints the error message and stack trace to `stderr`.
// console.error(err.stack);
// });
// req.on("data", function (piece) {
// console.log("IDHR1");
// body += piece.toString();
// });
// req.on("end", function () {
// obj = JSON.parse(body);
// var type = String(obj.callType);
// //console.log("IDHR");
// //if (type === "noti_token_provision") {
// //console.log("obj.token");
// tokken = obj.token;
// guid = obj.uuid;
// noti(tokken);
// // if (tokenList[guid] === undefined) {
// // tokenList[guid] = [tokken];
// // } else if (tokenList[guid] != tokken) {
// // tokenList[guid].push(tokken);
// // }
// // } else if (type === "updated_covid_pos") {
// // Timestampi = obj.timestamp;
// // User = obj.user;
// // Uuid = obj.uuid;
// // //addToCovidPos(db, Timestampi, User, Uuid);
// // console.log("Timestampi");
// // }
// //tokenList[guid] = tokken;
// //console.log(tokenList);
// });
// //res.writeHead(201, { "Content-Type": "text/html" });
// //return res.end("IF STATEMENT");
// }