This repository has been archived by the owner on Sep 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 324
/
customer.go
167 lines (144 loc) · 6.52 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
package goshopify
import (
"fmt"
"time"
"github.com/shopspring/decimal"
)
const customersBasePath = "admin/customers"
const 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(interface{}) ([]Customer, error)
Count(interface{}) (int, error)
Get(int, interface{}) (*Customer, error)
Search(interface{}) ([]Customer, error)
Create(Customer) (*Customer, error)
Update(Customer) (*Customer, error)
Delete(int) 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 int `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 int `json:"last_order_id,omitempty"`
LastOrderName string `json:"last_order_name,omitempty"`
AcceptsMarketing bool `json:"accepts_marketing,omitempty"`
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 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"`
}
// List customers
func (s *CustomerServiceOp) List(options interface{}) ([]Customer, error) {
path := fmt.Sprintf("%s.json", customersBasePath)
resource := new(CustomersResource)
err := s.client.Get(path, resource, options)
return resource.Customers, err
}
// Count customers
func (s *CustomerServiceOp) Count(options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", customersBasePath)
return s.client.Count(path, options)
}
// Get customer
func (s *CustomerServiceOp) Get(customerID int, options interface{}) (*Customer, error) {
path := fmt.Sprintf("%s/%v.json", customersBasePath, customerID)
resource := new(CustomerResource)
err := s.client.Get(path, resource, options)
return resource.Customer, err
}
// Create a new customer
func (s *CustomerServiceOp) Create(customer Customer) (*Customer, error) {
path := fmt.Sprintf("%s.json", customersBasePath)
wrappedData := CustomerResource{Customer: &customer}
resource := new(CustomerResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Customer, err
}
// Update an existing customer
func (s *CustomerServiceOp) Update(customer Customer) (*Customer, error) {
path := fmt.Sprintf("%s/%d.json", customersBasePath, customer.ID)
wrappedData := CustomerResource{Customer: &customer}
resource := new(CustomerResource)
err := s.client.Put(path, wrappedData, resource)
return resource.Customer, err
}
// Delete an existing customer
func (s *CustomerServiceOp) Delete(customerID int) error {
path := fmt.Sprintf("%s/%d.json", customersBasePath, customerID)
return s.client.Delete(path)
}
// Search customers
func (s *CustomerServiceOp) Search(options interface{}) ([]Customer, error) {
path := fmt.Sprintf("%s/search.json", customersBasePath)
resource := new(CustomersResource)
err := s.client.Get(path, resource, options)
return resource.Customers, err
}
// List metafields for a customer
func (s *CustomerServiceOp) ListMetafields(customerID int, options interface{}) ([]Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceID: customerID}
return metafieldService.List(options)
}
// Count metafields for a customer
func (s *CustomerServiceOp) CountMetafields(customerID int, options interface{}) (int, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceID: customerID}
return metafieldService.Count(options)
}
// Get individual metafield for a customer
func (s *CustomerServiceOp) GetMetafield(customerID int, metafieldID int, options interface{}) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceID: customerID}
return metafieldService.Get(metafieldID, options)
}
// Create a new metafield for a customer
func (s *CustomerServiceOp) CreateMetafield(customerID int, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceID: customerID}
return metafieldService.Create(metafield)
}
// Update an existing metafield for a customer
func (s *CustomerServiceOp) UpdateMetafield(customerID int, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceID: customerID}
return metafieldService.Update(metafield)
}
// // Delete an existing metafield for a customer
func (s *CustomerServiceOp) DeleteMetafield(customerID int, metafieldID int) error {
metafieldService := &MetafieldServiceOp{client: s.client, resource: customersResourceName, resourceID: customerID}
return metafieldService.Delete(metafieldID)
}