|
| 1 | +package plans |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + api "github.com/openmeterio/openmeter/api/v3" |
| 9 | + "github.com/openmeterio/openmeter/api/v3/apierrors" |
| 10 | + "github.com/openmeterio/openmeter/openmeter/productcatalog/plan" |
| 11 | + "github.com/openmeterio/openmeter/pkg/clock" |
| 12 | + "github.com/openmeterio/openmeter/pkg/framework/commonhttp" |
| 13 | + "github.com/openmeterio/openmeter/pkg/framework/transport/httptransport" |
| 14 | + "github.com/openmeterio/openmeter/pkg/models" |
| 15 | +) |
| 16 | + |
| 17 | +type ( |
| 18 | + ArchivePlanRequest = plan.ArchivePlanInput |
| 19 | + ArchivePlanResponse = api.BillingPlan |
| 20 | + ArchivePlanParams = string |
| 21 | + ArchivePlanHandler httptransport.HandlerWithArgs[ArchivePlanRequest, ArchivePlanResponse, ArchivePlanParams] |
| 22 | +) |
| 23 | + |
| 24 | +func (h *handler) ArchivePlan() ArchivePlanHandler { |
| 25 | + return httptransport.NewHandlerWithArgs( |
| 26 | + func(ctx context.Context, r *http.Request, planID ArchivePlanParams) (ArchivePlanRequest, error) { |
| 27 | + ns, err := h.resolveNamespace(ctx) |
| 28 | + if err != nil { |
| 29 | + return ArchivePlanRequest{}, err |
| 30 | + } |
| 31 | + |
| 32 | + return ArchivePlanRequest{ |
| 33 | + NamespacedID: models.NamespacedID{ |
| 34 | + Namespace: ns, |
| 35 | + ID: planID, |
| 36 | + }, |
| 37 | + EffectiveTo: clock.Now(), |
| 38 | + }, nil |
| 39 | + }, |
| 40 | + func(ctx context.Context, request ArchivePlanRequest) (ArchivePlanResponse, error) { |
| 41 | + p, err := h.service.ArchivePlan(ctx, request) |
| 42 | + if err != nil { |
| 43 | + return ArchivePlanResponse{}, err |
| 44 | + } |
| 45 | + |
| 46 | + if p == nil { |
| 47 | + return ArchivePlanResponse{}, fmt.Errorf("failed to archive plan") |
| 48 | + } |
| 49 | + |
| 50 | + return ToAPIBillingPlan(*p) |
| 51 | + }, |
| 52 | + commonhttp.JSONResponseEncoderWithStatus[ArchivePlanResponse](http.StatusOK), |
| 53 | + httptransport.AppendOptions( |
| 54 | + h.options, |
| 55 | + httptransport.WithOperationName("archive-plan"), |
| 56 | + httptransport.WithErrorEncoder(apierrors.GenericErrorEncoder()), |
| 57 | + )..., |
| 58 | + ) |
| 59 | +} |
0 commit comments