add cannot modify transaction type error

verify old transaction source account and destination account if changed
This commit is contained in:
MaysWind
2020-12-27 20:06:52 +08:00
parent 6cbf33920b
commit 6ccb4a0499
4 changed files with 37 additions and 0 deletions
+34
View File
@@ -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 {