|
| 1 | +package plans |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/samber/lo" |
| 7 | + |
| 8 | + api "github.com/openmeterio/openmeter/api/v3" |
| 9 | + "github.com/openmeterio/openmeter/openmeter/productcatalog" |
| 10 | + "github.com/openmeterio/openmeter/openmeter/productcatalog/plan" |
| 11 | + "github.com/openmeterio/openmeter/pkg/models" |
| 12 | +) |
| 13 | + |
| 14 | +func FromPlan(p plan.Plan) (api.BillingPlan, error) { |
| 15 | + validationIssues, _ := p.AsProductCatalogPlan().ValidationErrors() |
| 16 | + |
| 17 | + resp := api.BillingPlan{ |
| 18 | + BillingCadence: api.ISO8601Duration(p.BillingCadence.String()), |
| 19 | + CreatedAt: lo.ToPtr(p.CreatedAt), |
| 20 | + Currency: api.CurrencyCode(p.Currency.String()), |
| 21 | + DeletedAt: p.DeletedAt, |
| 22 | + Description: p.Description, |
| 23 | + EffectiveFrom: p.EffectiveFrom, |
| 24 | + EffectiveTo: p.EffectiveTo, |
| 25 | + Id: p.ID, |
| 26 | + Key: p.Key, |
| 27 | + Name: p.Name, |
| 28 | + UpdatedAt: lo.ToPtr(p.UpdatedAt), |
| 29 | + Version: p.Version, |
| 30 | + ProRatingEnabled: lo.ToPtr(p.ProRatingConfig.Enabled), |
| 31 | + ValidationErrors: fromValidationErrors(validationIssues), |
| 32 | + } |
| 33 | + |
| 34 | + var status api.BillingPlanStatus |
| 35 | + switch p.Status() { |
| 36 | + case productcatalog.PlanStatusDraft: |
| 37 | + status = api.BillingPlanStatusDraft |
| 38 | + case productcatalog.PlanStatusActive: |
| 39 | + status = api.BillingPlanStatusActive |
| 40 | + case productcatalog.PlanStatusArchived: |
| 41 | + status = api.BillingPlanStatusArchived |
| 42 | + case productcatalog.PlanStatusScheduled: |
| 43 | + status = api.BillingPlanStatusScheduled |
| 44 | + default: |
| 45 | + return resp, fmt.Errorf("invalid PlanStatus: %s", p.Status()) |
| 46 | + } |
| 47 | + |
| 48 | + resp.Status = status |
| 49 | + |
| 50 | + resp.Phases = make([]api.BillingPlanPhase, 0, len(p.Phases)) |
| 51 | + for _, phase := range p.Phases { |
| 52 | + billingPhase, err := fromPlanPhase(phase) |
| 53 | + if err != nil { |
| 54 | + return resp, fmt.Errorf("failed to convert plan phase: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + resp.Phases = append(resp.Phases, billingPhase) |
| 58 | + } |
| 59 | + |
| 60 | + return resp, nil |
| 61 | +} |
| 62 | + |
| 63 | +func fromPlanPhase(p plan.Phase) (api.BillingPlanPhase, error) { |
| 64 | + phase := api.BillingPlanPhase{ |
| 65 | + Description: p.Description, |
| 66 | + Duration: (*api.ISO8601Duration)(p.Duration.ISOStringPtrOrNil()), |
| 67 | + Key: p.Key, |
| 68 | + Name: p.Name, |
| 69 | + // TODO: convert rate cards to BillingRateCard |
| 70 | + RateCards: make([]api.BillingRateCard, 0, len(p.RateCards)), |
| 71 | + } |
| 72 | + |
| 73 | + return phase, nil |
| 74 | +} |
| 75 | + |
| 76 | +func fromValidationErrors(issues models.ValidationIssues) *[]api.ProductCatalogValidationError { |
| 77 | + if len(issues) == 0 { |
| 78 | + return nil |
| 79 | + } |
| 80 | + |
| 81 | + result := make([]api.ProductCatalogValidationError, 0, len(issues)) |
| 82 | + for _, issue := range issues { |
| 83 | + result = append(result, api.ProductCatalogValidationError{ |
| 84 | + Code: string(issue.Code()), |
| 85 | + Field: issue.Field().JSONPath(), |
| 86 | + Message: issue.Message(), |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + return &result |
| 91 | +} |
0 commit comments