Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

feat(core-plugin): add top-level vendor inventory location-levels batch endpoint

vholika0c28fd11119
diff --git a/packages/core-plugin/src/api/vendor/inventory-items/location-levels/batch/route.ts b/packages/core-plugin/src/api/vendor/inventory-items/location-levels/batch/route.ts
new file mode 100644
index 000000000..dda5eb9f1
--- /dev/null
+++ b/packages/core-plugin/src/api/vendor/inventory-items/location-levels/batch/route.ts
@@ -0,0 +1,30 @@
+import {
+ AuthenticatedMedusaRequest,
+ MedusaResponse,
+} from "@medusajs/framework/http"
+import { batchInventoryItemLevelsWorkflow } from "@medusajs/core-flows"
+import { HttpTypes } from "@mercurjs/types"
+
+import { VendorBatchInventoryItemLevelsType } from "../../validators"
+
+export const POST = async (
+ req: AuthenticatedMedusaRequest<VendorBatchInventoryItemLevelsType>,
+ res: MedusaResponse<HttpTypes.VendorBatchInventoryItemLevelResponse>
+) => {
+ const body = req.validatedBody
+
+ const { result } = await batchInventoryItemLevelsWorkflow(req.scope).run({
+ input: {
+ create: body.create ?? [],
+ update: body.update ?? [],
+ delete: body.delete ?? [],
+ force: body.force ?? false,
+ },
+ })
+
+ res.json({
+ created: result.created,
+ updated: result.updated,
+ deleted: result.deleted,
+ })
+}
diff --git a/packages/core-plugin/src/api/vendor/inventory-items/middlewares.ts b/packages/core-plugin/src/api/vendor/inventory-items/middlewares.ts
index 431f2e008..3ad18cf3f 100644
--- a/packages/core-plugin/src/api/vendor/inventory-items/middlewares.ts
+++ b/packages/core-plugin/src/api/vendor/inventory-items/middlewares.ts
@@ -12,6 +12,7 @@ import {
import { vendorInventoryItemQueryConfig, vendorLocationLevelQueryConfig } from "./query-config"
import {
+ VendorBatchInventoryItemLevels,
VendorBatchInventoryItemLocationsLevel,
VendorCreateInventoryItem,
VendorCreateInventoryLocationLevel,
@@ -96,6 +97,17 @@ export const vendorInventoryItemsMiddlewares: MiddlewareRoute[] = [
),
],
},
+ {
+ method: ["POST"],
+ matcher: "/vendor/inventory-items/location-levels/batch",
+ middlewares: [
+ validateAndTransformBody(VendorBatchInventoryItemLevels),
+ validateAndTransformQuery(
+ VendorGetInventoryLocationLevelParams,
+ vendorLocationLevelQueryConfig.retrieve
+ ),
+ ],
+ },
{
method: ["POST"],
matcher: "/vendor/inventory-items/:id/location-levels",
diff --git a/packages/core-plugin/src/api/vendor/inventory-items/validators.ts b/packages/core-plugin/src/api/vendor/inventory-items/validators.ts
index 3504b5883..094e7df18 100644
--- a/packages/core-plugin/src/api/vendor/inventory-items/validators.ts
+++ b/packages/core-plugin/src/api/vendor/inventory-items/validators.ts
@@ -91,6 +91,25 @@ export const VendorBatchInventoryItemLocationsLevel = z.object({
force: z.boolean().optional(),
})
+const VendorBatchInventoryLocationLevel = z.object({
+ inventory_item_id: z.string(),
+ location_id: z.string(),
+ stocked_quantity: z.number().min(0).optional(),
+ incoming_quantity: z.number().min(0).optional(),
+})
+
+export type VendorBatchInventoryItemLevelsType = z.infer<
+ typeof VendorBatchInventoryItemLevels
+>
+export const VendorBatchInventoryItemLevels = z
+ .object({
+ create: z.array(VendorBatchInventoryLocationLevel).optional(),
+ update: z.array(VendorBatchInventoryLocationLevel).optional(),
+ delete: z.array(z.string()).optional(),
+ force: z.boolean().optional(),
+ })
+ .strict()
+
export type VendorUpdateInventoryLocationLevelType = z.infer<
typeof VendorUpdateInventoryLocationLevel
>
diff --git a/packages/vendor/src/get-route-map.tsx b/packages/vendor/src/get-route-map.tsx
index e66292946..d82c74241 100644
--- a/packages/vendor/src/get-route-map.tsx
+++ b/packages/vendor/src/get-route-map.tsx
@@ -402,6 +402,10 @@ export function getRouteMap({
path: "create",
lazy: () => import("./pages/inventory/create"),
},
+ {
+ path: "stock",
+ lazy: () => import("./pages/inventory/[id]/stock"),
+ },
],
},
{
@@ -434,10 +438,6 @@ export function getRouteMap({
lazy: () =>
import("./pages/inventory/[id]/_components/edit-inventory-item-attributes"),
},
- {
- path: "stock",
- lazy: () => import("./pages/inventory/[id]/stock"),
- },
{
path: "metadata",
lazy: () =>
diff --git a/packages/vendor/src/hooks/api/inventory.tsx b/packages/vendor/src/hooks/api/inventory.tsx
index b1ced95ff..a02275135 100644
--- a/packages/vendor/src/hooks/api/inventory.tsx
+++ b/packages/vendor/src/hooks/api/inventory.tsx
@@ -2,28 +2,28 @@ import {
ClientError,
InferClientInput,
InferClientOutput,
-} from "@mercurjs/client"
+} from "@mercurjs/client";
import {
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
-} from "@tanstack/react-query"
+} from "@tanstack/react-query";
-import { sdk, fetchQuery } from "../../lib/client"
-import { queryClient } from "../../lib/query-client"
-import { queryKeysFactory } from "../../lib/query-key-factory"
-import { variantsQueryKeys } from "./products"
+import { sdk, fetchQuery } from "../../lib/client";
+import { queryClient } from "../../lib/query-client";
+import { queryKeysFactory } from "../../lib/query-key-factory";
+import { variantsQueryKeys } from "./products";
-const INVENTORY_ITEMS_QUERY_KEY = "inventory_items" as const
+const INVENTORY_ITEMS_QUERY_KEY = "inventory_items" as const;
export const inventoryItemsQueryKeys = queryKeysFactory(
- INVENTORY_ITEMS_QUERY_KEY
-)
+ INVENTORY_ITEMS_QUERY_KEY,
+);
-const INVENTORY_ITEM_LEVELS_QUERY_KEY = "inventory_item_levels" as const
+const INVENTORY_ITEM_LEVELS_QUERY_KEY = "inventory_item_levels" as const;
export const inventoryItemLevelsQueryKeys = queryKeysFactory(
- INVENTORY_ITEM_LEVELS_QUERY_KEY
-)
+ INVENTORY_ITEM_LEVELS_QUERY_KEY,
+);
export const useInventoryItems = (
query?: InferClientInput<typeof sdk.vendor.inventoryItems.query>,
@@ -31,56 +31,56 @@ export const useInventoryItems = (
unknown,
ClientError,
InferClientOutput<typeof sdk.vendor.inventoryItems.query>
- >
+ >,
) => {
const { data, ...rest } = useQuery({
queryFn: () => sdk.vendor.inventoryItems.query({ ...query }),
queryKey: inventoryItemsQueryKeys.list(query),
...options,
- })
+ });
- return { ...data, ...rest }
-}
+ return { ...data, ...rest };
+};
export const useInventoryItem = (
id: string,
query?: Omit<
InferClientInput<typeof sdk.vendor.inventoryItems.$id.query>,
- "$id"
+ "$id"
>,
options?: UseQueryOptions<
unknown,
ClientError,
InferClientOutput<typeof sdk.vendor.inventoryItems.$id.query>
- >
+ >,
) => {
const { data, ...rest } = useQuery({
queryFn: () => sdk.vendor.inventoryItems.$id.query({ $id: id, ...query }),
queryKey: inventoryItemsQueryKeys.detail(id),
...options,
- })
+ });
- return { ...data, ...rest }
-}
+ return { ...data, ...rest };
+};
export const useCreateInventoryItem = (
options?: UseMutationOptions<
InferClientOutput<typeof sdk.vendor.inventoryItems.mutate>,
ClientError,
InferClientInput<typeof sdk.vendor.inventoryItems.mutate>
- >
+ >,
) => {
return useMutation({
mutationFn: (payload) => sdk.vendor.inventoryItems.mutate(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.lists(),
- })
- options?.onSuccess?.(data, variables, context)
+ });
+ options?.onSuccess?.(data, variables, context);
},
...options,
- })
-}
+ });
+};
export const useUpdateInventoryItem = (
id: string,
@@ -88,7 +88,7 @@ export const useUpdateInventoryItem = (
InferClientOutput<typeof sdk.vendor.inventoryItems.$id.mutate>,
ClientError,
Omit<InferClientInput<typeof sdk.vendor.inventoryItems.$id.mutate>, "$id">
- >
+ >,
) => {
return useMutation({
mutationFn: (payload) =>
@@ -96,15 +96,15 @@ export const useUpdateInventoryItem = (
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.lists(),
- })
+ });
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.detail(id),
- })
- options?.onSuccess?.(data, variables, context)
+ });
+ options?.onSuccess?.(data, variables, context);
},
...options,
- })
-}
+ });
+};
export const useDeleteInventoryItem = (
id: string,
@@ -112,31 +112,33 @@ export const useDeleteInventoryItem = (
InferClientOutput<typeof sdk.vendor.inventoryItems.$id.delete>,
ClientError,
void
- >
+ >,
) => {
return useMutation({
mutationFn: () => sdk.vendor.inventoryItems.$id.delete({ $id: id }),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.lists(),
- })
+ });
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.detail(id),
- })
- options?.onSuccess?.(data, variables, context)
+ });
+ options?.onSuccess?.(data, variables, context);
},
...options,
- })
-}
+ });
+};
export const useDeleteInventoryItemLevel = (
inventoryItemId: string,
locationId: string,
options?: UseMutationOptions<
- InferClientOutput<typeof sdk.vendor.inventoryItems.$id.locationLevels.$locationId.delete>,
+ InferClientOutput<
+ typeof sdk.vendor.inventoryItems.$id.locationLevels.$locationId.delete
+ >,
ClientError,
void
- >
+ >,
) => {
return useMutation({
mutationFn: () =>
@@ -147,63 +149,74 @@ export const useDeleteInventoryItemLevel = (
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.lists(),
- })
+ });
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId),
- })
+ });
queryClient.invalidateQueries({
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
- })
- options?.onSuccess?.(data, variables, context)
+ });
+ options?.onSuccess?.(data, variables, context);
},
...options,
- })
-}
+ });
+};
export const useInventoryItemLevels = (
inventoryItemId: string,
query?: Omit<
InferClientInput<typeof sdk.vendor.inventoryItems.$id.locationLevels.query>,
- "$id"
+ "$id"
>,
options?: UseQueryOptions<
unknown,
ClientError,
InferClientOutput<typeof sdk.vendor.inventoryItems.$id.locationLevels.query>
- >
+ >,
) => {
const { data, ...rest } = useQuery({
queryFn: () => {
if (!inventoryItemId) {
- throw new Error("inventoryItemId is required for useInventoryItemLevels")
+ throw new Error(
+ "inventoryItemId is required for useInventoryItemLevels",
+ );
}
- return fetchQuery(`/vendor/inventory-items/${inventoryItemId}/location-levels`, {
- method: "GET",
- query: { ...query },
- })
+ return fetchQuery(
+ `/vendor/inventory-items/${inventoryItemId}/location-levels`,
+ {
+ method: "GET",
+ query: { ...query },
+ },
+ );
},
queryKey: inventoryItemLevelsQueryKeys.list({
...(query || {}),
inventoryItemId,
}),
...options,
- enabled: !!inventoryItemId && (options?.enabled !== undefined ? options.enabled : true),
- })
+ enabled:
+ !!inventoryItemId &&
+ (options?.enabled !== undefined ? options.enabled : true),
+ });
- return { ...data, ...rest }
-}
+ return { ...data, ...rest };
+};
export const useUpdateInventoryLevel = (
inventoryItemId: string,
locationId: string,
options?: UseMutationOptions<
- InferClientOutput<typeof sdk.vendor.inventoryItems.$id.locationLevels.$locationId.mutate>,
+ InferClientOutput<
+ typeof sdk.vendor.inventoryItems.$id.locationLevels.$locationId.mutate
+ >,
ClientError,
Omit<
- InferClientInput<typeof sdk.vendor.inventoryItems.$id.locationLevels.$locationId.mutate>,
+ InferClientInput<
+ typeof sdk.vendor.inventoryItems.$id.locationLevels.$locationId.mutate
+ >,
"$id" | "$locationId"
>
- >
+ >,
) => {
return useMutation({
mutationFn: (payload) =>
@@ -215,32 +228,36 @@ export const useUpdateInventoryLevel = (
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.lists(),
- })
+ });
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId),
- })
+ });
queryClient.invalidateQueries({
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
- })
+ });
queryClient.invalidateQueries({
queryKey: variantsQueryKeys.details(),
- })
- options?.onSuccess?.(data, variables, context)
+ });
+ options?.onSuccess?.(data, variables, context);
},
...options,
- })
-}
+ });
+};
export const useBatchInventoryItemLocationLevels = (
inventoryItemId: string,
options?: UseMutationOptions<
- InferClientOutput<typeof sdk.vendor.inventoryItems.$id.locationLevels.batch.mutate>,
+ InferClientOutput<
+ typeof sdk.vendor.inventoryItems.$id.locationLevels.batch.mutate
+ >,
ClientError,
Omit<
- InferClientInput<typeof sdk.vendor.inventoryItems.$id.locationLevels.batch.mutate>,
+ InferClientInput<
+ typeof sdk.vendor.inventoryItems.$id.locationLevels.batch.mutate
+ >,
"$id"
>
- >
+ >,
) => {
return useMutation({
mutationFn: (payload) =>
@@ -251,28 +268,32 @@ export const useBatchInventoryItemLocationLevels = (
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.lists(),
- })
+ });
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId),
- })
+ });
queryClient.invalidateQueries({
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId),
- })
+ });
queryClient.invalidateQueries({
queryKey: inventoryItemLevelsQueryKeys.list({ inventoryItemId }),
- })
- options?.onSuccess?.(data, variables, context)
+ });
+ options?.onSuccess?.(data, variables, context);
},
...options,
- })
-}
+ });
+};
export const useBatchInventoryItemsLocationLevels = (
options?: UseMutationOptions<
- InferClientOutput<typeof sdk.vendor.inventoryItems.locationLevels.batch.mutate>,
+ InferClientOutput<
+ typeof sdk.vendor.inventoryItems.locationLevels.batch.mutate
+ >,
ClientError,
- InferClientInput<typeof sdk.vendor.inventoryItems.locationLevels.batch.mutate>
- >
+ InferClientInput<
+ typeof sdk.vendor.inventoryItems.locationLevels.batch.mutate
+ >
+ >,
) => {
return useMutation({
mutationFn: (payload) =>
@@ -280,12 +301,12 @@ export const useBatchInventoryItemsLocationLevels = (
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.all,
- })
+ });
queryClient.invalidateQueries({
queryKey: variantsQueryKeys.lists(),
- })
- options?.onSuccess?.(data, variables, context)
+ });
+ options?.onSuccess?.(data, variables, context);
},
...options,
- })
-}
+ });
+};
diff --git a/packages/vendor/src/pages/inventory/[id]/stock/index.tsx b/packages/vendor/src/pages/inventory/[id]/stock/index.tsx
index 02b74f6fd..8bd3e1c31 100644
--- a/packages/vendor/src/pages/inventory/[id]/stock/index.tsx
+++ b/packages/vendor/src/pages/inventory/[id]/stock/index.tsx
@@ -1,29 +1,38 @@
// Route: /inventory/:id/stock
-import { useTranslation } from "react-i18next"
-import { useSearchParams } from "react-router-dom"
-import { RouteFocusModal } from "@components/modals"
-import { useInventoryItems, useStockLocations } from "@hooks/api"
-import { InventoryStockForm } from "./inventory-stock-form"
+import { useTranslation } from "react-i18next";
+import { useSearchParams } from "react-router-dom";
+import { RouteFocusModal } from "@components/modals";
+import { useInventoryItems, useStockLocations } from "@hooks/api";
+import { InventoryStockForm } from "./inventory-stock-form";
-const INVENTORY_ITEM_IDS_KEY = "inventory_item_ids"
+const INVENTORY_ITEM_IDS_KEY = "inventory_item_ids";
export const Component = () => {
- const { t } = useTranslation()
- const [searchParams] = useSearchParams()
- const inventoryItemIds = searchParams.get(INVENTORY_ITEM_IDS_KEY)?.split(",") || undefined
+ const { t } = useTranslation();
+ const [searchParams] = useSearchParams();
+ const inventoryItemIds =
+ searchParams.get(INVENTORY_ITEM_IDS_KEY)?.split(",") || undefined;
- const { inventory_items, isPending, isError, error } = useInventoryItems(
- { fields: "id,sku,title,*stock_locations,*location_levels" },
- undefined,
- { id: inventoryItemIds! }
- )
+ const { inventory_items, isPending, isError, error } = useInventoryItems({
+ fields: "id,sku,title,*stock_locations,*location_levels",
+ id: inventoryItemIds!,
+ });
- const { stock_locations, isPending: isPendingStockLocations, isError: isErrorStockLocations, error: errorStockLocations } = useStockLocations({ limit: 9999, fields: "id,name" })
+ const {
+ stock_locations,
+ isPending: isPendingStockLocations,
+ isError: isErrorStockLocations,
+ error: errorStockLocations,
+ } = useStockLocations({ limit: 9999, fields: "id,name" });
- const ready = !isPending && !!inventory_items && !isPendingStockLocations && !!stock_locations
+ const ready =
+ !isPending &&
+ !!inventory_items &&
+ !isPendingStockLocations &&
+ !!stock_locations;
- if (isError) throw error
- if (isErrorStockLocations) throw errorStockLocations
+ if (isError) throw error;
+ if (isErrorStockLocations) throw errorStockLocations;
return (
<RouteFocusModal>
@@ -33,7 +42,12 @@ export const Component = () => {
<RouteFocusModal.Description asChild>
<span className="sr-only">{t("inventory.stock.description")}</span>
</RouteFocusModal.Description>
- {ready && <InventoryStockForm items={inventory_items} locations={stock_locations} />}
+ {ready && (
+ <InventoryStockForm
+ items={inventory_items}
+ locations={stock_locations}
+ />
+ )}
</RouteFocusModal>
- )
-}
+ );
+};