From 191ea89eb99ec26f461367876a5864b924c9b030 Mon Sep 17 00:00:00 2001 From: alexfaxe Date: Wed, 11 Sep 2024 16:05:46 +0200 Subject: [PATCH 1/2] feat(cart): support for custom fields in addLineItem --- .changeset/hip-pianos-play.md | 5 ++++ src/repositories/cart/actions.ts | 3 +- src/services/cart.test.ts | 48 ++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 .changeset/hip-pianos-play.md diff --git a/.changeset/hip-pianos-play.md b/.changeset/hip-pianos-play.md new file mode 100644 index 00000000..05c0de8e --- /dev/null +++ b/.changeset/hip-pianos-play.md @@ -0,0 +1,5 @@ +--- +"@labdigital/commercetools-mock": patch +--- + +Adds support for custom fields in 'addLineItem' diff --git a/src/repositories/cart/actions.ts b/src/repositories/cart/actions.ts index dd6a4c1c..b82e1367 100644 --- a/src/repositories/cart/actions.ts +++ b/src/repositories/cart/actions.ts @@ -72,7 +72,7 @@ export class CartUpdateHandler addLineItem( context: RepositoryContext, resource: Writable, - { productId, variantId, sku, quantity = 1 }: CartAddLineItemAction, + { productId, variantId, sku, custom, quantity = 1 }: CartAddLineItemAction, ) { let product: Product | null = null; @@ -175,6 +175,7 @@ export class CartUpdateHandler lineItemMode: "Standard", priceMode: "Platform", state: [], + custom: createCustomFields(custom, context.projectKey, this._storage), }); } diff --git a/src/services/cart.test.ts b/src/services/cart.test.ts index 64d345d7..ea064581 100644 --- a/src/services/cart.test.ts +++ b/src/services/cart.test.ts @@ -259,6 +259,54 @@ describe("Cart Update Actions", () => { expect(response.body.totalPrice.centAmount).toEqual(total); }); + test("addLineItem with custom field", async () => { + const product = await supertest(ctMock.app) + .post(`/dummy/products`) + .send(productDraft) + .then((x) => x.body); + + const type = await supertest(ctMock.app) + .post(`/dummy/types`) + .send({ + key: "my-type", + name: { + en: "My Type", + }, + description: { + en: "My Type Description", + }, + fieldDefinitions: [ + { + name: "foo", + label: { + en: "foo", + }, + required: false, + type: { + name: "String", + }, + inputHint: "SingleLine", + }, + ], + }) + .then((x) => x.body); + + assert(type, "type not created"); + assert(cart, "cart not created"); + assert(product, "product not created"); + + const response = await supertest(ctMock.app) + .post(`/dummy/carts/${cart.id}`) + .send({ + version: 1, + actions: [{ action: "addLineItem", sku: "1337", quantity: 2, custom: { type: { typeId: 'type', key: 'my-type' }, fields: { foo: 'bar' }} }], + }); + expect(response.status).toBe(200); + expect(response.body.version).toBe(2); + expect(response.body.lineItems).toHaveLength(1); + expect(response.body.lineItems[0].custom).toEqual({ type: { typeId: 'type', id: expect.any(String) }, fields: { foo: 'bar' } }); + }); + test("addLineItem unknown product", async () => { assert(cart, "cart not created"); From ced3d4011bdc41aad6c3d1edf765b5677373dcfc Mon Sep 17 00:00:00 2001 From: alexfaxe Date: Wed, 11 Sep 2024 16:14:26 +0200 Subject: [PATCH 2/2] fix(styling): prettier --- src/services/cart.test.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/services/cart.test.ts b/src/services/cart.test.ts index ea064581..7104c9db 100644 --- a/src/services/cart.test.ts +++ b/src/services/cart.test.ts @@ -299,12 +299,25 @@ describe("Cart Update Actions", () => { .post(`/dummy/carts/${cart.id}`) .send({ version: 1, - actions: [{ action: "addLineItem", sku: "1337", quantity: 2, custom: { type: { typeId: 'type', key: 'my-type' }, fields: { foo: 'bar' }} }], + actions: [ + { + action: "addLineItem", + sku: "1337", + quantity: 2, + custom: { + type: { typeId: "type", key: "my-type" }, + fields: { foo: "bar" }, + }, + }, + ], }); expect(response.status).toBe(200); expect(response.body.version).toBe(2); expect(response.body.lineItems).toHaveLength(1); - expect(response.body.lineItems[0].custom).toEqual({ type: { typeId: 'type', id: expect.any(String) }, fields: { foo: 'bar' } }); + expect(response.body.lineItems[0].custom).toEqual({ + type: { typeId: "type", id: expect.any(String) }, + fields: { foo: "bar" }, + }); }); test("addLineItem unknown product", async () => {