forked from Invoiced/invoiced-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.go
208 lines (149 loc) · 4.12 KB
/
plan.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package invdapi
import (
"errors"
"github.com/gabrielsoaressantos/invoiced-go/invdendpoint"
"strings"
)
type Plan struct {
*Connection
*invdendpoint.Plan
}
type Plans []*Plan
func (c *Connection) NewPlan() *Plan {
plan := new(invdendpoint.Plan)
return &Plan{c, plan}
}
func (c *Plan) Create(plan *Plan) (*Plan, error) {
endpoint := invdendpoint.PlanEndpoint
planResp := new(Plan)
if plan == nil {
return nil, errors.New("plan is nil")
}
// safe prune file data for creation
invdPlanDataToCreate, err := SafePlanForCreation(plan.Plan)
if err != nil {
return nil, err
}
apiErr := c.create(endpoint, invdPlanDataToCreate, planResp)
if apiErr != nil {
return nil, apiErr
}
planResp.Connection = c.Connection
return planResp, nil
}
func (c *Plan) Save() error {
endpoint := invdendpoint.PlanEndpoint + "/" + c.Id
planResp := new(Plan)
planDataToUpdate, err := SafePlanForUpdating(c.Plan)
if err != nil {
return err
}
apiErr := c.update(endpoint, planDataToUpdate, planResp)
if apiErr != nil {
return apiErr
}
c.Plan = planResp.Plan
return nil
}
func (c *Plan) Delete() error {
endpoint := invdendpoint.PlanEndpoint + "/" + c.Id
err := c.delete(endpoint)
if err != nil {
return err
}
return nil
}
func (c *Plan) Retrieve(id string) (*Plan, error) {
endpoint := invdendpoint.PlanEndpoint + "/" + id
planEndpoint := new(invdendpoint.Plan)
plan := &Plan{c.Connection, planEndpoint}
_, err := c.retrieveDataFromAPI(endpoint, plan)
if err != nil {
return nil, err
}
return plan, nil
}
func (c *Plan) RetrieveWithSubNumber(id string) (*Plan, error) {
endpoint := invdendpoint.PlanEndpoint + "/" + id + "?include=num_subscriptions"
planEndpoint := new(invdendpoint.Plan)
plan := &Plan{c.Connection, planEndpoint}
_, err := c.retrieveDataFromAPI(endpoint, plan)
if err != nil {
return nil, err
}
return plan, nil
}
func (c *Plan) ListAllSubNumber(filter *invdendpoint.Filter, sort *invdendpoint.Sort) (Plans, error) {
endpoint := invdendpoint.PlanEndpoint
endpoint = addFilterAndSort(endpoint, filter, sort)
if strings.Contains(endpoint, "?") {
endpoint = endpoint + "&include=num_subscriptions"
} else {
endpoint = endpoint + "?include=num_subscriptions"
}
plans := make(Plans, 0)
NEXT:
tmpPlans := make(Plans, 0)
endpoint, apiErr := c.retrieveDataFromAPI(endpoint, &tmpPlans)
if apiErr != nil {
return nil, apiErr
}
plans = append(plans, tmpPlans...)
if endpoint != "" {
goto NEXT
}
for _, plan := range plans {
plan.Connection = c.Connection
}
return plans, nil
}
func (c *Plan) ListAll(filter *invdendpoint.Filter, sort *invdendpoint.Sort) (Plans, error) {
endpoint := invdendpoint.PlanEndpoint
endpoint = addFilterAndSort(endpoint, filter, sort)
plans := make(Plans, 0)
NEXT:
tmpPlans := make(Plans, 0)
endpoint, apiErr := c.retrieveDataFromAPI(endpoint, &tmpPlans)
if apiErr != nil {
return nil, apiErr
}
plans = append(plans, tmpPlans...)
if endpoint != "" {
goto NEXT
}
for _, plan := range plans {
plan.Connection = c.Connection
}
return plans, nil
}
// SafeCustomerForCreation prunes plan data for just fields that can be used for creation of a plan
func SafePlanForCreation(plan *invdendpoint.Plan) (*invdendpoint.Plan, error) {
if plan == nil {
return nil, errors.New("plan is nil")
}
planData := new(invdendpoint.Plan)
planData.Id = plan.Id
planData.Object = plan.Object
planData.Item = plan.Item
planData.Name = plan.Name
planData.Currency = plan.Currency
planData.Amount = plan.Amount
planData.PricingMode = plan.PricingMode
planData.QuantityType = plan.QuantityType
planData.Interval = plan.Interval
planData.IntervalCount = plan.IntervalCount
planData.Tiers = plan.Tiers
planData.CreatedAt = plan.CreatedAt
planData.Metadata = plan.Metadata
return planData, nil
}
// SafeTaskForUpdating prunes plan data for just fields that can be used for updating of a plan
func SafePlanForUpdating(plan *invdendpoint.Plan) (*invdendpoint.Plan, error) {
if plan == nil {
return nil, errors.New("plan is nil")
}
planData := new(invdendpoint.Plan)
planData.Name = plan.Name
planData.Metadata = plan.Metadata
return planData, nil
}