Skip to content

Commit 71c219e

Browse files
authored
fix: Add subgroup notfound error (#304)
* fix: Add subgroup notfound * fix: Validation query error
1 parent 0b05074 commit 71c219e

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

internal/services/aggregate_group_service.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,14 @@ func (s *AggregateGroupService) AddSubGroups(ctx context.Context, groupID uint,
173173

174174
// Check if there are existing sub groups and get their validation endpoint
175175
var existingEndpoint string
176-
var existingSubGroup models.GroupSubGroup
177-
if err := s.db.WithContext(ctx).Where("group_id = ?", groupID).First(&existingSubGroup).Error; err == nil {
178-
// If we have existing sub groups, get one of their validation endpoints
176+
var existingSubGroups []models.GroupSubGroup
177+
if err := s.db.WithContext(ctx).Where("group_id = ?", groupID).Find(&existingSubGroups).Error; err != nil {
178+
return err
179+
}
180+
181+
if len(existingSubGroups) > 0 {
179182
var existingGroup models.Group
180-
if err := s.db.WithContext(ctx).First(&existingGroup, existingSubGroup.SubGroupID).Error; err == nil {
183+
if err := s.db.WithContext(ctx).First(&existingGroup, existingSubGroups[0].SubGroupID).Error; err == nil {
181184
existingEndpoint = utils.GetValidationEndpoint(&existingGroup)
182185
}
183186
}
@@ -188,12 +191,6 @@ func (s *AggregateGroupService) AddSubGroups(ctx context.Context, groupID uint,
188191
return err
189192
}
190193

191-
// Manually query existing sub groups
192-
var existingSubGroups []models.GroupSubGroup
193-
if err := s.db.WithContext(ctx).Where("group_id = ?", groupID).Find(&existingSubGroups).Error; err != nil {
194-
return err
195-
}
196-
197194
// Check for duplicates with existing sub groups
198195
existingSubGroupIDs := make(map[uint]bool)
199196
for _, sg := range existingSubGroups {
@@ -208,21 +205,27 @@ func (s *AggregateGroupService) AddSubGroups(ctx context.Context, groupID uint,
208205
}
209206

210207
// Add new sub groups
211-
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
208+
err = s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
212209
for _, newSg := range result.SubGroups {
213210
newSg.GroupID = groupID
214211
if err := tx.Create(&newSg).Error; err != nil {
215212
return app_errors.ParseDBError(err)
216213
}
217214
}
218215

219-
// 触发缓存更新
220-
if err := s.groupManager.Invalidate(); err != nil {
221-
logrus.WithContext(ctx).WithError(err).Error("failed to invalidate group cache after adding sub groups")
222-
}
223-
224216
return nil
225217
})
218+
219+
if err != nil {
220+
return err
221+
}
222+
223+
// 触发缓存更新
224+
if err := s.groupManager.Invalidate(); err != nil {
225+
logrus.WithContext(ctx).WithError(err).Error("failed to invalidate group cache after adding sub groups")
226+
}
227+
228+
return nil
226229
}
227230

228231
// UpdateSubGroupWeight updates the weight of a specific sub group

internal/services/subgroup_manager.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,21 @@ func (m *SubGroupManager) SelectSubGroup(group *models.Group) (string, error) {
5858

5959
// RebuildSelectors rebuild all selectors based on the incoming group
6060
func (m *SubGroupManager) RebuildSelectors(groups map[string]*models.Group) {
61-
m.mu.Lock()
62-
m.selectors = make(map[uint]*selector)
63-
m.mu.Unlock()
61+
newSelectors := make(map[uint]*selector)
6462

65-
rebuildCount := 0
6663
for _, group := range groups {
6764
if group.GroupType == "aggregate" && len(group.SubGroups) > 0 {
68-
if sel := m.getSelector(group); sel != nil {
69-
rebuildCount++
65+
if sel := m.createSelector(group); sel != nil {
66+
newSelectors[group.ID] = sel
7067
}
7168
}
7269
}
7370

74-
logrus.WithField("new_count", rebuildCount).Debug("Rebuilt selectors for aggregate groups")
71+
m.mu.Lock()
72+
m.selectors = newSelectors
73+
m.mu.Unlock()
74+
75+
logrus.WithField("new_count", len(newSelectors)).Debug("Rebuilt selectors for aggregate groups")
7576
}
7677

7778
// getSelector retrieves or creates a selector for the aggregate group

0 commit comments

Comments
 (0)