feat(core-flows,medusa): adds inventory kit creation to variants endpoint (#7599)
what: When creating a variant, we can now create inventory as a part of the product and variants create endpoint. This applies only to variants where `manage_inventory=true`. 2 cases present itself: 1. When inventory_items are present - Link an inventory item with required_quantity to the variant - the inventory item already needs to be present 2. When inventory_items are not present - A default inventory item will be created - links the created item to the variant with a default required_quantity RESOLVES CORE-2220
This commit is contained in:
1 parent
6646a203df
commit
e7005a0aac
9 files changed
+521
-179
No files matched your search
@@ -59,5 +59,6 @@ export const POST = async (
|
||||
req.scope,
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ product: remapProductResponse(product) })
|
||||
}
|
||||
@@ -128,27 +128,37 @@ export const AdminCreateProductType = z.object({
|
||||
export type AdminCreateProductVariantType = z.infer<
|
||||
typeof AdminCreateProductVariant
|
||||
>
|
||||
export const AdminCreateProductVariant = z.object({
|
||||
title: z.string(),
|
||||
sku: z.string().nullable().optional(),
|
||||
ean: z.string().nullable().optional(),
|
||||
upc: z.string().nullable().optional(),
|
||||
barcode: z.string().nullable().optional(),
|
||||
hs_code: z.string().nullable().optional(),
|
||||
mid_code: z.string().nullable().optional(),
|
||||
allow_backorder: z.boolean().optional().default(false),
|
||||
manage_inventory: z.boolean().optional().default(true),
|
||||
variant_rank: z.number().optional(),
|
||||
weight: z.number().nullable().optional(),
|
||||
length: z.number().nullable().optional(),
|
||||
height: z.number().nullable().optional(),
|
||||
width: z.number().nullable().optional(),
|
||||
origin_country: z.string().nullable().optional(),
|
||||
material: z.string().nullable().optional(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
prices: z.array(AdminCreateVariantPrice),
|
||||
options: z.record(z.string()).optional(),
|
||||
})
|
||||
export const AdminCreateProductVariant = z
|
||||
.object({
|
||||
title: z.string(),
|
||||
sku: z.string().nullable().optional(),
|
||||
ean: z.string().nullable().optional(),
|
||||
upc: z.string().nullable().optional(),
|
||||
barcode: z.string().nullable().optional(),
|
||||
hs_code: z.string().nullable().optional(),
|
||||
mid_code: z.string().nullable().optional(),
|
||||
allow_backorder: z.boolean().optional().default(false),
|
||||
manage_inventory: z.boolean().optional().default(true),
|
||||
variant_rank: z.number().optional(),
|
||||
weight: z.number().nullable().optional(),
|
||||
length: z.number().nullable().optional(),
|
||||
height: z.number().nullable().optional(),
|
||||
width: z.number().nullable().optional(),
|
||||
origin_country: z.string().nullable().optional(),
|
||||
material: z.string().nullable().optional(),
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
prices: z.array(AdminCreateVariantPrice),
|
||||
options: z.record(z.string()).optional(),
|
||||
inventory_items: z
|
||||
.array(
|
||||
z.object({
|
||||
inventory_item_id: z.string(),
|
||||
required_quantity: z.number(),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
})
|
||||
.strict()
|
||||
|
||||
export type AdminUpdateProductVariantType = z.infer<
|
||||
typeof AdminUpdateProductVariant
|
||||
|
||||
@@ -87,8 +87,8 @@ export const wrapVariantsWithInventoryQuantity = async (
|
||||
|
||||
for (const link of links) {
|
||||
const requiredQuantity = link.required_quantity
|
||||
const availableQuantity = (link.inventory.location_levels || []).reduce(
|
||||
(sum, level) => sum + level.available_quantity || 0,
|
||||
const availableQuantity = (link.inventory?.location_levels || []).reduce(
|
||||
(sum, level) => sum + (level?.available_quantity || 0),
|
||||
0
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user