mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-17 00:12:11 +08:00
add cannot modify transaction type error
verify old transaction source account and destination account if changed
This commit is contained in:
@@ -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")
|
||||
)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user