diff --git a/.changeset/fair-turtles-end.md b/.changeset/fair-turtles-end.md new file mode 100644 index 0000000000..2b4a067deb --- /dev/null +++ b/.changeset/fair-turtles-end.md @@ -0,0 +1,5 @@ +--- +"@medusajs/product": patch +--- + +fix(product): Return updated collections 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 666be95310..b27fa6adf4 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 @@ -384,6 +384,22 @@ moduleIntegrationTestRunner({ ) }) + it("should respond with collections when products are updated", async () => { + const collections = await service.updateProductCollections( + collectionId, + { + title: "Updated Collection", + } + ) + + expect(collections).toEqual( + expect.objectContaining({ + id: collectionId, + title: "Updated Collection", + }) + ) + }) + it("should throw an error when an id does not exist", async () => { let error diff --git a/packages/modules/product/src/services/product-module-service.ts b/packages/modules/product/src/services/product-module-service.ts index 036ebf3849..cbf36f4706 100644 --- a/packages/modules/product/src/services/product-module-service.ts +++ b/packages/modules/product/src/services/product-module-service.ts @@ -1188,55 +1188,53 @@ export default class ProductModuleService ) const collections: InferEntityType[] = [] + const toUpdate: { + selector: ProductTypes.FilterableProductProps + data: ProductTypes.UpdateProductDTO + }[] = [] - const updateSelectorAndData = updatedCollections.flatMap( - (collectionData) => { - const input = normalizedInput.find((c) => c.id === collectionData.id) - const productsToUpdate = (input as any)?.products + updatedCollections.forEach((collectionData) => { + const input = normalizedInput.find((c) => c.id === collectionData.id) + const productsToUpdate = (input as any)?.products - const dissociateSelector = { - collection_id: collectionData.id, - } - const associateSelector = {} - - 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[] = [ - { - selector: dissociateSelector, - data: { - collection_id: null, - }, - }, - ] - - if (isPresent(associateSelector)) { - result.push({ - selector: associateSelector, - data: { - collection_id: collectionData.id, - }, - }) - } - - collections.push({ - ...collectionData, - products: productsToUpdate ?? [], - } as InferEntityType) - - return result + const dissociateSelector = { + collection_id: collectionData.id, } - ) + const associateSelector = {} - if (updateSelectorAndData.length) { - await this.productService_.update(updateSelectorAndData, sharedContext) + if (isDefined(productsToUpdate)) { + const productIds = productsToUpdate.map((p) => p.id) + + dissociateSelector["id"] = { $nin: productIds } + associateSelector["id"] = { $in: productIds } + } + + if (isPresent(dissociateSelector["id"])) { + toUpdate.push({ + selector: dissociateSelector, + data: { + collection_id: null, + }, + }) + } + + if (isPresent(associateSelector["id"])) { + toUpdate.push({ + selector: associateSelector, + data: { + collection_id: collectionData.id, + }, + }) + } + + collections.push({ + ...collectionData, + products: productsToUpdate ?? [], + } as InferEntityType) + }) + + if (toUpdate.length) { + await this.productService_.update(toUpdate, sharedContext) } return collections