File size: 2,044 Bytes
cee2387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package chargeadapter

import (
	"github.com/samber/lo"

	chargecreditpurchase "github.com/openmeterio/openmeter/openmeter/billing/charges/creditpurchase"
	chargeflatfee "github.com/openmeterio/openmeter/openmeter/billing/charges/flatfee"
	"github.com/openmeterio/openmeter/openmeter/billing/charges/meta"
	chargeusagebased "github.com/openmeterio/openmeter/openmeter/billing/charges/usagebased"
	"github.com/openmeterio/openmeter/openmeter/ledger"
	"github.com/openmeterio/openmeter/pkg/models"
)

func chargeAnnotationsForCreditPurchaseCharge(charge chargecreditpurchase.Charge) models.Annotations {
	return chargeTransactionAnnotations(
		models.NamespacedID{
			Namespace: charge.Namespace,
			ID:        charge.ID,
		},
		charge.Intent.Subscription,
		nil,
	)
}

func chargeAnnotationsForFlatFeeCharge(charge chargeflatfee.Charge) models.Annotations {
	return chargeTransactionAnnotations(
		models.NamespacedID{
			Namespace: charge.Namespace,
			ID:        charge.ID,
		},
		charge.Intent.GetSubscription(),
		charge.State.FeatureID,
	)
}

func chargeAnnotationsForUsageBasedCharge(charge chargeusagebased.Charge) models.Annotations {
	return chargeTransactionAnnotations(
		models.NamespacedID{
			Namespace: charge.Namespace,
			ID:        charge.ID,
		},
		charge.Intent.GetSubscription(),
		lo.EmptyableToPtr(charge.State.FeatureID),
	)
}

func chargeTransactionAnnotations(chargeID models.NamespacedID, subscription *meta.SubscriptionReference, featureID *string) models.Annotations {
	var subscriptionID *string
	var subscriptionPhaseID *string
	var subscriptionItemID *string

	if subscription != nil {
		subscriptionID = &subscription.SubscriptionID
		subscriptionPhaseID = &subscription.PhaseID
		subscriptionItemID = &subscription.ItemID
	}

	return ledger.ChargeTransactionAnnotations(ledger.ChargeTransactionAnnotationsInput{
		ChargeID:            chargeID,
		SubscriptionID:      subscriptionID,
		SubscriptionPhaseID: subscriptionPhaseID,
		SubscriptionItemID:  subscriptionItemID,
		FeatureID:           featureID,
	})
}