Skip to content

Commit

Permalink
removeLineItems
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 11, 2024
1 parent 8551bef commit a205061
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
])
)
})
})
})
17 changes: 13 additions & 4 deletions packages/cart/src/services/cart-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,18 @@ export default class CartModuleService implements ICartModuleService {
sharedContext
)
}
async removeLineItems(
itemIds: string[],
sharedContext?: Context
): Promise<void>
async removeLineItems(itemIds: string, sharedContext?: Context): Promise<void>

// @InjectTransactionManager("baseRepository_")
// async removeLineItems(lineItemIds: string[], sharedContext?: Context): Promise<void> {

// }
@InjectTransactionManager("baseRepository_")
async removeLineItems(
itemIds: string | string[],
sharedContext?: Context
): Promise<void> {
const toDelete = Array.isArray(itemIds) ? itemIds : [itemIds]
await this.lineItemService_.delete(toDelete, sharedContext)
}
}
3 changes: 2 additions & 1 deletion packages/types/src/cart/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,6 @@ export interface ICartModuleService extends IModuleService {
sharedContext?: Context
): Promise<CartLineItemDTO[]>

// removeLineItems(lineItemIds: string[], sharedContext?: Context): Promise<void>
removeLineItems(itemIds: string[], sharedContext?: Context): Promise<void>
removeLineItems(itemIds: string, sharedContext?: Context): Promise<void>
}

0 comments on commit a205061

Please sign in to comment.