Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(order): add missing addParcelCustomField action to order #248

Merged
merged 7 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ten-clocks-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": minor
---

add missing addParcelCustomField action to order
38 changes: 26 additions & 12 deletions src/repositories/order/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
OrderSetDeliveryCustomFieldAction,
OrderSetLocaleAction,
OrderSetOrderNumberAction,
OrderSetParcelCustomFieldAction,
OrderSetPurchaseOrderNumberAction,
OrderSetShippingAddressAction,
OrderSetStoreAction,
Expand All @@ -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";
Expand Down Expand Up @@ -194,18 +194,14 @@ export class OrderUpdateHandler
resource: Writable<Order>,
{ 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;
}
}
}

Expand All @@ -225,6 +221,24 @@ export class OrderUpdateHandler
resource.orderNumber = orderNumber;
}

setParcelCustomField(
context: RepositoryContext,
resource: Writable<Order>,
{ 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;
}
}
}
}

gijsmin marked this conversation as resolved.
Show resolved Hide resolved
setPurchaseOrderNumber(
context: RepositoryContext,
resource: Writable<Order>,
Expand Down
150 changes: 150 additions & 0 deletions src/services/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down