diff --git a/packages/admin-next/dashboard/src/i18n/translations/en.json b/packages/admin-next/dashboard/src/i18n/translations/en.json index 6d3cebdbfd236..f7ffa6495ee86 100644 --- a/packages/admin-next/dashboard/src/i18n/translations/en.json +++ b/packages/admin-next/dashboard/src/i18n/translations/en.json @@ -894,6 +894,9 @@ "cancelClaim": { "successToast": "Claim was successfully canceled." } + }, + "tooltips": { + "onlyReturnShippingOptions": "This list will consist of only return shipping options." } }, "reservations": { diff --git a/packages/admin-next/dashboard/src/routes/orders/order-create-claim/claim-create.tsx b/packages/admin-next/dashboard/src/routes/orders/order-create-claim/claim-create.tsx index ad40dc71bcb1a..16a669422f46e 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-create-claim/claim-create.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-create-claim/claim-create.tsx @@ -6,6 +6,7 @@ import { useNavigate, useParams } from "react-router-dom" import { RouteFocusModal } from "../../../components/modals" import { useClaim, useCreateClaim } from "../../../hooks/api/claims" import { useOrder, useOrderPreview } from "../../../hooks/api/orders" +import { useReturn } from "../../../hooks/api/returns" import { DEFAULT_FIELDS } from "../order-detail/constants" import { ClaimCreateForm } from "./components/claim-create-form" @@ -27,6 +28,9 @@ export const ClaimCreate = () => { const { claim } = useClaim(activeClaimId!, undefined, { enabled: !!activeClaimId, }) + const { return: orderReturn } = useReturn(claim?.return_id!, undefined, { + enabled: !!claim?.return_id, + }) useEffect(() => { async function run() { @@ -68,7 +72,12 @@ export const ClaimCreate = () => { return ( {claim && preview && order && ( - + )} ) diff --git a/packages/admin-next/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-create-form.tsx b/packages/admin-next/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-create-form.tsx index 0739a06129085..c220f7603dc1d 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-create-form.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-create-form.tsx @@ -36,6 +36,7 @@ import { AddClaimItemsTable } from "../add-claim-items-table" import { ClaimInboundItem } from "./claim-inbound-item.tsx" import { ClaimCreateSchema, CreateClaimSchemaType } from "./schema" +import { AdminReturn } from "@medusajs/types" import { useAddClaimInboundItems, useAddClaimInboundShipping, @@ -45,6 +46,7 @@ import { useUpdateClaimInboundItem, useUpdateClaimInboundShipping, } from "../../../../../hooks/api/claims" +import { useUpdateReturn } from "../../../../../hooks/api/returns.tsx" import { sdk } from "../../../../../lib/client" import { currencies } from "../../../../../lib/data/currencies" import { ClaimOutboundSection } from "./claim-outbound-section" @@ -54,6 +56,7 @@ type ReturnCreateFormProps = { order: AdminOrder claim: AdminClaim preview: AdminOrderPreview + orderReturn?: AdminReturn } let itemsToAdd: string[] = [] @@ -64,6 +67,7 @@ export const ClaimCreateForm = ({ order, preview, claim, + orderReturn, }: ReturnCreateFormProps) => { const { t } = useTranslation() const { handleSuccess } = useRouteModal() @@ -78,18 +82,6 @@ export const ClaimCreateForm = ({ Record >({}) - /** - * HOOKS - */ - const { stock_locations = [] } = useStockLocations({ limit: 999 }) - const { shipping_options = [] } = useShippingOptions({ - limit: 999, - fields: "*prices,+service_zone.fulfillment_set.location.id", - /** - * TODO: this should accept filter for location_id - */ - }) - /** * MUTATIONS */ @@ -100,7 +92,11 @@ export const ClaimCreateForm = ({ useCancelClaimRequest(claim.id, order.id) // TODO: implement update claim request - const { mutateAsync: updateClaimRequest, isPending: isUpdating } = {} // useUpdateClaim(claim.id, order.id) + + const { mutateAsync: updateReturn, isPending: isUpdating } = useUpdateReturn( + claim.return_id!, + order.id + ) const { mutateAsync: addInboundShipping, @@ -166,9 +162,17 @@ export const ClaimCreateForm = ({ */ const form = useForm({ defaultValues: () => { - const method = preview.shipping_methods.find( - (s) => !!s.actions?.find((a) => a.action === "SHIPPING_ADD") - ) + const inboundShippingMethod = preview.shipping_methods.find((s) => { + const action = s.actions?.find((a) => a.action === "SHIPPING_ADD") + + return !!action?.return?.id + }) + + const outboundShippingMethod = preview.shipping_methods.find((s) => { + const action = s.actions?.find((a) => a.action === "SHIPPING_ADD") + + return action && !!!action?.return?.id + }) return Promise.resolve({ inbound_items: inboundPreviewItems.map((i) => { @@ -189,16 +193,43 @@ export const ClaimCreateForm = ({ variant_id: i.variant_id, quantity: i.detail.quantity, })), - inbound_option_id: method ? method.shipping_option_id : "", - // TODO: pick up shipping method for outbound when available - outbound_option_id: method ? method.shipping_option_id : "", - location_id: "", + inbound_option_id: inboundShippingMethod + ? inboundShippingMethod.shipping_option_id + : "", + outbound_option_id: outboundShippingMethod + ? outboundShippingMethod.shipping_option_id + : "", + location_id: orderReturn?.location_id, send_notification: false, }) }, resolver: zodResolver(ClaimCreateSchema), }) + const locationId = form.watch("location_id") + + /** + * HOOKS + */ + const { stock_locations = [] } = useStockLocations({ limit: 999 }) + const { shipping_options = [] } = useShippingOptions( + { + limit: 999, + fields: "*prices,+service_zone.fulfillment_set.location.id", + stock_location_id: locationId, + }, + { + enabled: !!locationId, + } + ) + + const inboundShippingOptions = shipping_options.filter( + (shippingOption) => + !!shippingOption.rules.find( + (r) => r.attribute === "is_return" && r.value === "true" + ) + ) + const { fields: inboundItems, append, @@ -257,8 +288,11 @@ export const ClaimCreateForm = ({ } }, [preview.shipping_methods]) + useEffect(() => { + form.setValue("location_id", orderReturn?.location_id) + }, [orderReturn]) + const showInboundItemsPlaceholder = !inboundItems.length - const locationId = form.watch("location_id") const shippingOptionId = form.watch("inbound_option_id") const handleSubmit = form.handleSubmit(async (data) => { @@ -307,7 +341,7 @@ export const ClaimCreateForm = ({ } const onLocationChange = async (selectedLocationId?: string | null) => { - await updateClaimRequest({ location_id: selectedLocationId }) + await updateReturn({ location_id: selectedLocationId }) } const onShippingOptionChange = async (selectedOptionId: string) => { @@ -379,7 +413,7 @@ export const ClaimCreateForm = ({ ).variants variants.forEach((variant) => { - ret[variant.id] = variant.inventory[0]?.location_levels || [] + ret[variant.id] = variant.inventory?.[0]?.location_levels || [] }) return ret @@ -549,12 +583,12 @@ export const ClaimCreateForm = ({ { onChange(v) onLocationChange(v) }} - {...field} options={(stock_locations ?? []).map( (stockLocation) => ({ label: stockLocation.name, @@ -572,9 +606,14 @@ export const ClaimCreateForm = ({ {/*INBOUND SHIPPING*/}
- + {t("orders.returns.inboundShipping")} + {t("orders.returns.inboundShippingHint")} @@ -595,23 +634,10 @@ export const ClaimCreateForm = ({ val && onShippingOptionChange(val) }} {...field} - options={(shipping_options ?? []) - .filter( - (so) => - (locationId - ? so.service_zone.fulfillment_set! - .location.id === locationId - : true) && - !!so.rules.find( - (r) => - r.attribute === "is_return" && - r.value === "true" - ) - ) - .map((so) => ({ - label: so.name, - value: so.id, - }))} + options={inboundShippingOptions.map((so) => ({ + label: so.name, + value: so.id, + }))} disabled={!locationId} /> diff --git a/packages/admin-next/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-outbound-section.tsx b/packages/admin-next/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-outbound-section.tsx index da1da35c3cea1..052f47027def1 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-outbound-section.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-create-claim/components/claim-create-form/claim-outbound-section.tsx @@ -24,7 +24,6 @@ import { useUpdateClaimOutboundItems, } from "../../../../../hooks/api/claims" import { useShippingOptions } from "../../../../../hooks/api/shipping-options" -import { useStockLocations } from "../../../../../hooks/api/stock-locations" import { sdk } from "../../../../../lib/client" import { AddClaimOutboundItemsTable } from "../add-claim-outbound-items-table" import { ClaimOutboundItem } from "./claim-outbound-item" @@ -57,13 +56,9 @@ export const ClaimOutboundSection = ({ /** * HOOKS */ - const { stock_locations = [] } = useStockLocations({ limit: 999 }) const { shipping_options = [] } = useShippingOptions({ limit: 999, fields: "*prices,+service_zone.fulfillment_set.location.id", - /** - * TODO: this should accept filter for location_id - */ }) const { mutateAsync: addOutboundShipping } = useAddClaimOutboundShipping( @@ -154,17 +149,6 @@ export const ClaimOutboundSection = ({ }) }, [previewOutboundItems]) - useEffect(() => { - // TODO: Pick the shipping methods from actions where return_id is null for outbound - const method = preview.shipping_methods.find( - (s) => !!s.actions?.find((a) => a.action === "SHIPPING_ADD") - ) - - if (method) { - form.setValue("outbound_option_id", method.shipping_option_id) - } - }, [preview.shipping_methods]) - const locationId = form.watch("location_id") const showOutboundItemsPlaceholder = !outboundItems.length @@ -201,17 +185,16 @@ export const ClaimOutboundSection = ({ setIsOpen("outbound-items", false) } - // TODO: implement outbound shipping - const { mutateAsync: updateClaimRequest, isPending: isUpdating } = {} // useUpdateClaim(claim.id, order.id) - const onLocationChange = async (selectedLocationId?: string | null) => { - await updateClaimRequest({ location_id: selectedLocationId }) - } - const onShippingOptionChange = async (selectedOptionId: string) => { - const promises = preview.shipping_methods - .map((s) => s.actions?.find((a) => a.action === "SHIPPING_ADD")?.id) + const outboundShippingMethods = preview.shipping_methods.filter((s) => { + const action = s.actions?.find((a) => a.action === "SHIPPING_ADD") + + return action && !!!action?.return?.id + }) + + const promises = outboundShippingMethods .filter(Boolean) - .map(deleteOutboundShipping) + .map((action) => deleteOutboundShipping(action.id)) await Promise.all(promises) @@ -270,7 +253,7 @@ export const ClaimOutboundSection = ({ ).variants variants.forEach((variant) => { - ret[variant.id] = variant.inventory[0]?.location_levels || [] + ret[variant.id] = variant.inventory?.[0]?.location_levels || [] }) return ret @@ -403,24 +386,11 @@ export const ClaimOutboundSection = ({ val && onShippingOptionChange(val) }} {...field} - options={(shipping_options ?? []) - .filter( - (so) => - (locationId - ? so.service_zone.fulfillment_set!.location - .id === locationId - : true) && - !!so.rules.find( - (r) => - r.attribute === "is_return" && - r.value === "true" - ) - ) - .map((so) => ({ - label: so.name, - value: so.id, - }))} - disabled={!locationId} + options={shipping_options.map((so) => ({ + label: so.name, + value: so.id, + }))} + disabled={!shipping_options.length} /> diff --git a/packages/core/types/src/http/return/admin.ts b/packages/core/types/src/http/return/admin.ts index 7ea64f9fed672..c766025e6c8f5 100644 --- a/packages/core/types/src/http/return/admin.ts +++ b/packages/core/types/src/http/return/admin.ts @@ -17,6 +17,7 @@ export interface AdminReturn { order_id: string status?: string exchange_id?: string + location_id?: string claim_id?: string order_version: number display_id: number @@ -79,7 +80,7 @@ export interface AdminConfirmReturnRequest { } export interface AdminUpdateReturnRequest { - location_id?: string + location_id?: string | null no_notification?: boolean metadata?: Record | null } diff --git a/packages/core/types/src/http/shipping-option/admin/queries.ts b/packages/core/types/src/http/shipping-option/admin/queries.ts index ccd74fc1e6741..10f0648ed39d0 100644 --- a/packages/core/types/src/http/shipping-option/admin/queries.ts +++ b/packages/core/types/src/http/shipping-option/admin/queries.ts @@ -5,6 +5,7 @@ export interface AdminShippingOptionListParams extends FindParams { id?: string | string[] q?: string service_zone_id?: string + stock_location_id?: string | string[] shipping_profile_id?: string provider_id?: string shipping_option_type_id?: string