forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customer.go
247 lines (209 loc) · 9.92 KB
/
customer.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package goshopify
import (
"context"
"fmt"
"time"
"github.com/shopspring/decimal"
)
const (
customersBasePath = "customers"
customersResourceName = "customers"
)
// CustomerService is an interface for interfacing with the customers endpoints
// of the Shopify API.
// See: https://help.shopify.com/api/reference/customer
type CustomerService interface {
List(context.Context, interface{}) ([]Customer, error)
ListAll(context.Context, interface{}) ([]Customer, error)
ListWithPagination(ctx context.Context, options interface{}) ([]Customer, *Pagination, error)
Count(context.Context, interface{}) (int, error)
Get(context.Context, uint64, interface{}) (*Customer, error)
Search(context.Context, interface{}) ([]Customer, error)
Create(context.Context, Customer) (*Customer, error)
Update(context.Context, Customer) (*Customer, error)
Delete(context.Context, uint64) error
ListOrders(context.Context, uint64, interface{}) ([]Order, error)
ListTags(context.Context, interface{}) ([]string, error)
// MetafieldsService used for Customer resource to communicate with Metafields resource
MetafieldsService
}
// CustomerServiceOp handles communication with the product related methods of
// the Shopify API.
type CustomerServiceOp struct {
client *Client
}
// Customer represents a Shopify customer
type Customer struct {
Id uint64 `json:"id,omitempty"`
Email string `json:"email,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
State string `json:"state,omitempty"`
Note string `json:"note,omitempty"`
VerifiedEmail bool `json:"verified_email,omitempty"`
MultipassIdentifier string `json:"multipass_identifier,omitempty"`
OrdersCount int `json:"orders_count,omitempty"`
TaxExempt bool `json:"tax_exempt,omitempty"`
TotalSpent *decimal.Decimal `json:"total_spent,omitempty"`
Phone string `json:"phone,omitempty"`
Tags string `json:"tags,omitempty"`
LastOrderId uint64 `json:"last_order_id,omitempty"`
LastOrderName string `json:"last_order_name,omitempty"`
AcceptsMarketing bool `json:"accepts_marketing,omitempty"`
AcceptsMarketingUpdatedAt *time.Time `json:"accepts_marketing_updated_at,omitempty"`
EmailMarketingConsent *EmailMarketingConsent `json:"email_marketing_consent"`
SMSMarketingConsent *SMSMarketingConsent `json:"sms_marketing_consent"`
DefaultAddress *CustomerAddress `json:"default_address,omitempty"`
Addresses []*CustomerAddress `json:"addresses,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
Metafields []Metafield `json:"metafields,omitempty"`
}
// Represents the result from the customers/X.json endpoint
type CustomerResource struct {
Customer *Customer `json:"customer"`
}
// Represents the result from the customers.json endpoint
type CustomersResource struct {
Customers []Customer `json:"customers"`
}
// Represents the result from the customers/tags.json endpoint
type CustomerTagsResource struct {
Tags []string `json:"tags"`
}
// Represents the options available when searching for a customer
type CustomerSearchOptions struct {
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
Fields string `url:"fields,omitempty"`
Order string `url:"order,omitempty"`
Query string `url:"query,omitempty"`
}
type EmailMarketingConsent struct {
State string `json:"state"`
OptInLevel string `json:"opt_in_level"`
ConsentUpdatedAt *time.Time `json:"consent_updated_at"`
}
type SMSMarketingConsent struct {
State string `json:"state"`
OptInLevel string `json:"opt_in_level"`
ConsentUpdatedAt *time.Time `json:"consent_updated_at"`
ConsentCollectedFrom string `json:"consent_collected_from"`
}
// List customers
func (s *CustomerServiceOp) List(ctx context.Context, options interface{}) ([]Customer, error) {
path := fmt.Sprintf("%s.json", customersBasePath)
resource := new(CustomersResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Customers, err
}
// ListAll Lists all customers, iterating over pages
func (s *CustomerServiceOp) ListAll(ctx context.Context, options interface{}) ([]Customer, error) {
collector := []Customer{}
for {
entities, pagination, err := s.ListWithPagination(ctx, options)
if err != nil {
return collector, err
}
collector = append(collector, entities...)
if pagination.NextPageOptions == nil {
break
}
options = pagination.NextPageOptions
}
return collector, nil
}
// ListWithPagination lists customers and return pagination to retrieve next/previous results.
func (s *CustomerServiceOp) ListWithPagination(ctx context.Context, options interface{}) ([]Customer, *Pagination, error) {
path := fmt.Sprintf("%s.json", customersBasePath)
resource := new(CustomersResource)
pagination, err := s.client.ListWithPagination(ctx, path, resource, options)
if err != nil {
return nil, nil, err
}
return resource.Customers, pagination, nil
}
// Count customers
func (s *CustomerServiceOp) Count(ctx context.Context, options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", customersBasePath)
return s.client.Count(ctx, path, options)
}
// Get customer
func (s *CustomerServiceOp) Get(ctx context.Context, customerId uint64, options interface{}) (*Customer, error) {
path := fmt.Sprintf("%s/%v.json", customersBasePath, customerId)
resource := new(CustomerResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Customer, err
}
// Create a new customer
func (s *CustomerServiceOp) Create(ctx context.Context, customer Customer) (*Customer, error) {
path := fmt.Sprintf("%s.json", customersBasePath)
wrappedData := CustomerResource{Customer: &customer}
resource := new(CustomerResource)
err := s.client.Post(ctx, path, wrappedData, resource)
return resource.Customer, err
}
// Update an existing customer
func (s *CustomerServiceOp) Update(ctx context.Context, customer Customer) (*Customer, error) {
path := fmt.Sprintf("%s/%d.json", customersBasePath, customer.Id)
wrappedData := CustomerResource{Customer: &customer}
resource := new(CustomerResource)
err := s.client.Put(ctx, path, wrappedData, resource)
return resource.Customer, err
}
// Delete an existing customer
func (s *CustomerServiceOp) Delete(ctx context.Context, customerId uint64) error {
path := fmt.Sprintf("%s/%d.json", customersBasePath, customerId)
return s.client.Delete(ctx, path)
}
// Search customers
func (s *CustomerServiceOp) Search(ctx context.Context, options interface{}) ([]Customer, error) {
path := fmt.Sprintf("%s/search.json", customersBasePath)
resource := new(CustomersResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Customers, err
}
// ListOrders retrieves all orders from a customer
func (s *CustomerServiceOp) ListOrders(ctx context.Context, customerId uint64, options interface{}) ([]Order, error) {
path := fmt.Sprintf("%s/%d/orders.json", customersBasePath, customerId)
resource := new(OrdersResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Orders, err
}
// ListTags retrieves all unique tags across all customers
func (s *CustomerServiceOp) ListTags(ctx context.Context, options interface{}) ([]string, error) {
path := fmt.Sprintf("%s/tags.json", customersBasePath)
resource := new(CustomerTagsResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Tags, err
}
// List metafields for a customer
func (s *CustomerServiceOp) ListMetafields(ctx context.Context, customerId uint64, options interface{}) ([]Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceId: customerId}
return metafieldService.List(ctx, options)
}
// Count metafields for a customer
func (s *CustomerServiceOp) CountMetafields(ctx context.Context, customerId uint64, options interface{}) (int, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceId: customerId}
return metafieldService.Count(ctx, options)
}
// Get individual metafield for a customer
func (s *CustomerServiceOp) GetMetafield(ctx context.Context, customerId uint64, metafieldId uint64, options interface{}) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceId: customerId}
return metafieldService.Get(ctx, metafieldId, options)
}
// Create a new metafield for a customer
func (s *CustomerServiceOp) CreateMetafield(ctx context.Context, customerId uint64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceId: customerId}
return metafieldService.Create(ctx, metafield)
}
// Update an existing metafield for a customer
func (s *CustomerServiceOp) UpdateMetafield(ctx context.Context, customerId uint64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceId: customerId}
return metafieldService.Update(ctx, metafield)
}
// // Delete an existing metafield for a customer
func (s *CustomerServiceOp) DeleteMetafield(ctx context.Context, customerId uint64, metafieldId uint64) error {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceId: customerId}
return metafieldService.Delete(ctx, metafieldId)
}