@@ -275,7 +275,7 @@ func validateMailingListUpdate(ctx context.Context, existing *model.GrpsIOMailin
275275 }
276276
277277 // Validate main group restrictions (critical business rule from Groups.io)
278- if parentService != nil && existing . IsMainGroup ( parentService ) {
278+ if parentService != nil && isMainGroupForService ( existing , parentService ) {
279279 // Main groups must remain public announcement lists
280280 if payload .Type != nil && * payload .Type != "announcement" {
281281 return errors .NewValidation ("main group must be an announcement list" )
@@ -360,20 +360,8 @@ func validateMailingListUpdate(ctx context.Context, existing *model.GrpsIOMailin
360360// validateMailingListDeleteProtection validates deletion protection rules
361361func validateMailingListDeleteProtection (mailingList * model.GrpsIOMailingList , parentService * model.GrpsIOService ) error {
362362 // Check if it's a main group (any service type)
363- if parentService != nil {
364- isMainGroup := false
365-
366- switch parentService .Type {
367- case "primary" :
368- isMainGroup = mailingList .GroupName == parentService .GroupName
369- case "formation" , "shared" :
370- // Formation and shared services use prefix as main group identifier
371- isMainGroup = mailingList .GroupName == parentService .Prefix
372- }
373-
374- if isMainGroup {
375- return errors .NewValidation (fmt .Sprintf ("cannot delete the main group of a %s service" , parentService .Type ))
376- }
363+ if parentService != nil && isMainGroupForService (mailingList , parentService ) {
364+ return errors .NewValidation (fmt .Sprintf ("cannot delete the main group of a %s service" , parentService .Type ))
377365 }
378366
379367 // Protect announcement lists (typically used for critical communications)
@@ -436,6 +424,18 @@ func contains(slice []string, item string) bool {
436424 return false
437425}
438426
427+ // isMainGroupForService determines if a list is the "main" group for its parent.
428+ func isMainGroupForService (ml * model.GrpsIOMailingList , svc * model.GrpsIOService ) bool {
429+ switch svc .Type {
430+ case "primary" :
431+ return ml .GroupName == svc .GroupName
432+ case "formation" , "shared" :
433+ return ml .GroupName == svc .Prefix
434+ default :
435+ return false
436+ }
437+ }
438+
439439// validateMemberUpdate validates that immutable fields are not changed during updates
440440func validateMemberUpdate (existing , updated * model.GrpsIOMember ) error {
441441 if existing == nil || updated == nil {
0 commit comments