fix(product): updating collections with products fix (#10668)
This commit is contained in:
5
.changeset/eight-pigs-wave.md
Normal file
5
.changeset/eight-pigs-wave.md
Normal 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 {
|
||||
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`,
|
||||
|
||||
@@ -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<IProductModuleService>({
|
||||
{
|
||||
id: collectionId,
|
||||
title: "New Collection",
|
||||
product_ids: ["product_id"],
|
||||
},
|
||||
])
|
||||
|
||||
|
||||
@@ -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<string, any>[] = [
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user