-
Notifications
You must be signed in to change notification settings - Fork 68
/
orderexport.coffee
178 lines (158 loc) · 5.86 KB
/
orderexport.coffee
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
_ = require 'underscore'
_.mixin require('underscore-mixins')
Promise = require 'bluebird'
fs = Promise.promisifyAll require('fs')
{SphereClient} = require 'sphere-node-sdk'
{ElasticIo} = require 'sphere-node-utils'
XmlMapping = require './mapping-utils/xml'
CsvMapping = require './mapping-utils/csv'
BASE64 = 'base64'
CHANNEL_KEY = 'OrderXmlFileExport'
CHANNEL_ROLE = 'OrderExport'
CONTAINER_PAYMENT = 'checkoutInfo'
class OrderExport
constructor: (options = {}) ->
@_exportOptions = _.defaults (options.export or {}),
standardShippingMethod: 'None'
exportType: 'xml'
fillAllRows: false
exportUnsyncedOnly: true
@client = new SphereClient options.client
@xmlMapping = new XmlMapping @_exportOptions
@csvMapping = new CsvMapping @_exportOptions
@ordersExported = false # Flag to avoid empty file being generated if no order is exported
elasticio: (msg, cfg, next, snapshot) ->
if _.isEmpty msg or _.isEmpty msg.body
ElasticIo.returnSuccess 'No data from elastic.io!', next
return
# TODO: xml only export?
@client.channels.ensure(CHANNEL_KEY, CHANNEL_ROLE)
.then (result) =>
@channel = result.body
@xmlExport @_unsyncedOnly(msg.body.results, @channel)
.then (xmlOrders) =>
now = new Buffer(new Date().toISOString()).toString(BASE64)
data =
body: {}
attachments:
'touch-timestamp.txt':
content: now
syncInfos = []
for xmlOrder in xmlOrders
content = xmlOrder.xml.end(pretty: true, indent: ' ', newline: "\n")
fileName = "#{xmlOrder.id}.xml"
base64 = new Buffer(content).toString(BASE64)
data.attachments[fileName] =
content: base64
syncInfos.push @syncOrder xmlOrder, fileName
Promise.all(syncInfos)
.then -> ElasticIo.returnSuccess data, next
.catch (err) -> ElasticIo.returnFailure err, err.message, next
run: ->
switch @_exportOptions.exportType.toLowerCase()
when 'csv' then @_fetchOrders().then (orders) => @csvExport(orders)
when 'xml' then @_fetchOrders().then (orders) => @xmlExport(orders)
else Promise.reject "Undefined export type '#{@_exportOptions.exportType}', supported 'csv' or 'xml'"
runCSVAndStreamToFile: (writeToFile) ->
@_getCSVOrderTemplate()
.then (template) => @csvMapping._analyseTemplate(template)
.then ([header, mappings]) =>
# write first the header
writeToFile(header.join(','))
.then () =>
if @_exportOptions.where
@client.orders.where(@_exportOptions.where)
@client.orders
.expand('lineItems[*].state[*].state')
.expand('lineItems[*].supplyChannel')
.expand('discountCodes[*].discountCode')
.expand('customerGroup')
.perPage(@_exportOptions.perPage)
.process (payload) =>
orders = payload.body.results
rows = _.map orders, (order) => @csvMapping._mapOrder(order, mappings)
data = _.flatten rows, true
@csvMapping.toCSVWithoutHeader(data)
.then (exportedOrders) ->
# write chunk of data
writeToFile(exportedOrders)
, { accumulate: false }
csvExport: (orders) ->
# TODO: export all fields if no csvTemplate is defined?
@_getCSVOrderTemplate().then (content) => @csvMapping.mapOrders content, orders
xmlExport: (orders) ->
Promise.map orders, (order) => @_processXmlOrder order
_fetchOrders: ->
@client.channels.ensure(CHANNEL_KEY, CHANNEL_ROLE)
.then (result) =>
@channel = result.body
# TODO: query also for syncInfo? -> not well supported by the API at the moment
if @_exportOptions.where
@client.orders.where(@_exportOptions.where)
else
@client.orders.all()
@client.orders
.expand('lineItems[*].state[*].state')
.expand('lineItems[*].supplyChannel')
.expand('discountCodes[*].discountCode')
.expand('customerGroup')
.fetch()
.then (result) =>
allOrders = result.body.results
if @_exportOptions.exportUnsyncedOnly
_unSyncedOrders = @_unsyncedOnly(allOrders, @channel)
if _unSyncedOrders.length
@ordersExported = true
Promise.resolve _unSyncedOrders
else
if allOrders.length
@ordersExported = true
Promise.resolve allOrders
_unsyncedOnly: (orders, channel) ->
_.filter orders, (order) ->
order.syncInfo or= []
if _.isEmpty order.syncInfo
return true
else
not _.find order.syncInfo, (syncInfo) ->
syncInfo.channel.id is channel?.id
_processXmlOrder: (order) ->
# TODO: what if customObject is not found?
# TODO: why not doing it also for CSV export?
customObjectId = if order.cart
order.cart.id
else
order.id
@client.customObjects.byId("#{CONTAINER_PAYMENT}/#{customObjectId}").fetch()
.then (result) =>
paymentInfo = result.body
if order.customerId?
@client.customers.byId(order.customerId).fetch()
.then (result) =>
entry =
id: order.id
xml: @xmlMapping.mapOrder order, paymentInfo, result.body
version: order.version
Promise.resolve entry
else
entry =
id: order.id
xml: @xmlMapping.mapOrder order, paymentInfo
version: order.version
Promise.resolve entry
syncOrder: (xmlOrder, filename) ->
data =
version: xmlOrder.version
actions: [
action: 'updateSyncInfo'
channel:
typeId: 'channel'
id: @channel.id
externalId: filename
]
@client.orders.byId(xmlOrder.id).update(data)
_getCSVOrderTemplate: () ->
unless @_exportOptions.csvTemplate
throw new Error 'You need to provide a csv template for exporting order information'
fs.readFileAsync(@_exportOptions.csvTemplate, {encoding: 'utf-8'})
module.exports = OrderExport