feat(core-flows,dashboard,types,medusa): delete shipping methods when all inbound/outbound items are deleted (#9106)
* feat(core-flows,dashboard,types,medusa): delete shipping methods when all inbound/outbound items are deleted * chore: fix specs
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
useMutation,
|
||||
@@ -307,6 +307,10 @@ export const useRemoveClaimInboundItem = (
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.details(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
@@ -6,7 +7,6 @@ import {
|
||||
useQuery,
|
||||
UseQueryOptions,
|
||||
} from "@tanstack/react-query"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
@@ -280,11 +280,16 @@ export const useRemoveExchangeInboundItem = (
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.exchange.removeInboundItem(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
queryKey: returnsQueryKeys.details(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
@@ -356,6 +361,7 @@ export const useDeleteExchangeInboundShipping = (
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
@@ -423,9 +429,14 @@ export const useRemoveExchangeOutboundItem = (
|
||||
mutationFn: (actionId: string) =>
|
||||
sdk.admin.exchange.removeOutboundItem(id, actionId),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
|
||||
@@ -7,11 +7,11 @@ import {
|
||||
UseQueryOptions,
|
||||
} from "@tanstack/react-query"
|
||||
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { ordersQueryKeys } from "./orders"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
|
||||
const RETURNS_QUERY_KEY = "returns" as const
|
||||
export const returnsQueryKeys = queryKeysFactory(RETURNS_QUERY_KEY)
|
||||
@@ -169,9 +169,11 @@ export const useCancelReturnRequest = (
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.details(),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
@@ -257,6 +259,10 @@ export const useRemoveReturnItem = (
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.details(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
@@ -369,6 +375,10 @@ export const useDeleteReturnShipping = (
|
||||
queryKey: ordersQueryKeys.preview(orderId),
|
||||
})
|
||||
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: returnsQueryKeys.details(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
|
||||
+28
-9
@@ -323,22 +323,26 @@ export const ClaimCreateForm = ({
|
||||
}, [previewItems])
|
||||
|
||||
useEffect(() => {
|
||||
let method = preview.shipping_methods.find(
|
||||
const inboundShipping = preview.shipping_methods.find(
|
||||
(s) =>
|
||||
!!s.actions?.find((a) => a.action === "SHIPPING_ADD" && !!a.return_id)
|
||||
)
|
||||
|
||||
if (method) {
|
||||
form.setValue("inbound_option_id", method.shipping_option_id)
|
||||
if (inboundShipping) {
|
||||
form.setValue("inbound_option_id", inboundShipping.shipping_option_id)
|
||||
} else {
|
||||
form.setValue("inbound_option_id", null)
|
||||
}
|
||||
|
||||
method = preview.shipping_methods.find(
|
||||
const outboundShipping = preview.shipping_methods.find(
|
||||
(s) =>
|
||||
!!s.actions?.find((a) => a.action === "SHIPPING_ADD" && !a.return_id)
|
||||
)
|
||||
|
||||
if (method) {
|
||||
form.setValue("outbound_option_id", method.shipping_option_id)
|
||||
if (outboundShipping) {
|
||||
form.setValue("outbound_option_id", outboundShipping.shipping_option_id)
|
||||
} else {
|
||||
form.setValue("outbound_option_id", null)
|
||||
}
|
||||
}, [preview.shipping_methods])
|
||||
|
||||
@@ -420,10 +424,25 @@ export const ClaimCreateForm = ({
|
||||
}
|
||||
|
||||
const onShippingOptionChange = async (selectedOptionId: string) => {
|
||||
const promises = preview.shipping_methods
|
||||
.map((s) => s.actions?.find((a) => a.action === "SHIPPING_ADD")?.id)
|
||||
const inboundShippingMethods = preview.shipping_methods.filter((s) => {
|
||||
const action = s.actions?.find(
|
||||
(a) => a.action === "SHIPPING_ADD" && !!a.return_id
|
||||
)
|
||||
|
||||
return action && !!!action?.return_id
|
||||
})
|
||||
|
||||
const promises = inboundShippingMethods
|
||||
.filter(Boolean)
|
||||
.map(deleteInboundShipping)
|
||||
.map((inboundShippingMethod) => {
|
||||
const action = inboundShippingMethod.actions?.find(
|
||||
(a) => a.action === "SHIPPING_ADD" && !!a.return_id
|
||||
)
|
||||
|
||||
if (action) {
|
||||
return deleteInboundShipping(action.id)
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all(promises)
|
||||
|
||||
|
||||
+6
-4
@@ -191,20 +191,22 @@ export const ClaimOutboundSection = ({
|
||||
|
||||
const onShippingOptionChange = async (selectedOptionId: string) => {
|
||||
const outboundShippingMethods = preview.shipping_methods.filter((s) => {
|
||||
const action = s.actions?.find((a) => a.action === "SHIPPING_ADD")
|
||||
const action = s.actions?.find(
|
||||
(a) => a.action === "SHIPPING_ADD" && !a.return_id
|
||||
)
|
||||
|
||||
return action && !!!action?.return?.id
|
||||
return action && !!!action?.return_id
|
||||
})
|
||||
|
||||
const promises = outboundShippingMethods
|
||||
.filter(Boolean)
|
||||
.map((outboundShippingMethod) => {
|
||||
const action = outboundShippingMethod.actions?.find(
|
||||
(a) => a.action === "SHIPPING_ADD"
|
||||
(a) => a.action === "SHIPPING_ADD" && !a.return_id
|
||||
)
|
||||
|
||||
if (action) {
|
||||
deleteOutboundShipping(action.id)
|
||||
return deleteOutboundShipping(action.id)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
+10
-12
@@ -188,17 +188,17 @@ export const ExchangeInboundSection = ({
|
||||
}, [previewInboundItems])
|
||||
|
||||
useEffect(() => {
|
||||
const inboundShippingMethod = preview.shipping_methods.find((s) => {
|
||||
const action = s.actions?.find((a) => a.action === "SHIPPING_ADD")
|
||||
|
||||
return !!action?.return?.id
|
||||
})
|
||||
const inboundShippingMethod = preview.shipping_methods.find((s) =>
|
||||
s.actions?.find((a) => a.action === "SHIPPING_ADD" && !!a.return_id)
|
||||
)
|
||||
|
||||
if (inboundShippingMethod) {
|
||||
form.setValue(
|
||||
"inbound_option_id",
|
||||
inboundShippingMethod.shipping_option_id
|
||||
)
|
||||
} else {
|
||||
form.setValue("inbound_option_id", null)
|
||||
}
|
||||
}, [preview.shipping_methods])
|
||||
|
||||
@@ -246,21 +246,19 @@ export const ExchangeInboundSection = ({
|
||||
}
|
||||
|
||||
const onShippingOptionChange = async (selectedOptionId: string) => {
|
||||
const inboundShippingMethods = preview.shipping_methods.filter((s) => {
|
||||
const action = s.actions?.find((a) => a.action === "SHIPPING_ADD")
|
||||
|
||||
return action && !!action?.return?.id
|
||||
})
|
||||
const inboundShippingMethods = preview.shipping_methods.filter((s) =>
|
||||
s.actions?.find((a) => a.action === "SHIPPING_ADD" && !!a.return_id)
|
||||
)
|
||||
|
||||
const promises = inboundShippingMethods
|
||||
.filter(Boolean)
|
||||
.map((inboundShippingMethod) => {
|
||||
const action = inboundShippingMethod.actions?.find(
|
||||
(a) => a.action === "SHIPPING_ADD"
|
||||
(a) => a.action === "SHIPPING_ADD" && !!a.return_id
|
||||
)
|
||||
|
||||
if (action) {
|
||||
deleteInboundShipping(action.id)
|
||||
return deleteInboundShipping(action.id)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
+19
-7
@@ -188,22 +188,34 @@ export const ExchangeOutboundSection = ({
|
||||
setIsOpen("outbound-items", false)
|
||||
}
|
||||
|
||||
const onShippingOptionChange = async (selectedOptionId: string) => {
|
||||
const outboundShippingMethods = preview.shipping_methods.filter((s) => {
|
||||
const action = s.actions?.find((a) => a.action === "SHIPPING_ADD")
|
||||
useEffect(() => {
|
||||
const outboundShipping = preview.shipping_methods.find(
|
||||
(s) =>
|
||||
!!s.actions?.find((a) => a.action === "SHIPPING_ADD" && !a.return_id)
|
||||
)
|
||||
|
||||
return !action?.return_id
|
||||
})
|
||||
if (outboundShipping) {
|
||||
form.setValue("outbound_option_id", outboundShipping.shipping_option_id)
|
||||
} else {
|
||||
form.setValue("outbound_option_id", null)
|
||||
}
|
||||
}, [preview.shipping_methods])
|
||||
|
||||
const onShippingOptionChange = async (selectedOptionId: string) => {
|
||||
const outboundShippingMethods = preview.shipping_methods.filter(
|
||||
(s) =>
|
||||
!!s.actions?.find((a) => a.action === "SHIPPING_ADD" && !a.return_id)
|
||||
)
|
||||
|
||||
const promises = outboundShippingMethods
|
||||
.filter(Boolean)
|
||||
.map((outboundShippingMethod) => {
|
||||
const action = outboundShippingMethod.actions?.find(
|
||||
(a) => a.action === "SHIPPING_ADD"
|
||||
(a) => a.action === "SHIPPING_ADD" && !a.return_id
|
||||
)
|
||||
|
||||
if (action) {
|
||||
deleteOutboundShipping(action.id)
|
||||
return deleteOutboundShipping(action.id)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
+19
-8
@@ -1,6 +1,11 @@
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { PencilSquare } from "@medusajs/icons"
|
||||
import { AdminOrder, InventoryLevelDTO, ReturnDTO } from "@medusajs/types"
|
||||
import {
|
||||
AdminOrder,
|
||||
AdminOrderPreview,
|
||||
AdminReturn,
|
||||
InventoryLevelDTO,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
Alert,
|
||||
Button,
|
||||
@@ -47,8 +52,8 @@ import { ReturnCreateSchema, ReturnCreateSchemaType } from "./schema"
|
||||
|
||||
type ReturnCreateFormProps = {
|
||||
order: AdminOrder
|
||||
activeReturn: ReturnDTO // TODO: AdminReturn
|
||||
preview: AdminOrder // TODO
|
||||
activeReturn: AdminReturn
|
||||
preview: AdminOrderPreview
|
||||
}
|
||||
|
||||
let selectedItems: string[] = []
|
||||
@@ -62,7 +67,7 @@ export const ReturnCreateForm = ({
|
||||
const { handleSuccess } = useRouteModal()
|
||||
|
||||
const itemsMap = useMemo(
|
||||
() => new Map(order.items.map((i) => [i.id, i])),
|
||||
() => new Map((order.items || []).map((i) => [i.id, i])),
|
||||
[order.items]
|
||||
)
|
||||
|
||||
@@ -171,7 +176,7 @@ export const ReturnCreateForm = ({
|
||||
?.reason_id,
|
||||
})),
|
||||
option_id: method ? method.shipping_option_id : "",
|
||||
location_id: "",
|
||||
location_id: activeReturn?.location_id,
|
||||
send_notification: false,
|
||||
})
|
||||
},
|
||||
@@ -189,7 +194,7 @@ export const ReturnCreateForm = ({
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
const existingItemsMap = {}
|
||||
const existingItemsMap: Record<string, boolean> = {}
|
||||
|
||||
previewItems.forEach((i) => {
|
||||
const ind = items.findIndex((field) => field.item_id === i.id)
|
||||
@@ -229,12 +234,14 @@ export const ReturnCreateForm = ({
|
||||
}, [previewItems])
|
||||
|
||||
useEffect(() => {
|
||||
const method = preview.shipping_methods.find(
|
||||
const method = preview.shipping_methods?.find(
|
||||
(s) => !!s.actions?.find((a) => a.action === "SHIPPING_ADD")
|
||||
)
|
||||
|
||||
if (method) {
|
||||
form.setValue("option_id", method.shipping_option_id)
|
||||
form.setValue("option_id", method.shipping_option_id!)
|
||||
} else {
|
||||
form.setValue("option_id", "")
|
||||
}
|
||||
}, [preview.shipping_methods])
|
||||
|
||||
@@ -300,6 +307,10 @@ export const ReturnCreateForm = ({
|
||||
}
|
||||
}, [isShippingPriceEdit])
|
||||
|
||||
useEffect(() => {
|
||||
form.setValue("location_id", activeReturn?.location_id || "")
|
||||
}, [activeReturn])
|
||||
|
||||
const showLevelsWarning = useMemo(() => {
|
||||
if (!locationId) {
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user