Skip to content

Commit

Permalink
feat: add cart recalculate action
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedebock committed Jan 10, 2024
1 parent 0fcfd07 commit 82613b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/repositories/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type CartAddItemShippingAddressAction,
type CartSetLineItemShippingDetailsAction,
type CartDraft,
type CartRecalculateAction,

Check warning on line 10 in src/repositories/cart.ts

View workflow job for this annotation

GitHub Actions / Lint codebase

'CartRecalculateAction' is defined but never used

Check failure on line 10 in src/repositories/cart.ts

View workflow job for this annotation

GitHub Actions / Lint codebase

'CartRecalculateAction' is defined but never used
type CartRemoveLineItemAction,
type CartSetBillingAddressAction,
type CartSetCountryAction,
Expand Down Expand Up @@ -230,6 +231,15 @@ export class CartRepository extends AbstractResourceRepository<'cart'> {
// Update cart total price
resource.totalPrice.centAmount = calculateCartTotalPrice(resource)
},
recalculate: () => {
// Dummy action when triggering a recalculation of the cart
//
// From commercetools documentation:
// This update action does not set any Cart field in particular,
// but it triggers several Cart updates to bring prices and discounts to the latest state.
// Those can become stale over time when no Cart updates have been performed for a while
// and prices on related Products have changed in the meanwhile.
},
addItemShippingAddress: (
context: RepositoryContext,
resource: Writable<Cart>,
Expand Down
24 changes: 24 additions & 0 deletions src/services/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,30 @@ describe('Cart Update Actions', () => {
expect(response.body.lineItems).toHaveLength(0)
})

test('recalculate', async () => {
const product = await supertest(ctMock.app)

Check warning on line 307 in src/services/cart.test.ts

View workflow job for this annotation

GitHub Actions / Lint codebase

'product' is assigned a value but never used
.post(`/dummy/products`)
.send(productDraft)
.then((x) => x.body)

assert(cart, 'cart not created')

const response = await supertest(ctMock.app)
.post(`/dummy/carts/${cart.id}`)
.send({
version: 1,
actions: [
{
action: 'recalculate',
updateProductData: true
},
],
})

expect(response.status).toBe(200)
expect(response.body.version).toBe(1)
})

test('removeLineItem', async () => {
const product = await supertest(ctMock.app)
.post(`/dummy/products`)
Expand Down

0 comments on commit 82613b9

Please sign in to comment.