-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopify-customer-order.js
97 lines (90 loc) · 2.78 KB
/
shopify-customer-order.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
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
}
}
/**
* Returns all of a customers orders, given their email address
*/
exports.customerOrders = function (customerId, status) {
assert.ok(customerId)
let queryStatus = 'status=any'
if (status) queryStatus = `status=${status}`
return new Promise(
(resolve, reject) => {
const options = {
url: `${config.adminUrl}/${C.URL_CUSTOMERS}/${customerId}/${C.URL_ORDERS}${C.URL_JSON_FORMAT}?${queryStatus}`,
method: C.HTTP_METHOD_GET,
}
console.log(options)
request(options, (error, response, body) => {
if (C.HTTP_STATUS_OK === response.statusCode) {
const json = JSON.parse(body)
if (config.returnArray) { resolve(json.orders) }
resolve(json)
} else {
reject(body)
}
}).auth(config.username, config.password, true)
}
)
}
exports.customerOrdersByEmail = function (email, status) {
assert.ok(email)
}
exports.customerDraftOrdersByEmail = function (email, status) {
assert.ok(email)
let queryStatus = 'status=open'
if (status) queryStatus = `status=${status}`
return new Promise(
(resolve, reject) => {
const options = {
url: `${config.adminUrl}/${C.URL_DRAFT_ORDERS}${C.URL_JSON_FORMAT}?${queryStatus}`,
method: C.HTTP_METHOD_GET,
}
request(options, (error, response, body) => {
if (C.HTTP_STATUS_OK === response.statusCode) {
const json = JSON.parse(body)
const draftOrders = []
json.draft_orders.forEach((d) => {
if (d.email === email) {
draftOrders.push(d)
}
})
if (config.returnArray) resolve(draftOrders)
resolve({ draft_orders: draftOrders })
} else {
reject(body)
}
}).auth(config.username, config.password, true)
}
)
}
/**
* Will delete all the draft orders associated with the customer email provided
*/
exports.deleteCustomerDraftOrders = function (email) {
assert.ok(email)
return this.customerDraftOrders(email).then(
(draftOrders) => {
const promises = []
draftOrders.forEach((d) => {
const promise = this.deleteDraftOrder(d.id)
promises.push(promise)
})
return Promise.all(promises)
}
)
}