@@ -20,13 +20,6 @@ import (
2020 "fmt"
2121 "time"
2222
23- "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/projectversion"
24-
25- "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/project"
26-
27- "entgo.io/ent/dialect/sql/sqljson"
28-
29- "entgo.io/ent/dialect/sql"
3023 "github.com/chainloop-dev/chainloop/app/controlplane/pkg/authz"
3124 "github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz"
3225 "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent"
@@ -36,10 +29,14 @@ import (
3629 "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/organization"
3730 "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/orginvitation"
3831 "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/predicate"
32+ "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/project"
33+ "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/projectversion"
3934 "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/user"
4035 "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/workflow"
4136 "github.com/chainloop-dev/chainloop/app/controlplane/pkg/pagination"
4237
38+ "entgo.io/ent/dialect/sql"
39+ "entgo.io/ent/dialect/sql/sqljson"
4340 "github.com/go-kratos/kratos/v2/log"
4441 "github.com/google/uuid"
4542)
@@ -231,13 +228,9 @@ func (g GroupRepo) Create(ctx context.Context, orgID uuid.UUID, opts *biz.Create
231228 SetDescription (opts .Description ).
232229 SetOrganizationID (orgID )
233230
234- // Only set the member count and add member if userID is provided
231+ // Add member if userID is provided
235232 if opts .UserID != nil {
236- builder = builder .
237- AddMemberIDs (* opts .UserID ).
238- SetMemberCount (1 )
239- } else {
240- builder = builder .SetMemberCount (0 )
233+ builder = builder .AddMemberIDs (* opts .UserID )
241234 }
242235
243236 gr , err := builder .Save (ctx )
@@ -287,6 +280,11 @@ func (g GroupRepo) Create(ctx context.Context, orgID uuid.UUID, opts *biz.Create
287280 return nil , fmt .Errorf ("failed to create group: %w" , err )
288281 }
289282
283+ // Update the member count based on actual query
284+ if err := g .UpdateGroupMemberCount (ctx , entGroup .ID ); err != nil {
285+ g .log .Warnf ("failed to update member count for newly created group %s: %v" , entGroup .ID , err )
286+ }
287+
290288 return g .FindByOrgAndID (ctx , orgID , entGroup .ID )
291289}
292290
@@ -474,16 +472,16 @@ func (g GroupRepo) AddMemberToGroup(ctx context.Context, orgID uuid.UUID, groupI
474472 }
475473 }
476474
477- // Increment the member count of the group
478- if err := tx .Group .UpdateOneID (groupID ).AddMemberCount (1 ).Exec (ctx ); err != nil {
479- return fmt .Errorf ("failed to increment group member count: %w" , err )
480- }
481-
482475 return nil
483476 }); err != nil {
484477 return nil , fmt .Errorf ("failed to add member to group: %w" , err )
485478 }
486479
480+ // Update the member count based on actual query after transaction
481+ if err := g .UpdateGroupMemberCount (ctx , groupID ); err != nil {
482+ g .log .Warnf ("failed to update member count after adding member to group %s: %v" , groupID , err )
483+ }
484+
487485 // Return the newly created membership
488486 return g .FindGroupMembershipByGroupAndID (ctx , groupID , userID )
489487}
@@ -527,18 +525,18 @@ func (g GroupRepo) RemoveMemberFromGroup(ctx context.Context, orgID uuid.UUID, g
527525 }
528526 }
529527
530- // Decrement the member count of the group
531- if err := tx .Group .UpdateOneID (groupID ).AddMemberCount (- 1 ).Exec (ctx ); err != nil {
532- return fmt .Errorf ("failed to increment group member count: %w" , err )
533- }
534-
535528 return nil
536529 })
537530
538531 if err != nil {
539532 return err
540533 }
541534
535+ // Update the member count based on actual query after transaction
536+ if err := g .UpdateGroupMemberCount (ctx , groupID ); err != nil {
537+ g .log .Warnf ("failed to update member count after removing member from group %s: %v" , groupID , err )
538+ }
539+
542540 return nil
543541}
544542
@@ -758,3 +756,27 @@ func entGroupMembershipToBiz(gu *ent.GroupMembership) *biz.GroupMembership {
758756 DeletedAt : toTimePtr (gu .DeletedAt ),
759757 }
760758}
759+
760+ // UpdateGroupMemberCount updates the member count of a group based on an actual count query
761+ // This should be called after membership changes have been committed.
762+ func (g GroupRepo ) UpdateGroupMemberCount (ctx context.Context , groupID uuid.UUID ) error {
763+ // Count active members in the group
764+ count , err := g .data .DB .GroupMembership .Query ().
765+ Where (
766+ groupmembership .GroupIDEQ (groupID ),
767+ groupmembership .DeletedAtIsNil (),
768+ ).
769+ Count (ctx )
770+ if err != nil {
771+ return fmt .Errorf ("failed to count group members: %w" , err )
772+ }
773+
774+ // Update the group's member count to the actual count
775+ if _ , err := g .data .DB .Group .UpdateOneID (groupID ).
776+ SetMemberCount (count ).
777+ Save (ctx ); err != nil {
778+ return fmt .Errorf ("failed to update group member count: %w" , err )
779+ }
780+
781+ return nil
782+ }
0 commit comments