Rename user contracts
This commit is contained in:
parent
b928d0e70c
commit
bcd86ddce9
|
@ -1,61 +0,0 @@
|
|||
import * as admin from 'firebase-admin'
|
||||
import * as _ from 'lodash'
|
||||
import { Bet } from '../types/bet'
|
||||
import { Contract } from '../types/contract'
|
||||
|
||||
type DocRef = admin.firestore.DocumentReference
|
||||
|
||||
// Generate your own private key, and set the path below:
|
||||
// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
|
||||
const serviceAccount = require('../../../../Downloads/dev-mantic-markets-firebase-adminsdk-sir5m-b2d27f8970.json')
|
||||
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
})
|
||||
const firestore = admin.firestore()
|
||||
|
||||
async function recalculateContract(contractRef: DocRef, contract: Contract) {
|
||||
const bets = await contractRef
|
||||
.collection('bets')
|
||||
.get()
|
||||
.then((snap) => snap.docs.map((bet) => bet.data() as Bet))
|
||||
|
||||
const openBets = bets.filter((b) => !b.isSold && !b.sale)
|
||||
|
||||
const totalShares = {
|
||||
YES: _.sumBy(openBets, (bet) => (bet.outcome === 'YES' ? bet.shares : 0)),
|
||||
NO: _.sumBy(openBets, (bet) => (bet.outcome === 'NO' ? bet.shares : 0)),
|
||||
}
|
||||
|
||||
const totalBets = {
|
||||
YES: _.sumBy(openBets, (bet) => (bet.outcome === 'YES' ? bet.amount : 0)),
|
||||
NO: _.sumBy(openBets, (bet) => (bet.outcome === 'NO' ? bet.amount : 0)),
|
||||
}
|
||||
|
||||
await contractRef.update({ totalShares, totalBets })
|
||||
|
||||
console.log(
|
||||
'calculating totals for "',
|
||||
contract.question,
|
||||
'" total bets:',
|
||||
totalBets
|
||||
)
|
||||
console.log()
|
||||
}
|
||||
|
||||
async function migrateContracts() {
|
||||
console.log('Recalculating contract info')
|
||||
|
||||
const snapshot = await firestore.collection('contracts').get()
|
||||
const contracts = snapshot.docs.map((doc) => doc.data() as Contract)
|
||||
|
||||
console.log('Loaded', contracts.length, 'contracts')
|
||||
|
||||
for (const contract of contracts) {
|
||||
const contractRef = firestore.doc(`contracts/${contract.id}`)
|
||||
|
||||
await recalculateContract(contractRef, contract)
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) migrateContracts().then(() => process.exit())
|
46
functions/src/scripts/rename-user-contracts.ts
Normal file
46
functions/src/scripts/rename-user-contracts.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
import * as admin from 'firebase-admin'
|
||||
import * as _ from 'lodash'
|
||||
import { Contract } from '../types/contract'
|
||||
import { getValues } from '../utils'
|
||||
|
||||
// Generate your own private key, and set the path below:
|
||||
// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
|
||||
// James:
|
||||
const serviceAccount = require('../../../../Downloads/mantic-markets-firebase-adminsdk-1ep46-820891bb87.json')
|
||||
// Stephen:
|
||||
// const serviceAccount = require('../../../../Downloads/dev-mantic-markets-firebase-adminsdk-sir5m-b2d27f8970.json')
|
||||
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
})
|
||||
const firestore = admin.firestore()
|
||||
|
||||
async function renameUserContracts(
|
||||
username: string,
|
||||
newNames: { name: string; username: string }
|
||||
) {
|
||||
console.log(`Renaming contracts of ${username} to`, newNames)
|
||||
|
||||
const contracts = await getValues<Contract>(
|
||||
firestore.collection('contracts').where('creatorUsername', '==', username)
|
||||
)
|
||||
|
||||
console.log('Loaded', contracts.length, 'contracts by', username)
|
||||
|
||||
for (const contract of contracts) {
|
||||
const contractRef = firestore.doc(`contracts/${contract.id}`)
|
||||
|
||||
console.log('Renaming', contract.slug)
|
||||
|
||||
await contractRef.update({
|
||||
creatorUsername: newNames.username,
|
||||
creatorName: newNames.name,
|
||||
} as Partial<Contract>)
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module)
|
||||
renameUserContracts('ManticMarkets', {
|
||||
username: 'ManifoldMarkets',
|
||||
name: 'Manifold Markets',
|
||||
}).then(() => process.exit())
|
|
@ -4,14 +4,14 @@ import { Contract } from './types/contract'
|
|||
import { User } from './types/user'
|
||||
|
||||
export const getValue = async <T>(collection: string, doc: string) => {
|
||||
const snap = await admin.firestore()
|
||||
.collection(collection)
|
||||
.doc(doc)
|
||||
.get()
|
||||
const snap = await admin.firestore().collection(collection).doc(doc).get()
|
||||
|
||||
return snap.exists
|
||||
? snap.data() as T
|
||||
: undefined
|
||||
return snap.exists ? (snap.data() as T) : undefined
|
||||
}
|
||||
|
||||
export const getValues = async <T>(query: admin.firestore.Query) => {
|
||||
const snap = await query.get()
|
||||
return snap.docs.map((doc) => doc.data() as T)
|
||||
}
|
||||
|
||||
export const getContract = (contractId: string) => {
|
||||
|
@ -20,4 +20,4 @@ export const getContract = (contractId: string) => {
|
|||
|
||||
export const getUser = (userId: string) => {
|
||||
return getValue<User>('users', userId)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user