fix(dashboard, medusa): mark shipped flow (#8065)
* fix: mark shipped routing * fix: naming
This commit is contained in:
@@ -43,10 +43,10 @@ export const useCancelFulfillment = (
|
||||
})
|
||||
}
|
||||
|
||||
export const useCreateShipment = (
|
||||
export const useCreateFulfillmentShipment = (
|
||||
fulfillmentId: string,
|
||||
options?: UseMutationOptions<
|
||||
{ order: HttpTypes.AdminOrder },
|
||||
{ fulfillment: HttpTypes.AdminFulfillment },
|
||||
Error,
|
||||
HttpTypes.AdminCreateFulfillmentShipment
|
||||
>
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { AdminCreateOrderShipment, HttpTypes } from "@medusajs/types"
|
||||
|
||||
const ORDERS_QUERY_KEY = "orders" as const
|
||||
export const ordersQueryKeys = queryKeysFactory(ORDERS_QUERY_KEY)
|
||||
@@ -81,6 +82,28 @@ export const useCancelOrderFulfillment = (
|
||||
})
|
||||
}
|
||||
|
||||
export const useCreateOrderShipment = (
|
||||
orderId: string,
|
||||
fulfillmentId: string,
|
||||
options?: UseMutationOptions<
|
||||
{ order: HttpTypes.AdminOrder },
|
||||
Error,
|
||||
HttpTypes.AdminCreateOrderShipment
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: HttpTypes.AdminCreateOrderShipment) =>
|
||||
sdk.admin.order.createShipment(orderId, fulfillmentId, payload),
|
||||
onSuccess: (data: any, variables: any, context: any) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ordersQueryKeys.details(),
|
||||
})
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useCancelOrder = (
|
||||
orderId: string,
|
||||
options?: UseMutationOptions<any, Error, any>
|
||||
|
||||
+8
-10
@@ -1,15 +1,13 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const CreateShipmentSchema = z.object({
|
||||
labels: z
|
||||
.array(
|
||||
z.object({
|
||||
tracking_number: z.string(),
|
||||
// TODO: this 2 are not optional in the API
|
||||
tracking_url: z.string().optional(),
|
||||
label_url: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.min(1),
|
||||
labels: z.array(
|
||||
z.object({
|
||||
tracking_number: z.string(),
|
||||
// TODO: this 2 are not optional in the API
|
||||
tracking_url: z.string().optional(),
|
||||
label_url: z.string().optional(),
|
||||
})
|
||||
),
|
||||
send_notification: z.boolean().optional(),
|
||||
})
|
||||
|
||||
+8
-5
@@ -11,8 +11,8 @@ import {
|
||||
RouteFocusModal,
|
||||
useRouteModal,
|
||||
} from "../../../../../components/modals"
|
||||
import { useCreateShipment } from "../../../../../hooks/api/fulfillment.tsx"
|
||||
import { CreateShipmentSchema } from "./constants"
|
||||
import { useCreateOrderShipment } from "../../../../../hooks/api"
|
||||
|
||||
type OrderCreateFulfillmentFormProps = {
|
||||
order: AdminOrder
|
||||
@@ -27,12 +27,11 @@ export function OrderCreateShipmentForm({
|
||||
const { handleSuccess } = useRouteModal()
|
||||
|
||||
const { mutateAsync: createShipment, isPending: isMutating } =
|
||||
useCreateShipment(fulfillment.id)
|
||||
useCreateOrderShipment(order.id, fulfillment.id)
|
||||
|
||||
const form = useForm<zod.infer<typeof CreateShipmentSchema>>({
|
||||
defaultValues: {
|
||||
labels: [{ tracking_number: "" }],
|
||||
send_notification: !order.no_notification, // TODO: not supported in the API
|
||||
send_notification: !order.no_notification,
|
||||
},
|
||||
resolver: zodResolver(CreateShipmentSchema),
|
||||
})
|
||||
@@ -45,6 +44,10 @@ export function OrderCreateShipmentForm({
|
||||
const handleSubmit = form.handleSubmit(async (data) => {
|
||||
await createShipment(
|
||||
{
|
||||
items: fulfillment.items.map((i) => ({
|
||||
id: i.line_item_id,
|
||||
quantity: i.quantity,
|
||||
})),
|
||||
labels: data.labels
|
||||
.filter((l) => !!l.tracking_number)
|
||||
.map((l) => ({
|
||||
@@ -52,7 +55,7 @@ export function OrderCreateShipmentForm({
|
||||
tracking_url: "#",
|
||||
label_url: "#",
|
||||
})),
|
||||
// no_notification: !data.send_notification,
|
||||
no_notification: !data.send_notification,
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ export function OrderCreateShipment() {
|
||||
const { id, f_id } = useParams()
|
||||
|
||||
const { order, isLoading, isError, error } = useOrder(id!, {
|
||||
fields: "*fulfillments",
|
||||
fields: "*fulfillments,*fulfillments.items",
|
||||
})
|
||||
|
||||
if (isError) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
AdminCreateOrderShipment,
|
||||
FindParams,
|
||||
HttpTypes,
|
||||
PaginatedResponse,
|
||||
@@ -77,4 +78,22 @@ export class Order {
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
async createShipment(
|
||||
id: string,
|
||||
fulfillmentId: string,
|
||||
body: HttpTypes.AdminCreateOrderShipment,
|
||||
query?: SelectParams,
|
||||
headers?: ClientHeaders
|
||||
) {
|
||||
return await this.client.fetch<{ order: HttpTypes.AdminOrder }>(
|
||||
`/admin/orders/${id}/fulfillments/${fulfillmentId}/shipments`,
|
||||
{
|
||||
method: "POST",
|
||||
headers,
|
||||
body,
|
||||
query,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,17 @@ export interface AdminCreateOrderFulfillment {
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
|
||||
export interface AdminCreateOrderShipment {
|
||||
items: { id: string; quantity: number }[]
|
||||
labels?: {
|
||||
tracking_number: string
|
||||
tracking_url: string
|
||||
label_url: string
|
||||
}[]
|
||||
no_notification?: boolean
|
||||
metadata?: Record<string, any>
|
||||
}
|
||||
|
||||
export interface AdminCancelOrderFulfillment {
|
||||
no_notification?: boolean
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
AdminGetOrdersParams,
|
||||
AdminOrderCancelFulfillment,
|
||||
AdminOrderCreateFulfillment,
|
||||
AdminOrderCreateShipment,
|
||||
} from "./validators"
|
||||
|
||||
export const adminOrderRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
@@ -88,4 +89,15 @@ export const adminOrderRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/orders/:id/fulfillments/:fulfillment_id/shipments",
|
||||
middlewares: [
|
||||
validateAndTransformBody(AdminOrderCreateShipment),
|
||||
validateAndTransformQuery(
|
||||
AdminGetOrdersOrderParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
AdminPostReturnsReqSchema,
|
||||
} from "./validators"
|
||||
|
||||
export const adminOrderRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
export const adminReturnRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/returns",
|
||||
|
||||
@@ -47,6 +47,7 @@ import { storeProductRoutesMiddlewares } from "./store/products/middlewares"
|
||||
import { storeRegionRoutesMiddlewares } from "./store/regions/middlewares"
|
||||
import { storeReturnReasonRoutesMiddlewares } from "./store/return-reasons/middlewares"
|
||||
import { storeShippingOptionRoutesMiddlewares } from "./store/shipping-options/middlewares"
|
||||
import { adminReturnRoutesMiddlewares } from "./admin/returns/middlewares"
|
||||
|
||||
export const config: MiddlewaresConfig = {
|
||||
routes: [
|
||||
@@ -67,6 +68,7 @@ export const config: MiddlewaresConfig = {
|
||||
...adminWorkflowsExecutionsMiddlewares,
|
||||
...storeRegionRoutesMiddlewares,
|
||||
...adminRegionRoutesMiddlewares,
|
||||
...adminReturnRoutesMiddlewares,
|
||||
...adminUserRoutesMiddlewares,
|
||||
...adminInviteRoutesMiddlewares,
|
||||
...adminTaxRateRoutesMiddlewares,
|
||||
|
||||
Reference in New Issue
Block a user