2021-12-11 00:06:17 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
|
2022-01-10 21:07:57 +00:00
|
|
|
import { Contract } from '../../common/contract'
|
2022-01-19 03:36:46 +00:00
|
|
|
import { PrivateUser, User } from '../../common/user'
|
2021-12-11 00:06:17 +00:00
|
|
|
|
|
|
|
export const getValue = 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
return getValue<Contract>('contracts', contractId)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getUser = (userId: string) => {
|
|
|
|
return getValue<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) => {
|
|
|
|
return getValue<PrivateUser>('private-users', userId)
|
|
|
|
}
|
|
|
|
|
2022-01-18 07:18:38 +00:00
|
|
|
export const getUserByUsername = async (username: string) => {
|
|
|
|
const snap = await firestore
|
|
|
|
.collection('users')
|
|
|
|
.where('username', '==', username)
|
|
|
|
.get()
|
|
|
|
|
|
|
|
return snap.empty ? undefined : (snap.docs[0].data() as User)
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:48:48 +00:00
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
const updateUserBalance = (
|
|
|
|
userId: string,
|
|
|
|
delta: number,
|
|
|
|
isDeposit = false
|
|
|
|
) => {
|
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
|
|
|
|
|
|
|
|
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-01-10 22:48:48 +00:00
|
|
|
if (!isFinite(payout) || payout <= 0)
|
|
|
|
throw new Error('Payout is not positive: ' + payout)
|
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
return updateUserBalance(userId, payout, isDeposit)
|
2022-01-10 22:48:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const chargeUser = (userId: string, charge: number) => {
|
|
|
|
if (!isFinite(charge) || charge <= 0)
|
|
|
|
throw new Error('User charge is not positive: ' + charge)
|
|
|
|
|
|
|
|
return updateUserBalance(userId, -charge)
|
|
|
|
}
|