File size: 2,232 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
package subscription

import (
	"errors"
	"fmt"

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

type PlanRef struct {
	Id      string `json:"id"`
	Key     string `json:"key"`
	Version int    `json:"version"`
}

func (p PlanRef) GetPath() SpecPath {
	return SpecPath(fmt.Sprintf("%s/%d", p.Key, p.Version))
}

func (p PlanRef) Equal(p2 PlanRef) bool {
	if p.Id != p2.Id {
		return false
	}
	if p.Key != p2.Key {
		return false
	}
	if p.Version != p2.Version {
		return false
	}
	return true
}

func (p *PlanRef) NilEqual(p2 *PlanRef) bool {
	if p == nil && p2 == nil {
		return true
	}
	if p != nil && p2 != nil {
		return p.Equal(*p2)
	}

	return false
}

// All methods are expected to return stable values.
type PlanRateCard interface {
	ToCreateSubscriptionItemPlanInput() CreateSubscriptionItemPlanInput
	GetKey() string
}

// All methods are expected to return stable values.
type PlanPhase interface {
	ToCreateSubscriptionPhasePlanInput() CreateSubscriptionPhasePlanInput
	GetRateCards() []PlanRateCard
	GetKey() string
}

// All methods are expected to return stable values.
type Plan interface {
	ToCreateSubscriptionPlanInput() CreateSubscriptionPlanInput

	GetName() string

	// Phases are expected to be returned in the order they activate.
	GetPhases() []PlanPhase

	// Will not make sense on the long term
	Currency() currencyx.Code
}

// NewPlanNotFoundError returns a new PlanNotFoundError.
func NewPlanNotFoundError(key string, version int) error {
	return &PlanNotFoundError{
		err: models.NewGenericNotFoundError(
			fmt.Errorf("plan %s with version %d not found", key, version),
		),
	}
}

var _ models.GenericError = &PlanNotFoundError{}

// PlanNotFoundError is returned when a meter is not found.
type PlanNotFoundError struct {
	err error
}

// Error returns the error message.
func (e *PlanNotFoundError) Error() string {
	return e.err.Error()
}

// Unwrap returns the wrapped error.
func (e *PlanNotFoundError) Unwrap() error {
	return e.err
}

// IsPlanNotFoundError returns true if the error is a PlanNotFoundError.
func IsPlanNotFoundError(err error) bool {
	if err == nil {
		return false
	}

	var e *PlanNotFoundError

	return errors.As(err, &e)
}