manifold/functions/src/scripts/create-private-users.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

import * as admin from 'firebase-admin'
2022-01-30 21:11:01 +00:00
import { initAdmin } from './script-init'
initAdmin()
import { PrivateUser, User } from 'common/user'
import { STARTING_BALANCE } from 'common/economy'
const firestore = admin.firestore()
async function main() {
const snap = await firestore.collection('users').get()
const users = snap.docs.map((d) => d.data() as User)
for (const user of users) {
const fbUser = await admin.auth().getUser(user.id)
const email = fbUser.email
const { username } = user
const privateUser: PrivateUser = {
id: user.id,
email,
username,
}
if (user.totalDeposits === undefined) {
await firestore
.collection('users')
.doc(user.id)
.update({ totalDeposits: STARTING_BALANCE })
console.log('set starting balance for:', user.username)
}
try {
await firestore
.collection('private-users')
.doc(user.id)
.create(privateUser)
console.log('created private user for:', user.username)
} catch (_) {
// private user already created
}
}
}
if (require.main === module) main().then(() => process.exit())