File size: 7,240 Bytes
1c4c66b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package httpdriver

import (
	"github.com/samber/lo"

	"github.com/openmeterio/openmeter/api"
	"github.com/openmeterio/openmeter/openmeter/customer"
	"github.com/openmeterio/openmeter/openmeter/entitlement"
	entitlementdriver "github.com/openmeterio/openmeter/openmeter/entitlement/driver"
	meteredentitlement "github.com/openmeterio/openmeter/openmeter/entitlement/metered"
	subscriptionhttp "github.com/openmeterio/openmeter/openmeter/productcatalog/subscription/http"
	"github.com/openmeterio/openmeter/openmeter/subscription"
	"github.com/openmeterio/openmeter/pkg/currencyx"
	"github.com/openmeterio/openmeter/pkg/models"
)

type EntitlementValueV2 struct {
	HasAccess                 bool                `json:"hasAccess,omitempty"`
	Balance                   *float64            `json:"balance,omitempty"`
	Config                    *string             `json:"config,omitempty"`
	Overage                   *float64            `json:"overage,omitempty"`
	TotalAvailableGrantAmount *float64            `json:"totalAvailableGrantAmount,omitempty"`
	Usage                     *float64            `json:"usage,omitempty"`
	GrantBalances             *map[string]float64 `json:"grantBalances,omitempty"`
}

type CustomerAccessV2 struct {
	Entitlements map[string]EntitlementValueV2 `json:"entitlements"`
}

func MapCustomerCreate(body api.CustomerCreate) customer.CustomerMutate {
	var metadata *models.Metadata

	if body.Metadata != nil {
		metadata = lo.ToPtr(models.NewMetadata(*body.Metadata))
	}

	mut := customer.CustomerMutate{
		Key:            body.Key,
		Name:           body.Name,
		Description:    body.Description,
		PrimaryEmail:   body.PrimaryEmail,
		BillingAddress: MapAddress(body.BillingAddress),
		Currency:       mapCurrency(body.Currency),
		Metadata:       metadata,
	}

	if body.UsageAttribution != nil {
		mut.UsageAttribution = &customer.CustomerUsageAttribution{
			SubjectKeys: body.UsageAttribution.SubjectKeys,
		}
	}

	return mut
}

func MapCustomerReplaceUpdate(body api.CustomerReplaceUpdate) customer.CustomerMutate {
	var metadata *models.Metadata

	if body.Metadata != nil {
		metadata = lo.ToPtr(models.NewMetadata(*body.Metadata))
	}

	mut := customer.CustomerMutate{
		Key:            body.Key,
		Name:           body.Name,
		Description:    body.Description,
		PrimaryEmail:   body.PrimaryEmail,
		BillingAddress: MapAddress(body.BillingAddress),
		Currency:       mapCurrency(body.Currency),
		Metadata:       metadata,
	}

	if body.UsageAttribution != nil {
		mut.UsageAttribution = &customer.CustomerUsageAttribution{
			SubjectKeys: body.UsageAttribution.SubjectKeys,
		}
	}

	return mut
}

func mapCurrency(apiCurrency *string) *currencyx.Code {
	if apiCurrency == nil {
		return nil
	}

	return lo.ToPtr(currencyx.Code(*apiCurrency))
}

func MapAddress(apiAddress *api.Address) *models.Address {
	if apiAddress == nil {
		return nil
	}

	address := models.Address{
		City:        apiAddress.City,
		State:       apiAddress.State,
		PostalCode:  apiAddress.PostalCode,
		Line1:       apiAddress.Line1,
		Line2:       apiAddress.Line2,
		PhoneNumber: apiAddress.PhoneNumber,
	}

	if apiAddress.Country != nil {
		address.Country = lo.ToPtr(models.CountryCode(*apiAddress.Country))
	}

	return &address
}

func FromMetadata(metadata models.Metadata) *api.Metadata {
	if len(metadata) == 0 {
		return nil
	}

	result := make(api.Metadata)
	if len(metadata) > 0 {
		for k, v := range metadata {
			result[k] = v
		}
	}

	return &result
}

func FromAnnotations(annotations models.Annotations) *api.Annotations {
	if len(annotations) == 0 {
		return nil
	}

	result := make(api.Annotations)
	if len(annotations) > 0 {
		for k, v := range annotations {
			result[k] = v
		}
	}

	return &result
}

// CustomerToAPI converts a Customer to an API Customer
func CustomerToAPI(c customer.Customer, subscriptions []subscription.Subscription, expand customer.Expands) (api.Customer, error) {
	// Map the customer to the API Customer
	apiCustomer := api.Customer{
		Id:           c.ManagedResource.ID,
		Key:          c.Key,
		Name:         c.Name,
		PrimaryEmail: c.PrimaryEmail,
		Description:  c.Description,
		CreatedAt:    c.CreatedAt,
		UpdatedAt:    c.UpdatedAt,
		DeletedAt:    c.DeletedAt,
		Metadata:     FromMetadata(lo.FromPtr(c.Metadata)),
		Annotations:  FromAnnotations(lo.FromPtr(c.Annotation)),
	}

	if c.UsageAttribution != nil {
		apiCustomer.UsageAttribution = &api.CustomerUsageAttribution{SubjectKeys: c.UsageAttribution.SubjectKeys}
	}

	if c.BillingAddress != nil {
		address := api.Address{
			City:        c.BillingAddress.City,
			State:       c.BillingAddress.State,
			PostalCode:  c.BillingAddress.PostalCode,
			Line1:       c.BillingAddress.Line1,
			Line2:       c.BillingAddress.Line2,
			PhoneNumber: c.BillingAddress.PhoneNumber,
		}

		if c.BillingAddress.Country != nil {
			address.Country = lo.ToPtr(string(*c.BillingAddress.Country))
		}

		apiCustomer.BillingAddress = &address
	}

	if c.Currency != nil {
		apiCustomer.Currency = lo.ToPtr(string(*c.Currency))
	}

	// Map the subscriptions to the API Subscriptions
	if len(subscriptions) > 0 {
		// Let's find the active one
		// FIXME: this will only work with single subscription per customer
		apiCustomer.CurrentSubscriptionId = lo.ToPtr(subscriptions[0].ID)

		// Map the subscriptions to the API Subscriptions if the expand is set
		if lo.Contains(expand, customer.ExpandSubscriptions) {
			apiCustomer.Subscriptions = lo.ToPtr(lo.Map(subscriptions, func(s subscription.Subscription, _ int) api.Subscription {
				return subscriptionhttp.MapSubscriptionToAPI(s)
			}))
		}
	}

	return apiCustomer, nil
}

func MapAccessToAPI(access entitlement.Access) (api.CustomerAccess, error) {
	entitlements := make(map[string]api.EntitlementValue)

	for fKey, v := range access.Entitlements {
		apiVal, err := entitlementdriver.MapEntitlementValueToAPI(v.Value)
		if err != nil {
			return api.CustomerAccess{}, err
		}

		entitlements[fKey] = apiVal
	}

	return api.CustomerAccess{
		Entitlements: entitlements,
	}, nil
}

func MapEntitlementValueToAPIV2(v entitlement.EntitlementValue) (EntitlementValueV2, error) {
	base, err := entitlementdriver.MapEntitlementValueToAPI(v)
	if err != nil {
		return EntitlementValueV2{}, err
	}

	result := EntitlementValueV2{
		HasAccess:                 base.HasAccess,
		Balance:                   base.Balance,
		Config:                    base.Config,
		Overage:                   base.Overage,
		TotalAvailableGrantAmount: base.TotalAvailableGrantAmount,
		Usage:                     base.Usage,
	}

	if metered, ok := v.(*meteredentitlement.MeteredEntitlementValue); ok {
		copied := make(map[string]float64, len(metered.GrantBalances))
		for k, val := range metered.GrantBalances {
			copied[k] = val
		}
		result.GrantBalances = &copied
	}

	return result, nil
}

func MapAccessToAPIV2(access entitlement.Access) (CustomerAccessV2, error) {
	entitlements := make(map[string]EntitlementValueV2, len(access.Entitlements))

	for fKey, v := range access.Entitlements {
		apiVal, err := MapEntitlementValueToAPIV2(v.Value)
		if err != nil {
			return CustomerAccessV2{}, err
		}

		entitlements[fKey] = apiVal
	}

	return CustomerAccessV2{
		Entitlements: entitlements,
	}, nil
}