From bcd86ddce9a795f2453bf4b039211f60b1d229ca Mon Sep 17 00:00:00 2001 From: jahooma Date: Thu, 6 Jan 2022 18:05:48 -0600 Subject: [PATCH] Rename user contracts --- functions/src/scripts/recalculate.ts | 61 ------------------- .../src/scripts/rename-user-contracts.ts | 46 ++++++++++++++ functions/src/utils.ts | 16 ++--- 3 files changed, 54 insertions(+), 69 deletions(-) delete mode 100644 functions/src/scripts/recalculate.ts create mode 100644 functions/src/scripts/rename-user-contracts.ts diff --git a/functions/src/scripts/recalculate.ts b/functions/src/scripts/recalculate.ts deleted file mode 100644 index 103152c9..00000000 --- a/functions/src/scripts/recalculate.ts +++ /dev/null @@ -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()) diff --git a/functions/src/scripts/rename-user-contracts.ts b/functions/src/scripts/rename-user-contracts.ts new file mode 100644 index 00000000..2debd9e7 --- /dev/null +++ b/functions/src/scripts/rename-user-contracts.ts @@ -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( + 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) + } +} + +if (require.main === module) + renameUserContracts('ManticMarkets', { + username: 'ManifoldMarkets', + name: 'Manifold Markets', + }).then(() => process.exit()) diff --git a/functions/src/utils.ts b/functions/src/utils.ts index ff3e8fb3..85f5b3fa 100644 --- a/functions/src/utils.ts +++ b/functions/src/utils.ts @@ -4,14 +4,14 @@ import { Contract } from './types/contract' import { User } from './types/user' export const getValue = async (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 (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('users', userId) -} \ No newline at end of file +}