File size: 5,269 Bytes
16cdcb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package subscriptiontestutils

import (
	"fmt"
	"testing"

	"github.com/invopop/gobl/currency"
	"github.com/samber/lo"
	"github.com/stretchr/testify/require"

	"github.com/openmeterio/openmeter/openmeter/productcatalog"
	"github.com/openmeterio/openmeter/openmeter/productcatalog/plan"
	"github.com/openmeterio/openmeter/openmeter/subscription"
	"github.com/openmeterio/openmeter/pkg/clock"
	"github.com/openmeterio/openmeter/pkg/currencyx"
	"github.com/openmeterio/openmeter/pkg/datetime"
	"github.com/openmeterio/openmeter/pkg/models"
)

// First let's add utils for building plans
type testPlanbuilder struct {
	p plan.CreatePlanInput
}

func (b *testPlanbuilder) AddPhase(dur *datetime.ISODuration, rcs ...productcatalog.RateCard) *testPlanbuilder {
	idx := len(b.p.Plan.Phases) + 1

	b.p.Plan.Phases = append(b.p.Plan.Phases, productcatalog.Phase{
		PhaseMeta: productcatalog.PhaseMeta{
			Key:         fmt.Sprintf("test_phase_%d", idx),
			Name:        fmt.Sprintf("Test Phase %d", idx),
			Description: lo.ToPtr(fmt.Sprintf("Test Phase %d Description", idx)),
			Duration:    dur,
		},
		RateCards: rcs,
	})

	return b
}

func (b *testPlanbuilder) SetMeta(meta productcatalog.PlanMeta) *testPlanbuilder {
	b.p.Plan.PlanMeta = meta
	return b
}

func (b *testPlanbuilder) Build() plan.CreatePlanInput {
	return b.p
}

func BuildTestPlanInput(t *testing.T) *testPlanbuilder {
	b := &testPlanbuilder{
		p: plan.CreatePlanInput{
			NamespacedModel: models.NamespacedModel{
				Namespace: ExampleNamespace,
			},
			Plan: productcatalog.Plan{
				PlanMeta: productcatalog.PlanMeta{
					Name:           "Test Plan",
					Key:            "test_plan",
					Version:        1,
					Currency:       currency.USD,
					BillingCadence: datetime.MustParseDuration(t, "P1M"),
					ProRatingConfig: productcatalog.ProRatingConfig{
						Enabled: true,
						Mode:    productcatalog.ProRatingModeProratePrices,
					},
					SettlementMode: productcatalog.CreditThenInvoiceSettlementMode,
				},
				Phases: []productcatalog.Phase{},
			},
		},
	}

	return b
}

type testSubscriptionSpecBuilder struct {
	s subscription.SubscriptionSpec
	t *testing.T
}

func (b *testSubscriptionSpecBuilder) AddPhase(dur *datetime.ISODuration, rcs ...productcatalog.RateCard) *testSubscriptionSpecBuilder {
	idx := len(b.s.Phases) + 1
	startAfter := datetime.ISODurationBetween(b.s.ActiveFrom, b.s.ActiveFrom)

	if idx > 1 {
		phases := b.s.GetSortedPhases()
		cad, err := b.s.GetPhaseCadence(phases[idx-1].PhaseKey)
		require.NoError(b.t, err)
		if cad.ActiveTo == nil {
			b.t.Fatalf("phase %s has no active to, cannot add new phase without specifying duration for previous", phases[idx-1].PhaseKey)
		}
		startAfter = datetime.ISODurationBetween(b.s.ActiveFrom, *cad.ActiveTo)
	}

	// Let's build the new phase
	newPhase := subscription.SubscriptionPhaseSpec{
		CreateSubscriptionPhasePlanInput: subscription.CreateSubscriptionPhasePlanInput{
			PhaseKey:    fmt.Sprintf("test_phase_%d", idx),
			Name:        fmt.Sprintf("Test Phase %d", idx),
			Description: lo.ToPtr(fmt.Sprintf("Test Phase %d Description", idx)),
			StartAfter:  startAfter,
			SortHint:    lo.ToPtr(uint8(idx)),
		},
		CreateSubscriptionPhaseCustomerInput: subscription.CreateSubscriptionPhaseCustomerInput{},
		ItemsByKey:                           make(map[string][]*subscription.SubscriptionItemSpec),
	}

	// Let's add the RateCards as Items
	for _, rc := range rcs {
		newPhase.ItemsByKey[rc.Key()] = append(newPhase.ItemsByKey[rc.Key()], &subscription.SubscriptionItemSpec{
			CreateSubscriptionItemInput: subscription.CreateSubscriptionItemInput{
				CreateSubscriptionItemPlanInput: subscription.CreateSubscriptionItemPlanInput{
					PhaseKey: newPhase.PhaseKey,
					ItemKey:  rc.Key(),
					RateCard: rc,
				},
				CreateSubscriptionItemCustomerInput: subscription.CreateSubscriptionItemCustomerInput{},
			},
		})
	}

	for key, items := range newPhase.ItemsByKey {
		if len(items) > 1 {
			b.t.Fatalf("multiple ratecards specified with same key %s", key)
		}
	}

	b.s.Phases[newPhase.PhaseKey] = &newPhase

	return b
}

func (b *testSubscriptionSpecBuilder) Build() (subscription.SubscriptionSpec, error) {
	spec := b.s

	if err := spec.SyncAnnotations(); err != nil {
		return spec, fmt.Errorf("failed to sync annotations: %w", err)
	}

	if err := spec.Validate(); err != nil {
		return spec, fmt.Errorf("failed to validate spec: %w", err)
	}

	return spec, nil
}

func BuildTestSubscriptionSpec(t *testing.T) *testSubscriptionSpecBuilder {
	now := clock.Now()

	return &testSubscriptionSpecBuilder{
		s: subscription.SubscriptionSpec{
			CreateSubscriptionPlanInput: subscription.CreateSubscriptionPlanInput{
				Plan: &subscription.PlanRef{
					Key:     "test_plan",
					Version: 1,
				},
				BillingCadence: datetime.MustParseDuration(t, "P1M"),
				SettlementMode: productcatalog.CreditThenInvoiceSettlementMode,
			},
			CreateSubscriptionCustomerInput: subscription.CreateSubscriptionCustomerInput{
				CustomerId:    "01K6JCPG631MH1EKEQB2YMDBJW",
				ActiveFrom:    now,
				ActiveTo:      nil,
				Name:          "test_subscription",
				BillingAnchor: now,
				Currency:      currencyx.Code(currency.USD),
			},
			Phases: make(map[string]*subscription.SubscriptionPhaseSpec),
		},
		t: t,
	}
}