Raw
1 import { FetchError } from "@medusajs/js-sdk"
2 import { CreateOrderCreditLineDTO, 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, TQueryKey } from "../../lib/query-key-factory"
13 import { inventoryItemsQueryKeys } from "./inventory"
14 import { reservationItemsQueryKeys } from "./reservations"
15
16 const ORDERS_QUERY_KEY = "orders" as const
17 const _orderKeys = queryKeysFactory(ORDERS_QUERY_KEY) as TQueryKey<"orders"> & {
18 preview: (orderId: string) => any
19 changes: (orderId: string) => any
20 lineItems: (orderId: string, query?: any) => any
21 shippingOptions: (orderId: string) => any
22 }
23
24 _orderKeys.preview = function (id: string) {
25 return [this.detail(id), "preview"]
26 }
27
28 _orderKeys.changes = function (id: string) {
29 return [this.detail(id), "changes"]
30 }
31
32 _orderKeys.lineItems = function (id: string, query?: any) {
33 return [this.detail(id), query ? { query } : undefined, "lineItems"].filter(
34 (k) => !!k
35 )
36 }
37
38 _orderKeys.shippingOptions = function (id: string) {
39 return [this.detail(id), "shippingOptions"]
40 }
41
42 export const ordersQueryKeys = _orderKeys
43
44 export const useOrder = (
45 id: string,
46 query?: Record<string, any>,
47 options?: Omit<
48 UseQueryOptions<
49 HttpTypes.AdminOrderResponse,
50 FetchError,
51 HttpTypes.AdminOrderResponse,
52 QueryKey
53 >,
54 "queryFn" | "queryKey"
55 >
56 ) => {
57 const { data, ...rest } = useQuery({
58 queryFn: async () => sdk.admin.order.retrieve(id, query),
59 queryKey: ordersQueryKeys.detail(id, query),
60 ...options,
61 })
62
63 return { ...data, ...rest }
64 }
65
66 export const useUpdateOrder = (
67 id: string,
68 options?: UseMutationOptions<
69 HttpTypes.AdminOrderResponse,
70 FetchError,
71 HttpTypes.AdminUpdateOrder
72 >
73 ) => {
74 return useMutation({
75 mutationFn: (payload: HttpTypes.AdminUpdateOrder) =>
76 sdk.admin.order.update(id, payload),
77 onSuccess: (data: any, variables: any, context: any) => {
78 queryClient.invalidateQueries({
79 queryKey: ordersQueryKeys.detail(id),
80 })
81
82 queryClient.invalidateQueries({
83 queryKey: ordersQueryKeys.changes(id),
84 })
85
86 // TODO: enable when needed
87 // queryClient.invalidateQueries({
88 // queryKey: ordersQueryKeys.lists(),
89 // })
90
91 options?.onSuccess?.(data, variables, context)
92 },
93 ...options,
94 })
95 }
96
97 export const useOrderPreview = (
98 id: string,
99 query?: HttpTypes.AdminOrderFilters,
100 options?: Omit<
101 UseQueryOptions<
102 HttpTypes.AdminOrderPreviewResponse,
103 FetchError,
104 HttpTypes.AdminOrderPreviewResponse,
105 QueryKey
106 >,
107 "queryFn" | "queryKey"
108 >
109 ) => {
110 const { data, ...rest } = useQuery({
111 queryFn: async () => sdk.admin.order.retrievePreview(id, query),
112 queryKey: ordersQueryKeys.preview(id),
113 ...options,
114 })
115
116 return { ...data, ...rest }
117 }
118
119 export const useOrders = (
120 query?: HttpTypes.AdminOrderFilters,
121 options?: Omit<
122 UseQueryOptions<
123 HttpTypes.AdminOrderListResponse,
124 FetchError,
125 HttpTypes.AdminOrderListResponse,
126 QueryKey
127 >,
128 "queryFn" | "queryKey"
129 >
130 ) => {
131 const { data, ...rest } = useQuery({
132 queryFn: async () => sdk.admin.order.list(query),
133 queryKey: ordersQueryKeys.list(query),
134 ...options,
135 })
136
137 return { ...data, ...rest }
138 }
139
140 export const useOrderShippingOptions = (
141 id: string,
142 query?: HttpTypes.AdminGetOrderShippingOptionList,
143 options?: Omit<
144 UseQueryOptions<
145 { shipping_options: HttpTypes.AdminShippingOption[] },
146 FetchError,
147 { shipping_options: HttpTypes.AdminShippingOption[] },
148 QueryKey
149 >,
150 "queryFn" | "queryKey"
151 >
152 ) => {
153 const { data, ...rest } = useQuery({
154 queryFn: async () => sdk.admin.order.listShippingOptions(id, query),
155 queryKey: ordersQueryKeys.shippingOptions(id),
156 ...options,
157 })
158
159 return { ...data, ...rest }
160 }
161
162 export const useOrderChanges = (
163 id: string,
164 query?: HttpTypes.AdminOrderChangesFilters,
165 options?: Omit<
166 UseQueryOptions<
167 HttpTypes.AdminOrderChangesResponse,
168 FetchError,
169 HttpTypes.AdminOrderChangesResponse,
170 QueryKey
171 >,
172 "queryFn" | "queryKey"
173 >
174 ) => {
175 const { data, ...rest } = useQuery({
176 queryFn: async () => sdk.admin.order.listChanges(id, query),
177 queryKey: ordersQueryKeys.changes(id),
178 ...options,
179 })
180
181 return { ...data, ...rest }
182 }
183
184 export const useOrderLineItems = (
185 id: string,
186 query?: HttpTypes.AdminOrderItemsFilters,
187 options?: Omit<
188 UseQueryOptions<
189 HttpTypes.AdminOrderLineItemsListResponse,
190 FetchError,
191 HttpTypes.AdminOrderLineItemsListResponse,
192 QueryKey
193 >,
194 "queryFn" | "queryKey"
195 >
196 ) => {
197 const { data, ...rest } = useQuery({
198 queryFn: async () => sdk.admin.order.listLineItems(id, query),
199 queryKey: ordersQueryKeys.lineItems(id, query),
200 ...options,
201 })
202
203 return { ...data, ...rest }
204 }
205
206 export const useCreateOrderFulfillment = (
207 orderId: string,
208 options?: UseMutationOptions<
209 HttpTypes.AdminOrderResponse,
210 FetchError,
211 HttpTypes.AdminCreateOrderFulfillment
212 >
213 ) => {
214 return useMutation({
215 mutationFn: (payload: HttpTypes.AdminCreateOrderFulfillment) =>
216 sdk.admin.order.createFulfillment(orderId, payload),
217 onSuccess: (data: any, variables: any, context: any) => {
218 queryClient.invalidateQueries({
219 queryKey: ordersQueryKeys.all,
220 })
221
222 queryClient.invalidateQueries({
223 queryKey: ordersQueryKeys.preview(orderId),
224 })
225
226 queryClient.invalidateQueries({
227 queryKey: reservationItemsQueryKeys.lists(),
228 })
229
230 queryClient.invalidateQueries({
231 queryKey: inventoryItemsQueryKeys.details(),
232 })
233
234 options?.onSuccess?.(data, variables, context)
235 },
236 ...options,
237 })
238 }
239
240 export const useCancelOrderFulfillment = (
241 orderId: string,
242 fulfillmentId: string,
243 options?: UseMutationOptions<any, FetchError, any>
244 ) => {
245 return useMutation({
246 mutationFn: (payload: { no_notification?: boolean }) =>
247 sdk.admin.order.cancelFulfillment(orderId, fulfillmentId, payload),
248 onSuccess: (data: any, variables: any, context: any) => {
249 queryClient.invalidateQueries({
250 queryKey: ordersQueryKeys.all,
251 })
252
253 queryClient.invalidateQueries({
254 queryKey: ordersQueryKeys.preview(orderId),
255 })
256
257 queryClient.invalidateQueries({
258 queryKey: reservationItemsQueryKeys.lists(),
259 })
260
261 queryClient.invalidateQueries({
262 queryKey: inventoryItemsQueryKeys.details(),
263 })
264
265 options?.onSuccess?.(data, variables, context)
266 },
267 ...options,
268 })
269 }
270
271 export const useCreateOrderShipment = (
272 orderId: string,
273 fulfillmentId: string,
274 options?: UseMutationOptions<
275 { order: HttpTypes.AdminOrder },
276 FetchError,
277 HttpTypes.AdminCreateOrderShipment
278 >
279 ) => {
280 return useMutation({
281 mutationFn: (payload: HttpTypes.AdminCreateOrderShipment) =>
282 sdk.admin.order.createShipment(orderId, fulfillmentId, payload),
283 onSuccess: (data: any, variables: any, context: any) => {
284 queryClient.invalidateQueries({
285 queryKey: ordersQueryKeys.all,
286 })
287
288 queryClient.invalidateQueries({
289 queryKey: ordersQueryKeys.preview(orderId),
290 })
291
292 options?.onSuccess?.(data, variables, context)
293 },
294 ...options,
295 })
296 }
297
298 export const useMarkOrderFulfillmentAsDelivered = (
299 orderId: string,
300 fulfillmentId: string,
301 options?: UseMutationOptions<
302 { order: HttpTypes.AdminOrder },
303 FetchError,
304 HttpTypes.AdminMarkOrderFulfillmentAsDelivered
305 >
306 ) => {
307 return useMutation({
308 mutationFn: (payload: HttpTypes.AdminMarkOrderFulfillmentAsDelivered) =>
309 sdk.admin.order.markAsDelivered(orderId, fulfillmentId, payload),
310 onSuccess: (data: any, variables: any, context: any) => {
311 queryClient.invalidateQueries({
312 queryKey: ordersQueryKeys.all,
313 })
314
315 queryClient.invalidateQueries({
316 queryKey: ordersQueryKeys.preview(orderId),
317 })
318
319 options?.onSuccess?.(data, variables, context)
320 },
321 ...options,
322 })
323 }
324
325 export const useCancelOrder = (
326 orderId: string,
327 options?: UseMutationOptions<HttpTypes.AdminOrderResponse, FetchError, void>
328 ) => {
329 return useMutation({
330 mutationFn: () => sdk.admin.order.cancel(orderId),
331 onSuccess: (data: any, variables: any, context: any) => {
332 queryClient.invalidateQueries({
333 queryKey: ordersQueryKeys.detail(orderId),
334 })
335
336 queryClient.invalidateQueries({
337 queryKey: ordersQueryKeys.preview(orderId),
338 })
339
340 options?.onSuccess?.(data, variables, context)
341 },
342 ...options,
343 })
344 }
345
346 export const useRequestTransferOrder = (
347 orderId: string,
348 options?: UseMutationOptions<
349 HttpTypes.AdminOrderResponse,
350 FetchError,
351 HttpTypes.AdminRequestOrderTransfer
352 >
353 ) => {
354 return useMutation({
355 mutationFn: (payload: HttpTypes.AdminRequestOrderTransfer) =>
356 sdk.admin.order.requestTransfer(orderId, payload),
357 onSuccess: (data: any, variables: any, context: any) => {
358 queryClient.invalidateQueries({
359 queryKey: ordersQueryKeys.preview(orderId),
360 })
361
362 queryClient.invalidateQueries({
363 queryKey: ordersQueryKeys.changes(orderId),
364 })
365
366 options?.onSuccess?.(data, variables, context)
367 },
368 ...options,
369 })
370 }
371
372 export const useTransferOrderToGuest = (
373 orderId: string,
374 options?: UseMutationOptions<
375 HttpTypes.AdminOrderResponse,
376 FetchError,
377 HttpTypes.AdminTransferOrderToGuest
378 >
379 ) => {
380 return useMutation({
381 mutationFn: (payload: HttpTypes.AdminTransferOrderToGuest) =>
382 sdk.admin.order.transferToGuest(orderId, payload),
383 onSuccess: (data: any, variables: any, context: any) => {
384 queryClient.invalidateQueries({
385 queryKey: ordersQueryKeys.preview(orderId),
386 })
387
388 queryClient.invalidateQueries({
389 queryKey: ordersQueryKeys.changes(orderId),
390 })
391
392 queryClient.invalidateQueries({
393 queryKey: ordersQueryKeys.detail(orderId),
394 })
395
396 options?.onSuccess?.(data, variables, context)
397 },
398 ...options,
399 })
400 }
401
402 export const useCancelOrderTransfer = (
403 orderId: string,
404 options?: UseMutationOptions<any, FetchError, void>
405 ) => {
406 return useMutation({
407 mutationFn: () => sdk.admin.order.cancelTransfer(orderId),
408 onSuccess: (data: any, variables: any, context: any) => {
409 queryClient.invalidateQueries({
410 queryKey: ordersQueryKeys.preview(orderId),
411 })
412
413 queryClient.invalidateQueries({
414 queryKey: ordersQueryKeys.changes(orderId),
415 })
416
417 options?.onSuccess?.(data, variables, context)
418 },
419 ...options,
420 })
421 }
422
423 export const useCreateOrderCreditLine = (
424 orderId: string,
425 options?: UseMutationOptions<
426 HttpTypes.AdminOrderResponse,
427 FetchError,
428 Omit<CreateOrderCreditLineDTO, "order_id">
429 >
430 ) => {
431 return useMutation({
432 mutationFn: (payload) => sdk.admin.order.createCreditLine(orderId, payload),
433 onSuccess: (data, variables, context) => {
434 queryClient.invalidateQueries({
435 queryKey: ordersQueryKeys.details(),
436 })
437
438 queryClient.invalidateQueries({
439 queryKey: ordersQueryKeys.preview(orderId),
440 })
441
442 options?.onSuccess?.(data, variables, context)
443 },
444 ...options,
445 })
446 }
447
448 export const useAuthorizePaymentSession = (
449 orderId: string,
450 sessionId: string,
451 options?: UseMutationOptions<
452 HttpTypes.AdminAuthorizeOrderPaymentSessionResponse,
453 FetchError,
454 void
455 >
456 ) => {
457 return useMutation({
458 mutationFn: () =>
459 sdk.admin.order.authorizePaymentSession(orderId, sessionId),
460 onSuccess: (data, variables, context) => {
461 queryClient.invalidateQueries({
462 queryKey: ordersQueryKeys.detail(orderId),
463 })
464
465 queryClient.invalidateQueries({
466 queryKey: ordersQueryKeys.preview(orderId),
467 })
468
469 options?.onSuccess?.(data, variables, context)
470 },
471 ...options,
472 })
473 }
474
475 export const useUpdateOrderChange = (
476 orderChangeId: string,
477 options?: UseMutationOptions<
478 HttpTypes.AdminOrderChangeResponse,
479 FetchError,
480 { carry_over_promotions?: boolean; internal_note?: string | null }
481 >
482 ) => {
483 return useMutation({
484 mutationFn: (
485 payload: { carry_over_promotions?: boolean; internal_note?: string | null }
486 ) => sdk.admin.order.updateOrderChange(orderChangeId, payload),
487 onSuccess: (data, variables, context) => {
488 const orderId = data.order_change.order_id
489
490 queryClient.invalidateQueries({
491 queryKey: ordersQueryKeys.details(),
492 })
493
494 queryClient.invalidateQueries({
495 queryKey: ordersQueryKeys.preview(orderId),
496 })
497
498 queryClient.invalidateQueries({
499 queryKey: ordersQueryKeys.changes(orderId),
500 })
501
502 options?.onSuccess?.(data, variables, context)
503 },
504 ...options,
505 })
506 }
507
508 export const useExportOrders = (
509 query?: HttpTypes.AdminOrderFilters,
510 options?: UseMutationOptions<
511 { transaction_id: string },
512 FetchError,
513 HttpTypes.AdminOrderFilters
514 >
515 ) => {
516 return useMutation({
517 mutationFn: () => sdk.admin.order.export(query),
518 onSuccess: (data, variables, context) => {
519 options?.onSuccess?.(data, variables, context)
520 },
521 ...options,
522 })
523 }