From 240c439beb76626e58006ceb86a1cd425f14d4fa Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Tue, 26 Sep 2023 11:48:26 +0200 Subject: [PATCH] fix(medusa): Product model before insert hook (#5200) **What** When the id is provided, the rest of the hook should still apply. At the moment, when the id is provided the hook is skipped --- .changeset/beige-bottles-ring.md | 5 ++++ .../admin/__snapshots__/discount.js.snap | 4 +-- .../api/__tests__/store/products.js | 3 +- .../__tests__/inventory/order/order.js | 2 +- .../__snapshots__/index.js.snap | 30 +++++++++---------- packages/medusa/src/models/product.ts | 3 +- 6 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 .changeset/beige-bottles-ring.md diff --git a/.changeset/beige-bottles-ring.md b/.changeset/beige-bottles-ring.md new file mode 100644 index 0000000000..43737e2760 --- /dev/null +++ b/.changeset/beige-bottles-ring.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): Product model before insert hook diff --git a/integration-tests/api/__tests__/admin/__snapshots__/discount.js.snap b/integration-tests/api/__tests__/admin/__snapshots__/discount.js.snap index 6e105a829c..09c9e177c5 100644 --- a/integration-tests/api/__tests__/admin/__snapshots__/discount.js.snap +++ b/integration-tests/api/__tests__/admin/__snapshots__/discount.js.snap @@ -33,7 +33,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "handmade-frozen-chair", "height": null, "hs_code": null, "id": "test-product", @@ -149,7 +149,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "practical-frozen-fish", "height": null, "hs_code": null, "id": "test-product", diff --git a/integration-tests/api/__tests__/store/products.js b/integration-tests/api/__tests__/store/products.js index be669bc344..bb52f40a39 100644 --- a/integration-tests/api/__tests__/store/products.js +++ b/integration-tests/api/__tests__/store/products.js @@ -24,6 +24,7 @@ describe("/store/products", () => { const giftCardId = "giftcard" const testProductId = "test-product" const testProductId1 = "test-product1" + const testProductId2 = "test-product2" const testProductFilteringId1 = "test-product_filtering_1" const testProductFilteringId2 = "test-product_filtering_2" @@ -156,7 +157,7 @@ describe("/store/products", () => { response.data.products.find((p) => p.id === testProductId1) ) const testProduct2Index = response.data.products.indexOf( - response.data.products.find((p) => p.id === "test-product2") + response.data.products.find((p) => p.id === testProductId2) ) expect(testProduct2Index).toBe(3) // 200 diff --git a/integration-tests/plugins/__tests__/inventory/order/order.js b/integration-tests/plugins/__tests__/inventory/order/order.js index 26bba7ae17..0f3926075d 100644 --- a/integration-tests/plugins/__tests__/inventory/order/order.js +++ b/integration-tests/plugins/__tests__/inventory/order/order.js @@ -179,7 +179,7 @@ describe("/store/carts", () => { stocked_quantity: 100, }) - const customer = await simpleCustomerFactory(dbConnection, {}) + const customer = await simpleCustomerFactory(dbConnection, {}, 100) const cart = await simpleCartFactory(dbConnection, { email: "testme@email.com", diff --git a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap index 9a07da02fb..f4bc042560 100644 --- a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap +++ b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap @@ -73,7 +73,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "gorgeous-cotton-table", "height": null, "hs_code": null, "id": "test-product", @@ -232,7 +232,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "gorgeous-cotton-table", "height": null, "hs_code": null, "id": "test-product", @@ -389,7 +389,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "gorgeous-cotton-table", "height": null, "hs_code": null, "id": "test-product", @@ -511,7 +511,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "gorgeous-cotton-table", "height": null, "hs_code": null, "id": "test-product", @@ -691,7 +691,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "gorgeous-cotton-table", "height": null, "hs_code": null, "id": "test-product", @@ -861,7 +861,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "awesome-metal-ball", "height": null, "hs_code": null, "id": "test-product", @@ -1114,7 +1114,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "gorgeous-cotton-table", "height": null, "hs_code": null, "id": "test-product", @@ -1365,7 +1365,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "gorgeous-cotton-table", "height": null, "hs_code": null, "id": "test-product", @@ -1576,7 +1576,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "gorgeous-cotton-table", "height": null, "hs_code": null, "id": "test-product", @@ -1698,7 +1698,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "gorgeous-cotton-table", "height": null, "hs_code": null, "id": "test-product", @@ -1878,7 +1878,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "gorgeous-cotton-table", "height": null, "hs_code": null, "id": "test-product", @@ -2042,7 +2042,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "awesome-metal-ball", "height": null, "hs_code": null, "id": "test-product", @@ -2153,7 +2153,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "awesome-metal-ball", "height": null, "hs_code": null, "id": "test-product", @@ -2271,7 +2271,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "awesome-metal-ball", "height": null, "hs_code": null, "id": "test-product", @@ -2390,7 +2390,7 @@ Object { "description": null, "discountable": true, "external_id": null, - "handle": null, + "handle": "awesome-metal-ball", "height": null, "hs_code": null, "id": "test-product", diff --git a/packages/medusa/src/models/product.ts b/packages/medusa/src/models/product.ts index 95c84826c1..7ad68202a5 100644 --- a/packages/medusa/src/models/product.ts +++ b/packages/medusa/src/models/product.ts @@ -193,9 +193,8 @@ export class Product extends SoftDeletableEntity { @BeforeInsert() private beforeInsert(): void { - if (this.id) return - this.id = generateEntityId(this.id, "prod") + if (!this.handle) { this.handle = _.kebabCase(this.title) }