From 7927c24c35e1213755dca65235b70078113e9075 Mon Sep 17 00:00:00 2001 From: Mike de Bock <1261006+mikedebock@users.noreply.github.com> Date: Wed, 10 Jan 2024 12:07:49 +0100 Subject: [PATCH] feat: add cart recalculate action (#136) --- .changeset/sixty-turtles-happen.md | 5 +++++ src/repositories/cart.ts | 9 +++++++++ src/services/cart.test.ts | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 .changeset/sixty-turtles-happen.md diff --git a/.changeset/sixty-turtles-happen.md b/.changeset/sixty-turtles-happen.md new file mode 100644 index 00000000..72dcb3f2 --- /dev/null +++ b/.changeset/sixty-turtles-happen.md @@ -0,0 +1,5 @@ +--- +'@labdigital/commercetools-mock': minor +--- + +add support for cart recalculate action diff --git a/src/repositories/cart.ts b/src/repositories/cart.ts index 6e8d0aa0..86beef62 100644 --- a/src/repositories/cart.ts +++ b/src/repositories/cart.ts @@ -230,6 +230,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, diff --git a/src/services/cart.test.ts b/src/services/cart.test.ts index 8d3b145a..243145f8 100644 --- a/src/services/cart.test.ts +++ b/src/services/cart.test.ts @@ -303,6 +303,30 @@ describe('Cart Update Actions', () => { expect(response.body.lineItems).toHaveLength(0) }) + test('recalculate', async () => { + const product = await supertest(ctMock.app) + .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`)