From 8db69f64c85648ba699a1f29927e0106643d9ad9 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sat, 17 Jan 2026 23:13:16 +0800 Subject: [PATCH] place the transaction category at the end of the primary category after changing primary category --- pkg/api/transaction_categories.go | 11 ++++++++++- pkg/services/transaction_categories.go | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/api/transaction_categories.go b/pkg/api/transaction_categories.go index c379e842..95cea8fb 100644 --- a/pkg/api/transaction_categories.go +++ b/pkg/api/transaction_categories.go @@ -214,6 +214,7 @@ func (a *TransactionCategoriesApi) CategoryModifyHandler(c *core.WebContext) (an Uid: uid, ParentCategoryId: categoryModifyReq.ParentId, Name: categoryModifyReq.Name, + DisplayOrder: category.DisplayOrder, Icon: categoryModifyReq.Icon, Color: categoryModifyReq.Color, Comment: categoryModifyReq.Comment, @@ -259,6 +260,15 @@ func (a *TransactionCategoriesApi) CategoryModifyHandler(c *core.WebContext) (an if toPrimaryCategory.ParentCategoryId != models.LevelOneTransactionCategoryParentId { return nil, errs.Or(err, errs.ErrNotAllowUseSecondaryTransactionAsPrimaryCategory) } + + maxOrderId, err := a.categories.GetMaxSubCategoryDisplayOrder(c, uid, category.Type, newCategory.ParentCategoryId) + + if err != nil { + log.Errorf(c, "[transaction_categories.CategoryModifyHandler] failed to get max display order for user \"uid:%d\", because %s", uid, err.Error()) + return nil, errs.Or(err, errs.ErrOperationFailed) + } + + newCategory.DisplayOrder = maxOrderId + 1 } err = a.categories.ModifyCategory(c, newCategory) @@ -271,7 +281,6 @@ func (a *TransactionCategoriesApi) CategoryModifyHandler(c *core.WebContext) (an log.Infof(c, "[transaction_categories.CategoryModifyHandler] user \"uid:%d\" has updated category \"id:%d\" successfully", uid, categoryModifyReq.Id) newCategory.Type = category.Type - newCategory.DisplayOrder = category.DisplayOrder categoryResp := newCategory.ToTransactionCategoryInfoResponse() return categoryResp, nil diff --git a/pkg/services/transaction_categories.go b/pkg/services/transaction_categories.go index 8b23d42d..1898130e 100644 --- a/pkg/services/transaction_categories.go +++ b/pkg/services/transaction_categories.go @@ -296,7 +296,7 @@ func (s *TransactionCategoryService) ModifyCategory(c core.Context, category *mo category.UpdatedUnixTime = time.Now().Unix() return s.UserDataDB(category.Uid).DoTransaction(c, func(sess *xorm.Session) error { - updatedRows, err := sess.ID(category.CategoryId).Cols("parent_category_id", "name", "icon", "color", "comment", "hidden", "updated_unix_time").Where("uid=? AND deleted=?", category.Uid, false).Update(category) + updatedRows, err := sess.ID(category.CategoryId).Cols("parent_category_id", "name", "display_order", "icon", "color", "comment", "hidden", "updated_unix_time").Where("uid=? AND deleted=?", category.Uid, false).Update(category) if err != nil { return err