|
1
|
import { |
|
2
|
Context, |
|
3
|
CreateOrderChangeActionDTO, |
|
4
|
OrderTypes, |
|
5
|
} from "@medusajs/framework/types" |
|
6
|
import { |
|
7
|
ChangeActionType, |
|
8
|
OrderChangeType, |
|
9
|
ReturnStatus, |
|
10
|
getShippingMethodsTotals, |
|
11
|
isString, |
|
12
|
promiseAll, |
|
13
|
toMikroORMEntity, |
|
14
|
} from "@medusajs/framework/utils" |
|
15
|
import { OrderExchange, OrderExchangeItem, Return, ReturnItem } from "@models" |
|
16
|
|
|
17
|
function createExchangeAndReturnEntities(em, data, order) { |
|
18
|
const exchangeReference = em.create(toMikroORMEntity(OrderExchange), { |
|
19
|
order_id: data.order_id, |
|
20
|
order_version: order.version, |
|
21
|
no_notification: data.no_notification, |
|
22
|
allow_backorder: data.allow_backorder, |
|
23
|
difference_due: data.difference_due, |
|
24
|
}) |
|
25
|
|
|
26
|
const returnReference = em.create(toMikroORMEntity(Return), { |
|
27
|
order_id: data.order_id, |
|
28
|
order_version: order.version, |
|
29
|
status: ReturnStatus.REQUESTED, |
|
30
|
exchange_id: exchangeReference.id, |
|
31
|
refund_amount: (data.refund_amount as unknown) ?? null, |
|
32
|
}) |
|
33
|
|
|
34
|
exchangeReference.return = returnReference |
|
35
|
|
|
36
|
return { exchangeReference, returnReference } |
|
37
|
} |
|
38
|
|
|
39
|
function createReturnItems( |
|
40
|
em, |
|
41
|
data, |
|
42
|
exchangeReference, |
|
43
|
returnReference, |
|
44
|
actions |
|
45
|
) { |
|
46
|
return data.return_items?.map((item) => { |
|
47
|
actions.push({ |
|
48
|
action: ChangeActionType.RETURN_ITEM, |
|
49
|
reference: "return", |
|
50
|
reference_id: returnReference.id, |
|
51
|
details: { |
|
52
|
reference_id: item.id, |
|
53
|
quantity: item.quantity, |
|
54
|
metadata: item.metadata, |
|
55
|
}, |
|
56
|
}) |
|
57
|
|
|
58
|
return em.create(toMikroORMEntity(ReturnItem), { |
|
59
|
item_id: item.id, |
|
60
|
return_id: returnReference.id, |
|
61
|
reason: item.reason, |
|
62
|
quantity: item.quantity, |
|
63
|
note: item.note, |
|
64
|
metadata: item.metadata, |
|
65
|
}) |
|
66
|
}) |
|
67
|
} |
|
68
|
|
|
69
|
async function processAdditionalItems( |
|
70
|
em, |
|
71
|
service, |
|
72
|
data, |
|
73
|
order, |
|
74
|
exchangeReference, |
|
75
|
actions, |
|
76
|
sharedContext |
|
77
|
) { |
|
78
|
const itemsToAdd: any[] = [] |
|
79
|
const additionalNewItems: any[] = [] |
|
80
|
const additionalItems: any[] = [] |
|
81
|
data.additional_items?.forEach((item) => { |
|
82
|
const hasItem = item.id |
|
83
|
? order.items.find((o) => o.item.id === item.id) |
|
84
|
: false |
|
85
|
|
|
86
|
if (hasItem) { |
|
87
|
actions.push({ |
|
88
|
action: ChangeActionType.ITEM_ADD, |
|
89
|
exchange_id: exchangeReference.id, |
|
90
|
internal_note: item.internal_note, |
|
91
|
reference: "exchange", |
|
92
|
reference_id: exchangeReference.id, |
|
93
|
details: { |
|
94
|
reference_id: item.id, |
|
95
|
quantity: item.quantity, |
|
96
|
unit_price: item.unit_price ?? hasItem.item.unit_price, |
|
97
|
metadata: item.metadata, |
|
98
|
}, |
|
99
|
}) |
|
100
|
|
|
101
|
additionalItems.push( |
|
102
|
em.create(toMikroORMEntity(OrderExchangeItem), { |
|
103
|
item_id: item.id, |
|
104
|
quantity: item.quantity, |
|
105
|
note: item.note, |
|
106
|
metadata: item.metadata, |
|
107
|
is_additional_item: true, |
|
108
|
}) |
|
109
|
) |
|
110
|
} else { |
|
111
|
itemsToAdd.push(item) |
|
112
|
|
|
113
|
additionalNewItems.push( |
|
114
|
em.create(toMikroORMEntity(OrderExchangeItem), { |
|
115
|
quantity: item.quantity, |
|
116
|
unit_price: item.unit_price, |
|
117
|
note: item.note, |
|
118
|
metadata: item.metadata, |
|
119
|
is_additional_item: true, |
|
120
|
}) |
|
121
|
) |
|
122
|
} |
|
123
|
}) |
|
124
|
|
|
125
|
const createItems = await service.orderLineItemService_.create( |
|
126
|
itemsToAdd, |
|
127
|
sharedContext |
|
128
|
) |
|
129
|
|
|
130
|
createItems.forEach((item, index) => { |
|
131
|
const addedItem = itemsToAdd[index] |
|
132
|
|
|
133
|
additionalNewItems[index].item_id = item.id |
|
134
|
|
|
135
|
actions.push({ |
|
136
|
action: ChangeActionType.ITEM_ADD, |
|
137
|
exchange_id: exchangeReference.id, |
|
138
|
internal_note: addedItem.internal_note, |
|
139
|
reference: "exchange", |
|
140
|
reference_id: exchangeReference.id, |
|
141
|
details: { |
|
142
|
reference_id: item.id, |
|
143
|
exchange_id: exchangeReference.id, |
|
144
|
quantity: addedItem.quantity, |
|
145
|
unit_price: item.unit_price, |
|
146
|
metadata: addedItem.metadata, |
|
147
|
}, |
|
148
|
}) |
|
149
|
}) |
|
150
|
|
|
151
|
return additionalNewItems.concat(additionalItems) |
|
152
|
} |
|
153
|
|
|
154
|
async function processShippingMethods( |
|
155
|
service, |
|
156
|
data, |
|
157
|
exchangeReference, |
|
158
|
actions, |
|
159
|
sharedContext |
|
160
|
) { |
|
161
|
for (const shippingMethod of data.shipping_methods ?? []) { |
|
162
|
let shippingMethodId |
|
163
|
|
|
164
|
if (!isString(shippingMethod)) { |
|
165
|
const methods = await service.createOrderShippingMethods( |
|
166
|
[ |
|
167
|
{ |
|
168
|
...shippingMethod, |
|
169
|
order_id: data.order_id, |
|
170
|
exchange_id: exchangeReference.id, |
|
171
|
}, |
|
172
|
], |
|
173
|
sharedContext |
|
174
|
) |
|
175
|
shippingMethodId = methods[0].id |
|
176
|
} else { |
|
177
|
shippingMethodId = shippingMethod |
|
178
|
} |
|
179
|
|
|
180
|
const method = await service.retrieveOrderShippingMethod( |
|
181
|
shippingMethodId, |
|
182
|
{ relations: ["tax_lines", "adjustments"] }, |
|
183
|
sharedContext |
|
184
|
) |
|
185
|
|
|
186
|
const calculatedAmount = getShippingMethodsTotals([method as any], {})[ |
|
187
|
method.id |
|
188
|
] |
|
189
|
|
|
190
|
actions.push({ |
|
191
|
action: ChangeActionType.SHIPPING_ADD, |
|
192
|
reference: "order_shipping_method", |
|
193
|
reference_id: shippingMethodId, |
|
194
|
exchange_id: exchangeReference.id, |
|
195
|
amount: calculatedAmount.total, |
|
196
|
}) |
|
197
|
} |
|
198
|
} |
|
199
|
|
|
200
|
async function processReturnShipping( |
|
201
|
service, |
|
202
|
data, |
|
203
|
exchangeReference, |
|
204
|
returnReference, |
|
205
|
actions, |
|
206
|
sharedContext |
|
207
|
) { |
|
208
|
let returnShippingMethodId |
|
209
|
|
|
210
|
if (!isString(data.return_shipping)) { |
|
211
|
const methods = await service.createOrderShippingMethods( |
|
212
|
[ |
|
213
|
{ |
|
214
|
...data.return_shipping, |
|
215
|
order_id: data.order_id, |
|
216
|
exchange_id: exchangeReference.id, |
|
217
|
return_id: returnReference.id, |
|
218
|
}, |
|
219
|
], |
|
220
|
sharedContext |
|
221
|
) |
|
222
|
returnShippingMethodId = methods[0].id |
|
223
|
} else { |
|
224
|
returnShippingMethodId = data.return_shipping |
|
225
|
} |
|
226
|
|
|
227
|
const method = await service.retrieveOrderShippingMethod( |
|
228
|
returnShippingMethodId, |
|
229
|
{ relations: ["tax_lines", "adjustments"] }, |
|
230
|
sharedContext |
|
231
|
) |
|
232
|
|
|
233
|
const calculatedAmount = getShippingMethodsTotals([method as any], {})[ |
|
234
|
method.id |
|
235
|
] |
|
236
|
|
|
237
|
actions.push({ |
|
238
|
action: ChangeActionType.SHIPPING_ADD, |
|
239
|
reference: "order_shipping_method", |
|
240
|
reference_id: returnShippingMethodId, |
|
241
|
return_id: returnReference.id, |
|
242
|
exchange_id: exchangeReference.id, |
|
243
|
amount: calculatedAmount.total, |
|
244
|
}) |
|
245
|
} |
|
246
|
|
|
247
|
export async function createExchange( |
|
248
|
this: any, |
|
249
|
data: OrderTypes.CreateOrderExchangeDTO, |
|
250
|
sharedContext?: Context |
|
251
|
) { |
|
252
|
const order = await this.orderService_.retrieve( |
|
253
|
data.order_id, |
|
254
|
{ relations: ["items"] }, |
|
255
|
sharedContext |
|
256
|
) |
|
257
|
const actions: CreateOrderChangeActionDTO[] = [] |
|
258
|
const em = sharedContext!.transactionManager as any |
|
259
|
const { exchangeReference, returnReference } = |
|
260
|
createExchangeAndReturnEntities(em, data, order) |
|
261
|
|
|
262
|
returnReference.items = createReturnItems( |
|
263
|
em, |
|
264
|
data, |
|
265
|
exchangeReference, |
|
266
|
returnReference, |
|
267
|
actions |
|
268
|
) |
|
269
|
|
|
270
|
exchangeReference.additional_items = await processAdditionalItems( |
|
271
|
em, |
|
272
|
this, |
|
273
|
data, |
|
274
|
order, |
|
275
|
exchangeReference, |
|
276
|
actions, |
|
277
|
sharedContext |
|
278
|
) |
|
279
|
await processShippingMethods( |
|
280
|
this, |
|
281
|
data, |
|
282
|
exchangeReference, |
|
283
|
actions, |
|
284
|
sharedContext |
|
285
|
) |
|
286
|
await processReturnShipping( |
|
287
|
this, |
|
288
|
data, |
|
289
|
exchangeReference, |
|
290
|
returnReference, |
|
291
|
actions, |
|
292
|
sharedContext |
|
293
|
) |
|
294
|
|
|
295
|
const change = await this.createOrderChange_( |
|
296
|
{ |
|
297
|
order_id: data.order_id, |
|
298
|
exchange_id: exchangeReference.id, |
|
299
|
return_id: returnReference.id, |
|
300
|
change_type: OrderChangeType.EXCHANGE, |
|
301
|
reference: "exchange", |
|
302
|
reference_id: exchangeReference.id, |
|
303
|
description: data.description, |
|
304
|
internal_note: data.internal_note, |
|
305
|
created_by: data.created_by, |
|
306
|
metadata: data.metadata, |
|
307
|
actions, |
|
308
|
}, |
|
309
|
sharedContext |
|
310
|
) |
|
311
|
|
|
312
|
await promiseAll([ |
|
313
|
this.createReturns([returnReference], sharedContext), |
|
314
|
this.createOrderExchanges([exchangeReference], sharedContext), |
|
315
|
this.confirmOrderChange(change[0].id, sharedContext), |
|
316
|
]) |
|
317
|
return exchangeReference |
|
318
|
} |