Clean up txn types
This commit is contained in:
parent
902f165aec
commit
4d5f1b7d1f
|
@ -5,23 +5,15 @@ export type Txn = {
|
|||
createdTime: number
|
||||
|
||||
fromId: string
|
||||
fromType: 'user' | 'contract' | 'bank_of_manifold'
|
||||
fromType: source_type
|
||||
|
||||
toId: string
|
||||
toType: 'user' | 'contract' | 'charity' | 'bank_of_manifold'
|
||||
toType: source_type
|
||||
|
||||
amount: number
|
||||
|
||||
category: TxnCategory
|
||||
// Human-readable description
|
||||
description?: string
|
||||
// Structured metadata for different kinds of txns
|
||||
data?: TxnData
|
||||
}
|
||||
|
||||
export type TxnCategory = 'TO_CHARITY' // | 'TIP' | 'BET' | ...
|
||||
export type TxnData = CharityData // | TipData | BetData | ...
|
||||
|
||||
export type CharityData = {
|
||||
charityId: string
|
||||
}
|
||||
export type source_type = 'user' | 'contract' | 'charity' | 'bank'
|
||||
|
|
|
@ -7,20 +7,11 @@ import { removeUndefinedProps } from '../../common/util/object'
|
|||
|
||||
export const transact = functions
|
||||
.runWith({ minInstances: 1 })
|
||||
.https.onCall(async (data: Exclude<Txn, 'id' | 'createdTime'>, context) => {
|
||||
.https.onCall(async (data: Omit<Txn, 'id' | 'createdTime'>, context) => {
|
||||
const userId = context?.auth?.uid
|
||||
if (!userId) return { status: 'error', message: 'Not authorized' }
|
||||
|
||||
const {
|
||||
amount,
|
||||
fromType,
|
||||
fromId,
|
||||
toId,
|
||||
toType,
|
||||
category,
|
||||
description,
|
||||
data: txnData,
|
||||
} = data
|
||||
const { amount, fromType, fromId, toId, toType, description } = data
|
||||
|
||||
if (fromType !== 'user')
|
||||
return {
|
||||
|
@ -68,22 +59,20 @@ export const transact = functions
|
|||
const txn: Txn = removeUndefinedProps({
|
||||
id: newTxnDoc.id,
|
||||
createdTime: Date.now(),
|
||||
|
||||
fromId,
|
||||
fromType,
|
||||
toId,
|
||||
toType,
|
||||
|
||||
amount,
|
||||
|
||||
category,
|
||||
description,
|
||||
data: txnData,
|
||||
})
|
||||
|
||||
transaction.create(newTxnDoc, txn)
|
||||
transaction.update(fromDoc, { balance: fromUser.balance - amount })
|
||||
|
||||
return { status: 'success', txnId: newTxnDoc.id }
|
||||
return { status: 'success', txn }
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { httpsCallable } from 'firebase/functions'
|
||||
import { Fold } from '../../../common/fold'
|
||||
import { Txn } from '../../../common/txn'
|
||||
import { User } from '../../../common/user'
|
||||
import { randomString } from '../../../common/util/random'
|
||||
import './init'
|
||||
|
@ -15,7 +16,10 @@ export const createFold = cloudFunction<
|
|||
{ status: 'error' | 'success'; message?: string; fold?: Fold }
|
||||
>('createFold')
|
||||
|
||||
export const transact = cloudFunction('transact')
|
||||
export const transact = cloudFunction<
|
||||
Omit<Txn, 'id' | 'createdTime'>,
|
||||
{ status: 'error' | 'success'; message?: string; txn?: Txn }
|
||||
>('transact')
|
||||
|
||||
export const placeBet = cloudFunction('placeBet')
|
||||
|
||||
|
|
|
@ -143,7 +143,7 @@ function DonationBox(props: { user?: User | null; charity: Charity }) {
|
|||
const donateDisabled = isSubmitting || !amount || error
|
||||
|
||||
const onSubmit: React.FormEventHandler = async (e) => {
|
||||
if (!user) return
|
||||
if (!user || donateDisabled) return
|
||||
|
||||
e.preventDefault()
|
||||
setIsSubmitting(true)
|
||||
|
@ -154,9 +154,8 @@ function DonationBox(props: { user?: User | null; charity: Charity }) {
|
|||
fromType: 'user',
|
||||
toId: charity.id,
|
||||
toType: 'charity',
|
||||
category: 'TO_CHARITY',
|
||||
description: `${user.name} donated M$ ${amount} to ${charity.name}`,
|
||||
})
|
||||
}).catch((err) => console.log('Error', err))
|
||||
setIsSubmitting(false)
|
||||
setAmount(undefined)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user