Skip to content

Commit

Permalink
Active subscriptions request fix (#4659)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo authored May 17, 2024
1 parent 432f4c4 commit 19cb8c9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
9 changes: 9 additions & 0 deletions api/types/billing_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ type ListCustomerUsageRequest struct {
CurrentPeriod bool `json:"current_period,omitempty"`
}

// Subscription is the subscription for a customer
type Subscription struct {
ExternalID string `json:"external_id"`
ExternalCustomerID string `json:"external_customer_id"`
Status string `json:"status"`
SubscriptionAt string `json:"subscription_at"`
EndingAt string `json:"ending_at"`
}

// Usage is the aggregated usage for a customer
type Usage struct {
FromDatetime string `json:"from_datetime"`
Expand Down
57 changes: 44 additions & 13 deletions internal/billing/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,38 +153,34 @@ func (m LagoClient) GetCustomerActivePlan(ctx context.Context, projectID uint, s
}

customerID := m.generateLagoID(CustomerIDPrefix, projectID, sandboxEnabled)
subscriptionListInput := lago.SubscriptionListInput{
ExternalCustomerID: customerID,
}

telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "customer_id", Value: customerID},
)

activeSubscriptions, lagoErr := m.client.Subscription().GetList(ctx, subscriptionListInput)
if lagoErr != nil {
return plan, telemetry.Error(ctx, span, lagoErr.Err, "failed to get active subscription")
activeSubscriptions, err := m.getCustomerActiveSubscription(ctx, customerID)
if err != nil {
return plan, telemetry.Error(ctx, span, err, "failed to get active subscriptions")
}

if activeSubscriptions == nil {
return plan, telemetry.Error(ctx, span, err, "no active subscriptions found")
}

for _, subscription := range activeSubscriptions.Subscriptions {
if subscription.Status != lago.SubscriptionStatusActive {
for _, subscription := range activeSubscriptions {
if subscription.Status != string(lago.SubscriptionStatusActive) {
continue
}

plan.ID = subscription.ExternalID
plan.CustomerID = subscription.ExternalCustomerID
plan.StartingOn = subscription.SubscriptionAt.Format(time.RFC3339)
plan.StartingOn = subscription.SubscriptionAt

if subscription.EndingAt != nil {
plan.EndingBefore = subscription.EndingAt.Format(time.RFC3339)
if subscription.EndingAt != "" {
plan.EndingBefore = subscription.EndingAt
}

if strings.Contains(subscription.ExternalID, TrialIDPrefix) {
plan.TrialInfo.EndingBefore = subscription.EndingAt.Format(time.RFC3339)
plan.TrialInfo.EndingBefore = subscription.EndingAt
}

break
Expand Down Expand Up @@ -505,6 +501,41 @@ func (m LagoClient) addCustomerPlan(ctx context.Context, customerID string, plan
return nil
}

func (m LagoClient) getCustomerActiveSubscription(ctx context.Context, customerID string) (subscriptions []types.Subscription, err error) {
ctx, span := telemetry.NewSpan(ctx, "list-customer-active-subscriptions")
defer span.End()

url := fmt.Sprintf("%s/api/v1/subscriptions?external_customer_id=%s&status[]=%s", lagoBaseURL, customerID, lago.SubscriptionStatusActive)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return subscriptions, telemetry.Error(ctx, span, err, "failed to create list subscriptions request")
}

req.Header.Set("Authorization", "Bearer "+m.lagoApiKey)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return subscriptions, telemetry.Error(ctx, span, err, "failed to get customer subscriptions")
}

var response struct {
Subscriptions []types.Subscription `json:"subscriptions"`
}

err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return subscriptions, telemetry.Error(ctx, span, err, "failed to decode subscriptions list response")
}

err = resp.Body.Close()
if err != nil {
return subscriptions, telemetry.Error(ctx, span, err, "failed to close response body")
}

return response.Subscriptions, nil
}

func (m LagoClient) listCustomerWallets(ctx context.Context, customerID string) (walletList []types.Wallet, err error) {
ctx, span := telemetry.NewSpan(ctx, "list-lago-customer-wallets")
defer span.End()
Expand Down

0 comments on commit 19cb8c9

Please sign in to comment.