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: address custom type actions #224

Merged
merged 2 commits into from
Oct 7, 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/afraid-donuts-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": patch
---

Added the setShippingAddressCustomType and setBillingAddressCustomType actions to the cart repository
66 changes: 66 additions & 0 deletions src/repositories/cart/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
type CartRemoveDiscountCodeAction,
type CartRemoveLineItemAction,
type CartSetBillingAddressAction,
type CartSetBillingAddressCustomTypeAction,
type CartSetCountryAction,
type CartSetCustomFieldAction,
type CartSetCustomShippingMethodAction,
Expand All @@ -21,6 +22,7 @@ import {
type CartSetLineItemShippingDetailsAction,
type CartSetLocaleAction,
type CartSetShippingAddressAction,
type CartSetShippingAddressCustomTypeAction,
type CartSetShippingMethodAction,
type CustomFields,
type GeneralError,
Expand Down Expand Up @@ -323,6 +325,38 @@ export class CartUpdateHandler
);
}

setBillingAddressCustomType(
context: RepositoryContext,
resource: Writable<Cart>,
custom: CartSetBillingAddressCustomTypeAction,
) {
if (!resource.billingAddress) {
throw new Error("Resource has no billing address");
}

if (!custom.type) {
resource.billingAddress.custom = undefined;
return;
}

const resolvedType = this._storage.getByResourceIdentifier<"type">(
context.projectKey,
custom.type,
);

if (!resolvedType) {
throw new Error(`Type ${custom.type} not found`);
}

resource.billingAddress.custom = {
type: {
typeId: "type",
id: resolvedType.id,
},
fields: custom.fields || {},
};
}

setCountry(
context: RepositoryContext,
resource: Writable<Cart>,
Expand Down Expand Up @@ -503,6 +537,38 @@ export class CartUpdateHandler
};
}

setShippingAddressCustomType(
context: RepositoryContext,
resource: Writable<Cart>,
custom: CartSetShippingAddressCustomTypeAction,
) {
if (!resource.shippingAddress) {
throw new Error("Resource has no shipping address");
}

if (!custom.type) {
resource.shippingAddress.custom = undefined;
return;
}

const resolvedType = this._storage.getByResourceIdentifier<"type">(
context.projectKey,
custom.type,
);

if (!resolvedType) {
throw new Error(`Type ${custom.type} not found`);
}

resource.shippingAddress.custom = {
type: {
typeId: "type",
id: resolvedType.id,
},
fields: custom.fields || {},
};
}

setShippingMethod(
context: RepositoryContext,
resource: Writable<Cart>,
Expand Down
134 changes: 134 additions & 0 deletions src/services/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,140 @@ describe("Cart Update Actions", () => {
expect(response.body.shippingAddress).toEqual(address);
});

test("setBillingAddressCustomType", async () => {
assert(cart, "cart not created");

const address: Address = {
streetName: "Street name",
city: "Utrecht",
country: "NL",
};

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");

const response = await supertest(ctMock.app)
.post(`/dummy/carts/${cart.id}`)
.send({
version: 1,
actions: [
{ action: "setBillingAddress", address },
{
action: "setBillingAddressCustomType",
type: {
typeId: "type",
type: "my-type",
key: "my-type",
},
fields: {
foo: "bar",
},
},
],
});

expect(response.status).toBe(200);
expect(response.body.version).toBe(3);
expect(response.body.billingAddress).toEqual({
...address,
custom: {
type: { typeId: "type", id: type.id },
fields: { foo: "bar" },
},
});
});
test("setShippingAddressCustomType", async () => {
assert(cart, "cart not created");

const address: Address = {
streetName: "Street name",
city: "Utrecht",
country: "NL",
};

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");

const response = await supertest(ctMock.app)
.post(`/dummy/carts/${cart.id}`)
.send({
version: 1,
actions: [
{ action: "setShippingAddress", address },
{
action: "setShippingAddressCustomType",
type: {
typeId: "type",
type: "my-type",
key: "my-type",
},
fields: {
foo: "bar",
},
},
],
});

expect(response.status).toBe(200);
expect(response.body.version).toBe(3);
expect(response.body.shippingAddress).toEqual({
...address,
custom: {
type: { typeId: "type", id: type.id },
fields: { foo: "bar" },
},
});
});
test("setLineItemShippingDetails", async () => {
const product = await supertest(ctMock.app)
.post(`/dummy/products`)
Expand Down