diff --git a/backend/go-app/main.go b/backend/go-app/main.go index 57887c9a..f1d53d63 100755 --- a/backend/go-app/main.go +++ b/backend/go-app/main.go @@ -936,6 +936,58 @@ func handleInfo(resp http.ResponseWriter, request *http.Request) { log.Printf("[DEBUG] Failed to get org during getinfo: %s", err) } + childOrgs := []shuffle.Org{} + if len(org.CreatorOrg) > 0 { + childOrgs, err = shuffle.GetAllChildOrgs(ctx, org.CreatorOrg) + if err != nil { + log.Printf("[ERROR] Failed to get child orgs during getinfo: %s", err) + childOrgs = []shuffle.Org{} + } + } + + failToLoadOrgs := []string{} + sort.Slice(childOrgs, func(i, j int) bool { + return childOrgs[i].Created < childOrgs[j].Created + }) + + for index, org := range childOrgs { + + if index < 3 { + continue + } + + failToLoadOrgs = append(failToLoadOrgs, org.Id) + } + + if len(org.CreatorOrg) > 0 && len(childOrgs) > 3 && shuffle.ArrayContains(failToLoadOrgs, userInfo.ActiveOrg.Id) { + parentOrg, err := shuffle.GetOrg(ctx, org.CreatorOrg) + if err != nil { + log.Printf("[ERROR] Failed to get parent org during getinfo: %s", err) + } else { + parent := shuffle.HandleCheckLicense(ctx, *parentOrg) + parentOrg := &parent + if !parentOrg.SyncFeatures.MultiTenant.Active { + userInfo.ActiveOrg = shuffle.OrgMini{ + Id: parentOrg.Id, + Name: parentOrg.Name, + Role: userInfo.Role, + Branding: parentOrg.Branding, + Image: parentOrg.Image, + } + log.Printf("[INFO] Parent org %s has more than 3 child orgs and is not licensed. Moving user %s to parent org %s", parentOrg.Name, userInfo.Username, parentOrg.Name) + + err = shuffle.SetUser(ctx, &userInfo, false) + if err != nil { + log.Printf("[WARNING] Failed setting user to parent org: %s", err) + } + + resp.WriteHeader(200) + resp.Write([]byte(`{"success": true, "reason": "Parent org has more than 3 child orgs and is not licensed. Moving to parent org. Contact support@shuffler.io for more information", "switch_parent": true}`)) + return + } + } + } + //if err == nil { if len(org.Id) > 0 { if userInfo.Role == "" { @@ -3895,15 +3947,7 @@ func remoteOrgJobController(org shuffle.Org, body []byte) error { if err != nil { log.Printf("[ERROR] Failed to marshal SyncFeatures for cache: %s", err) } else { - shuffle.SetCache(ctx, cacheKey, featuresBytes, 30) - } - - subscriptionCacheKey := fmt.Sprintf("org_subscriptions_%s", org.Id) - subscriptionsBytes, err := json.Marshal(responseData.Subscriptions) - if err != nil { - log.Printf("[ERROR] Failed to marshal Subscriptions for cache: %s", err) - } else { - shuffle.SetCache(ctx, subscriptionCacheKey, subscriptionsBytes, 1800) + shuffle.SetCache(ctx, cacheKey, featuresBytes, 1800) } for _, job := range responseData.Jobs { @@ -4910,8 +4954,16 @@ func handleCloudSetup(resp http.ResponseWriter, request *http.Request) { // 2. Add iterative sync schedule for interval seconds // 3. Add another environment for the org's users org.CloudSync = true - org.SyncFeatures = responseData.SyncFeatures - org.Subscriptions = responseData.Subscriptions + + // set cache here for 30 min + cacheKey := fmt.Sprintf("org_sync_features_%s", org.Id) + featuresBytes, err := json.Marshal(responseData.SyncFeatures) + if err != nil { + log.Printf("[ERROR] Failed to marshal SyncFeatures for cache: %s", err) + } else { + shuffle.SetCache(ctx, cacheKey, featuresBytes, 1800) + } + org.SyncConfig = shuffle.SyncConfig{ Apikey: responseData.SessionKey, Interval: responseData.IntervalSeconds, diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 9425a1c6..75363d20 100755 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -180,6 +180,13 @@ const App = (message, props) => { var userInfo = {}; if (responseJson.success === true) { //console.log("USER: ", responseJson); + if (responseJson?.switch_parent === true) { + toast.info(responseJson.reason) + setTimeout(() => { + window.location.reload(); + }, 3000); + return + } userInfo = responseJson; setIsLoggedIn(true);