fix(product): Return updated collections (#12354)

Collection updates without a `products` payload were not returned from the service layer due to an incorrect early return

Fixes SUP-1488
This commit is contained in:
Oli Juhl authored and GitHub committed 2025-05-05 14:41:45 +00:00
1 parent f25a4a5e34
commit 77c3023200
3 files changed
+36 -17

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/product": patch
---
fix(product): Return updated collections
@@ -384,6 +384,22 @@ moduleIntegrationTestRunner<IProductModuleService>({
) )
}) })
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 () => { it("should throw an error when an id does not exist", async () => {
let error let error
@@ -1188,9 +1188,12 @@ export default class ProductModuleService
) )
const collections: InferEntityType<typeof ProductCollection>[] = [] const collections: InferEntityType<typeof ProductCollection>[] = []
const toUpdate: {
selector: ProductTypes.FilterableProductProps
data: ProductTypes.UpdateProductDTO
}[] = []
const updateSelectorAndData = updatedCollections.flatMap( updatedCollections.forEach((collectionData) => {
(collectionData) => {
const input = normalizedInput.find((c) => c.id === collectionData.id) const input = normalizedInput.find((c) => c.id === collectionData.id)
const productsToUpdate = (input as any)?.products const productsToUpdate = (input as any)?.products
@@ -1199,26 +1202,24 @@ export default class ProductModuleService
} }
const associateSelector = {} const associateSelector = {}
if (!!productsToUpdate?.length) { if (isDefined(productsToUpdate)) {
const productIds = productsToUpdate.map((p) => p.id) const productIds = productsToUpdate.map((p) => p.id)
dissociateSelector["id"] = { $nin: productIds } dissociateSelector["id"] = { $nin: productIds }
associateSelector["id"] = { $in: productIds } associateSelector["id"] = { $in: productIds }
} else if (!isDefined(productsToUpdate)) {
return []
} }
const result: Record<string, any>[] = [ if (isPresent(dissociateSelector["id"])) {
{ toUpdate.push({
selector: dissociateSelector, selector: dissociateSelector,
data: { data: {
collection_id: null, collection_id: null,
}, },
}, })
] }
if (isPresent(associateSelector)) { if (isPresent(associateSelector["id"])) {
result.push({ toUpdate.push({
selector: associateSelector, selector: associateSelector,
data: { data: {
collection_id: collectionData.id, collection_id: collectionData.id,
@@ -1230,13 +1231,10 @@ export default class ProductModuleService
...collectionData, ...collectionData,
products: productsToUpdate ?? [], products: productsToUpdate ?? [],
} as InferEntityType<typeof ProductCollection>) } as InferEntityType<typeof ProductCollection>)
})
return result if (toUpdate.length) {
} await this.productService_.update(toUpdate, sharedContext)
)
if (updateSelectorAndData.length) {
await this.productService_.update(updateSelectorAndData, sharedContext)
} }
return collections return collections