From 1dc79590b3539af09dbc8fbf931d9b5ee225fb0d Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Wed, 11 Jan 2023 19:21:05 +0100 Subject: [PATCH] fix(medusa): Rename quantity to required quantity (#2963) --- .changeset/seven-sloths-provide.md | 5 +++ .../1671711415179-multi_location.ts | 2 +- .../models/product-variant-inventory-item.ts | 36 ++++++++++++++++++- .../src/services/product-variant-inventory.ts | 11 +++--- 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 .changeset/seven-sloths-provide.md diff --git a/.changeset/seven-sloths-provide.md b/.changeset/seven-sloths-provide.md new file mode 100644 index 0000000000..2999efb489 --- /dev/null +++ b/.changeset/seven-sloths-provide.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +Fix(medusa): rename variant inventory item quantity to required_quantity diff --git a/packages/medusa/src/migrations/1671711415179-multi_location.ts b/packages/medusa/src/migrations/1671711415179-multi_location.ts index 7531c8fd1e..8f1d9e4464 100644 --- a/packages/medusa/src/migrations/1671711415179-multi_location.ts +++ b/packages/medusa/src/migrations/1671711415179-multi_location.ts @@ -14,7 +14,7 @@ export class multiLocation1671711415179 implements MigrationInterface { `CREATE INDEX "IDX_c2203162ca946a71aeb98390b0" ON "sales_channel_location" ("location_id") ` ) await queryRunner.query( - `CREATE TABLE "product_variant_inventory_item" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "inventory_item_id" text NOT NULL, "variant_id" text NOT NULL, "quantity" integer NOT NULL DEFAULT '1', CONSTRAINT "UQ_c9be7c1b11a1a729eb51d1b6bca" UNIQUE ("variant_id", "inventory_item_id"), CONSTRAINT "PK_9a1188b8d36f4d198303b4f7efa" PRIMARY KEY ("id"))` + `CREATE TABLE "product_variant_inventory_item" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "inventory_item_id" text NOT NULL, "variant_id" text NOT NULL, "required_quantity" integer NOT NULL DEFAULT '1', CONSTRAINT "UQ_c9be7c1b11a1a729eb51d1b6bca" UNIQUE ("variant_id", "inventory_item_id"), CONSTRAINT "PK_9a1188b8d36f4d198303b4f7efa" PRIMARY KEY ("id"))` ) await queryRunner.query( `CREATE INDEX "IDX_c74e8c2835094a37dead376a3b" ON "product_variant_inventory_item" ("inventory_item_id") ` diff --git a/packages/medusa/src/models/product-variant-inventory-item.ts b/packages/medusa/src/models/product-variant-inventory-item.ts index 387bb46299..e351e34067 100644 --- a/packages/medusa/src/models/product-variant-inventory-item.ts +++ b/packages/medusa/src/models/product-variant-inventory-item.ts @@ -14,10 +14,44 @@ export class ProductVariantInventoryItem extends BaseEntity { variant_id: string @Column({ type: "int", default: 1 }) - quantity: number + required_quantity: number @BeforeInsert() private beforeInsert(): void { this.id = generateEntityId(this.id, "pvitem") } } + +/** + * @schema ProductVariantInventoryItem + * title: "Product Variant Inventory Item" + * description: "Product Variant Inventory Items link variants with inventory items and denote the number of inventory items constituting a variant." + * type: object + * properties: + * id: + * type: string + * description: The product variant inventory item's ID + * example: pvitem_01G8X9A7ESKAJXG2H0E6F1MW7A + * inventory_item_id: + * description: "The id of the inventory item" + * type: string + * variant_id: + * description: "The id of the variant." + * type: string + * required_quantity: + * description: "The quantity of an inventory item required for one quantity of the variant." + * type: integer + * default: 1 + * created_at: + * type: string + * description: "The date with timezone at which the resource was created." + * format: date-time + * updated_at: + * type: string + * description: "The date with timezone at which the resource was updated." + * format: date-time + * deleted_at: + * type: string + * description: "The date with timezone at which the resource was deleted." + * format: date-time + */ diff --git a/packages/medusa/src/services/product-variant-inventory.ts b/packages/medusa/src/services/product-variant-inventory.ts index 8d6c03bcda..72282d4015 100644 --- a/packages/medusa/src/services/product-variant-inventory.ts +++ b/packages/medusa/src/services/product-variant-inventory.ts @@ -105,7 +105,7 @@ class ProductVariantInventoryService extends TransactionBaseService { const hasInventory = await Promise.all( variantInventory.map(async (inventoryPart) => { - const itemQuantity = inventoryPart.quantity * quantity + const itemQuantity = inventoryPart.required_quantity * quantity return await this.inventoryService_.confirmInventory( inventoryPart.inventory_item_id, locations, @@ -284,7 +284,7 @@ class ProductVariantInventoryService extends TransactionBaseService { const variantInventory = variantInventoryRepo.create({ variant_id: variantId, inventory_item_id: inventoryItemId, - quantity: quantityToStore, + required_quantity: quantityToStore, }) return await variantInventoryRepo.save(variantInventory) @@ -373,7 +373,7 @@ class ProductVariantInventoryService extends TransactionBaseService { await Promise.all( variantInventory.map(async (inventoryPart) => { - const itemQuantity = inventoryPart.quantity * quantity + const itemQuantity = inventoryPart.required_quantity * quantity return await this.inventoryService_.createReservationItem({ ...toReserve, location_id: locationId as string, @@ -438,7 +438,8 @@ class ProductVariantInventoryService extends TransactionBaseService { ) const reservationQtyUpdate = - reservation.quantity - quantity * productVariantInventory.quantity + reservation.quantity - + quantity * productVariantInventory.required_quantity if (reservationQtyUpdate === 0) { await this.inventoryService_.deleteReservationItem(reservation.id) @@ -561,7 +562,7 @@ class ProductVariantInventoryService extends TransactionBaseService { await Promise.all( variantInventory.map(async (inventoryPart) => { - const itemQuantity = inventoryPart.quantity * quantity + const itemQuantity = inventoryPart.required_quantity * quantity return await this.inventoryService_.adjustInventory( inventoryPart.inventory_item_id, locationId,