Skip to content

Commit 5c99f86

Browse files
authored
Merge pull request #333 from Shuffle/revert-332-fix
Revert "Subscription / Billing bug fixes and some enhancements "
2 parents 8728553 + 70657bb commit 5c99f86

File tree

1 file changed

+11
-114
lines changed

1 file changed

+11
-114
lines changed

shared.go

Lines changed: 11 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ func HandleGetOrg(resp http.ResponseWriter, request *http.Request) {
11131113
//log.Printf("LIMIT: %s", org.SyncFeatures.AppExecutions.Limit)
11141114
orgChanged := false
11151115
if org.SyncFeatures.AppExecutions.Limit == 0 || org.SyncFeatures.AppExecutions.Limit == 1500 {
1116-
org.SyncFeatures.AppExecutions.Limit = 2000
1116+
org.SyncFeatures.AppExecutions.Limit = 10000
11171117
orgChanged = true
11181118
}
11191119

@@ -1161,83 +1161,18 @@ func HandleGetOrg(resp http.ResponseWriter, request *http.Request) {
11611161
org.SyncFeatures.MultiEnv.Usage = int64(len(envs))
11621162
}
11631163

1164-
// Backfill subscription IDs if any subscription is missing an ID
1165-
addSubId := false
1166-
for _, sub := range org.Subscriptions {
1167-
if sub.Id == "" {
1168-
addSubId = true
1169-
break
1170-
}
1171-
}
1172-
1173-
if addSubId {
1174-
for i := range org.Subscriptions {
1175-
if org.Subscriptions[i].Id == "" {
1176-
org.Subscriptions[i].Id = uuid.NewV4().String()
1177-
}
1178-
}
1179-
if err := SetOrg(ctx, *org, org.Id); err != nil {
1180-
log.Printf("[WARNING] Failed to backfill subscription IDs for org %s: %s", org.Id, err)
1181-
} else {
1182-
log.Printf("[INFO] Backfilled subscription IDs for org %s", org.Id)
1183-
}
1184-
}
1185-
11861164
if len(org.Subscriptions) == 0 && len(org.CreatorOrg) == 0 {
11871165
// Only when there is no subscription in the org and it's not a suborg :)
11881166
// Placeholder subscription that to add at very first time
1189-
base := BuildBaseSubscription(*org, org.SyncFeatures.AppExecutions.Limit)
1167+
base := buildBaseSubscription(*org, org.SyncFeatures.AppExecutions.Limit)
11901168
org.Subscriptions = append(org.Subscriptions, base)
11911169

11921170
if err := SetOrg(ctx, *org, org.Id); err != nil {
11931171
log.Printf("[WARNING] Failed to persist base subscription for org %s: %s", org.Id, err)
11941172
} else {
11951173
log.Printf("[INFO] Added a base subscription (%s) for org %s", base.Name, org.Id)
11961174
}
1197-
} else if len(org.CreatorOrg) == 0 && len(org.Subscriptions) >= 1 {
1198-
hasActivePaidSubscription := false
1199-
hasFreeSubscription := false
1200-
1201-
updateSub := false
1202-
1203-
for _, sub := range org.Subscriptions {
1204-
if sub.Active && sub.Amount != "0" {
1205-
hasActivePaidSubscription = true
1206-
}
1207-
if sub.Amount == "0" && sub.Reference == "" {
1208-
hasFreeSubscription = true
1209-
}
1210-
}
1211-
1212-
if hasActivePaidSubscription && hasFreeSubscription {
1213-
// Remove free subscriptions since user has active paid plan
1214-
var filteredSubs []PaymentSubscription
1215-
for _, sub := range org.Subscriptions {
1216-
if !(sub.Amount == "0" && sub.Reference == "") {
1217-
filteredSubs = append(filteredSubs, sub)
1218-
}
1219-
}
1220-
org.Subscriptions = filteredSubs
1221-
updateSub = true
1222-
log.Printf("[INFO] Removed free subscription for org %s (active paid subscription exists)", org.Id)
1223-
} else if !hasActivePaidSubscription && !hasFreeSubscription {
1224-
// No active paid subscription and no free plan, add one
1225-
org.Subscriptions = append(org.Subscriptions, BuildBaseSubscription(*org, 2000))
1226-
updateSub = true
1227-
log.Printf("[INFO] Added free subscription for org %s (no active paid subscriptions found)", org.Id)
1228-
}
1229-
1230-
// Persist any subscription changes made above
1231-
if updateSub {
1232-
if err := SetOrg(ctx, *org, org.Id); err != nil {
1233-
log.Printf("[ERROR] Failed to persist subscription changes for org %s: %v", org.Id, err)
1234-
} else {
1235-
log.Printf("[DEBUG] Successfully persisted subscription changes for org %s", org.Id)
1236-
}
1237-
}
1238-
}
1239-
1240-
if len(org.CreatorOrg) == 0 && project.Environment == "onprem" {
1175+
} else if len(org.CreatorOrg) == 0 && project.Environment == "onprem" {
12411176
// This is used to update the subscription for the onprem orgs
12421177
// That have cloud sync active
12431178
// Not a suborg
@@ -12495,7 +12430,7 @@ func getSignatureSample(org Org) PaymentSubscription {
1249512430
return PaymentSubscription{}
1249612431
}
1249712432

12498-
func BuildBaseSubscription(org Org, monthlyExecLimit int64) PaymentSubscription {
12433+
func buildBaseSubscription(org Org, monthlyExecLimit int64) PaymentSubscription {
1249912434

1250012435
now := int64(time.Now().Unix())
1250112436
log.Printf("[DEBUG] Building base subscription for org %s that has %d monthly exec limit", org.Id, monthlyExecLimit)
@@ -12534,7 +12469,7 @@ func BuildBaseSubscription(org Org, monthlyExecLimit int64) PaymentSubscription
1253412469
"15 Users",
1253512470
"Select Datacenter Region",
1253612471
}
12537-
amount = fmt.Sprintf("%d", int64(((monthlyExecLimit-2000)/10000)*32)) // Calculate based on app runs: (paid_runs / 10k) * $32
12472+
amount = "32" // Just for placeholder
1253812473
} else if monthlyExecLimit >= 2000 && monthlyExecLimit < 12000 {
1253912474
planName = "Free License"
1254012475
supportLevel = "Community Support"
@@ -12566,7 +12501,6 @@ func BuildBaseSubscription(org Org, monthlyExecLimit int64) PaymentSubscription
1256612501
endDate := int64(firstNextMonth.Unix())
1256712502

1256812503
return PaymentSubscription{
12569-
Id: uuid.NewV4().String(),
1257012504
Active: true,
1257112505
Startdate: now,
1257212506
Enddate: endDate,
@@ -12641,7 +12575,7 @@ func HandleEditOrg(resp http.ResponseWriter, request *http.Request) {
1264112575

1264212576
CreatorConfig string `json:"creator_config" datastore:"creator_config"`
1264312577
Subscription PaymentSubscription `json:"subscription" datastore:"subscription"`
12644-
SubscriptionIndex string `json:"subscription_index" datastore:"subscription_index"`
12578+
SubscriptionIndex int `json:"subscription_index" datastore:"subscription_index"`
1264512579

1264612580
SyncFeatures SyncFeatures `json:"sync_features" datastore:"sync_features"`
1264712581
Billing Billing `json:"billing" datastore:"billing"`
@@ -12735,24 +12669,16 @@ func HandleEditOrg(resp http.ResponseWriter, request *http.Request) {
1273512669

1273612670
// Allow editing a specific subscription card from UI except Eula and Reference
1273712671
if tmpData.Editing == "subscription_update" {
12738-
// Find subscription by ID (SubscriptionIndex now holds the ID string)
12739-
var idx int = -1
12740-
for i, sub := range org.Subscriptions {
12741-
if sub.Id == tmpData.SubscriptionIndex {
12742-
idx = i
12743-
break
12744-
}
12745-
}
12746-
if idx == -1 {
12672+
idx := tmpData.SubscriptionIndex
12673+
if idx < 0 || idx >= len(org.Subscriptions) {
1274712674
resp.WriteHeader(400)
12748-
resp.Write([]byte(`{"success": false, "reason": "subscription not found by ID"}`))
12675+
resp.Write([]byte(`{"success": false, "reason": "invalid subscription index"}`))
1274912676
return
1275012677
}
1275112678

1275212679
// Preserve immutable fields
1275312680
existing := org.Subscriptions[idx]
1275412681
updated := tmpData.Subscription
12755-
updated.Id = existing.Id
1275612682
updated.Eula = existing.Eula
1275712683

1275812684
// Do not overwrite existing EULA signature info if it's already signed
@@ -12764,41 +12690,12 @@ func HandleEditOrg(resp http.ResponseWriter, request *http.Request) {
1276412690
updated.EulaSigned = true
1276512691
updated.EulaSignedBy = user.Username
1276612692
}
12693+
// Do not overwrite subscription reference number
12694+
updated.Reference = existing.Reference
1276712695

1276812696
// Apply the rest
1276912697
org.Subscriptions[idx] = updated
1277012698

12771-
// This is to set user in free plan is the current plan is Inactive by us
12772-
if len(org.CreatorOrg) == 0 {
12773-
hasActivePaidSubscription := false
12774-
hasFreeSubscription := false
12775-
12776-
for _, sub := range org.Subscriptions {
12777-
if sub.Active && sub.Amount != "0" {
12778-
hasActivePaidSubscription = true
12779-
}
12780-
if sub.Amount == "0" && sub.Reference == "" {
12781-
hasFreeSubscription = true
12782-
}
12783-
}
12784-
12785-
if hasActivePaidSubscription && hasFreeSubscription {
12786-
// Remove free subscriptions since user has active paid plan
12787-
var filteredSubs []PaymentSubscription
12788-
for _, sub := range org.Subscriptions {
12789-
if !(sub.Amount == "0" && sub.Reference == "") {
12790-
filteredSubs = append(filteredSubs, sub)
12791-
}
12792-
}
12793-
org.Subscriptions = filteredSubs
12794-
log.Printf("[INFO] Removed free subscription for org %s (active paid subscription exists)", org.Id)
12795-
} else if !hasActivePaidSubscription && !hasFreeSubscription {
12796-
// No active paid subscription and no free plan, add one
12797-
org.Subscriptions = append(org.Subscriptions, BuildBaseSubscription(*org, 2000))
12798-
log.Printf("[INFO] Added free subscription for org %s (no active paid subscriptions found)", org.Id)
12799-
}
12800-
}
12801-
1280212699
if err := SetOrg(ctx, *org, org.Id); err != nil {
1280312700
log.Printf("[WARNING] Failed to update subscription for org %s: %s", org.Id, err)
1280412701
resp.WriteHeader(500)

0 commit comments

Comments
 (0)