-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathswitch-data.js
More file actions
73 lines (61 loc) · 1.82 KB
/
Copy pathswitch-data.js
File metadata and controls
73 lines (61 loc) · 1.82 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
const fb = require("firebase-admin");
const serviceAccount = require("../private/service-account.json");
// Their old account with the data
const FROM_EMAIL = "5841@ccbcali.edu.co";
// Their new account
const TO_EMAIL = "jmanyoma20@gmail.com";
//////
fb.initializeApp({
credential: fb.credential.cert(serviceAccount),
databaseURL: "https://parallel-beta-31dc4.firebaseio.com",
});
const getAnswerCount = (data) =>
data.answers ? Object.keys(data.answers).length : 0;
const getUserData = (email) =>
fb.auth()
.getUserByEmail(email)
.then((user) => fb.firestore().collection("users").doc(user.uid).get())
.then((doc) => doc.data())
.then((data) => {
console.log(`Account "${email}" has ${getAnswerCount(data)} answers`);
return data;
}).catch(err => {
err.message = `${err.message} - ${email}`
throw err;
});
const updateUserData = (email, data) =>
fb.auth()
.getUserByEmail(email)
.then((user) =>
fb
.firestore()
.collection("users")
.doc(user.uid)
.update(data)
);
const run = async () => {
if(FROM_EMAIL === TO_EMAIL) throw new Error("Both emails are the same")
const fromUser = await getUserData(FROM_EMAIL);
const toUser = await getUserData(TO_EMAIL);
if (!getAnswerCount(fromUser)) {
throw new Error(`Account "${FROM_EMAIL}" has no answers to transfer`);
}
const combinedUser = {
...toUser,
answers: {
...fromUser.answers,
...toUser.answers, // Retain their new accounts latest answers, over their old account
},
};
await updateUserData(TO_EMAIL, combinedUser);
};
run()
.then(() => {
console.error("Success switching data!");
process.exit(0);
})
.catch((err) => {
console.error("\nError switching data!");
console.error(err.message + '\n');
process.exit(1);
});