From d08b71f9b87c68350e57016e62e406a9bdd82342 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 19 Dec 2024 16:31:45 +0100 Subject: [PATCH] fix(product): updating collections with products fix (#10668) --- .changeset/eight-pigs-wave.md | 5 ++ .../collection/admin/colllection.spec.ts | 54 +++++++++++++++++-- .../product-collections.spec.ts | 3 +- .../src/services/product-module-service.ts | 17 ++++-- 4 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 .changeset/eight-pigs-wave.md diff --git a/.changeset/eight-pigs-wave.md b/.changeset/eight-pigs-wave.md new file mode 100644 index 0000000000..787524470f --- /dev/null +++ b/.changeset/eight-pigs-wave.md @@ -0,0 +1,5 @@ +--- +"@medusajs/product": patch +--- + +fix(product): updating collections with products fix diff --git a/integration-tests/http/__tests__/collection/admin/colllection.spec.ts b/integration-tests/http/__tests__/collection/admin/colllection.spec.ts index 3ee978230f..85d32a55b6 100644 --- a/integration-tests/http/__tests__/collection/admin/colllection.spec.ts +++ b/integration-tests/http/__tests__/collection/admin/colllection.spec.ts @@ -1,7 +1,7 @@ import { medusaIntegrationTestRunner } from "@medusajs/test-utils" import { - createAdminUser, adminHeaders, + createAdminUser, } from "../../../../helpers/create-admin-user" jest.setTimeout(30000) @@ -216,9 +216,7 @@ medusaIntegrationTestRunner({ it("adds products to collection", async () => { const response = await api.post( `/admin/collections/${baseCollection.id}/products?fields=*products`, - { - add: [baseProduct.id, baseProduct1.id], - }, + { add: [baseProduct.id, baseProduct1.id] }, adminHeaders ) @@ -242,6 +240,54 @@ medusaIntegrationTestRunner({ ) }) + it("should not remove products from collection when updating collection", async () => { + const addProductsResponse = await api.post( + `/admin/collections/${baseCollection.id}/products?fields=*products`, + { add: [baseProduct.id, baseProduct1.id] }, + adminHeaders + ) + + expect(addProductsResponse.status).toEqual(200) + expect(addProductsResponse.data.collection).toEqual( + expect.objectContaining({ + id: baseCollection.id, + products: expect.arrayContaining([ + expect.objectContaining({ + collection_id: baseCollection.id, + title: "test-product", + }), + expect.objectContaining({ + collection_id: baseCollection.id, + title: "test-product1", + }), + ]), + }) + ) + + const updateCollectionResponse = await api.post( + `/admin/collections/${baseCollection.id}?fields=*products`, + { title: "test collection update" }, + adminHeaders + ) + + expect(updateCollectionResponse.status).toEqual(200) + expect(updateCollectionResponse.data.collection).toEqual( + expect.objectContaining({ + title: "test collection update", + products: expect.arrayContaining([ + expect.objectContaining({ + collection_id: baseCollection.id, + title: "test-product", + }), + expect.objectContaining({ + collection_id: baseCollection.id, + title: "test-product1", + }), + ]), + }) + ) + }) + it("removes products from collection", async () => { await api.post( `/admin/collections/${baseCollection.id}/products`, diff --git a/packages/modules/product/integration-tests/__tests__/product-module-service/product-collections.spec.ts b/packages/modules/product/integration-tests/__tests__/product-module-service/product-collections.spec.ts index 73d761b715..666be95310 100644 --- a/packages/modules/product/integration-tests/__tests__/product-module-service/product-collections.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/product-module-service/product-collections.spec.ts @@ -7,11 +7,11 @@ import { ProductStatus, toMikroORMEntity, } from "@medusajs/framework/utils" -import { Product, ProductCollection } from "@models" import { MockEventBusService, moduleIntegrationTestRunner, } from "@medusajs/test-utils" +import { Product, ProductCollection } from "@models" import { createCollections } from "../../__fixtures__/product" jest.setTimeout(30000) @@ -318,6 +318,7 @@ moduleIntegrationTestRunner({ { id: collectionId, title: "New Collection", + product_ids: ["product_id"], }, ]) diff --git a/packages/modules/product/src/services/product-module-service.ts b/packages/modules/product/src/services/product-module-service.ts index 4da5262fe3..4c8b0a7dad 100644 --- a/packages/modules/product/src/services/product-module-service.ts +++ b/packages/modules/product/src/services/product-module-service.ts @@ -27,6 +27,7 @@ import { EmitEvents, InjectManager, InjectTransactionManager, + isDefined, isPresent, isString, isValidHandle, @@ -1056,6 +1057,7 @@ export default class ProductModuleService if (forCreate.length) { created = await this.createCollections_(forCreate, sharedContext) } + if (forUpdate.length) { updated = await this.updateCollections_(forUpdate, sharedContext) } @@ -1173,8 +1175,11 @@ export default class ProductModuleService if (!!productsToUpdate?.length) { const productIds = productsToUpdate.map((p) => p.id) + dissociateSelector["id"] = { $nin: productIds } associateSelector["id"] = { $in: productIds } + } else if (!isDefined(productsToUpdate)) { + return [] } const result: Record[] = [ @@ -1204,7 +1209,10 @@ export default class ProductModuleService } ) - await this.productService_.update(updateSelectorAndData, sharedContext) + if (updateSelectorAndData.length) { + await this.productService_.update(updateSelectorAndData, sharedContext) + } + return collections } @@ -1888,12 +1896,11 @@ export default class ProductModuleService collection: ProductTypes.CreateProductCollectionDTO | UpdateCollectionInput ): ProductTypes.CreateProductCollectionDTO | UpdateCollectionInput { const collectionData = { ...collection } - if (collectionData.product_ids?.length) { + if (Array.isArray(collectionData.product_ids)) { ;(collectionData as any).products = collectionData.product_ids.map( - (pid) => ({ - id: pid, - }) + (pid) => ({ id: pid }) ) + delete collectionData.product_ids }