diff --git a/db-connector.go b/db-connector.go index 9b872929..1ee26570 100755 --- a/db-connector.go +++ b/db-connector.go @@ -4736,6 +4736,7 @@ func SetOrg(ctx context.Context, data Org, id string) error { } data.Users = newUsers + if len(data.Tutorials) == 0 { data = *GetTutorials(ctx, data, false) } @@ -6048,6 +6049,52 @@ func fixUserOrg(ctx context.Context, user *User) *User { return user } +// fixUsersForOrg syncs user.Orgs with org.Users +// For each user in org.Users, ensures the org is in user.Orgs +// This is a standalone function to fix data sync issues manually +// Only call this when you need to fix broken org/user relationships +func fixUsersForOrg(ctx context.Context, org *Org) *Org { + // Made it background due to potential timeouts if this is + // used in API calls + ctx = context.Background() + + // For each user in org.Users + for _, orgUser := range org.Users { + if len(orgUser.Id) == 0 { + continue + } + + go func(userId string, orgId string) { + user, err := GetUser(ctx, userId) + if err != nil { + log.Printf("[WARNING] Error getting user %s in fixUsersForOrg: %s", userId, err) + return + } + + found := false + for _, userOrgId := range user.Orgs { + if userOrgId == orgId { + found = true + break + } + } + + if !found { + user.Orgs = append(user.Orgs, orgId) + + err = SetUser(ctx, user, false) // false to avoid recursive call + if err != nil { + log.Printf("[WARNING] Failed setting user %s in fixUsersForOrg: %s", userId, err) + } else { + log.Printf("[INFO] Added org %s to user %s's Orgs list", orgId, userId) + } + } + }(orgUser.Id, org.Id) + } + + return org +} + func GetAllWorkflowAppAuth(ctx context.Context, orgId string) ([]AppAuthenticationStorage, error) { var allworkflowappAuths []AppAuthenticationStorage nameKey := "workflowappauth"