-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshopify-customer.js
169 lines (161 loc) · 4.85 KB
/
shopify-customer.js
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
const request = require('request')
const assert = require('assert')
const C = require('./constants')
const config = {}
exports.init = (shopifyConfig) => {
assert.ok(shopifyConfig)
config.store = shopifyConfig.store
config.username = shopifyConfig.username
config.password = shopifyConfig.password
config.sharedSecret = shopifyConfig.sharedSecret
config.webhookKey = shopifyConfig.webhookKey
config.adminUrl = shopifyConfig.adminUrl
config.returnArray = false
if (shopifyConfig.returnArray) {
config.returnArray = shopifyConfig.returnArray
}
}
/**
* Gets a customer given their shopify customer id
*/
exports.customer = function (id) {
assert.ok(id, 'must provide a customer id')
return new Promise(
(resolve, reject) => {
const options = {
url: `${config.adminUrl}/${C.URL_CUSTOMERS}/${id}${C.URL_JSON_FORMAT}`,
method: C.HTTP_METHOD_GET,
}
request(options, (error, response, body) => {
if (error) reject(error)
const json = JSON.parse(body)
if (C.HTTP_STATUS_OK === response.statusCode) {
resolve(json)
} else {
reject(json)
}
}).auth(config.username, config.password, true)
}
)
}
/**
* Returns a customer, given their email
* @todo: should validate the email addy
*/
exports.customerByEmail = function (email) {
assert.ok(email, 'must supply a valid email')
return new Promise(
(resolve, reject) => {
const options = {
url: `${config.adminUrl}/${C.URL_CUSTOMERS}/${C.URL_SEARCH}${C.URL_JSON_FORMAT}?query=${email}`,
method: C.HTTP_METHOD_GET,
}
request(options, (error, response, body) => {
if (error) reject(error)
const json = JSON.parse(body)
if (C.HTTP_STATUS_OK === response.statusCode) {
if (json.customers.length > 0) {
resolve({
customer: json.customers[0]
})
} else {
resolve({})
}
} else {
reject(JSON.parse(body))
}
}).auth(config.username, config.password, true)
}
)
}
/**
* Returns all the current customers
*/
exports.customers = function () {
return new Promise(
(resolve, reject) => {
const options = {
url: `${config.adminUrl}/${C.URL_CUSTOMERS}${C.URL_JSON_FORMAT}`,
method: C.HTTP_METHOD_GET,
}
request(options, (error, response, body) => {
if (error) reject(error)
const json = JSON.parse(body)
if (C.HTTP_STATUS_OK === response.statusCode) {
if (config.returnArray) { resolve(json.customers) }
resolve(json)
} else {
reject(json)
}
}).auth(config.username, config.password, true)
}
)
}
/**
* Creates a new customer record
* @todo: should check that the customer record has the right details
*/
exports.createCustomer = function (customerDetails) {
assert.ok(customerDetails, 'must provide new customer details')
return new Promise(
(resolve, reject) => {
const options = {
url: `${config.adminUrl}/${C.URL_CUSTOMERS}${C.URL_JSON_FORMAT}`,
method: C.HTTP_METHOD_POST,
json: customerDetails,
}
request(options, (error, response, body) => {
if (error) reject(error)
const json = body instanceof Object ? body : JSON.parse(body)
if (C.HTTP_STATUS_CREATED === response.statusCode) {
resolve(json)
} else {
reject(json)
}
}).auth(config.username, config.password, true)
}
)
}
exports.updateCustomer = function (customerUpdate) {
assert.ok(customerUpdate, 'must provide an update object')
return new Promise(
(resolve, reject) => {
const options = {
url: `${config.adminUrl}/${C.URL_CUSTOMERS}${C.URL_JSON_FORMAT}`,
method: C.HTTP_METHOD_PUT,
json: customerUpdate,
}
request(options, (error, response, body) => {
if (error) reject(error)
const json = JSON.parse(body)
if (C.HTTP_STATUS_CREATED === response.statusCode) {
resolve(json)
} else {
reject(json)
}
}).auth(config.username, config.password, true)
}
)
}
/**
* Deletes a customer given their customer id, does not delete dependent objects (order, draft_order, etc)
*/
exports.deleteCustomer = function (customerId) {
assert.ok(customerId, 'must provide a customer id')
return new Promise(
(resolve, reject) => {
const options = {
url: `${config.adminUrl}/${C.URL_CUSTOMERS}/${customerId}${C.URL_JSON_FORMAT}`,
method: C.HTTP_METHOD_DELETE
}
request(options, (error, response, body) => {
if (error) reject(error)
if (C.HTTP_STATUS_OK === response.statusCode) {
resolve({ customer: { id: customerId } })
} else {
reject(body)
}
}).auth(config.username, config.password, true)
}
)
}