-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood_and_mentor_badges.js
69 lines (65 loc) · 2.54 KB
/
food_and_mentor_badges.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
require("dotenv").config({ path: "./.env" });
const fs = require('fs');
const firebase = require("firebase-admin");
const serviceAccount = require("./servicer.json");
// establish firebase connection
firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount),
databaseURL: process.env.DATABASE_URL
});
var db = firebase.firestore();
try {
var all_emails = fs.readFileSync('mentor_feedback_emails.txt', 'utf8');
all_emails = all_emails.replace(/\s+/g, ' ').trim();
emails = all_emails.split(' ');
for (let i = 0; i < emails.length; i++)
{
// emails[i] = RemovePeriodsInEmail(emails[i]);
}
} catch(e) {
console.log('Error:', e.stack);
}
// This block gives every user a "Yummy" badge on the website if they were a valid email
// in Firebase that had previously received a code
db.collection("used_codes").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
db.collection("users").where("email", "==", doc.id)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
let updatedBadges = JSON.parse(JSON.stringify(doc.data().badges));
updatedBadges["Yummy"] = new Date(Date.now()).toDateString();
db.collection("users").doc(doc.id).set({
badges: updatedBadges
}, { merge: true })
.then(function() {
console.log(updatedBadges);
})
})
})
.catch(function(error) {
console.log("Error getting documents: ", error);
})
});
});
// This block parses from a .txt file called mentor_feedback_emails.txt and gives every email
// on its own line in that file the "Mentored" code for filling out a mentored feedback form from HD 2021
for (let i = 0; i < emails.length; i++) {
db.collection("users").where("email", "==", emails[i])
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
let updatedBadges = JSON.parse(JSON.stringify(doc.data().badges));
updatedBadges["Mentored"] = new Date(Date.now()).toDateString();
db.collection("users").doc(doc.id).set({
badges: updatedBadges
}, { merge: true })
.then(function() {
console.log(updatedBadges);
})
})
})
.catch(function(error) {
console.log("Error getting documents: ", error);
})
}