Merge branch 'manifold'
This commit is contained in:
commit
6d86a5eb49
|
@ -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())
|
|
@ -1,12 +1,12 @@
|
||||||
import * as mailgun from 'mailgun-js'
|
import * as mailgun from 'mailgun-js'
|
||||||
import * as functions from 'firebase-functions'
|
import * as functions from 'firebase-functions'
|
||||||
|
|
||||||
const DOMAIN = 'mg.mantic.markets'
|
const DOMAIN = 'mg.manifold.markets'
|
||||||
const mg = mailgun({ apiKey: functions.config().mailgun.key, domain: DOMAIN })
|
const mg = mailgun({ apiKey: functions.config().mailgun.key, domain: DOMAIN })
|
||||||
|
|
||||||
export const sendEmail = (to: string, subject: string, text: string) => {
|
export const sendEmail = (to: string, subject: string, text: string) => {
|
||||||
const data = {
|
const data = {
|
||||||
from: 'Mantic Markets <no-reply@mantic.markets>',
|
from: 'Manifold Markets <no-reply@manifold.markets>',
|
||||||
to,
|
to,
|
||||||
subject,
|
subject,
|
||||||
text,
|
text,
|
||||||
|
|
|
@ -4,14 +4,14 @@ import { Contract } from './types/contract'
|
||||||
import { User } from './types/user'
|
import { User } from './types/user'
|
||||||
|
|
||||||
export const getValue = async <T>(collection: string, doc: string) => {
|
export const getValue = async <T>(collection: string, doc: string) => {
|
||||||
const snap = await admin.firestore()
|
const snap = await admin.firestore().collection(collection).doc(doc).get()
|
||||||
.collection(collection)
|
|
||||||
.doc(doc)
|
|
||||||
.get()
|
|
||||||
|
|
||||||
return snap.exists
|
return snap.exists ? (snap.data() as T) : undefined
|
||||||
? 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) => {
|
export const getContract = (contractId: string) => {
|
||||||
|
@ -20,4 +20,4 @@ export const getContract = (contractId: string) => {
|
||||||
|
|
||||||
export const getUser = (userId: string) => {
|
export const getUser = (userId: string) => {
|
||||||
return getValue<User>('users', userId)
|
return getValue<User>('users', userId)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user