Skip to content

Commit 4afc0cc

Browse files
authored
feat(api): plan and planaddon handlers (#4041)
1 parent dfad7ca commit 4afc0cc

20 files changed

Lines changed: 3019 additions & 12 deletions

File tree

api/v3/handlers/plans/archive.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)