Skip to content

Commit c95a4fc

Browse files
authored
fix: return billing period for free phases too (#3391)
1 parent 223b9c8 commit c95a4fc

File tree

4 files changed

+7
-21
lines changed

4 files changed

+7
-21
lines changed

openmeter/productcatalog/subscription/http/mapping.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,16 +393,13 @@ func MapSubscriptionViewToAPI(view subscription.SubscriptionView) (api.Subscript
393393
BillablesMustAlign: lo.ToPtr(true), // this field is deprecated on API, we'll get rid of it eventually
394394
}
395395

396-
if currPhase, ok := view.Spec.GetCurrentPhaseAt(clock.Now()); ok && currPhase.HasBillables() {
396+
{
397397
period, err := view.Spec.GetAlignedBillingPeriodAt(clock.Now())
398398
if err != nil {
399-
// GetAlignedBillingPeriodAt cannot be calculated for all aligned subscriptions.
400-
if _, ok := lo.ErrorsAs[subscription.NoBillingPeriodError](err); !ok {
399+
if !subscription.IsErrSubscriptionBillingPeriodQueriedBeforeSubscriptionStart(err) {
401400
return api.SubscriptionExpanded{}, err
402401
}
403-
}
404-
405-
if err == nil {
402+
} else {
406403
alg.CurrentAlignedBillingPeriod = &api.Period{
407404
From: period.From,
408405
To: period.To,

openmeter/subscription/errors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func NewErrSubscriptionBillingPeriodQueriedBeforeSubscriptionStart(queriedAt, su
2424
return ErrSubscriptionBillingPeriodQueriedBeforeSubscriptionStart.WithAttr("queried_at", queriedAt).WithAttr("subscription_start", subscriptionStart)
2525
}
2626

27+
func IsErrSubscriptionBillingPeriodQueriedBeforeSubscriptionStart(err error) bool {
28+
return IsValidationIssueWithCode(err, ErrCodeSubscriptionBillingPeriodQueriedBeforeSubscriptionStart)
29+
}
30+
2731
// TODO(galexi): "ValidationIssue" is not the right concept here. We should have a different kind of error with all this capability. It's used here as a hack to localize things for the time being.
2832

2933
func IsValidationIssueWithCode(err error, code models.ErrorCode) bool {

openmeter/subscription/subscriptionspec.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,16 +1153,3 @@ func (e AlignmentError) Error() string {
11531153
func (e AlignmentError) Unwrap() error {
11541154
return e.Inner
11551155
}
1156-
1157-
// NoBillingPeriodError is an error that occurs when a phase has no billing period.
1158-
type NoBillingPeriodError struct {
1159-
Inner error
1160-
}
1161-
1162-
func (e NoBillingPeriodError) Error() string {
1163-
return fmt.Sprintf("no billing period: %s", e.Inner)
1164-
}
1165-
1166-
func (e NoBillingPeriodError) Unwrap() error {
1167-
return e.Inner
1168-
}

openmeter/subscription/workflow/errors.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ func MapSubscriptionErrors(err error) error {
1616
return models.NewGenericValidationError(sErr)
1717
} else if sErr, ok := lo.ErrorsAs[*subscription.AlignmentError](err); ok {
1818
return models.NewGenericConflictError(sErr)
19-
} else if sErr, ok := lo.ErrorsAs[*subscription.NoBillingPeriodError](err); ok {
20-
return models.NewGenericValidationError(sErr)
2119
}
2220

2321
return err

0 commit comments

Comments
 (0)