From 4548bf151ca268a7523eed98db8c0cc8685f9021 Mon Sep 17 00:00:00 2001 From: Hari Krishna Date: Fri, 21 Nov 2025 21:36:41 +0530 Subject: [PATCH 1/2] sync org users with user orgs bidirectionally --- db-connector.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/db-connector.go b/db-connector.go index 9b872929..88be7616 100755 --- a/db-connector.go +++ b/db-connector.go @@ -4736,6 +4736,13 @@ func SetOrg(ctx context.Context, data Org, id string) error { } data.Users = newUsers + + // Fix users that are in org.Users but not in user.Orgs + // Only run in main region to avoid conflicts + if gceProject == "shuffler" { + data = *fixUsersForOrg(ctx, &data) + } + if len(data.Tutorials) == 0 { data = *GetTutorials(ctx, data, false) } @@ -6048,6 +6055,48 @@ func fixUserOrg(ctx context.Context, user *User) *User { return user } +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" From b8ce95fc98d35c823cd619d8c321c533f75a03a4 Mon Sep 17 00:00:00 2001 From: Hari Krishna Date: Mon, 24 Nov 2025 09:26:32 +0530 Subject: [PATCH 2/2] remove automatic org-user sync from SetOrg --- db-connector.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/db-connector.go b/db-connector.go index 88be7616..1ee26570 100755 --- a/db-connector.go +++ b/db-connector.go @@ -4737,12 +4737,6 @@ func SetOrg(ctx context.Context, data Org, id string) error { data.Users = newUsers - // Fix users that are in org.Users but not in user.Orgs - // Only run in main region to avoid conflicts - if gceProject == "shuffler" { - data = *fixUsersForOrg(ctx, &data) - } - if len(data.Tutorials) == 0 { data = *GetTutorials(ctx, data, false) } @@ -6055,6 +6049,10 @@ 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