2022-04-29 23:35:56 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
|
2022-05-15 17:39:42 +00:00
|
|
|
import { User } from '../../common/user'
|
|
|
|
import { Txn } from '../../common/txn'
|
|
|
|
import { removeUndefinedProps } from '../../common/util/object'
|
2022-04-29 23:35:56 +00:00
|
|
|
|
2022-06-23 08:07:52 +00:00
|
|
|
export type TxnData = Omit<Txn, 'id' | 'createdTime'>
|
|
|
|
|
2022-04-29 23:35:56 +00:00
|
|
|
export const transact = functions
|
|
|
|
.runWith({ minInstances: 1 })
|
2022-06-23 08:07:52 +00:00
|
|
|
.https.onCall(async (data: TxnData, context) => {
|
2022-04-29 23:35:56 +00:00
|
|
|
const userId = context?.auth?.uid
|
|
|
|
if (!userId) return { status: 'error', message: 'Not authorized' }
|
|
|
|
|
2022-06-23 08:07:52 +00:00
|
|
|
const { amount, fromType, fromId } = data
|
2022-04-29 23:35:56 +00:00
|
|
|
|
|
|
|
if (fromType !== 'USER')
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: "From type is only implemented for type 'user'.",
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fromId !== userId)
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: 'Must be authenticated with userId equal to specified fromId.',
|
|
|
|
}
|
|
|
|
|
2022-06-18 03:28:16 +00:00
|
|
|
if (isNaN(amount) || !isFinite(amount))
|
2022-04-29 23:35:56 +00:00
|
|
|
return { status: 'error', message: 'Invalid amount' }
|
|
|
|
|
|
|
|
// Run as transaction to prevent race conditions.
|
|
|
|
return await firestore.runTransaction(async (transaction) => {
|
2022-06-23 08:07:52 +00:00
|
|
|
await runTxn(transaction, data)
|
|
|
|
})
|
|
|
|
})
|
2022-04-29 23:35:56 +00:00
|
|
|
|
2022-06-23 08:07:52 +00:00
|
|
|
export async function runTxn(
|
|
|
|
fbTransaction: admin.firestore.Transaction,
|
|
|
|
data: TxnData
|
|
|
|
) {
|
|
|
|
const { amount, fromId, toId, toType } = data
|
|
|
|
|
|
|
|
const fromDoc = firestore.doc(`users/${fromId}`)
|
|
|
|
const fromSnap = await fbTransaction.get(fromDoc)
|
|
|
|
if (!fromSnap.exists) {
|
|
|
|
return { status: 'error', message: 'User not found' }
|
|
|
|
}
|
|
|
|
const fromUser = fromSnap.data() as User
|
|
|
|
|
|
|
|
if (fromUser.balance < amount) {
|
|
|
|
return {
|
|
|
|
status: 'error',
|
|
|
|
message: `Insufficient balance: ${fromUser.username} needed ${amount} but only had ${fromUser.balance} `,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Track payments received by charities, bank, contracts too.
|
|
|
|
if (toType === 'USER') {
|
|
|
|
const toDoc = firestore.doc(`users/${toId}`)
|
|
|
|
const toSnap = await fbTransaction.get(toDoc)
|
|
|
|
if (!toSnap.exists) {
|
|
|
|
return { status: 'error', message: 'User not found' }
|
|
|
|
}
|
|
|
|
const toUser = toSnap.data() as User
|
|
|
|
fbTransaction.update(toDoc, {
|
|
|
|
balance: toUser.balance + amount,
|
|
|
|
totalDeposits: toUser.totalDeposits + amount,
|
2022-04-29 23:35:56 +00:00
|
|
|
})
|
2022-06-23 08:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const newTxnDoc = firestore.collection(`txns/`).doc()
|
|
|
|
const txn = { id: newTxnDoc.id, createdTime: Date.now(), ...data }
|
|
|
|
fbTransaction.create(newTxnDoc, removeUndefinedProps(txn))
|
|
|
|
fbTransaction.update(fromDoc, {
|
|
|
|
balance: fromUser.balance - amount,
|
|
|
|
totalDeposits: fromUser.totalDeposits - amount,
|
2022-04-29 23:35:56 +00:00
|
|
|
})
|
|
|
|
|
2022-06-23 08:07:52 +00:00
|
|
|
return { status: 'success', txn }
|
|
|
|
}
|
|
|
|
|
2022-04-29 23:35:56 +00:00
|
|
|
const firestore = admin.firestore()
|