|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { |
|
4
|
QueryKey, |
|
5
|
useMutation, |
|
6
|
UseMutationOptions, |
|
7
|
useQuery, |
|
8
|
UseQueryOptions, |
|
9
|
} from "@tanstack/react-query" |
|
10
|
import { sdk } from "../../lib/client" |
|
11
|
import { queryClient } from "../../lib/query-client" |
|
12
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
13
|
import { ordersQueryKeys } from "./orders" |
|
14
|
import { returnsQueryKeys } from "./returns" |
|
15
|
|
|
16
|
const EXCHANGES_QUERY_KEY = "exchanges" as const |
|
17
|
export const exchangesQueryKeys = queryKeysFactory(EXCHANGES_QUERY_KEY) |
|
18
|
|
|
19
|
export const useExchange = ( |
|
20
|
id: string, |
|
21
|
query?: HttpTypes.AdminExchangeListParams, |
|
22
|
options?: Omit< |
|
23
|
UseQueryOptions< |
|
24
|
HttpTypes.AdminExchangeResponse, |
|
25
|
FetchError, |
|
26
|
HttpTypes.AdminExchangeResponse, |
|
27
|
QueryKey |
|
28
|
>, |
|
29
|
"queryFn" | "queryKey" |
|
30
|
> |
|
31
|
) => { |
|
32
|
const { data, ...rest } = useQuery({ |
|
33
|
queryFn: async () => sdk.admin.exchange.retrieve(id, query), |
|
34
|
queryKey: exchangesQueryKeys.detail(id, query), |
|
35
|
...options, |
|
36
|
}) |
|
37
|
|
|
38
|
return { ...data, ...rest } |
|
39
|
} |
|
40
|
|
|
41
|
export const useExchanges = ( |
|
42
|
query?: HttpTypes.AdminExchangeListParams, |
|
43
|
options?: Omit< |
|
44
|
UseQueryOptions< |
|
45
|
HttpTypes.AdminExchangeListParams, |
|
46
|
FetchError, |
|
47
|
HttpTypes.AdminExchangeListResponse, |
|
48
|
QueryKey |
|
49
|
>, |
|
50
|
"queryFn" | "queryKey" |
|
51
|
> |
|
52
|
) => { |
|
53
|
const { data, ...rest } = useQuery({ |
|
54
|
queryFn: async () => sdk.admin.exchange.list(query), |
|
55
|
queryKey: exchangesQueryKeys.list(query), |
|
56
|
...options, |
|
57
|
}) |
|
58
|
|
|
59
|
return { ...data, ...rest } |
|
60
|
} |
|
61
|
|
|
62
|
export const useCreateExchange = ( |
|
63
|
orderId: string, |
|
64
|
options?: UseMutationOptions< |
|
65
|
HttpTypes.AdminExchangeResponse, |
|
66
|
FetchError, |
|
67
|
HttpTypes.AdminCreateExchange |
|
68
|
> |
|
69
|
) => { |
|
70
|
return useMutation({ |
|
71
|
mutationFn: (payload: HttpTypes.AdminCreateExchange) => |
|
72
|
sdk.admin.exchange.create(payload), |
|
73
|
onSuccess: (data: any, variables: any, context: any) => { |
|
74
|
queryClient.invalidateQueries({ |
|
75
|
queryKey: ordersQueryKeys.details(), |
|
76
|
}) |
|
77
|
|
|
78
|
queryClient.invalidateQueries({ |
|
79
|
queryKey: ordersQueryKeys.preview(orderId), |
|
80
|
}) |
|
81
|
|
|
82
|
queryClient.invalidateQueries({ |
|
83
|
queryKey: exchangesQueryKeys.lists(), |
|
84
|
}) |
|
85
|
|
|
86
|
options?.onSuccess?.(data, variables, context) |
|
87
|
}, |
|
88
|
...options, |
|
89
|
}) |
|
90
|
} |
|
91
|
|
|
92
|
export const useCancelExchange = ( |
|
93
|
id: string, |
|
94
|
orderId: string, |
|
95
|
options?: UseMutationOptions<HttpTypes.AdminExchangeResponse, FetchError> |
|
96
|
) => { |
|
97
|
return useMutation({ |
|
98
|
mutationFn: () => sdk.admin.exchange.cancel(id), |
|
99
|
onSuccess: (data: any, variables: any, context: any) => { |
|
100
|
queryClient.invalidateQueries({ |
|
101
|
queryKey: ordersQueryKeys.details(), |
|
102
|
}) |
|
103
|
|
|
104
|
queryClient.invalidateQueries({ |
|
105
|
queryKey: ordersQueryKeys.preview(orderId), |
|
106
|
}) |
|
107
|
|
|
108
|
queryClient.invalidateQueries({ |
|
109
|
queryKey: exchangesQueryKeys.details(), |
|
110
|
}) |
|
111
|
|
|
112
|
queryClient.invalidateQueries({ |
|
113
|
queryKey: exchangesQueryKeys.lists(), |
|
114
|
}) |
|
115
|
options?.onSuccess?.(data, variables, context) |
|
116
|
}, |
|
117
|
...options, |
|
118
|
}) |
|
119
|
} |
|
120
|
|
|
121
|
export const useAddExchangeInboundItems = ( |
|
122
|
id: string, |
|
123
|
orderId: string, |
|
124
|
options?: UseMutationOptions< |
|
125
|
HttpTypes.AdminExchangeReturnResponse, |
|
126
|
FetchError, |
|
127
|
HttpTypes.AdminAddExchangeInboundItems |
|
128
|
> |
|
129
|
) => { |
|
130
|
return useMutation({ |
|
131
|
mutationFn: (payload: HttpTypes.AdminAddExchangeInboundItems) => |
|
132
|
sdk.admin.exchange.addInboundItems(id, payload), |
|
133
|
onSuccess: (data: any, variables: any, context: any) => { |
|
134
|
queryClient.invalidateQueries({ |
|
135
|
queryKey: ordersQueryKeys.preview(orderId), |
|
136
|
}) |
|
137
|
options?.onSuccess?.(data, variables, context) |
|
138
|
}, |
|
139
|
...options, |
|
140
|
}) |
|
141
|
} |
|
142
|
|
|
143
|
export const useUpdateExchangeInboundItem = ( |
|
144
|
id: string, |
|
145
|
orderId: string, |
|
146
|
options?: UseMutationOptions< |
|
147
|
HttpTypes.AdminExchangeReturnResponse, |
|
148
|
FetchError, |
|
149
|
HttpTypes.AdminUpdateExchangeInboundItem & { actionId: string } |
|
150
|
> |
|
151
|
) => { |
|
152
|
return useMutation({ |
|
153
|
mutationFn: ({ |
|
154
|
actionId, |
|
155
|
...payload |
|
156
|
}: HttpTypes.AdminUpdateExchangeInboundItem & { actionId: string }) => { |
|
157
|
return sdk.admin.exchange.updateInboundItem(id, actionId, payload) |
|
158
|
}, |
|
159
|
onSuccess: (data: any, variables: any, context: any) => { |
|
160
|
queryClient.invalidateQueries({ |
|
161
|
queryKey: ordersQueryKeys.preview(orderId), |
|
162
|
}) |
|
163
|
options?.onSuccess?.(data, variables, context) |
|
164
|
}, |
|
165
|
...options, |
|
166
|
}) |
|
167
|
} |
|
168
|
|
|
169
|
export const useRemoveExchangeInboundItem = ( |
|
170
|
id: string, |
|
171
|
orderId: string, |
|
172
|
options?: UseMutationOptions< |
|
173
|
HttpTypes.AdminExchangeReturnResponse, |
|
174
|
FetchError, |
|
175
|
string |
|
176
|
> |
|
177
|
) => { |
|
178
|
return useMutation({ |
|
179
|
mutationFn: (actionId: string) => |
|
180
|
sdk.admin.exchange.removeInboundItem(id, actionId), |
|
181
|
onSuccess: (data: any, variables: any, context: any) => { |
|
182
|
queryClient.invalidateQueries({ |
|
183
|
queryKey: ordersQueryKeys.details(), |
|
184
|
}) |
|
185
|
|
|
186
|
queryClient.invalidateQueries({ |
|
187
|
queryKey: ordersQueryKeys.preview(orderId), |
|
188
|
}) |
|
189
|
|
|
190
|
queryClient.invalidateQueries({ |
|
191
|
queryKey: returnsQueryKeys.details(), |
|
192
|
}) |
|
193
|
|
|
194
|
options?.onSuccess?.(data, variables, context) |
|
195
|
}, |
|
196
|
...options, |
|
197
|
}) |
|
198
|
} |
|
199
|
|
|
200
|
export const useAddExchangeInboundShipping = ( |
|
201
|
id: string, |
|
202
|
orderId: string, |
|
203
|
options?: UseMutationOptions< |
|
204
|
HttpTypes.AdminExchangeReturnResponse, |
|
205
|
FetchError, |
|
206
|
HttpTypes.AdminExchangeAddInboundShipping |
|
207
|
> |
|
208
|
) => { |
|
209
|
return useMutation({ |
|
210
|
mutationFn: (payload: HttpTypes.AdminExchangeAddInboundShipping) => |
|
211
|
sdk.admin.exchange.addInboundShipping(id, payload), |
|
212
|
onSuccess: (data: any, variables: any, context: any) => { |
|
213
|
queryClient.invalidateQueries({ |
|
214
|
queryKey: ordersQueryKeys.preview(orderId), |
|
215
|
}) |
|
216
|
options?.onSuccess?.(data, variables, context) |
|
217
|
}, |
|
218
|
...options, |
|
219
|
}) |
|
220
|
} |
|
221
|
|
|
222
|
export const useUpdateExchangeInboundShipping = ( |
|
223
|
id: string, |
|
224
|
orderId: string, |
|
225
|
options?: UseMutationOptions< |
|
226
|
HttpTypes.AdminExchangeReturnResponse, |
|
227
|
FetchError, |
|
228
|
HttpTypes.AdminExchangeUpdateInboundShipping & { actionId: string } |
|
229
|
> |
|
230
|
) => { |
|
231
|
return useMutation({ |
|
232
|
mutationFn: ({ |
|
233
|
actionId, |
|
234
|
...payload |
|
235
|
}: HttpTypes.AdminExchangeUpdateInboundShipping & { actionId: string }) => |
|
236
|
sdk.admin.exchange.updateInboundShipping(id, actionId, payload), |
|
237
|
onSuccess: (data: any, variables: any, context: any) => { |
|
238
|
queryClient.invalidateQueries({ |
|
239
|
queryKey: ordersQueryKeys.preview(orderId), |
|
240
|
}) |
|
241
|
options?.onSuccess?.(data, variables, context) |
|
242
|
}, |
|
243
|
...options, |
|
244
|
}) |
|
245
|
} |
|
246
|
|
|
247
|
export const useDeleteExchangeInboundShipping = ( |
|
248
|
id: string, |
|
249
|
orderId: string, |
|
250
|
options?: UseMutationOptions< |
|
251
|
HttpTypes.AdminExchangeReturnResponse, |
|
252
|
FetchError, |
|
253
|
string |
|
254
|
> |
|
255
|
) => { |
|
256
|
return useMutation({ |
|
257
|
mutationFn: (actionId: string) => |
|
258
|
sdk.admin.exchange.deleteInboundShipping(id, actionId), |
|
259
|
onSuccess: (data: any, variables: any, context: any) => { |
|
260
|
queryClient.invalidateQueries({ |
|
261
|
queryKey: ordersQueryKeys.preview(orderId), |
|
262
|
}) |
|
263
|
|
|
264
|
options?.onSuccess?.(data, variables, context) |
|
265
|
}, |
|
266
|
...options, |
|
267
|
}) |
|
268
|
} |
|
269
|
|
|
270
|
export const useAddExchangeOutboundItems = ( |
|
271
|
id: string, |
|
272
|
orderId: string, |
|
273
|
options?: UseMutationOptions< |
|
274
|
HttpTypes.AdminExchangeResponse, |
|
275
|
FetchError, |
|
276
|
HttpTypes.AdminAddExchangeOutboundItems |
|
277
|
> |
|
278
|
) => { |
|
279
|
return useMutation({ |
|
280
|
mutationFn: (payload: HttpTypes.AdminAddExchangeOutboundItems) => |
|
281
|
sdk.admin.exchange.addOutboundItems(id, payload), |
|
282
|
onSuccess: (data: any, variables: any, context: any) => { |
|
283
|
queryClient.invalidateQueries({ |
|
284
|
queryKey: ordersQueryKeys.preview(orderId), |
|
285
|
}) |
|
286
|
options?.onSuccess?.(data, variables, context) |
|
287
|
}, |
|
288
|
...options, |
|
289
|
}) |
|
290
|
} |
|
291
|
|
|
292
|
export const useUpdateExchangeOutboundItems = ( |
|
293
|
id: string, |
|
294
|
orderId: string, |
|
295
|
options?: UseMutationOptions< |
|
296
|
HttpTypes.AdminExchangeResponse, |
|
297
|
FetchError, |
|
298
|
HttpTypes.AdminUpdateExchangeOutboundItem & { actionId: string } |
|
299
|
> |
|
300
|
) => { |
|
301
|
return useMutation({ |
|
302
|
mutationFn: ({ |
|
303
|
actionId, |
|
304
|
...payload |
|
305
|
}: HttpTypes.AdminUpdateExchangeOutboundItem & { actionId: string }) => { |
|
306
|
return sdk.admin.exchange.updateOutboundItem(id, actionId, payload) |
|
307
|
}, |
|
308
|
onSuccess: (data: any, variables: any, context: any) => { |
|
309
|
queryClient.invalidateQueries({ |
|
310
|
queryKey: ordersQueryKeys.preview(orderId), |
|
311
|
}) |
|
312
|
options?.onSuccess?.(data, variables, context) |
|
313
|
}, |
|
314
|
...options, |
|
315
|
}) |
|
316
|
} |
|
317
|
|
|
318
|
export const useRemoveExchangeOutboundItem = ( |
|
319
|
id: string, |
|
320
|
orderId: string, |
|
321
|
options?: UseMutationOptions< |
|
322
|
HttpTypes.AdminExchangeResponse, |
|
323
|
FetchError, |
|
324
|
string |
|
325
|
> |
|
326
|
) => { |
|
327
|
return useMutation({ |
|
328
|
mutationFn: (actionId: string) => |
|
329
|
sdk.admin.exchange.removeOutboundItem(id, actionId), |
|
330
|
onSuccess: (data: any, variables: any, context: any) => { |
|
331
|
queryClient.invalidateQueries({ |
|
332
|
queryKey: ordersQueryKeys.details(), |
|
333
|
}) |
|
334
|
|
|
335
|
queryClient.invalidateQueries({ |
|
336
|
queryKey: ordersQueryKeys.preview(orderId), |
|
337
|
}) |
|
338
|
|
|
339
|
options?.onSuccess?.(data, variables, context) |
|
340
|
}, |
|
341
|
...options, |
|
342
|
}) |
|
343
|
} |
|
344
|
|
|
345
|
export const useAddExchangeOutboundShipping = ( |
|
346
|
id: string, |
|
347
|
orderId: string, |
|
348
|
options?: UseMutationOptions< |
|
349
|
HttpTypes.AdminExchangeResponse, |
|
350
|
FetchError, |
|
351
|
HttpTypes.AdminExchangeAddOutboundShipping |
|
352
|
> |
|
353
|
) => { |
|
354
|
return useMutation({ |
|
355
|
mutationFn: (payload: HttpTypes.AdminExchangeAddOutboundShipping) => |
|
356
|
sdk.admin.exchange.addOutboundShipping(id, payload), |
|
357
|
onSuccess: (data: any, variables: any, context: any) => { |
|
358
|
queryClient.invalidateQueries({ |
|
359
|
queryKey: ordersQueryKeys.preview(orderId), |
|
360
|
}) |
|
361
|
options?.onSuccess?.(data, variables, context) |
|
362
|
}, |
|
363
|
...options, |
|
364
|
}) |
|
365
|
} |
|
366
|
|
|
367
|
export const useUpdateExchangeOutboundShipping = ( |
|
368
|
id: string, |
|
369
|
orderId: string, |
|
370
|
options?: UseMutationOptions< |
|
371
|
HttpTypes.AdminExchangePreviewResponse, |
|
372
|
FetchError, |
|
373
|
HttpTypes.AdminExchangeUpdateOutboundShipping & { actionId: string } |
|
374
|
> |
|
375
|
) => { |
|
376
|
return useMutation({ |
|
377
|
mutationFn: ({ |
|
378
|
actionId, |
|
379
|
...payload |
|
380
|
}: HttpTypes.AdminExchangeUpdateOutboundShipping & { actionId: string }) => |
|
381
|
sdk.admin.exchange.updateOutboundShipping(id, actionId, payload), |
|
382
|
onSuccess: (data: any, variables: any, context: any) => { |
|
383
|
queryClient.invalidateQueries({ |
|
384
|
queryKey: ordersQueryKeys.preview(orderId), |
|
385
|
}) |
|
386
|
options?.onSuccess?.(data, variables, context) |
|
387
|
}, |
|
388
|
...options, |
|
389
|
}) |
|
390
|
} |
|
391
|
|
|
392
|
export const useDeleteExchangeOutboundShipping = ( |
|
393
|
id: string, |
|
394
|
orderId: string, |
|
395
|
options?: UseMutationOptions< |
|
396
|
HttpTypes.AdminExchangeResponse, |
|
397
|
FetchError, |
|
398
|
string |
|
399
|
> |
|
400
|
) => { |
|
401
|
return useMutation({ |
|
402
|
mutationFn: (actionId: string) => |
|
403
|
sdk.admin.exchange.deleteOutboundShipping(id, actionId), |
|
404
|
onSuccess: (data: any, variables: any, context: any) => { |
|
405
|
queryClient.invalidateQueries({ |
|
406
|
queryKey: ordersQueryKeys.preview(orderId), |
|
407
|
}) |
|
408
|
options?.onSuccess?.(data, variables, context) |
|
409
|
}, |
|
410
|
...options, |
|
411
|
}) |
|
412
|
} |
|
413
|
|
|
414
|
export const useExchangeConfirmRequest = ( |
|
415
|
id: string, |
|
416
|
orderId: string, |
|
417
|
options?: UseMutationOptions< |
|
418
|
HttpTypes.AdminExchangeResponse, |
|
419
|
FetchError, |
|
420
|
HttpTypes.AdminRequestExchange |
|
421
|
> |
|
422
|
) => { |
|
423
|
return useMutation({ |
|
424
|
mutationFn: (payload: HttpTypes.AdminRequestExchange) => |
|
425
|
sdk.admin.exchange.request(id, payload), |
|
426
|
onSuccess: (data: any, variables: any, context: any) => { |
|
427
|
queryClient.invalidateQueries({ |
|
428
|
queryKey: returnsQueryKeys.all, |
|
429
|
}) |
|
430
|
|
|
431
|
queryClient.invalidateQueries({ |
|
432
|
queryKey: ordersQueryKeys.details(), |
|
433
|
}) |
|
434
|
|
|
435
|
queryClient.invalidateQueries({ |
|
436
|
queryKey: ordersQueryKeys.preview(orderId), |
|
437
|
}) |
|
438
|
|
|
439
|
queryClient.invalidateQueries({ |
|
440
|
queryKey: exchangesQueryKeys.lists(), |
|
441
|
}) |
|
442
|
|
|
443
|
options?.onSuccess?.(data, variables, context) |
|
444
|
}, |
|
445
|
...options, |
|
446
|
}) |
|
447
|
} |
|
448
|
|
|
449
|
export const useCancelExchangeRequest = ( |
|
450
|
id: string, |
|
451
|
orderId: string, |
|
452
|
options?: UseMutationOptions< |
|
453
|
HttpTypes.AdminExchangeDeleteResponse, |
|
454
|
FetchError |
|
455
|
> |
|
456
|
) => { |
|
457
|
return useMutation({ |
|
458
|
mutationFn: () => sdk.admin.exchange.cancelRequest(id), |
|
459
|
onSuccess: (data: any, variables: any, context: any) => { |
|
460
|
queryClient.invalidateQueries({ |
|
461
|
queryKey: ordersQueryKeys.details(), |
|
462
|
}) |
|
463
|
|
|
464
|
queryClient.invalidateQueries({ |
|
465
|
queryKey: ordersQueryKeys.preview(orderId), |
|
466
|
}) |
|
467
|
|
|
468
|
queryClient.invalidateQueries({ |
|
469
|
queryKey: exchangesQueryKeys.details(), |
|
470
|
}) |
|
471
|
queryClient.invalidateQueries({ |
|
472
|
queryKey: exchangesQueryKeys.lists(), |
|
473
|
}) |
|
474
|
options?.onSuccess?.(data, variables, context) |
|
475
|
}, |
|
476
|
...options, |
|
477
|
}) |
|
478
|
} |