@@ -13,6 +13,7 @@ import (
1313 "github.com/openmeterio/openmeter/openmeter/billing/charges/models/payment"
1414 "github.com/openmeterio/openmeter/openmeter/billing/creditgrant"
1515 "github.com/openmeterio/openmeter/openmeter/ledger"
16+ "github.com/openmeterio/openmeter/openmeter/ledger/customerbalance"
1617 "github.com/openmeterio/openmeter/openmeter/productcatalog"
1718 "github.com/openmeterio/openmeter/pkg/currencyx"
1819 "github.com/openmeterio/openmeter/pkg/models"
@@ -307,3 +308,104 @@ func convertBalance(currency currencyx.Code, balance ledger.Balance) api.CreditB
307308 Pending : balance .Pending ().String (),
308309 }
309310}
311+
312+ func fromAPIBillingCreditTransactionType (filter * api.BillingCreditTransactionType ) * customerbalance.CreditTransactionType {
313+ if filter == nil {
314+ return nil
315+ }
316+
317+ var txType customerbalance.CreditTransactionType
318+ switch * filter {
319+ case api .BillingCreditTransactionTypeFunded :
320+ txType = customerbalance .CreditTransactionTypeFunded
321+ case api .BillingCreditTransactionTypeConsumed :
322+ txType = customerbalance .CreditTransactionTypeConsumed
323+ case api .BillingCreditTransactionTypeAdjusted :
324+ txType = customerbalance .CreditTransactionTypeAdjusted
325+ default :
326+ return nil
327+ }
328+
329+ return & txType
330+ }
331+
332+ func toAPIBillingCreditTransactions (items []customerbalance.CreditTransaction ) []api.BillingCreditTransaction {
333+ out := make ([]api.BillingCreditTransaction , 0 , len (items ))
334+
335+ for _ , item := range items {
336+ out = append (out , toAPIBillingCreditTransaction (item ))
337+ }
338+
339+ return out
340+ }
341+
342+ func toAPIBillingCreditTransaction (tx customerbalance.CreditTransaction ) api.BillingCreditTransaction {
343+ apiTx := api.BillingCreditTransaction {
344+ Id : tx .ID .ID ,
345+ CreatedAt : & tx .CreatedAt ,
346+ BookedAt : tx .BookedAt ,
347+ Type : toAPIBillingCreditTransactionType (tx .Type ),
348+ Currency : api .BillingCurrencyCode (tx .Currency ),
349+ Amount : tx .Amount .String (),
350+ Name : tx .Name ,
351+ AvailableBalance : struct {
352+ After api.Numeric `json:"after"`
353+ Before api.Numeric `json:"before"`
354+ }{
355+ Before : tx .Balance .Before .String (),
356+ After : tx .Balance .After .String (),
357+ },
358+ }
359+
360+ labels := creditTransactionLabels (tx .Annotations )
361+ if len (labels ) > 0 {
362+ apiLabels := api .Labels (labels )
363+ apiTx .Labels = & apiLabels
364+ }
365+
366+ return apiTx
367+ }
368+
369+ func toAPIBillingCreditTransactionType (txType customerbalance.CreditTransactionType ) api.BillingCreditTransactionType {
370+ switch txType {
371+ case customerbalance .CreditTransactionTypeFunded :
372+ return api .BillingCreditTransactionTypeFunded
373+ case customerbalance .CreditTransactionTypeConsumed :
374+ return api .BillingCreditTransactionTypeConsumed
375+ default :
376+ return api .BillingCreditTransactionTypeAdjusted
377+ }
378+ }
379+
380+ func creditTransactionLabels (annotations models.Annotations ) map [string ]string {
381+ labels := make (map [string ]string )
382+
383+ setLabel := func (key , annotationKey string ) {
384+ value := stringAnnotation (annotations , annotationKey )
385+ if value != "" {
386+ labels [key ] = value
387+ }
388+ }
389+
390+ setLabel ("charge_id" , ledger .AnnotationChargeID )
391+ setLabel ("subscription_id" , ledger .AnnotationSubscriptionID )
392+ setLabel ("subscription_phase_id" , ledger .AnnotationSubscriptionPhaseID )
393+ setLabel ("subscription_item_id" , ledger .AnnotationSubscriptionItemID )
394+ setLabel ("feature_id" , ledger .AnnotationFeatureID )
395+
396+ return labels
397+ }
398+
399+ func stringAnnotation (annotations models.Annotations , key string ) string {
400+ raw , ok := annotations [key ]
401+ if ! ok {
402+ return ""
403+ }
404+
405+ value , ok := raw .(string )
406+ if ! ok {
407+ return ""
408+ }
409+
410+ return value
411+ }
0 commit comments