From 950941f789e35949c8656d385b42f226430853b9 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 14 Dec 2020 00:32:26 +0800 Subject: [PATCH] not allow to delete account when other transaction is using it --- pkg/api/accounts.go | 2 +- pkg/errs/account.go | 1 + pkg/services/accounts.go | 33 +++++++++++++++++++++++++++++---- src/locales/en.js | 1 + src/locales/zh_Hans.js | 1 + 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/pkg/api/accounts.go b/pkg/api/accounts.go index 06cce45c..03909e5e 100644 --- a/pkg/api/accounts.go +++ b/pkg/api/accounts.go @@ -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()) diff --git a/pkg/errs/account.go b/pkg/errs/account.go index 084b66e4..bb8131a4 100644 --- a/pkg/errs/account.go +++ b/pkg/errs/account.go @@ -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") ) diff --git a/pkg/services/accounts.go b/pkg/services/accounts.go index 18f12a45..e3c933fd 100644 --- a/pkg/services/accounts.go +++ b/pkg/services/accounts.go @@ -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 }) } diff --git a/src/locales/en.js b/src/locales/en.js index 5cb4b75e..eb9f7b7e 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -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', diff --git a/src/locales/zh_Hans.js b/src/locales/zh_Hans.js index dba9d27a..0d29ac63 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -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': '交易类型无效',