refactor: implement query suggestions from Adrien

This commit is contained in:
Harminder Virk committed 2024-05-17 14:42:57 +05:30
1 parent 9ff3b08b0b
commit 72f98ea50b
2 files changed
+45 -23

No files matched your search

@@ -248,7 +248,9 @@ export function internalModuleServiceFactory<
if (input_.selector) { if (input_.selector) {
const entitiesToUpdate = await this.list( const entitiesToUpdate = await this.list(
input_.selector, input_.selector,
{}, {
take: null,
},
sharedContext sharedContext
) )
// Create a pair of entity and data to update // Create a pair of entity and data to update
@@ -34,6 +34,7 @@ import {
removeUndefined, removeUndefined,
isValidHandle, isValidHandle,
toHandle, toHandle,
isPresent,
} from "@medusajs/utils" } from "@medusajs/utils"
import { import {
ProductCategoryEventData, ProductCategoryEventData,
@@ -925,34 +926,53 @@ export default class ProductModuleService<
sharedContext sharedContext
) )
const collectionWithProducts = await promiseAll( const collections: TProductCollection[] = []
updatedCollections.map(async (collection, i) => {
const input = normalizedInput.find((c) => c.id === collection.id) const updateSelectorAndData = updatedCollections
.map((collectionData) => {
const input = normalizedInput.find((c) => c.id === collectionData.id)
const productsToUpdate = (input as any)?.products const productsToUpdate = (input as any)?.products
await this.productService_.update( const dissociateSelector = {
{ collection_id: collectionData.id,
selector: { collection_id: collection.id }, }
data: { collection_id: null }, const associateSelector = {}
},
sharedContext
)
if (productsToUpdate?.length > 0) { if (!!productsToUpdate?.length) {
await this.productService_.update( const productIds = productsToUpdate.map((p) => p.id)
productsToUpdate.map((p) => ({ dissociateSelector["id"] = { $nin: productIds }
id: p.id, associateSelector["id"] = { $in: productIds }
collection_id: collection.id,
})),
sharedContext
)
} }
return { ...collection, products: productsToUpdate || [] } const result: Record<string, any>[] = [
}) {
) selector: dissociateSelector,
data: {
collection_id: null,
},
},
]
return collectionWithProducts if (isPresent(associateSelector)) {
result.push({
selector: associateSelector,
data: {
collection_id: collectionData.id,
},
})
}
collections.push({
...collectionData,
products: productsToUpdate ?? [],
})
return result
})
.flat()
await this.productService_.update(updateSelectorAndData, sharedContext)
return collections
} }
@InjectManager("baseRepository_") @InjectManager("baseRepository_")