From 4d5f1b7d1f97578614172d3f1745093a5f8e4081 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Tue, 26 Apr 2022 11:49:16 -0400 Subject: [PATCH] Clean up txn types --- common/txn.ts | 14 +++----------- functions/src/transact.ts | 19 ++++--------------- web/lib/firebase/api-call.ts | 6 +++++- web/pages/charity/[charitySlug].tsx | 5 ++--- 4 files changed, 14 insertions(+), 30 deletions(-) diff --git a/common/txn.ts b/common/txn.ts index f8b54e15..43e6287b 100644 --- a/common/txn.ts +++ b/common/txn.ts @@ -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' diff --git a/functions/src/transact.ts b/functions/src/transact.ts index 345c8b56..31d67b07 100644 --- a/functions/src/transact.ts +++ b/functions/src/transact.ts @@ -7,20 +7,11 @@ import { removeUndefinedProps } from '../../common/util/object' export const transact = functions .runWith({ minInstances: 1 }) - .https.onCall(async (data: Exclude, context) => { + .https.onCall(async (data: Omit, 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 } }) }) diff --git a/web/lib/firebase/api-call.ts b/web/lib/firebase/api-call.ts index a40c6b16..a71c2752 100644 --- a/web/lib/firebase/api-call.ts +++ b/web/lib/firebase/api-call.ts @@ -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, + { status: 'error' | 'success'; message?: string; txn?: Txn } +>('transact') export const placeBet = cloudFunction('placeBet') diff --git a/web/pages/charity/[charitySlug].tsx b/web/pages/charity/[charitySlug].tsx index ad13601e..739e512d 100644 --- a/web/pages/charity/[charitySlug].tsx +++ b/web/pages/charity/[charitySlug].tsx @@ -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) }