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
This commit is contained in:
Adrien de Peretti
2023-09-26 11:48:26 +02:00
committed by GitHub
parent 2caff2efc7
commit 240c439beb
6 changed files with 26 additions and 21 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix(medusa): Product model before insert hook

View File

@@ -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",

View File

@@ -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

View File

@@ -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",

View File

@@ -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",

View File

@@ -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)
}