From 7e999881c5f88f8aae138c3eb596b051374b4245 Mon Sep 17 00:00:00 2001 From: Sinclair Chen Date: Tue, 21 Jun 2022 09:52:02 -0700 Subject: [PATCH] Check toUser balance sufficient in negative txns (#555) --- functions/src/transact.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/functions/src/transact.ts b/functions/src/transact.ts index 0c9bd696..1ff53629 100644 --- a/functions/src/transact.ts +++ b/functions/src/transact.ts @@ -47,7 +47,7 @@ export const transact = functions } const fromUser = fromSnap.data() as User - if (fromUser.balance < amount) { + if (amount > 0 && fromUser.balance < amount) { return { status: 'error', message: `Insufficient balance: ${fromUser.username} needed ${amount} but only had ${fromUser.balance} `, @@ -61,6 +61,15 @@ export const transact = functions return { status: 'error', message: 'User not found' } } const toUser = toSnap.data() as User + if (amount < 0 && toUser.balance < -amount) { + return { + status: 'error', + message: `Insufficient balance: ${ + toUser.username + } needed ${-amount} but only had ${toUser.balance} `, + } + } + transaction.update(toDoc, { balance: toUser.balance + amount, totalDeposits: toUser.totalDeposits + amount,