The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
feat(core): copy variant shipping and customs attributes onto offer inventory items (#1624)
Viktor Holika79f277361fa
diff --git a/integration-tests/http/offer/vendor/create-offers-inventory-attributes.spec.ts b/integration-tests/http/offer/vendor/create-offers-inventory-attributes.spec.tsnew file mode 100644index 000000000..0a617349b--- /dev/null+++ b/integration-tests/http/offer/vendor/create-offers-inventory-attributes.spec.ts@@ -0,0 +1,175 @@+import { medusaIntegrationTestRunner } from "@medusajs/test-utils"+import { MedusaContainer } from "@medusajs/framework/types"+import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils"+import { createOffersWorkflow } from "@mercurjs/core/workflows"+import { createSellerUser } from "../../../helpers/create-seller-user"+import { createShippingProfile } from "../../../helpers/create-shipping-profile"+import { createVendorProduct } from "../../../helpers/create-product"++jest.setTimeout(120000)++const VARIANT_ATTRIBUTES = {+ weight: 1000,+ length: 20,+ width: 10,+ height: 30,+ hs_code: "610910",+ origin_country: "pl",+ mid_code: "PLMID123",+ material: "cotton",+}++const ATTRIBUTE_FIELDS = Object.keys(VARIANT_ATTRIBUTES)++medusaIntegrationTestRunner({+ testSuite: ({ getContainer, api }) => {+ describe("createOffersWorkflow - inventory item attributes", () => {+ let appContainer: MedusaContainer++ beforeAll(() => {+ appContainer = getContainer()+ })++ const setup = async () => {+ const tag = `t${Date.now()}${Math.floor(Math.random() * 1000)}`+ const { seller, member, headers } = await createSellerUser(+ appContainer,+ { email: `attrs-${tag}@test.com`, name: "Attrs Seller" }+ )++ const stockLocation = (+ await api.post(+ `/vendor/stock-locations`,+ { name: `WH ${tag}` },+ headers+ )+ ).data.stock_location++ const shippingProfile = await createShippingProfile(appContainer, {+ name: `Profile ${tag}`,+ })++ const product = await createVendorProduct(api, headers, {+ title: `Attrs Product ${tag}`,+ variants: [{ title: `Attrs Variant ${tag}` }],+ })++ const variant = product.variants[0]++ const offerInput = (+ inventoryItems: Array<{ sku: string; required_quantity?: number }>+ ) => ({+ seller_id: seller.id,+ created_by: member.id,+ variant_id: variant.id,+ shipping_profile_id: shippingProfile.id,+ sku: `OFFER-${tag}`,+ inventory_items: inventoryItems.map((item) => ({+ ...item,+ stock_levels: [+ { location_id: stockLocation.id, stocked_quantity: 10 },+ ],+ })),+ prices: [{ amount: 1000, currency_code: "usd" }],+ })++ return { tag, variant, offerInput }+ }++ const getOfferInventoryItems = async (offerId: string) => {+ const query = appContainer.resolve(ContainerRegistrationKeys.QUERY)+ const { data } = await query.graph({+ entity: "offer",+ fields: ATTRIBUTE_FIELDS.map((f) => `inventory_items.${f}`).concat(+ "inventory_items.id"+ ),+ filters: { id: offerId },+ })+ return (data[0]?.inventory_items ?? []) as Array<+ Record<string, unknown>+ >+ }++ it("copies the variant's shipping and customs attributes onto a single-item offer inventory item", async () => {+ const { tag, variant, offerInput } = await setup()++ await appContainer+ .resolve(Modules.PRODUCT)+ .updateProductVariants(variant.id, VARIANT_ATTRIBUTES)++ const { result } = await createOffersWorkflow(appContainer).run({+ input: { offers: [offerInput([{ sku: `INV-${tag}` }])] },+ })++ const items = await getOfferInventoryItems(result[0].id)++ expect(items).toHaveLength(1)+ expect(items[0]).toEqual(expect.objectContaining(VARIANT_ATTRIBUTES))+ })++ it("does not copy the variant's attributes onto kit offer inventory items", async () => {+ const { tag, variant, offerInput } = await setup()++ await appContainer+ .resolve(Modules.PRODUCT)+ .updateProductVariants(variant.id, VARIANT_ATTRIBUTES)++ const { result } = await createOffersWorkflow(appContainer).run({+ input: {+ offers: [+ offerInput([+ { sku: `INV-A-${tag}` },+ { sku: `INV-B-${tag}`, required_quantity: 2 },+ ]),+ ],+ },+ })++ const items = await getOfferInventoryItems(result[0].id)++ expect(items).toHaveLength(2)+ for (const item of items) {+ for (const field of ATTRIBUTE_FIELDS) {+ expect(item[field]).toBeNull()+ }+ }+ })++ it("does not copy the variant's attributes when the single item has a required quantity above one", async () => {+ const { tag, variant, offerInput } = await setup()++ await appContainer+ .resolve(Modules.PRODUCT)+ .updateProductVariants(variant.id, VARIANT_ATTRIBUTES)++ const { result } = await createOffersWorkflow(appContainer).run({+ input: {+ offers: [offerInput([{ sku: `INV-${tag}`, required_quantity: 3 }])],+ },+ })++ const items = await getOfferInventoryItems(result[0].id)++ expect(items).toHaveLength(1)+ for (const field of ATTRIBUTE_FIELDS) {+ expect(items[0][field]).toBeNull()+ }+ })++ it("leaves attributes empty when the variant has none", async () => {+ const { tag, offerInput } = await setup()++ const { result } = await createOffersWorkflow(appContainer).run({+ input: { offers: [offerInput([{ sku: `INV-${tag}` }])] },+ })++ const items = await getOfferInventoryItems(result[0].id)++ expect(items).toHaveLength(1)+ for (const field of ATTRIBUTE_FIELDS) {+ expect(items[0][field]).toBeNull()+ }+ })+ })+ },+})diff --git a/packages/core/src/workflows/offer/workflows/create-offers.ts b/packages/core/src/workflows/offer/workflows/create-offers.tsindex c32d5f5e2..31eb5ccc4 100644--- a/packages/core/src/workflows/offer/workflows/create-offers.ts+++ b/packages/core/src/workflows/offer/workflows/create-offers.ts@@ -13,6 +13,7 @@ import {} from "@medusajs/framework/types"import {createInventoryItemsWorkflow,+ type CreateInventoryItemsWorkflowInput,createRemoteLinkStep,emitEventStep,useQueryGraphStep,@@ -44,6 +45,17 @@ export type CreateOffersWorkflowHooks = [>,]+const VARIANT_INVENTORY_ATTRIBUTES = [+ "origin_country",+ "mid_code",+ "material",+ "weight",+ "length",+ "height",+ "width",+ "hs_code",+] as const+export const createOffersWorkflowId = "create-offers"export const createOffersWorkflow: ReturnWorkflow<@@ -61,25 +73,24 @@ export const createOffersWorkflow: ReturnWorkflow<const { data: variants } = useQueryGraphStep({entity: "product_variant",- fields: ["id", "title", "ean", "upc", "price_set.id", "product.id"],+ fields: [+ "id",+ "title",+ "ean",+ "upc",+ "price_set.id",+ "product.id",+ ...VARIANT_INVENTORY_ATTRIBUTES,+ ],filters: { id: variantIds },}).config({ name: "get-variants" })const inventoryItemsToCreate = transform({ input, variants },({ input, variants }) => {- const variantTitleById = new Map(- variants.map((v) => [v.id, v.title as string | undefined]),- )+ const variantById = new Map(variants.map((v) => [v.id, v]))- const items: Array<{- sku?: string- title: string- location_levels: Array<{- location_id: string- stocked_quantity: number- }>- }> = []+ const items: CreateInventoryItemsWorkflowInput["items"] = []const offerSpans: Array<{ start: number; length: number }> = []input.offers.forEach((offer) => {@@ -89,10 +100,26 @@ export const createOffersWorkflow: ReturnWorkflow<"Offer must have at least one inventory item",)}- const variantTitle = variantTitleById.get(offer.variant_id)+ const variant = variantById.get(offer.variant_id)+ const variantTitle = variant?.title as string | undefined+ // Variant measurements describe one sellable unit, so they only fit+ // an item that is the whole unit, not a kit component.+ const isSingleUnit =+ offer.inventory_items.length === 1 &&+ (offer.inventory_items[0].required_quantity ?? 1) === 1+ const variantAttributes =+ isSingleUnit && variant+ ? Object.fromEntries(+ VARIANT_INVENTORY_ATTRIBUTES.map((field) => [+ field,+ variant[field] ?? undefined,+ ]),+ )+ : {}const start = items.lengthoffer.inventory_items.forEach((item) => {items.push({+ ...variantAttributes,sku: item.sku,title: variantTitle ?? item.title ?? item.sku ?? offer.sku,location_levels: item.stock_levels ?? [],