|
| 1 | +package subscriptionaddons |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + apiv3 "github.com/openmeterio/openmeter/api/v3" |
| 8 | + "github.com/openmeterio/openmeter/api/v3/apierrors" |
| 9 | + "github.com/openmeterio/openmeter/api/v3/request" |
| 10 | + subscriptionaddon "github.com/openmeterio/openmeter/openmeter/subscription/addon" |
| 11 | + subscriptionworkflow "github.com/openmeterio/openmeter/openmeter/subscription/workflow" |
| 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 | + "github.com/samber/lo" |
| 16 | +) |
| 17 | + |
| 18 | +type ( |
| 19 | + CreateSubscriptionAddonRequest = struct { |
| 20 | + SubscriptionID models.NamespacedID |
| 21 | + AddonInput subscriptionworkflow.AddAddonWorkflowInput |
| 22 | + } |
| 23 | + CreateSubscriptionAddonResponse = apiv3.SubscriptionAddon |
| 24 | + CreateSubscriptionAddonParams = string |
| 25 | + CreateSubscriptionAddonHandler = httptransport.HandlerWithArgs[CreateSubscriptionAddonRequest, CreateSubscriptionAddonResponse, CreateSubscriptionAddonParams] |
| 26 | +) |
| 27 | + |
| 28 | +func (h *handler) CreateSubscriptionAddon() CreateSubscriptionAddonHandler { |
| 29 | + return httptransport.NewHandlerWithArgs( |
| 30 | + func(ctx context.Context, r *http.Request, subscriptionID CreateSubscriptionAddonParams) (CreateSubscriptionAddonRequest, error) { |
| 31 | + body := apiv3.CreateSubscriptionAddonRequest{} |
| 32 | + if err := request.ParseBody(r, &body); err != nil { |
| 33 | + return CreateSubscriptionAddonRequest{}, err |
| 34 | + } |
| 35 | + |
| 36 | + ns, err := h.resolveNamespace(ctx) |
| 37 | + if err != nil { |
| 38 | + return CreateSubscriptionAddonRequest{}, err |
| 39 | + } |
| 40 | + |
| 41 | + addonInput, err := mapCreateSubscriptionAddonRequestToInput(body) |
| 42 | + if err != nil { |
| 43 | + return CreateSubscriptionAddonRequest{}, err |
| 44 | + } |
| 45 | + |
| 46 | + return CreateSubscriptionAddonRequest{ |
| 47 | + SubscriptionID: models.NamespacedID{ |
| 48 | + Namespace: ns, |
| 49 | + ID: subscriptionID, |
| 50 | + }, |
| 51 | + AddonInput: addonInput, |
| 52 | + }, nil |
| 53 | + }, |
| 54 | + func(ctx context.Context, request CreateSubscriptionAddonRequest) (CreateSubscriptionAddonResponse, error) { |
| 55 | + subsAdds, err := h.addonService.List(ctx, request.SubscriptionID.Namespace, subscriptionaddon.ListSubscriptionAddonsInput{ |
| 56 | + SubscriptionID: request.SubscriptionID.ID, |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + return CreateSubscriptionAddonResponse{}, err |
| 60 | + } |
| 61 | + |
| 62 | + var added subscriptionaddon.SubscriptionAddon |
| 63 | + |
| 64 | + // If the addon is already present, we'll change the quantity instead as a convenience |
| 65 | + if sAdd, ok := lo.Find(subsAdds.Items, func(subAdd subscriptionaddon.SubscriptionAddon) bool { |
| 66 | + return subAdd.Addon.ID == request.AddonInput.AddonID |
| 67 | + }); ok { |
| 68 | + _, added, err = h.SubscriptionWorkflowService.ChangeAddonQuantity(ctx, request.SubscriptionID, subscriptionworkflow.ChangeAddonQuantityWorkflowInput{ |
| 69 | + SubscriptionAddonID: sAdd.NamespacedID, |
| 70 | + Quantity: request.AddonInput.InitialQuantity, |
| 71 | + Timing: request.AddonInput.Timing, |
| 72 | + }) |
| 73 | + } else { |
| 74 | + // Otherwise, we'll create it as per usual |
| 75 | + _, added, err = h.SubscriptionWorkflowService.AddAddon(ctx, request.SubscriptionID, request.AddonInput) |
| 76 | + } |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + return CreateSubscriptionAddonResponse{}, err |
| 80 | + } |
| 81 | + |
| 82 | + return toAPISubscriptionAddon(added) |
| 83 | + }, |
| 84 | + commonhttp.JSONResponseEncoderWithStatus[CreateSubscriptionAddonResponse](http.StatusCreated), |
| 85 | + httptransport.AppendOptions( |
| 86 | + h.options, |
| 87 | + httptransport.WithOperationName("create-plan-addon"), |
| 88 | + httptransport.WithErrorEncoder(apierrors.GenericErrorEncoder()), |
| 89 | + )..., |
| 90 | + ) |
| 91 | +} |
0 commit comments