chore(): Improve cart update line items (#11666)
**What** Currently, we are potentially providing an array of selector/data leading to fetching data sequentially before running on update which will fetch data again in batch and perform the update. Now we can pass the data directly which includes the id already and only perform one bulk fetch + one bulk update. This pr also include a fix on the inventory validation, currently, only the item to update inventory is being checked, with this pr we also check the inventory for the items that needs to be created
This commit is contained in:
@@ -676,6 +676,34 @@ moduleIntegrationTestRunner<ICartModuleService>({
|
||||
expect(updatedItem.title).toBe("test2")
|
||||
})
|
||||
|
||||
it("should update a line item in cart succesfully with data only approach", async () => {
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
currency_code: "eur",
|
||||
},
|
||||
])
|
||||
|
||||
const [item] = await service.addLineItems(createdCart.id, [
|
||||
{
|
||||
quantity: 1,
|
||||
unit_price: 100,
|
||||
title: "test",
|
||||
tax_lines: [],
|
||||
},
|
||||
])
|
||||
|
||||
expect(item.title).toBe("test")
|
||||
|
||||
const [updatedItem] = await service.updateLineItems([
|
||||
{
|
||||
id: item.id,
|
||||
title: "test2",
|
||||
},
|
||||
])
|
||||
|
||||
expect(updatedItem.title).toBe("test2")
|
||||
})
|
||||
|
||||
it("should update a line item in cart succesfully with id approach", async () => {
|
||||
const [createdCart] = await service.createCarts([
|
||||
{
|
||||
|
||||
@@ -489,6 +489,12 @@ export default class CartModuleService
|
||||
updateLineItems(
|
||||
data: CartTypes.UpdateLineItemWithSelectorDTO[]
|
||||
): Promise<CartTypes.CartLineItemDTO[]>
|
||||
|
||||
// @ts-ignore
|
||||
updateLineItems(
|
||||
data: (Partial<CartTypes.UpdateLineItemDTO> & { id: string })[]
|
||||
): Promise<CartTypes.CartLineItemDTO[]>
|
||||
|
||||
// @ts-expect-error
|
||||
updateLineItems(
|
||||
selector: Partial<CartTypes.CartLineItemDTO>,
|
||||
@@ -508,7 +514,8 @@ export default class CartModuleService
|
||||
lineItemIdOrDataOrSelector:
|
||||
| string
|
||||
| CartTypes.UpdateLineItemWithSelectorDTO[]
|
||||
| Partial<CartTypes.CartLineItemDTO>,
|
||||
| Partial<CartTypes.CartLineItemDTO>
|
||||
| (Partial<CartTypes.UpdateLineItemDTO> & { id: string })[],
|
||||
data?: CartTypes.UpdateLineItemDTO | Partial<CartTypes.UpdateLineItemDTO>,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<CartTypes.CartLineItemDTO[] | CartTypes.CartLineItemDTO> {
|
||||
@@ -521,10 +528,17 @@ export default class CartModuleService
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO>(
|
||||
item,
|
||||
{
|
||||
populate: true,
|
||||
}
|
||||
item
|
||||
)
|
||||
} else if (Array.isArray(lineItemIdOrDataOrSelector) && !data) {
|
||||
// We received an array of data including the ids
|
||||
const items = await this.lineItemService_.update(
|
||||
lineItemIdOrDataOrSelector,
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO[]>(
|
||||
items
|
||||
)
|
||||
}
|
||||
|
||||
@@ -537,7 +551,10 @@ export default class CartModuleService
|
||||
} as CartTypes.UpdateLineItemWithSelectorDTO,
|
||||
]
|
||||
|
||||
items = await this.updateLineItemsWithSelector_(toUpdate, sharedContext)
|
||||
items = await this.updateLineItemsWithSelector_(
|
||||
toUpdate as CartTypes.UpdateLineItemWithSelectorDTO[],
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<CartTypes.CartLineItemDTO[]>(
|
||||
items,
|
||||
|
||||
Reference in New Issue
Block a user