From 05f11a54a2092d4f853ea0e7b58550d5ef795fd5 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Tue, 8 Dec 2020 01:59:37 +0800 Subject: [PATCH] don't allow adding balance modification transaction when account already has other transaction --- pkg/errs/transaction.go | 1 + pkg/services/transactions.go | 10 +++++++++- src/locales/en.js | 1 + src/locales/zh_Hans.js | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/errs/transaction.go b/pkg/errs/transaction.go index b3ff4a62..4fa7d92a 100644 --- a/pkg/errs/transaction.go +++ b/pkg/errs/transaction.go @@ -12,4 +12,5 @@ var ( ErrTooMuchTransactionInOneSecond = NewNormalError(NORMAL_SUBCATEGORY_TRANSACTION, 6, http.StatusBadRequest, "too much transaction in one second") ErrBalanceModificationTransactionCannotSetCategory = NewNormalError(NORMAL_SUBCATEGORY_TRANSACTION, 7, http.StatusBadRequest, "balance modification transaction cannot set category") ErrBalanceModificationTransactionCannotChangeAccountId = NewNormalError(NORMAL_SUBCATEGORY_TRANSACTION, 8, http.StatusBadRequest, "balance modification transaction cannot change account id") + ErrBalanceModificationTransactionCannotAddWhenNotEmpty = NewNormalError(NORMAL_SUBCATEGORY_TRANSACTION, 9, http.StatusBadRequest, "balance modification transaction cannot add when other transaction exists") ) diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index bb609a21..f7ee693e 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -200,8 +200,16 @@ func (s *TransactionService) CreateTransaction(transaction *models.Transaction) return errs.ErrTransactionCategoryTypeInvalid } - // Calculate balance modification transaction real amount + // Verify balance modification transaction and calculate real amount if transaction.Type == models.TRANSACTION_TYPE_MODIFY_BALANCE { + otherTransactionExists, err := sess.Where("uid=? AND deleted=? AND destination_account_id=?", transaction.Uid, false, destinationAccount.AccountId).Limit(1).Exist(&models.Transaction{}) + + if err != nil { + return err + } else if otherTransactionExists { + return errs.ErrBalanceModificationTransactionCannotAddWhenNotEmpty + } + transaction.DestinationAmount = transaction.SourceAmount - destinationAccount.Balance } diff --git a/src/locales/en.js b/src/locales/en.js index a906d61c..0e99f15a 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -314,6 +314,7 @@ export default { 'too much transaction in one second': 'There are too much transaction in one second, please choose another time', 'balance modification transaction cannot set category': 'You cannot set category for balance modification transaction', 'balance modification transaction cannot change account id': 'You cannot change account ID for balance modification transaction', + 'balance modification transaction cannot add when other transaction exists': 'You cannot add balance modification transaction when other transaction already exists in this account', 'transaction category id is invalid': 'Transaction category ID is invalid', 'transaction category not found': 'Transaction category is not found', 'transaction category type is invalid': 'Transaction category type is invalid', diff --git a/src/locales/zh_Hans.js b/src/locales/zh_Hans.js index b8a95627..90b5d915 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -314,6 +314,7 @@ export default { 'too much transaction in one second': '一秒钟内交易太多,请选择其他时间', 'balance modification transaction cannot set category': '您无法对修改余额的交易设置分类', 'balance modification transaction cannot change account id': '您无法对修改余额的交易修改账户ID', + 'balance modification transaction cannot add when other transaction exists': '您不能在该账户已经存在其他交易时添加余额修改交易', 'transaction category id is invalid': '交易分类ID无效', 'transaction category not found': '交易分类不存在', 'transaction category type is invalid': '交易分类类型无效',