-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (67 loc) · 1.88 KB
/
index.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
import admin from 'firebase-admin'
import serviceAccount from './tocpc-admin.json'
import { exec } from 'child_process'
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://tocpc-prod-2021.firebaseio.com/',
})
const zeroPad = (num, places) => String(num).padStart(places, '0')
let id = 1
const nexec = (cmd, args) => {
return new Promise((resolve, reject) => {
exec(cmd + ' ' + args.join(' '), (error, stdout, stderr) => {
if (error) {
console.warn(error)
}
resolve(stdout ? stdout : stderr)
})
})
}
while (true) {
const data = await admin
.firestore()
.collection('users')
.where('username', '==', 'user' + zeroPad(id, 4))
.get()
let cnt = 0
data.forEach((d) => {
cnt++
})
if (cnt === 0) {
break
} else {
id++
}
}
admin
.firestore()
.collection('users')
.where('username', '==', '')
.where('password', '!=', '')
.limit(1)
.onSnapshot((docs) => {
docs.forEach(async (doc) => {
const uid = doc.id
const data = doc.data()
let username = 'user' + zeroPad(id++, 4)
await nexec('cmsAddUser ', [
'-H',
data.password.replaceAll('$', '\\$'),
'--bcrypt',
data.firstname,
data.lastname,
username,
])
if (data.anonymous) {
await nexec('cmsAddParticipation', ['--hidden', '-c', '1', username])
await nexec('cmsAddParticipation', ['--hidden', '-c', '2', username])
await nexec('cmsAddParticipation', ['--hidden', '-c', '3', username])
} else {
await nexec('cmsAddParticipation', ['-c', '1', username])
await nexec('cmsAddParticipation', ['-c', '2', username])
await nexec('cmsAddParticipation', ['-c', '3', username])
}
console.log('exec on user:', username)
admin.firestore().doc(`users/${uid}`).update({ username })
})
})