diff --git a/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts b/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts index 30633ea800b1d..35407fd9c0385 100644 --- a/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts +++ b/packages/cart/integration-tests/__tests__/services/cart-module/index.spec.ts @@ -478,4 +478,127 @@ describe("Cart Module Service", () => { ) }) }) + + describe("removeLineItems", () => { + it("should remove a line item succesfully", async () => { + const [createdCart] = await service.create([ + { + currency_code: "eur", + }, + ]) + + const [item] = await service.addLineItems(createdCart.id, [ + { + quantity: 1, + unit_price: 100, + title: "test", + tax_lines: [], + }, + ]) + + expect(item.title).toBe("test") + + await service.removeLineItems([item.id]) + + const cart = await service.retrieve(createdCart.id, { + relations: ["items"], + }) + + expect(cart.items?.length).toBe(0) + }) + + it("should remove multiple line items succesfully", async () => { + const [createdCart] = await service.create([ + { + currency_code: "eur", + }, + ]) + + const [item, item2] = await service.addLineItems(createdCart.id, [ + { + quantity: 1, + unit_price: 100, + title: "test", + }, + { + quantity: 1, + unit_price: 100, + title: "test-2", + }, + ]) + + await service.removeLineItems([item.id, item2.id]) + + const cart = await service.retrieve(createdCart.id, { + relations: ["items"], + }) + + expect(cart.items?.length).toBe(0) + }) + + it("should update multiples line items in cart succesfully", async () => { + const [createdCart] = await service.create([ + { + currency_code: "eur", + }, + ]) + + const items = await service.addLineItems(createdCart.id, [ + { + quantity: 1, + unit_price: 100, + title: "test", + }, + { + quantity: 2, + unit_price: 200, + title: "other-test", + }, + ]) + + expect(items).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + title: "test", + quantity: 1, + unit_price: 100, + }), + expect.objectContaining({ + title: "other-test", + quantity: 2, + unit_price: 200, + }), + ]) + ) + + const itemOne = items.find((i) => i.title === "test") + const itemTwo = items.find((i) => i.title === "other-test") + + const updatedItems = await service.updateLineItems(createdCart.id, [ + { + id: itemOne!.id, + title: "changed-test", + }, + { + id: itemTwo!.id, + title: "changed-other-test", + }, + ]) + + expect(updatedItems).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + title: "changed-test", + quantity: 1, + unit_price: 100, + }), + expect.objectContaining({ + title: "changed-other-test", + quantity: 2, + unit_price: 200, + }), + ]) + ) + }) + }) }) diff --git a/packages/cart/src/services/cart-module.ts b/packages/cart/src/services/cart-module.ts index e60b76077aabd..37d991c71bc8f 100644 --- a/packages/cart/src/services/cart-module.ts +++ b/packages/cart/src/services/cart-module.ts @@ -395,9 +395,18 @@ export default class CartModuleService implements ICartModuleService { sharedContext ) } + async removeLineItems( + itemIds: string[], + sharedContext?: Context + ): Promise + async removeLineItems(itemIds: string, sharedContext?: Context): Promise - // @InjectTransactionManager("baseRepository_") - // async removeLineItems(lineItemIds: string[], sharedContext?: Context): Promise { - - // } + @InjectTransactionManager("baseRepository_") + async removeLineItems( + itemIds: string | string[], + sharedContext?: Context + ): Promise { + const toDelete = Array.isArray(itemIds) ? itemIds : [itemIds] + await this.lineItemService_.delete(toDelete, sharedContext) + } } diff --git a/packages/types/src/cart/service.ts b/packages/types/src/cart/service.ts index a6b59b32123cd..bd43c136571fc 100644 --- a/packages/types/src/cart/service.ts +++ b/packages/types/src/cart/service.ts @@ -90,5 +90,6 @@ export interface ICartModuleService extends IModuleService { sharedContext?: Context ): Promise - // removeLineItems(lineItemIds: string[], sharedContext?: Context): Promise + removeLineItems(itemIds: string[], sharedContext?: Context): Promise + removeLineItems(itemIds: string, sharedContext?: Context): Promise }