From 230337aef99fa6989dd5f4537a4378de4e17ede6 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 16 May 2024 17:13:49 +0530 Subject: [PATCH 1/5] refactor: remove early return check when products list is undefined --- .../modules/product/src/services/product-module-service.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/modules/product/src/services/product-module-service.ts b/packages/modules/product/src/services/product-module-service.ts index 5ce5ba0be4..3f43a92c5a 100644 --- a/packages/modules/product/src/services/product-module-service.ts +++ b/packages/modules/product/src/services/product-module-service.ts @@ -929,9 +929,6 @@ export default class ProductModuleService< updatedCollections.map(async (collection, i) => { const input = normalizedInput.find((c) => c.id === collection.id) const productsToUpdate = (input as any)?.products - if (!productsToUpdate) { - return { ...collection, products: [] } - } await this.productService_.update( { @@ -941,7 +938,7 @@ export default class ProductModuleService< sharedContext ) - if (productsToUpdate.length > 0) { + if (productsToUpdate?.length > 0) { await this.productService_.update( productsToUpdate.map((p) => ({ id: p.id, @@ -951,7 +948,7 @@ export default class ProductModuleService< ) } - return { ...collection, products: productsToUpdate } + return { ...collection, products: productsToUpdate || [] } }) ) From a15f2b37b3d361089102519d4c09b89d45509774 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Fri, 17 May 2024 09:33:34 +0530 Subject: [PATCH 2/5] test: add tests --- .../product-collections.spec.ts | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/packages/modules/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts b/packages/modules/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts index 8fba59fbd6..557ace762c 100644 --- a/packages/modules/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/services/product-module-service/product-collections.spec.ts @@ -365,6 +365,73 @@ moduleIntegrationTestRunner({ "ProductCollection with id: does-not-exist was not found" ) }) + + it("should dissociate existing products when new products are synced", async () => { + await service.upsertCollections([ + { + id: collectionId, + product_ids: [productOne.id, productTwo.id], + }, + ]) + + /** + * Another upsert should remove the first productOne + */ + await service.upsertCollections([ + { + id: collectionId, + product_ids: [productTwo.id], + }, + ]) + + const productCollection = await service.retrieveCollection( + collectionId, + { + select: ["products.id"], + relations: ["products"], + } + ) + + expect(productCollection.products).toHaveLength(1) + expect(productCollection).toEqual( + expect.objectContaining({ + products: expect.arrayContaining([ + expect.objectContaining({ + id: productTwo.id, + }), + ]), + }) + ) + }) + + it("should dissociate all existing products", async () => { + await service.upsertCollections([ + { + id: collectionId, + product_ids: [productOne.id, productTwo.id], + }, + ]) + + /** + * Another upsert should remove the first productOne + */ + await service.upsertCollections([ + { + id: collectionId, + product_ids: [], + }, + ]) + + const productCollection = await service.retrieveCollection( + collectionId, + { + select: ["products.id"], + relations: ["products"], + } + ) + + expect(productCollection.products).toHaveLength(0) + }) }) describe("createCollections", () => { From 1abf9f19b3b0d3d09f55466139548bb5596676c5 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Fri, 17 May 2024 13:10:35 +0530 Subject: [PATCH 3/5] test: increase notification module tests timeout --- .../__tests__/notification-module-service/index.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts b/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts index 867581c2d4..8d2fb76c4a 100644 --- a/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts +++ b/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts @@ -25,6 +25,8 @@ let moduleOptions = { ], } +jest.setTimeout(30000) + moduleIntegrationTestRunner({ moduleName: Modules.NOTIFICATION, moduleOptions, From 72f98ea50b7460d411d94566828129e6b7bc5727 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Fri, 17 May 2024 14:42:27 +0530 Subject: [PATCH 4/5] refactor: implement query suggestions from Adrien --- .../internal-module-service-factory.ts | 4 +- .../src/services/product-module-service.ts | 64 ++++++++++++------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/packages/core/utils/src/modules-sdk/internal-module-service-factory.ts b/packages/core/utils/src/modules-sdk/internal-module-service-factory.ts index 26edd8a004..302854410a 100644 --- a/packages/core/utils/src/modules-sdk/internal-module-service-factory.ts +++ b/packages/core/utils/src/modules-sdk/internal-module-service-factory.ts @@ -248,7 +248,9 @@ export function internalModuleServiceFactory< if (input_.selector) { const entitiesToUpdate = await this.list( input_.selector, - {}, + { + take: null, + }, sharedContext ) // Create a pair of entity and data to update diff --git a/packages/modules/product/src/services/product-module-service.ts b/packages/modules/product/src/services/product-module-service.ts index 3f43a92c5a..1f5f1e3ca3 100644 --- a/packages/modules/product/src/services/product-module-service.ts +++ b/packages/modules/product/src/services/product-module-service.ts @@ -34,6 +34,7 @@ import { removeUndefined, isValidHandle, toHandle, + isPresent, } from "@medusajs/utils" import { ProductCategoryEventData, @@ -925,34 +926,53 @@ export default class ProductModuleService< sharedContext ) - const collectionWithProducts = await promiseAll( - updatedCollections.map(async (collection, i) => { - const input = normalizedInput.find((c) => c.id === collection.id) + const collections: TProductCollection[] = [] + + const updateSelectorAndData = updatedCollections + .map((collectionData) => { + const input = normalizedInput.find((c) => c.id === collectionData.id) const productsToUpdate = (input as any)?.products - await this.productService_.update( - { - selector: { collection_id: collection.id }, - data: { collection_id: null }, - }, - sharedContext - ) + const dissociateSelector = { + collection_id: collectionData.id, + } + const associateSelector = {} - if (productsToUpdate?.length > 0) { - await this.productService_.update( - productsToUpdate.map((p) => ({ - id: p.id, - collection_id: collection.id, - })), - sharedContext - ) + if (!!productsToUpdate?.length) { + const productIds = productsToUpdate.map((p) => p.id) + dissociateSelector["id"] = { $nin: productIds } + associateSelector["id"] = { $in: productIds } } - return { ...collection, products: productsToUpdate || [] } - }) - ) + const result: Record[] = [ + { + selector: dissociateSelector, + data: { + collection_id: null, + }, + }, + ] - return collectionWithProducts + if (isPresent(associateSelector)) { + result.push({ + selector: associateSelector, + data: { + collection_id: collectionData.id, + }, + }) + } + + collections.push({ + ...collectionData, + products: productsToUpdate ?? [], + }) + + return result + }) + .flat() + + await this.productService_.update(updateSelectorAndData, sharedContext) + return collections } @InjectManager("baseRepository_") From cf15f5d49810ba6b1c83d650e6c283d271d06e9a Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Fri, 17 May 2024 15:31:50 +0530 Subject: [PATCH 5/5] refactor: use flatMap --- .../product/src/services/product-module-service.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/modules/product/src/services/product-module-service.ts b/packages/modules/product/src/services/product-module-service.ts index 1f5f1e3ca3..2747597f91 100644 --- a/packages/modules/product/src/services/product-module-service.ts +++ b/packages/modules/product/src/services/product-module-service.ts @@ -928,8 +928,8 @@ export default class ProductModuleService< const collections: TProductCollection[] = [] - const updateSelectorAndData = updatedCollections - .map((collectionData) => { + const updateSelectorAndData = updatedCollections.flatMap( + (collectionData) => { const input = normalizedInput.find((c) => c.id === collectionData.id) const productsToUpdate = (input as any)?.products @@ -968,8 +968,8 @@ export default class ProductModuleService< }) return result - }) - .flat() + } + ) await this.productService_.update(updateSelectorAndData, sharedContext) return collections