fix(product): updating collections with products fix (#10668)

This commit is contained in:
Riqwan Thamir authored and GitHub committed 2024-12-19 16:31:45 +01:00
1 parent 5d1098ceb9
commit d08b71f9b8
4 files changed
+68 -9

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/product": patch
---
fix(product): updating collections with products fix
@@ -1,7 +1,7 @@
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { import {
createAdminUser,
adminHeaders, adminHeaders,
createAdminUser,
} from "../../../../helpers/create-admin-user" } from "../../../../helpers/create-admin-user"
jest.setTimeout(30000) jest.setTimeout(30000)
@@ -216,9 +216,7 @@ medusaIntegrationTestRunner({
it("adds products to collection", async () => { it("adds products to collection", async () => {
const response = await api.post( const response = await api.post(
`/admin/collections/${baseCollection.id}/products?fields=*products`, `/admin/collections/${baseCollection.id}/products?fields=*products`,
{ { add: [baseProduct.id, baseProduct1.id] },
add: [baseProduct.id, baseProduct1.id],
},
adminHeaders 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 () => { it("removes products from collection", async () => {
await api.post( await api.post(
`/admin/collections/${baseCollection.id}/products`, `/admin/collections/${baseCollection.id}/products`,
@@ -7,11 +7,11 @@ import {
ProductStatus, ProductStatus,
toMikroORMEntity, toMikroORMEntity,
} from "@medusajs/framework/utils" } from "@medusajs/framework/utils"
import { Product, ProductCollection } from "@models"
import { import {
MockEventBusService, MockEventBusService,
moduleIntegrationTestRunner, moduleIntegrationTestRunner,
} from "@medusajs/test-utils" } from "@medusajs/test-utils"
import { Product, ProductCollection } from "@models"
import { createCollections } from "../../__fixtures__/product" import { createCollections } from "../../__fixtures__/product"
jest.setTimeout(30000) jest.setTimeout(30000)
@@ -318,6 +318,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
{ {
id: collectionId, id: collectionId,
title: "New Collection", title: "New Collection",
product_ids: ["product_id"],
}, },
]) ])
@@ -27,6 +27,7 @@ import {
EmitEvents, EmitEvents,
InjectManager, InjectManager,
InjectTransactionManager, InjectTransactionManager,
isDefined,
isPresent, isPresent,
isString, isString,
isValidHandle, isValidHandle,
@@ -1056,6 +1057,7 @@ export default class ProductModuleService
if (forCreate.length) { if (forCreate.length) {
created = await this.createCollections_(forCreate, sharedContext) created = await this.createCollections_(forCreate, sharedContext)
} }
if (forUpdate.length) { if (forUpdate.length) {
updated = await this.updateCollections_(forUpdate, sharedContext) updated = await this.updateCollections_(forUpdate, sharedContext)
} }
@@ -1173,8 +1175,11 @@ export default class ProductModuleService
if (!!productsToUpdate?.length) { if (!!productsToUpdate?.length) {
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>[] = [ const result: Record<string, any>[] = [
@@ -1204,7 +1209,10 @@ export default class ProductModuleService
} }
) )
if (updateSelectorAndData.length) {
await this.productService_.update(updateSelectorAndData, sharedContext) await this.productService_.update(updateSelectorAndData, sharedContext)
}
return collections return collections
} }
@@ -1888,12 +1896,11 @@ export default class ProductModuleService
collection: ProductTypes.CreateProductCollectionDTO | UpdateCollectionInput collection: ProductTypes.CreateProductCollectionDTO | UpdateCollectionInput
): ProductTypes.CreateProductCollectionDTO | UpdateCollectionInput { ): ProductTypes.CreateProductCollectionDTO | UpdateCollectionInput {
const collectionData = { ...collection } const collectionData = { ...collection }
if (collectionData.product_ids?.length) { if (Array.isArray(collectionData.product_ids)) {
;(collectionData as any).products = collectionData.product_ids.map( ;(collectionData as any).products = collectionData.product_ids.map(
(pid) => ({ (pid) => ({ id: pid })
id: pid,
})
) )
delete collectionData.product_ids delete collectionData.product_ids
} }