Check toUser balance sufficient in negative txns (#555)

This commit is contained in:
Sinclair Chen 2022-06-21 09:52:02 -07:00 committed by GitHub
parent a8b874a32f
commit 7e999881c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,7 +47,7 @@ export const transact = functions
} }
const fromUser = fromSnap.data() as User const fromUser = fromSnap.data() as User
if (fromUser.balance < amount) { if (amount > 0 && fromUser.balance < amount) {
return { return {
status: 'error', status: 'error',
message: `Insufficient balance: ${fromUser.username} needed ${amount} but only had ${fromUser.balance} `, 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' } return { status: 'error', message: 'User not found' }
} }
const toUser = toSnap.data() as User 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, { transaction.update(toDoc, {
balance: toUser.balance + amount, balance: toUser.balance + amount,
totalDeposits: toUser.totalDeposits + amount, totalDeposits: toUser.totalDeposits + amount,