|
| 1 | +package plans |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/samber/lo" |
| 8 | + |
| 9 | + api "github.com/openmeterio/openmeter/api/v3" |
| 10 | + "github.com/openmeterio/openmeter/api/v3/apierrors" |
| 11 | + "github.com/openmeterio/openmeter/api/v3/response" |
| 12 | + "github.com/openmeterio/openmeter/openmeter/productcatalog/plan" |
| 13 | + "github.com/openmeterio/openmeter/pkg/framework/commonhttp" |
| 14 | + "github.com/openmeterio/openmeter/pkg/framework/transport/httptransport" |
| 15 | + "github.com/openmeterio/openmeter/pkg/pagination" |
| 16 | +) |
| 17 | + |
| 18 | +type ( |
| 19 | + ListPlansRequest = plan.ListPlansInput |
| 20 | + ListPlansResponse = response.PagePaginationResponse[api.BillingPlan] |
| 21 | + ListPlansParams = api.ListPlansParams |
| 22 | + ListPlansHandler httptransport.HandlerWithArgs[ListPlansRequest, ListPlansResponse, ListPlansParams] |
| 23 | +) |
| 24 | + |
| 25 | +func (h *handler) ListPlans() ListPlansHandler { |
| 26 | + return httptransport.NewHandlerWithArgs( |
| 27 | + func(ctx context.Context, r *http.Request, params ListPlansParams) (ListPlansRequest, error) { |
| 28 | + ns, err := h.resolveNamespace(ctx) |
| 29 | + if err != nil { |
| 30 | + return ListPlansRequest{}, err |
| 31 | + } |
| 32 | + |
| 33 | + page := pagination.NewPage(1, 20) |
| 34 | + if params.Page != nil { |
| 35 | + page = pagination.NewPage( |
| 36 | + lo.FromPtrOr(params.Page.Number, 1), |
| 37 | + lo.FromPtrOr(params.Page.Size, 20), |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + if err := page.Validate(); err != nil { |
| 42 | + return ListPlansRequest{}, apierrors.NewBadRequestError(ctx, err, apierrors.InvalidParameters{ |
| 43 | + apierrors.InvalidParameter{ |
| 44 | + Field: "page", |
| 45 | + Reason: err.Error(), |
| 46 | + Source: apierrors.InvalidParamSourceQuery, |
| 47 | + }, |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + return ListPlansRequest{ |
| 52 | + Namespaces: []string{ns}, |
| 53 | + Page: page, |
| 54 | + }, nil |
| 55 | + }, |
| 56 | + func(ctx context.Context, req ListPlansRequest) (ListPlansResponse, error) { |
| 57 | + result, err := h.service.ListPlans(ctx, req) |
| 58 | + if err != nil { |
| 59 | + return ListPlansResponse{}, err |
| 60 | + } |
| 61 | + |
| 62 | + items := make([]api.BillingPlan, 0, len(result.Items)) |
| 63 | + for _, p := range result.Items { |
| 64 | + // FIXME: For now we skip plans containing price types not representable in v3 (e.g., package, dynamic). We'll add full bidirectional transform later on. |
| 65 | + if hasUnsupportedV3Price(p) { |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + billingPlan, err := FromPlan(p) |
| 70 | + if err != nil { |
| 71 | + return ListPlansResponse{}, err |
| 72 | + } |
| 73 | + |
| 74 | + items = append(items, billingPlan) |
| 75 | + } |
| 76 | + |
| 77 | + return response.NewPagePaginationResponse(items, response.PageMetaPage{ |
| 78 | + Size: req.Page.PageSize, |
| 79 | + Number: req.Page.PageNumber, |
| 80 | + Total: lo.ToPtr(result.TotalCount), |
| 81 | + }), nil |
| 82 | + }, |
| 83 | + commonhttp.JSONResponseEncoderWithStatus[ListPlansResponse](http.StatusOK), |
| 84 | + httptransport.AppendOptions( |
| 85 | + h.options, |
| 86 | + httptransport.WithOperationName("list-plans"), |
| 87 | + httptransport.WithErrorEncoder(apierrors.GenericErrorEncoder()), |
| 88 | + )..., |
| 89 | + ) |
| 90 | +} |
0 commit comments