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
2025-05-05 16:41:45 +02:00
committed by GitHub
parent f25a4a5e34
commit 77c3023200
3 changed files with 64 additions and 45 deletions
+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 () => {
let error
@@ -1188,55 +1188,53 @@ export default class ProductModuleService
)
const collections: InferEntityType<typeof ProductCollection>[] = []
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<string, any>[] = [
{
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<typeof ProductCollection>)
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<typeof ProductCollection>)
})
if (toUpdate.length) {
await this.productService_.update(toUpdate, sharedContext)
}
return collections