2021-12-11 00:06:17 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
|
2022-06-12 23:22:29 +00:00
|
|
|
import { chunk } from 'lodash'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Contract } from '../../common/contract'
|
|
|
|
import { PrivateUser, User } from '../../common/user'
|
2022-07-07 23:23:13 +00:00
|
|
|
import { Group } from '../../common/group'
|
2022-08-29 15:06:17 +00:00
|
|
|
import { Post } from 'common/post'
|
2021-12-11 00:06:17 +00:00
|
|
|
|
2022-06-12 23:22:29 +00:00
|
|
|
export const log = (...args: unknown[]) => {
|
|
|
|
console.log(`[${new Date().toISOString()}]`, ...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const logMemory = () => {
|
|
|
|
const used = process.memoryUsage()
|
|
|
|
for (const [k, v] of Object.entries(used)) {
|
|
|
|
log(`${k} ${Math.round((v / 1024 / 1024) * 100) / 100} MB`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 21:31:39 +00:00
|
|
|
export type UpdateSpec = {
|
2022-06-15 00:38:38 +00:00
|
|
|
doc: admin.firestore.DocumentReference
|
|
|
|
fields: { [k: string]: unknown }
|
|
|
|
}
|
|
|
|
|
2022-06-22 20:29:40 +00:00
|
|
|
export const writeAsync = async (
|
2022-06-15 00:38:38 +00:00
|
|
|
db: admin.firestore.Firestore,
|
|
|
|
updates: UpdateSpec[],
|
2022-06-22 20:29:40 +00:00
|
|
|
operationType: 'update' | 'set' = 'update',
|
2022-06-15 00:38:38 +00:00
|
|
|
batchSize = 500 // 500 = Firestore batch limit
|
2022-06-12 23:22:29 +00:00
|
|
|
) => {
|
2022-06-15 00:38:38 +00:00
|
|
|
const chunks = chunk(updates, batchSize)
|
2022-06-12 23:22:29 +00:00
|
|
|
for (let i = 0; i < chunks.length; i++) {
|
2022-06-15 00:38:38 +00:00
|
|
|
log(`${i * batchSize}/${updates.length} updates written...`)
|
|
|
|
const batch = db.batch()
|
|
|
|
for (const { doc, fields } of chunks[i]) {
|
2022-06-22 20:29:40 +00:00
|
|
|
if (operationType === 'update') {
|
|
|
|
batch.update(doc, fields)
|
|
|
|
} else {
|
|
|
|
batch.set(doc, fields)
|
|
|
|
}
|
2022-06-15 00:38:38 +00:00
|
|
|
}
|
|
|
|
await batch.commit()
|
2022-06-12 23:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 05:13:38 +00:00
|
|
|
export const tryOrLogError = async <T>(task: Promise<T>) => {
|
|
|
|
try {
|
|
|
|
return await task
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 21:31:39 +00:00
|
|
|
export const isProd = () => {
|
|
|
|
return admin.instanceId().app.options.projectId === 'mantic-markets'
|
|
|
|
}
|
2022-02-23 01:35:25 +00:00
|
|
|
|
2022-05-01 16:36:54 +00:00
|
|
|
export const getDoc = async <T>(collection: string, doc: string) => {
|
2022-01-07 00:05:48 +00:00
|
|
|
const snap = await admin.firestore().collection(collection).doc(doc).get()
|
2021-12-11 00:06:17 +00:00
|
|
|
|
2022-01-07 00:05:48 +00:00
|
|
|
return snap.exists ? (snap.data() as T) : undefined
|
|
|
|
}
|
|
|
|
|
2022-05-01 16:36:54 +00:00
|
|
|
export const getValue = async <T>(ref: admin.firestore.DocumentReference) => {
|
|
|
|
const snap = await ref.get()
|
|
|
|
|
|
|
|
return snap.exists ? (snap.data() as T) : undefined
|
|
|
|
}
|
|
|
|
|
2022-01-07 00:05:48 +00:00
|
|
|
export const getValues = async <T>(query: admin.firestore.Query) => {
|
|
|
|
const snap = await query.get()
|
|
|
|
return snap.docs.map((doc) => doc.data() as T)
|
2021-12-11 00:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getContract = (contractId: string) => {
|
2022-05-01 16:36:54 +00:00
|
|
|
return getDoc<Contract>('contracts', contractId)
|
2021-12-11 00:06:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 23:23:13 +00:00
|
|
|
export const getGroup = (groupId: string) => {
|
|
|
|
return getDoc<Group>('groups', groupId)
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:06:17 +00:00
|
|
|
export const getPost = (postId: string) => {
|
|
|
|
return getDoc<Post>('posts', postId)
|
|
|
|
}
|
|
|
|
|
2021-12-11 00:06:17 +00:00
|
|
|
export const getUser = (userId: string) => {
|
2022-05-01 16:36:54 +00:00
|
|
|
return getDoc<User>('users', userId)
|
2022-01-07 00:05:48 +00:00
|
|
|
}
|
2022-01-10 22:48:48 +00:00
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
export const getPrivateUser = (userId: string) => {
|
2022-05-01 16:36:54 +00:00
|
|
|
return getDoc<PrivateUser>('private-users', userId)
|
2022-01-19 03:36:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 17:43:57 +00:00
|
|
|
export const getAllPrivateUsers = async () => {
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
const users = await firestore.collection('private-users').get()
|
|
|
|
return users.docs.map((doc) => doc.data() as PrivateUser)
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:18:38 +00:00
|
|
|
export const getUserByUsername = async (username: string) => {
|
2022-06-18 21:31:39 +00:00
|
|
|
const firestore = admin.firestore()
|
2022-01-18 07:18:38 +00:00
|
|
|
const snap = await firestore
|
|
|
|
.collection('users')
|
|
|
|
.where('username', '==', username)
|
|
|
|
.get()
|
|
|
|
|
|
|
|
return snap.empty ? undefined : (snap.docs[0].data() as User)
|
|
|
|
}
|
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
const updateUserBalance = (
|
|
|
|
userId: string,
|
|
|
|
delta: number,
|
|
|
|
isDeposit = false
|
|
|
|
) => {
|
2022-06-18 21:31:39 +00:00
|
|
|
const firestore = admin.firestore()
|
2022-01-10 22:48:48 +00:00
|
|
|
return firestore.runTransaction(async (transaction) => {
|
|
|
|
const userDoc = firestore.doc(`users/${userId}`)
|
|
|
|
const userSnap = await transaction.get(userDoc)
|
|
|
|
if (!userSnap.exists) return
|
|
|
|
const user = userSnap.data() as User
|
|
|
|
|
|
|
|
const newUserBalance = user.balance + delta
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
// if (newUserBalance < 0)
|
|
|
|
// throw new Error(
|
|
|
|
// `User (${userId}) balance cannot be negative: ${newUserBalance}`
|
|
|
|
// )
|
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
if (isDeposit) {
|
|
|
|
const newTotalDeposits = (user.totalDeposits || 0) + delta
|
|
|
|
transaction.update(userDoc, { totalDeposits: newTotalDeposits })
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:48:48 +00:00
|
|
|
transaction.update(userDoc, { balance: newUserBalance })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
export const payUser = (userId: string, payout: number, isDeposit = false) => {
|
2022-03-06 09:03:06 +00:00
|
|
|
if (!isFinite(payout)) throw new Error('Payout is not finite: ' + payout)
|
2022-01-10 22:48:48 +00:00
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
return updateUserBalance(userId, payout, isDeposit)
|
2022-01-10 22:48:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
export const chargeUser = (
|
|
|
|
userId: string,
|
|
|
|
charge: number,
|
|
|
|
isAnte?: boolean
|
|
|
|
) => {
|
2022-01-10 22:48:48 +00:00
|
|
|
if (!isFinite(charge) || charge <= 0)
|
|
|
|
throw new Error('User charge is not positive: ' + charge)
|
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
return updateUserBalance(userId, -charge, isAnte)
|
2022-01-10 22:48:48 +00:00
|
|
|
}
|