diff --git a/pkg/errs/transaction.go b/pkg/errs/transaction.go index 8592efe7..765f2dc1 100644 --- a/pkg/errs/transaction.go +++ b/pkg/errs/transaction.go @@ -17,4 +17,5 @@ var ( ErrCannotAddTransactionToHiddenAccount = NewNormalError(NormalSubcategoryTransaction, 10, http.StatusBadRequest, "cannot add transaction to hidden account") ErrCannotModifyTransactionInHiddenAccount = NewNormalError(NormalSubcategoryTransaction, 11, http.StatusBadRequest, "cannot modify transaction of hidden account") ErrCannotDeleteTransactionInHiddenAccount = NewNormalError(NormalSubcategoryTransaction, 12, http.StatusBadRequest, "cannot delete transaction in hidden account") + ErrCannotModifyTransactionType = NewNormalError(NormalSubcategoryTransaction, 13, http.StatusBadRequest, "cannot modify transaction type") ) diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index ce9a75f4..f60c0e4c 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -394,6 +394,10 @@ func (s *TransactionService) ModifyTransaction(transaction *models.Transaction, return errs.ErrTransactionNotFound } + if transaction.Type != oldTransaction.Type { + return errs.ErrCannotModifyTransactionType + } + if oldTransaction.Type == models.TRANSACTION_TYPE_MODIFY_BALANCE || oldTransaction.Type == models.TRANSACTION_TYPE_INCOME || oldTransaction.Type == models.TRANSACTION_TYPE_EXPENSE { @@ -413,6 +417,8 @@ func (s *TransactionService) ModifyTransaction(transaction *models.Transaction, // Get and verify source and destination account (if necessary) sourceAccount := &models.Account{} destinationAccount := &models.Account{} + oldSourceAccount := &models.Account{} + oldDestinationAccount := &models.Account{} has, err = sess.ID(transaction.SourceAccountId).Where("uid=? AND deleted=?", transaction.Uid, false).Get(sourceAccount) if err != nil { @@ -437,6 +443,34 @@ func (s *TransactionService) ModifyTransaction(transaction *models.Transaction, } } + if transaction.SourceAccountId == oldTransaction.SourceAccountId { + oldSourceAccount = sourceAccount + } else { + has, err = sess.ID(oldTransaction.SourceAccountId).Where("uid=? AND deleted=?", transaction.Uid, false).Get(oldSourceAccount) + + if err != nil { + return err + } else if !has { + return errs.ErrSourceAccountNotFound + } else if oldSourceAccount.Hidden { + return errs.ErrCannotModifyTransactionInHiddenAccount + } + } + + if transaction.DestinationAccountId == oldTransaction.DestinationAccountId { + oldDestinationAccount = destinationAccount + } else { + has, err = sess.ID(oldTransaction.DestinationAccountId).Where("uid=? AND deleted=?", transaction.Uid, false).Get(oldDestinationAccount) + + if err != nil { + return err + } else if !has { + return errs.ErrDestinationAccountNotFound + } else if oldDestinationAccount.Hidden { + return errs.ErrCannotModifyTransactionInHiddenAccount + } + } + // Append modified columns and verify if transaction.CategoryId != oldTransaction.CategoryId { if oldTransaction.Type == models.TRANSACTION_TYPE_MODIFY_BALANCE { diff --git a/src/locales/en.js b/src/locales/en.js index b3e6d4ad..b1d50791 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -320,6 +320,7 @@ export default { 'cannot add transaction to hidden account': 'You cannot add transaction to an hidden account', 'cannot modify transaction of hidden account': 'You cannot modify transaction of an hidden account', 'cannot delete transaction in hidden account': 'You cannot delete transaction in an hidden account', + 'cannot modify transaction type': 'You cannot modify transaction type', '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 7b6fcc1d..44d935a0 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -320,6 +320,7 @@ export default { 'cannot add transaction to hidden account': '您不能在隐藏账户中添加交易', 'cannot modify transaction of hidden account': '您不能修改隐藏账户中的交易', 'cannot delete transaction in hidden account': '您不能删除隐藏账户中的交易', + 'cannot modify transaction type': '您不能修改交易类型', 'transaction category id is invalid': '交易分类ID无效', 'transaction category not found': '交易分类不存在', 'transaction category type is invalid': '交易分类类型无效',