Skip to content

Commit 8728553

Browse files
authored
Merge pull request #332 from Monilprajapati/fix
Subscription / Billing bug fixes and some enhancements
2 parents a9e7ae4 + 12ad00e commit 8728553

File tree

1 file changed

+114
-11
lines changed

1 file changed

+114
-11
lines changed

shared.go

Lines changed: 114 additions & 11 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 = 10000
1116+
org.SyncFeatures.AppExecutions.Limit = 2000
11171117
orgChanged = true
11181118
}
11191119

@@ -1161,18 +1161,83 @@ 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+
11641186
if len(org.Subscriptions) == 0 && len(org.CreatorOrg) == 0 {
11651187
// Only when there is no subscription in the org and it's not a suborg :)
11661188
// Placeholder subscription that to add at very first time
1167-
base := buildBaseSubscription(*org, org.SyncFeatures.AppExecutions.Limit)
1189+
base := BuildBaseSubscription(*org, org.SyncFeatures.AppExecutions.Limit)
11681190
org.Subscriptions = append(org.Subscriptions, base)
11691191

11701192
if err := SetOrg(ctx, *org, org.Id); err != nil {
11711193
log.Printf("[WARNING] Failed to persist base subscription for org %s: %s", org.Id, err)
11721194
} else {
11731195
log.Printf("[INFO] Added a base subscription (%s) for org %s", base.Name, org.Id)
11741196
}
1175-
} else if len(org.CreatorOrg) == 0 && project.Environment == "onprem" {
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" {
11761241
// This is used to update the subscription for the onprem orgs
11771242
// That have cloud sync active
11781243
// Not a suborg
@@ -12430,7 +12495,7 @@ func getSignatureSample(org Org) PaymentSubscription {
1243012495
return PaymentSubscription{}
1243112496
}
1243212497

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

1243512500
now := int64(time.Now().Unix())
1243612501
log.Printf("[DEBUG] Building base subscription for org %s that has %d monthly exec limit", org.Id, monthlyExecLimit)
@@ -12469,7 +12534,7 @@ func buildBaseSubscription(org Org, monthlyExecLimit int64) PaymentSubscription
1246912534
"15 Users",
1247012535
"Select Datacenter Region",
1247112536
}
12472-
amount = "32" // Just for placeholder
12537+
amount = fmt.Sprintf("%d", int64(((monthlyExecLimit-2000)/10000)*32)) // Calculate based on app runs: (paid_runs / 10k) * $32
1247312538
} else if monthlyExecLimit >= 2000 && monthlyExecLimit < 12000 {
1247412539
planName = "Free License"
1247512540
supportLevel = "Community Support"
@@ -12501,6 +12566,7 @@ func buildBaseSubscription(org Org, monthlyExecLimit int64) PaymentSubscription
1250112566
endDate := int64(firstNextMonth.Unix())
1250212567

1250312568
return PaymentSubscription{
12569+
Id: uuid.NewV4().String(),
1250412570
Active: true,
1250512571
Startdate: now,
1250612572
Enddate: endDate,
@@ -12575,7 +12641,7 @@ func HandleEditOrg(resp http.ResponseWriter, request *http.Request) {
1257512641

1257612642
CreatorConfig string `json:"creator_config" datastore:"creator_config"`
1257712643
Subscription PaymentSubscription `json:"subscription" datastore:"subscription"`
12578-
SubscriptionIndex int `json:"subscription_index" datastore:"subscription_index"`
12644+
SubscriptionIndex string `json:"subscription_index" datastore:"subscription_index"`
1257912645

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

1267012736
// Allow editing a specific subscription card from UI except Eula and Reference
1267112737
if tmpData.Editing == "subscription_update" {
12672-
idx := tmpData.SubscriptionIndex
12673-
if idx < 0 || idx >= len(org.Subscriptions) {
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 {
1267412747
resp.WriteHeader(400)
12675-
resp.Write([]byte(`{"success": false, "reason": "invalid subscription index"}`))
12748+
resp.Write([]byte(`{"success": false, "reason": "subscription not found by ID"}`))
1267612749
return
1267712750
}
1267812751

1267912752
// Preserve immutable fields
1268012753
existing := org.Subscriptions[idx]
1268112754
updated := tmpData.Subscription
12755+
updated.Id = existing.Id
1268212756
updated.Eula = existing.Eula
1268312757

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

1269612768
// Apply the rest
1269712769
org.Subscriptions[idx] = updated
1269812770

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+
1269912802
if err := SetOrg(ctx, *org, org.Id); err != nil {
1270012803
log.Printf("[WARNING] Failed to update subscription for org %s: %s", org.Id, err)
1270112804
resp.WriteHeader(500)

0 commit comments

Comments
 (0)