Skip to content
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/seven-ravens-like.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": patch
---

Generate an ID for address when one has not been passed
7 changes: 2 additions & 5 deletions src/repositories/business-unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,12 @@ class BusinessUnitUpdateHandler
}

const newAddress = createAddress(
address,
{ ...address, id: addressId },
context.projectKey,
this._storage,
);
if (newAddress) {
resource.addresses[existingAddressIndex] = {
...newAddress,
id: addressId,
};
resource.addresses[existingAddressIndex] = newAddress;
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/repositories/customer/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,13 @@ export class CustomerUpdateHandler
);

const newAddress = createAddress(
address,
{ ...address, id: current.id },
context.projectKey,
this._storage,
);

if (newAddress) {
resource.addresses[oldAddressIndex] = {
id: addressId,
...newAddress,
};
resource.addresses[oldAddressIndex] = newAddress;
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/repositories/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { BaseAddress } from "@commercetools/platform-sdk";
import { describe, expect, test } from "vitest";
import { InMemoryStorage } from "#src/storage/index.ts";
import { createAddress } from "./helpers.ts";

describe("Helpers", () => {
const storage = new InMemoryStorage();
const projectKey = "test-project";

describe("createAddress", () => {
test("should generate random id when id is not provided", () => {
const baseAddress: BaseAddress = {
country: "US",
streetName: "Test Street",
city: "Test City",
};

const result = createAddress(baseAddress, projectKey, storage);
expect(result).toBeDefined();
expect(result?.id).toBeDefined();
expect(typeof result?.id).toBe("string");
expect(result?.id).toMatch(/^[a-z0-9]{8}$/);
});
});
});
5 changes: 5 additions & 0 deletions src/repositories/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ export const createAddress = (
throw new Error("Country is required");
}

// Generate a random 8-character alphanumeric string
// which is what addresses use instead of uuid
const generateRandomId = (): string =>
Math.random().toString(36).substring(2, 10).padEnd(8, "0");
return {
...base,
id: base.id ?? generateRandomId(),
};
};

Expand Down
6 changes: 5 additions & 1 deletion src/services/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,10 @@ describe("Cart Update Actions", () => {
});
expect(response.status).toBe(200);
expect(response.body.version).toBe(2);
expect(response.body.billingAddress).toEqual(address);
expect(response.body.billingAddress).toEqual({
...address,
id: expect.any(String),
});
});

test("setCountry", async () => {
Expand Down Expand Up @@ -1187,6 +1190,7 @@ describe("Cart Update Actions", () => {
expect(response.body.version).toBe(3);
expect(response.body.billingAddress).toEqual({
...address,
id: expect.any(String),
custom: {
type: { typeId: "type", id: type.id },
fields: { foo: "bar" },
Expand Down