-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate-participants.js
55 lines (41 loc) · 1.46 KB
/
update-participants.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
require('dotenv').config();
const admin = require('firebase-admin');
const firebaseConfig = {
// Your Firebase project configuration
};
const serviceAccount = JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT);
// Initialize Firebase Admin SDK
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const db = admin.firestore();
async function main() {
try {
// Get a reference to the 'participants' collection
const participantsRef = db.collection('participants');
// Get all participants
const snapshot = await participantsRef.get();
// Loop through all participants
snapshot.forEach(async (doc) => {
const participant = doc.data();
try {
// Get the user record by email
const user = await admin.auth().getUserByEmail(participant.participantEmail);
// Update the participant document with the UID
await participantsRef.doc(doc.id).update({
uid: user.uid
});
console.log(`Updated participant ${doc.id} with UID ${user.uid}`);
} catch (error) {
console.error(`Failed to update participant ${doc.id}:`, error.message);
// Handle the case where the user could not be found
if (error.code === 'auth/user-not-found') {
console.error(`No user found for email ${participant.participantEmail}`);
}
}
});
} catch (error) {
console.error('[Error] Processing participants:', error.message);
}
}
main();