mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-14 06:57:35 +08:00
not allow to delete account when other transaction is using it
This commit is contained in:
+1
-1
@@ -329,7 +329,7 @@ func (a *AccountsApi) AccountDeleteHandler(c *core.Context) (interface{}, *errs.
|
||||
}
|
||||
|
||||
uid := c.GetCurrentUid()
|
||||
err = a.accounts.DeleteAccounts(uid, []int64{accountDeleteReq.Id})
|
||||
err = a.accounts.DeleteAccount(uid, accountDeleteReq.Id)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[accounts.AccountDeleteHandler] failed to delete account \"id:%d\" for user \"uid:%d\", because %s", accountDeleteReq.Id, uid, err.Error())
|
||||
|
||||
@@ -15,4 +15,5 @@ var (
|
||||
ErrCannotAddOrDeleteSubAccountsWhenModify = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 9, http.StatusBadRequest, "cannot add or delete sub accounts when modify account")
|
||||
ErrSourceAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 10, http.StatusBadRequest, "source account not found")
|
||||
ErrDestinationAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 11, http.StatusBadRequest, "destination account not found")
|
||||
ErrAccountInUseCannotBeDeleted = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 12, http.StatusBadRequest, "account is in use and cannot be deleted")
|
||||
)
|
||||
|
||||
@@ -248,7 +248,7 @@ func (s *AccountService) ModifyAccountDisplayOrders(uid int64, accounts []*model
|
||||
})
|
||||
}
|
||||
|
||||
func (s *AccountService) DeleteAccounts(uid int64, ids []int64) error {
|
||||
func (s *AccountService) DeleteAccount(uid int64, accountId int64) error {
|
||||
if uid <= 0 {
|
||||
return errs.ErrUserIdInvalid
|
||||
}
|
||||
@@ -261,7 +261,34 @@ func (s *AccountService) DeleteAccounts(uid int64, ids []int64) error {
|
||||
}
|
||||
|
||||
return s.UserDataDB(uid).DoTransaction(func(sess *xorm.Session) error {
|
||||
deletedRows, err := sess.Cols("deleted", "deleted_unix_time").Where("uid=? AND deleted=?", uid, false).In("account_id", ids).Update(updateModel)
|
||||
var accountAndSubAccounts []*models.Account
|
||||
err := s.UserDataDB(uid).Where("uid=? AND deleted=? AND (account_id=? OR parent_account_id=?)", uid, false, accountId, accountId).Find(&accountAndSubAccounts)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
} else if len(accountAndSubAccounts) < 1 {
|
||||
return errs.ErrAccountNotFound
|
||||
}
|
||||
|
||||
accountAndSubAccountIds := make([]int64, len(accountAndSubAccounts))
|
||||
|
||||
for i := 0; i < len(accountAndSubAccounts); i++ {
|
||||
accountAndSubAccountIds[i] = accountAndSubAccounts[i].AccountId
|
||||
}
|
||||
|
||||
exists, err := sess.Cols("uid", "deleted", "source_account_id").Where("uid=? AND deleted=?", uid, false).In("source_account_id", accountAndSubAccountIds).Limit(1).Exist(&models.Transaction{})
|
||||
|
||||
if exists {
|
||||
return errs.ErrAccountInUseCannotBeDeleted
|
||||
}
|
||||
|
||||
exists, err = sess.Cols("uid", "deleted", "destination_account_id").Where("uid=? AND deleted=?", uid, false).In("destination_account_id", accountAndSubAccountIds).Limit(1).Exist(&models.Transaction{})
|
||||
|
||||
if exists {
|
||||
return errs.ErrAccountInUseCannotBeDeleted
|
||||
}
|
||||
|
||||
deletedRows, err := sess.Cols("deleted", "deleted_unix_time").Where("uid=? AND deleted=?", uid, false).In("account_id", accountAndSubAccountIds).Update(updateModel)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -269,8 +296,6 @@ func (s *AccountService) DeleteAccounts(uid int64, ids []int64) error {
|
||||
return errs.ErrAccountNotFound
|
||||
}
|
||||
|
||||
_, err = sess.Cols("deleted", "deleted_unix_time").Where("uid=? AND deleted=?", uid, false).In("parent_account_id", ids).Update(updateModel)
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
@@ -306,6 +306,7 @@ export default {
|
||||
'cannot add or delete sub accounts when modify account': 'You cannot add or delete sub accounts when modify account',
|
||||
'source account not found': 'Source account is not found',
|
||||
'destination account not found': 'Destination account is not found',
|
||||
'account is in use and cannot be deleted': 'Account is in use and it cannot be deleted',
|
||||
'transaction id is invalid': 'Transaction ID is invalid',
|
||||
'transaction not found': 'Transaction is not found',
|
||||
'transaction type is invalid': 'Transaction type is invalid',
|
||||
|
||||
@@ -306,6 +306,7 @@ export default {
|
||||
'cannot add or delete sub accounts when modify account': '您不能在修改账户时添加或删除子账户',
|
||||
'source account not found': '来源账户不存在',
|
||||
'destination account not found': '目标账户不存在',
|
||||
'account is in use and cannot be deleted': '账户正在被使用,无法删除',
|
||||
'transaction id is invalid': '交易ID无效',
|
||||
'transaction not found': '交易不存',
|
||||
'transaction type is invalid': '交易类型无效',
|
||||
|
||||
Reference in New Issue
Block a user