From 92df65f9c5f00d67fb411d85268e57b93dcfffca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Fri, 8 May 2026 18:34:24 -0400 Subject: [PATCH] fix: ensure group service populates the sort order of newly created groups --- services/group/group.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/services/group/group.go b/services/group/group.go index ad48baaeb7..7852bcfd9c 100644 --- a/services/group/group.go +++ b/services/group/group.go @@ -27,13 +27,14 @@ func NewGroup(ctx context.Context, g *group_model.Group) error { if len(g.Name) == 0 { return util.NewInvalidArgumentErrorf("empty group name") } - has, err := db.ExistByID[user_model.User](ctx, g.OwnerID) + owner, has, err := db.GetByID[user_model.User](ctx, g.OwnerID) if err != nil { return err } if !has { return organization.ErrOrgNotExist{ID: g.OwnerID} } + g.OwnerName = owner.Name g.LowerName = strings.ToLower(g.Name) ctx, committer, err := db.TxContext(ctx) if err != nil { @@ -41,6 +42,35 @@ func NewGroup(ctx context.Context, g *group_model.Group) error { } defer committer.Close() + if g.ParentGroupID > 0 { + ngrp, err := group_model.GetGroupByID(ctx, g.ParentGroupID) + if err != nil { + return err + } + if err = ngrp.LoadSubgroups(ctx, false); err != nil { + return err + } + g.SortOrder = len(ngrp.Subgroups) + gidChain, err := group_model.GetParentGroupIDChain(ctx, g.ParentGroupID) + if err != nil { + return err + } + if len(gidChain) >= 20 { + return group_model.ErrGroupTooDeep{ + ID: g.ParentGroupID, + } + } + } else { + siblings, err := group_model.FindGroups(ctx, &group_model.FindGroupsOptions{ + ParentGroupID: 0, + OwnerID: g.OwnerID, + }) + if err != nil { + return err + } + g.SortOrder = len(siblings) + } + if err = db.Insert(ctx, g); err != nil { return err }