From d57c739052afa81f6513d870b1e7b1426abfca80 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 28 Nov 2024 17:15:08 +0530 Subject: [PATCH] fix: do not initialize FKs to null (#10337) --- .../entity-builder/define-relationship.ts | 25 +++++++++++++++- .../__tests__/product-category.ts | 8 ++--- .../product-options.spec.ts | 6 ---- .../product-tags.spec.ts | 29 +++++++++++++++++-- .../integration-tests/__tests__/product.ts | 6 ---- 5 files changed, 52 insertions(+), 22 deletions(-) diff --git a/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts b/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts index fd86676d78..8a9afb5f8d 100644 --- a/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts +++ b/packages/core/utils/src/dml/helpers/entity-builder/define-relationship.ts @@ -237,7 +237,30 @@ export function defineBelongsToRelationship( return } - this[foreignKeyName] ??= this[relationship.name]?.id ?? null + /** + * Do not override the existing foreign key value if + * exists + */ + if (this[foreignKeyName] !== undefined) { + return + } + + /** + * Set the foreign key when the relationship is initialized + * as null + */ + if (this[relationship.name] === null) { + this[foreignKeyName] = null + return + } + + /** + * Set the foreign key when the relationship is initialized + * and as the id + */ + if (this[relationship.name] && "id" in this[relationship.name]) { + this[foreignKeyName] = this[relationship.name].id + } } /** diff --git a/packages/modules/product/integration-tests/__tests__/product-category.ts b/packages/modules/product/integration-tests/__tests__/product-category.ts index 38a0d3b07a..0562ccad33 100644 --- a/packages/modules/product/integration-tests/__tests__/product-category.ts +++ b/packages/modules/product/integration-tests/__tests__/product-category.ts @@ -43,7 +43,6 @@ moduleIntegrationTestRunner({ expect(productCategoryResults).toEqual([ expect.objectContaining({ id: "category-0", - parent_category_id: null, }), expect.objectContaining({ id: "category-1", @@ -130,7 +129,6 @@ moduleIntegrationTestRunner({ id: "category-0", handle: "category-0", mpath: "category-0", - parent_category_id: null, category_children: [ expect.objectContaining({ id: "category-1", @@ -263,9 +261,9 @@ moduleIntegrationTestRunner({ parent_category_id: "electronics", parent_category: expect.objectContaining({ id: "electronics", - parent_category_id: null, handle: "electronics", mpath: "electronics", + parent_category_id: null, parent_category: null, }), }), @@ -564,7 +562,6 @@ moduleIntegrationTestRunner({ id: "category-0", handle: "category-0", mpath: "category-0", - parent_category_id: null, category_children: [ expect.objectContaining({ id: "category-1", @@ -721,6 +718,7 @@ moduleIntegrationTestRunner({ expect(productCategoryResults[0]).toEqual([ expect.objectContaining({ id: "category-0", + parent_category_id: null, parent_category: null, }), expect.objectContaining({ @@ -813,7 +811,6 @@ moduleIntegrationTestRunner({ id: "category-0", handle: "category-0", mpath: "category-0", - parent_category_id: null, category_children: [ expect.objectContaining({ id: "category-1", @@ -874,7 +871,6 @@ moduleIntegrationTestRunner({ id: "category-0", handle: "category-0", mpath: "category-0", - parent_category_id: null, category_children: [ expect.objectContaining({ id: "category-1", diff --git a/packages/modules/product/integration-tests/__tests__/product-module-service/product-options.spec.ts b/packages/modules/product/integration-tests/__tests__/product-module-service/product-options.spec.ts index 67c934342b..84e5923675 100644 --- a/packages/modules/product/integration-tests/__tests__/product-module-service/product-options.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/product-module-service/product-options.spec.ts @@ -106,8 +106,6 @@ moduleIntegrationTestRunner({ product_id: productOne.id, product: { id: productOne.id, - type_id: null, - collection_id: null, }, }, ]) @@ -183,8 +181,6 @@ moduleIntegrationTestRunner({ product_id: productOne.id, product: { id: productOne.id, - type_id: null, - collection_id: null, }, }, ]) @@ -215,8 +211,6 @@ moduleIntegrationTestRunner({ id: "product-1", handle: "product-1", title: "product 1", - type_id: null, - collection_id: null, }, product_id: "product-1", }) diff --git a/packages/modules/product/integration-tests/__tests__/product-module-service/product-tags.spec.ts b/packages/modules/product/integration-tests/__tests__/product-module-service/product-tags.spec.ts index b8a91be243..56b046c336 100644 --- a/packages/modules/product/integration-tests/__tests__/product-module-service/product-tags.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/product-module-service/product-tags.spec.ts @@ -121,8 +121,33 @@ moduleIntegrationTestRunner({ value: tagOne.value, products: [ { + id: productOne.id, + }, + ], + }, + ]) + }) + + it("should set foreign key to null when relation is select and is null", async () => { + const tags = await service.listProductTags( + { + id: tagOne.id, + }, + { + select: ["value", "products.id"], + relations: ["products.collection"], + take: 1, + } + ) + + expect(tags).toEqual([ + { + id: tagOne.id, + value: tagOne.value, + products: [ + { + collection: null, collection_id: null, - type_id: null, id: productOne.id, }, ], @@ -199,8 +224,6 @@ moduleIntegrationTestRunner({ value: tagOne.value, products: [ { - collection_id: null, - type_id: null, id: productOne.id, }, ], diff --git a/packages/modules/product/integration-tests/__tests__/product.ts b/packages/modules/product/integration-tests/__tests__/product.ts index 3b310d995f..f6116af5e5 100644 --- a/packages/modules/product/integration-tests/__tests__/product.ts +++ b/packages/modules/product/integration-tests/__tests__/product.ts @@ -492,21 +492,18 @@ moduleIntegrationTestRunner({ name: "category 0", handle: "category-0", mpath: "category-0", - parent_category_id: null, }, { id: "category-1", name: "category 1", handle: "category-1", mpath: "category-0.category-1", - parent_category_id: null, }, { id: "category-1-a", name: "category 1 a", handle: "category-1-a", mpath: "category-0.category-1.category-1-a", - parent_category_id: null, }, ]) }) @@ -605,7 +602,6 @@ moduleIntegrationTestRunner({ title: workingProduct.title, handle: "product-1", collection_id: workingCollection.id, - type_id: null, collection: { handle: "col-1", id: workingCollection.id, @@ -639,7 +635,6 @@ moduleIntegrationTestRunner({ id: workingProduct.id, title: workingProduct.title, handle: "product-1", - type_id: null, collection_id: workingCollection.id, collection: { handle: "col-1", @@ -651,7 +646,6 @@ moduleIntegrationTestRunner({ id: workingProductTwo.id, title: workingProductTwo.title, handle: "product", - type_id: null, collection_id: workingCollectionTwo.id, collection: { handle: "col-2",