-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadjustments.go
209 lines (173 loc) · 6.06 KB
/
adjustments.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
209
package rize
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/google/go-querystring/query"
)
// Handles all Adjustment operations
type adjustmentService service
// Adjustment data type
type Adjustment struct {
UID string `json:"uid,omitempty"`
ExternalUID string `json:"external_uid,omitempty"`
CustomerUID string `json:"customer_uid,omitempty"`
USDAdjustmentAmount string `json:"usd_adjustment_amount,omitempty"`
AdjustmentType *AdjustmentType `json:"adjustment_type,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Status string `json:"status,omitempty"`
}
// AdjustmentType data type
type AdjustmentType struct {
UID string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Fee bool `json:"fee,omitempty"`
ProgramUID string `json:"program_uid,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
}
// AdjustmentListParams builds the query parameters used in querying Adjustments
type AdjustmentListParams struct {
CustomerUID string `url:"customer_uid,omitempty" json:"customer_uid,omitempty"`
AdjustmentTypeUID string `url:"adjustment_type_uid,omitempty" json:"adjustment_type_uid,omitempty"`
ExternalUID string `url:"external_uid,omitempty" json:"external_uid,omitempty"`
USDAdjustmentAmountMax int `url:"usd_adjustment_amount_max,omitempty" json:"usd_adjustment_amount_max,omitempty"`
USDAdjustmentAmountMin int `url:"usd_adjustment_amount_min,omitempty" json:"usd_adjustment_amount_min,omitempty"`
Sort string `url:"sort,omitempty" json:"sort,omitempty"`
}
// AdjustmentCreateParams are the body params used when creating a new Adjustment
type AdjustmentCreateParams struct {
ExternalUID string `json:"external_uid,omitempty"`
CustomerUID string `json:"customer_uid"`
USDAdjustmentAmount string `json:"usd_adjustment_amount"`
AdjustmentTypeUID string `json:"adjustment_type_uid"`
}
// AdjustmentTypeListParams builds the query parameters used in querying Adjustment Types
type AdjustmentTypeListParams struct {
CustomerUID string `url:"customer_uid,omitempty" json:"customer_uid,omitempty"`
ProgramUID string `url:"program_uid,omitempty" json:"program_uid,omitempty"`
ShowDeprecated bool `url:"show_deprecated,omitempty" json:"show_deprecated,omitempty"`
}
// AdjustmentListResponse is an API response containing a list of Adjustments
type AdjustmentListResponse struct {
ListResponse
Data []*Adjustment `json:"data"`
}
// AdjustmentTypeListResponse is an API response containing a list of Adjustments Types
type AdjustmentTypeListResponse struct {
ListResponse
Data []*AdjustmentType `json:"data"`
}
// List retrieves a list of Adjustments filtered by the given parameters
func (a *adjustmentService) List(ctx context.Context, params *AdjustmentListParams) (*AdjustmentListResponse, error) {
// Build AdjustmentListParams into query string params
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := a.client.doRequest(ctx, http.MethodGet, "adjustments", v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &AdjustmentListResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Create a new Adjustment with the provided specification
func (a *adjustmentService) Create(ctx context.Context, params *AdjustmentCreateParams) (*Adjustment, error) {
if params.CustomerUID == "" ||
params.USDAdjustmentAmount == "" ||
params.AdjustmentTypeUID == "" {
return nil, fmt.Errorf("CustomerUID, USDAdjustmentAmount and AdjustmentTypeUID are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := a.client.doRequest(ctx, http.MethodPost, "adjustments", nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &Adjustment{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Get returns a single Adjustment
func (a *adjustmentService) Get(ctx context.Context, uid string) (*Adjustment, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := a.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("adjustments/%s", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &Adjustment{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// ListAdjustmentTypes retrieves a list of Adjustment Types filtered by the given parameters
func (a *adjustmentService) ListAdjustmentTypes(ctx context.Context, params *AdjustmentTypeListParams) (*AdjustmentTypeListResponse, error) {
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := a.client.doRequest(ctx, http.MethodGet, "adjustment_types", v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &AdjustmentTypeListResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// GetAdjustmentType returns a single Adjustment Type
func (a *adjustmentService) GetAdjustmentType(ctx context.Context, uid string) (*AdjustmentType, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := a.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("adjustment_types/%s", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &AdjustmentType{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}