File size: 4,964 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
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
package ledger

import (
	"fmt"

	"github.com/openmeterio/openmeter/pkg/models"
)

const (
	AnnotationChargeNamespace     = "ledger.charge.namespace"
	AnnotationChargeID            = "ledger.charge.id"
	AnnotationSubscriptionID      = "ledger.subscription.id"
	AnnotationSubscriptionPhaseID = "ledger.subscription.phase.id"
	AnnotationSubscriptionItemID  = "ledger.subscription.item.id"
	AnnotationFeatureID           = "ledger.feature.id"

	AnnotationTransactionTemplateCode = "ledger.transaction.template_code"
	AnnotationTransactionDirection    = "ledger.transaction.direction"
	AnnotationCollectionType          = "ledger.collection.type"
	AnnotationCollectionSourceOrder   = "ledger.collection.source_order"
	AnnotationBreakageKind            = "ledger.breakage.kind"
	AnnotationBreakageRecordID        = "ledger.breakage.record_id"
	AnnotationBreakagePlanID          = "ledger.breakage.plan_id"
)

type ChargeTransactionAnnotationsInput struct {
	ChargeID models.NamespacedID

	SubscriptionID      *string
	SubscriptionPhaseID *string
	SubscriptionItemID  *string
	FeatureID           *string
}

type TransactionDirection string

const (
	TransactionDirectionForward    TransactionDirection = "forward"
	TransactionDirectionCorrection TransactionDirection = "correction"
)

const CollectionTypeBreakage = "breakage"

type BreakageKind string

const (
	BreakageKindPlan    BreakageKind = "plan"
	BreakageKindRelease BreakageKind = "release"
	BreakageKindReopen  BreakageKind = "reopen"
)

func (BreakageKind) Values() []string {
	return []string{
		string(BreakageKindPlan),
		string(BreakageKindRelease),
		string(BreakageKindReopen),
	}
}

type BreakageSourceKind string

const (
	BreakageSourceKindCreditPurchase           BreakageSourceKind = "credit_purchase"
	BreakageSourceKindUsage                    BreakageSourceKind = "usage"
	BreakageSourceKindUsageCorrection          BreakageSourceKind = "usage_correction"
	BreakageSourceKindCreditPurchaseCorrection BreakageSourceKind = "credit_purchase_correction"
	BreakageSourceKindAdvanceBackfill          BreakageSourceKind = "advance_backfill"
)

func (BreakageSourceKind) Values() []string {
	return []string{
		string(BreakageSourceKindCreditPurchase),
		string(BreakageSourceKindUsage),
		string(BreakageSourceKindUsageCorrection),
		string(BreakageSourceKindCreditPurchaseCorrection),
		string(BreakageSourceKindAdvanceBackfill),
	}
}

func ChargeAnnotations(chargeID models.NamespacedID) models.Annotations {
	return models.Annotations{
		AnnotationChargeNamespace: chargeID.Namespace,
		AnnotationChargeID:        chargeID.ID,
	}
}

func ChargeTransactionAnnotations(input ChargeTransactionAnnotationsInput) models.Annotations {
	annotations := ChargeAnnotations(input.ChargeID)

	if input.SubscriptionID != nil && *input.SubscriptionID != "" {
		annotations[AnnotationSubscriptionID] = *input.SubscriptionID
	}

	if input.SubscriptionPhaseID != nil && *input.SubscriptionPhaseID != "" {
		annotations[AnnotationSubscriptionPhaseID] = *input.SubscriptionPhaseID
	}

	if input.SubscriptionItemID != nil && *input.SubscriptionItemID != "" {
		annotations[AnnotationSubscriptionItemID] = *input.SubscriptionItemID
	}

	if input.FeatureID != nil && *input.FeatureID != "" {
		annotations[AnnotationFeatureID] = *input.FeatureID
	}

	return annotations
}

func TransactionAnnotations(templateCode string, direction TransactionDirection) models.Annotations {
	return models.Annotations{
		AnnotationTransactionTemplateCode: templateCode,
		AnnotationTransactionDirection:    string(direction),
	}
}

func BreakageAnnotations(kind BreakageKind, recordID string, planID *string) models.Annotations {
	annotations := models.Annotations{
		AnnotationCollectionType:   CollectionTypeBreakage,
		AnnotationBreakageKind:     string(kind),
		AnnotationBreakageRecordID: recordID,
	}

	if planID != nil && *planID != "" {
		annotations[AnnotationBreakagePlanID] = *planID
	}

	return annotations
}

func TransactionTemplateCodeFromAnnotations(annotations models.Annotations) (string, error) {
	raw, ok := annotations[AnnotationTransactionTemplateCode]
	if !ok {
		return "", fmt.Errorf("transaction template code annotation is required")
	}

	code, ok := raw.(string)
	if !ok || code == "" {
		return "", fmt.Errorf("transaction template code annotation is invalid")
	}

	return code, nil
}

func TransactionDirectionFromAnnotations(annotations models.Annotations) (TransactionDirection, error) {
	raw, ok := annotations[AnnotationTransactionDirection]
	if !ok {
		return "", fmt.Errorf("transaction direction annotation is required")
	}

	value, ok := raw.(string)
	if !ok || value == "" {
		return "", fmt.Errorf("transaction direction annotation is invalid")
	}

	direction := TransactionDirection(value)
	switch direction {
	case TransactionDirectionForward, TransactionDirectionCorrection:
		return direction, nil
	default:
		return "", fmt.Errorf("invalid transaction direction annotation %q", value)
	}
}