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