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