This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
veronica.js
183 lines (158 loc) · 4.22 KB
/
veronica.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
const program = require('commander');
const fs = require('fs');
const admin = require('firebase-admin');
const secretKeys = require('../veronica-keys/veronica-roma-firebase-keys.json');
const config = require('./config.json');
/**
* Firebase: initialize admin API
*/
admin.initializeApp({
credential: admin.credential.cert(secretKeys),
databaseURL: config['databaseURL']
});
const auth = admin.auth();
const firestore = admin.firestore();
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);
/**
* CLI: Create Users
*/
program
.command('create <object>')
.description('Create specified <object> in firebase database')
.action(async function(entity, args) {
hasCmd = true;
var json = JSON.parse(fs.readFileSync('../veronica-keys/veronica-roma-firebase-users.json'))
var users = json['users']
for (var i in users) {
await createUser(users[i]);
}
process.exit(0);
});
/**
* CLI: Clear all users and records from database
*/
program
.command('clear')
.description('Remove all users and records from database')
.action(async function(args) {
hasCmd = true;
await clear();
process.exit(0);
});
var hasCmd = false;
program.parse(process.argv);
if (! hasCmd) {
console.error('ERROR: Invalid command or no command was given');
process.exit(1);
}
/**
* Firebase: create user
*/
async function createUser(user) {
console.log('User: ' + user['email']);
var userRecord;
var errCode;
// remove user record if user already exists
try {
userRecord = await auth.getUserByEmail(user['email']);
} catch (err) {
errCode = err['code'];
}
if (errCode != 'auth/user-not-found') {
console.log('Warning: user already exists');
console.log(userRecord['uid']);
deleteUser(userRecord);
}
console.log('');
// create user
try {
// add user to firebase.authentication
userRecord = await auth.createUser({
email: user['email'],
emailVerified: false,
password: user['password'],
displayName: user['displayName'],
disable: false
});
// add user roles to firebase.firestore
await firestore.collection('users-roles').doc(userRecord['uid']).set({
roles: user['roles']
});
} catch (err) {
console.log(err);
}
}
/**
* Remove all records for the given user
*/
async function deleteUser(user) {
try {
await auth.deleteUser(user['uid']);
} catch (err) {
console.log(err);
}
}
/**
* Firebase: clear database
*/
async function clear() {
const usersPerPage = 25;
const firebaseDelay = 200;
var nextPageToken;
var usersIds = [];
// delay function in order do not overload firebase quota
const pauseFor = (delay) => new Promise(resolve => setTimeout(resolve, delay));
try {
// get list of all users
do {
result = await auth.listUsers(usersPerPage, nextPageToken);
result.users.forEach(userRecord => {
usersIds.push(userRecord.uid)
});
nextPageToken = result.pageToken;
} while (nextPageToken);
// remove users
for (let userId of usersIds) {
// var deleteUser = async (userId) => {
// await pauseFor(firebaseDelay);
// console.log(`User Id: ${userId}`);
// }
// await deleteUser(userId);
await (async () => {
await pauseFor(firebaseDelay);
await admin.auth().deleteUser(userId);
console.log(`Remove user: ${userId}`);
})();
}
// get list of all documents
var collectionRef, snapshot;
var docs = [];
collectionRef = firestore.collection('users');
snapshot = await collectionRef.get();
for (let doc of snapshot.docs) {
docs.push({
collection: 'users',
id: doc.id
});
}
collectionRef = firestore.collection('users-roles');
snapshot = await collectionRef.get();
for (let doc of snapshot.docs) {
docs.push({
collection: 'users-roles',
id: doc.id
});
}
// remove documents
for (let doc of docs) {
await (async () => {
await pauseFor(firebaseDelay);
await firestore.collection(doc.collection).doc(doc.id).delete();
console.log(`Remove document: ${doc.collection}/${doc.id}`);
})();
}
} catch (err) {
console.log(err);
}
}