File size: 8,186 Bytes
429334c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package httpdriver

import (
	"context"
	"fmt"
	"net/http"

	"github.com/samber/lo"

	"github.com/openmeterio/openmeter/api"
	"github.com/openmeterio/openmeter/openmeter/app"
	appcustominvoicing "github.com/openmeterio/openmeter/openmeter/app/custominvoicing"
	appsandbox "github.com/openmeterio/openmeter/openmeter/app/sandbox"
	appstripe "github.com/openmeterio/openmeter/openmeter/app/stripe"
	"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
	"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport"
	"github.com/openmeterio/openmeter/pkg/models"
	"github.com/openmeterio/openmeter/pkg/pagination"
)

// ListAppsHandler is a handler for listing apps
type (
	ListAppsRequest  = app.ListAppInput
	ListAppsResponse = api.AppPaginatedResponse
	ListAppsParams   = api.ListAppsParams
	ListAppsHandler  httptransport.HandlerWithArgs[ListAppsRequest, ListAppsResponse, ListAppsParams]
)

// ListApps returns a handler for listing apps
func (h *handler) ListApps() ListAppsHandler {
	return httptransport.NewHandlerWithArgs(
		func(ctx context.Context, r *http.Request, params ListAppsParams) (ListAppsRequest, error) {
			// Resolve namespace
			namespace, err := h.resolveNamespace(ctx)
			if err != nil {
				return ListAppsRequest{}, fmt.Errorf("failed to resolve namespace: %w", err)
			}

			return ListAppsRequest{
				Namespace: namespace,
				Page: pagination.Page{
					PageSize:   lo.FromPtrOr(params.PageSize, app.DefaultPageSize),
					PageNumber: lo.FromPtrOr(params.Page, app.DefaultPageNumber),
				},
			}, nil
		},
		func(ctx context.Context, request ListAppsRequest) (ListAppsResponse, error) {
			result, err := h.service.ListApps(ctx, request)
			if err != nil {
				return ListAppsResponse{}, fmt.Errorf("failed to list apps: %w", err)
			}

			items := make([]api.App, 0, len(result.Items))
			for _, item := range result.Items {
				app, err := MapAppToAPI(item)
				if err != nil {
					return ListAppsResponse{}, fmt.Errorf("failed to map app to api: %w", err)
				}

				items = append(items, app)
			}

			return ListAppsResponse{
				Page:       result.Page.PageNumber,
				PageSize:   result.Page.PageSize,
				TotalCount: result.TotalCount,
				Items:      items,
			}, nil
		},
		commonhttp.JSONResponseEncoderWithStatus[ListAppsResponse](http.StatusOK),
		httptransport.AppendOptions(
			h.options,
			httptransport.WithOperationName("listApps"),
		)...,
	)
}

// GetAppHandler is a handler to get an app by id
type (
	GetAppRequest  = app.GetAppInput
	GetAppResponse = api.App
	GetAppHandler  httptransport.HandlerWithArgs[GetAppRequest, GetAppResponse, string]
)

// GetApp returns an app handler
func (h *handler) GetApp() GetAppHandler {
	return httptransport.NewHandlerWithArgs(
		func(ctx context.Context, r *http.Request, appId string) (GetAppRequest, error) {
			// Resolve namespace
			namespace, err := h.resolveNamespace(ctx)
			if err != nil {
				return GetAppRequest{}, fmt.Errorf("failed to resolve namespace: %w", err)
			}

			return GetAppRequest{
				Namespace: namespace,
				ID:        appId,
			}, nil
		},
		func(ctx context.Context, request GetAppRequest) (GetAppResponse, error) {
			app, err := h.service.GetApp(ctx, request)
			if err != nil {
				return GetAppResponse{}, fmt.Errorf("failed to get app: %w", err)
			}

			return MapAppToAPI(app)
		},
		commonhttp.JSONResponseEncoderWithStatus[GetAppResponse](http.StatusOK),
		httptransport.AppendOptions(
			h.options,
			httptransport.WithOperationName("getApp"),
		)...,
	)
}

// UpdateAppHandler is a handler to update an app
type (
	UpdateAppRequest  = app.UpdateAppInput
	UpdateAppResponse = api.App
	UpdateAppHandler  httptransport.HandlerWithArgs[UpdateAppRequest, UpdateAppResponse, string]
)

// UpdateApp returns an app handler
func (h *handler) UpdateApp() UpdateAppHandler {
	return httptransport.NewHandlerWithArgs(
		func(ctx context.Context, r *http.Request, appId string) (UpdateAppRequest, error) {
			// Resolve namespace
			namespace, err := h.resolveNamespace(ctx)
			if err != nil {
				return UpdateAppRequest{}, fmt.Errorf("failed to resolve namespace: %w", err)
			}

			var body api.UpdateAppJSONRequestBody
			if err := commonhttp.JSONRequestBodyDecoder(r, &body); err != nil {
				return UpdateAppRequest{}, fmt.Errorf("field to decode upsert customer data request: %w", err)
			}

			updateType, err := body.Discriminator()
			if err != nil {
				return UpdateAppRequest{}, models.NewGenericValidationError(fmt.Errorf("failed to get update type: %w", err))
			}

			switch updateType {
			case string(app.AppTypeStripe):
				payload, err := body.AsStripeAppReplaceUpdate()
				if err != nil {
					return UpdateAppRequest{}, fmt.Errorf("failed to get stripe app replace update: %w", err)
				}

				return UpdateAppRequest{
					AppID: app.AppID{
						ID:        appId,
						Namespace: namespace,
					},
					Name:        payload.Name,
					Description: payload.Description,
					Metadata:    payload.Metadata,
					AppConfigUpdate: appstripe.Configuration{
						SecretAPIKey: payload.SecretAPIKey,
					},
				}, nil

			case string(app.AppTypeSandbox):
				payload, err := body.AsSandboxAppReplaceUpdate()
				if err != nil {
					return UpdateAppRequest{}, fmt.Errorf("failed to get sandbox app replace update: %w", err)
				}

				return UpdateAppRequest{
					AppID: app.AppID{
						ID:        appId,
						Namespace: namespace,
					},
					Name:            payload.Name,
					Description:     payload.Description,
					Metadata:        payload.Metadata,
					AppConfigUpdate: appsandbox.Configuration{},
				}, nil
			case string(app.AppTypeCustomInvoicing):
				payload, err := body.AsCustomInvoicingAppReplaceUpdate()
				if err != nil {
					return UpdateAppRequest{}, fmt.Errorf("failed to get custom invoicing app replace update: %w", err)
				}

				return UpdateAppRequest{
					AppID: app.AppID{
						ID:        appId,
						Namespace: namespace,
					},
					Name:        payload.Name,
					Description: payload.Description,
					Metadata:    payload.Metadata,
					AppConfigUpdate: appcustominvoicing.Configuration{
						EnableDraftSyncHook:   payload.EnableDraftSyncHook,
						EnableIssuingSyncHook: payload.EnableIssuingSyncHook,
					},
				}, nil
			default:
				return UpdateAppRequest{}, models.NewGenericValidationError(fmt.Errorf("invalid app type: %s", updateType))
			}
		},
		func(ctx context.Context, request UpdateAppRequest) (UpdateAppResponse, error) {
			app, err := h.service.UpdateApp(ctx, request)
			if err != nil {
				return UpdateAppResponse{}, fmt.Errorf("failed to update app: %w", err)
			}

			return MapAppToAPI(app)
		},
		commonhttp.JSONResponseEncoderWithStatus[UpdateAppResponse](http.StatusOK),
		httptransport.AppendOptions(
			h.options,
			httptransport.WithOperationName("updateApp"),
		)...,
	)
}

// UninstallAppHandler is a handler to uninstalls an app by id
type (
	UninstallAppRequest  = app.UninstallAppInput
	UninstallAppResponse = interface{}
	UninstallAppHandler  httptransport.HandlerWithArgs[UninstallAppRequest, UninstallAppResponse, string]
)

// UninstallApp uninstalls an app
func (h *handler) UninstallApp() UninstallAppHandler {
	return httptransport.NewHandlerWithArgs(
		func(ctx context.Context, r *http.Request, appId string) (UninstallAppRequest, error) {
			// Resolve namespace
			namespace, err := h.resolveNamespace(ctx)
			if err != nil {
				return UninstallAppRequest{}, fmt.Errorf("failed to resolve namespace: %w", err)
			}

			return UninstallAppRequest{
				Namespace: namespace,
				ID:        appId,
			}, nil
		},
		func(ctx context.Context, request UninstallAppRequest) (UninstallAppResponse, error) {
			// Check if the app is not used by any billing profile

			if err := h.billingService.IsAppUsed(ctx, request); err != nil {
				return nil, err
			}

			// Uninstall app
			err := h.service.UninstallApp(ctx, request)
			if err != nil {
				return nil, fmt.Errorf("failed to uninstall app: %w", err)
			}

			return nil, nil
		},
		commonhttp.EmptyResponseEncoder[UninstallAppResponse](http.StatusNoContent),
		httptransport.AppendOptions(
			h.options,
			httptransport.WithOperationName("uninstallApp"),
		)...,
	)
}