diff --git a/.changeset/ten-clocks-approve.md b/.changeset/ten-clocks-approve.md new file mode 100644 index 00000000..1dc91625 --- /dev/null +++ b/.changeset/ten-clocks-approve.md @@ -0,0 +1,5 @@ +--- +"@labdigital/commercetools-mock": minor +--- + +add missing addParcelCustomField action to order diff --git a/src/repositories/order/actions.ts b/src/repositories/order/actions.ts index b843db2e..f2b0b7ef 100644 --- a/src/repositories/order/actions.ts +++ b/src/repositories/order/actions.ts @@ -15,6 +15,7 @@ import type { OrderSetDeliveryCustomFieldAction, OrderSetLocaleAction, OrderSetOrderNumberAction, + OrderSetParcelCustomFieldAction, OrderSetPurchaseOrderNumberAction, OrderSetShippingAddressAction, OrderSetStoreAction, @@ -26,7 +27,6 @@ import type { Store, SyncInfo, } from "@commercetools/platform-sdk"; -import assert from "assert"; import { getBaseResourceProperties } from "~src/helpers"; import type { Writable } from "~src/types"; import type { RepositoryContext, UpdateHandlerInterface } from "../abstract"; @@ -194,18 +194,14 @@ export class OrderUpdateHandler resource: Writable, { deliveryId, name, value }: OrderSetDeliveryCustomFieldAction, ) { - assert(resource.shippingInfo, "shippingInfo is not defined"); + if (!resource.shippingInfo) { + throw new Error("Resource has no shipping info"); + } - if (Array.isArray(resource.shippingInfo.deliveries)) { - resource.shippingInfo.deliveries.map((delivery) => { - if (delivery.id !== deliveryId) throw "No matching delivery id found"; - if (delivery.custom) { - const update = delivery.custom.fields; - update[name] = value; - Object.assign(delivery.custom.fields, update); - } - return delivery; - }); + for (const delivery of resource.shippingInfo.deliveries || []) { + if (delivery.id === deliveryId && delivery.custom?.fields) { + delivery.custom.fields[name] = value; + } } } @@ -225,6 +221,24 @@ export class OrderUpdateHandler resource.orderNumber = orderNumber; } + setParcelCustomField( + context: RepositoryContext, + resource: Writable, + { parcelId, name, value }: OrderSetParcelCustomFieldAction, + ) { + if (!resource.shippingInfo) { + throw new Error("Resource has no shipping info"); + } + + for (const delivery of resource.shippingInfo.deliveries || []) { + for (const parcel of delivery.parcels || []) { + if (parcel.id === parcelId && parcel.custom?.fields) { + parcel.custom.fields[name] = value; + } + } + } + } + setPurchaseOrderNumber( context: RepositoryContext, resource: Writable, diff --git a/src/services/order.test.ts b/src/services/order.test.ts index 6465ce66..a40ff33c 100644 --- a/src/services/order.test.ts +++ b/src/services/order.test.ts @@ -593,6 +593,156 @@ describe("Order Update Actions", () => { ).toBe("dhl"); }); + test("setParcelCustomField", async () => { + const order: Order = { + ...getBaseResourceProperties(), + customLineItems: [], + lastMessageSequenceNumber: 0, + lineItems: [], + orderNumber: "1390", + orderState: "Open", + origin: "Customer", + paymentInfo: { + payments: [ + { + typeId: "payment", + id: generateRandomString(10), + }, + ], + }, + refusedGifts: [], + shippingInfo: { + shippingMethodName: "Home delivery (package)", + price: { + type: "centPrecision", + currencyCode: "EUR", + centAmount: 999, + fractionDigits: 2, + }, + shippingRate: { + price: { + type: "centPrecision", + currencyCode: "EUR", + centAmount: 999, + fractionDigits: 2, + }, + tiers: [ + { + type: "CartScore", + score: 24, + price: { + type: "centPrecision", + currencyCode: "EUR", + centAmount: 1998, + fractionDigits: 2, + }, + }, + { + type: "CartScore", + score: 47, + price: { + type: "centPrecision", + currencyCode: "EUR", + centAmount: 2997, + fractionDigits: 2, + }, + }, + { + type: "CartScore", + score: 70, + price: { + type: "centPrecision", + currencyCode: "EUR", + centAmount: 3996, + fractionDigits: 2, + }, + }, + { + type: "CartScore", + score: 93, + price: { + type: "centPrecision", + currencyCode: "EUR", + centAmount: 4995, + fractionDigits: 2, + }, + }, + ], + }, + deliveries: [ + { + id: "6a458cad-dd46-4f5f-8b73-debOede6a17d", + key: "CT-Z243002", + createdAt: "2024-07-29T13:37:48.047Z", + items: [ + { + id: "5d209544-2892-45c9-bef0-dde4e250188e", + quantity: 1, + }, + ], + parcels: [ + { + id: "7a458cad-dd46-4f5f-8b73-debOede6a17d", + createdAt: "2024-07-29T13:37:48.047Z", + items: [ + { + id: "5d209544-2892-45c9-bef0-dde4e250188e", + quantity: 1, + }, + ], + custom: { + type: { + typeId: "type", + id: "c493b7bb-d415-450c-b421-e128a8b26569", + }, + fields: { + status: "created", + }, + }, + }, + ], + }, + ], + shippingMethodState: "MatchesCart", + }, + shipping: [], + shippingMode: "Single", + syncInfo: [], + totalPrice: { + type: "centPrecision", + fractionDigits: 2, + centAmount: 2000, + currencyCode: "EUR", + }, + }; + ctMock.project("dummy").add("order", order); + + const response = await supertest(ctMock.app).get( + `/dummy/orders/order-number=${order.orderNumber}`, + ); + + // check if status is set + const _updateResponse = await supertest(ctMock.app) + .post(`/dummy/orders/${response.body.id}`) + .send({ + version: 0, + actions: [ + { + action: "setParcelCustomField", + parcelId: "7a458cad-dd46-4f5f-8b73-debOede6a17d", + name: "status", + value: "delayed", + }, + ], + }); + expect(_updateResponse.status).toBe(200); + expect(_updateResponse.body.version).toBe(1); + expect( + _updateResponse.body.shippingInfo.deliveries[0].parcels[0].custom.fields + .status, + ).toBe("delayed"); + }); + test("updateSyncInfo", async () => { assert(order, "order not created");