From f61e38dc32af4094c49618354c4ea393d35baaab Mon Sep 17 00:00:00 2001 From: Joost Smit <19669805+jsm1t@users.noreply.github.com> Date: Thu, 21 Nov 2024 23:56:58 +0100 Subject: [PATCH] fix(customer): customer repository allow no stores (#251) Empty arrays for the stores of a customer were wrongfully check for empty arrays. Fixes issue introduced in #249. --- .changeset/cold-bugs-approve.md | 5 +++++ src/repositories/customer/index.test.ts | 15 ++++++++++++++- src/repositories/customer/index.ts | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 .changeset/cold-bugs-approve.md diff --git a/.changeset/cold-bugs-approve.md b/.changeset/cold-bugs-approve.md new file mode 100644 index 0000000..ade5d17 --- /dev/null +++ b/.changeset/cold-bugs-approve.md @@ -0,0 +1,5 @@ +--- +"@labdigital/commercetools-mock": patch +--- + +Fix customer not being allowed to have no stores diff --git a/src/repositories/customer/index.test.ts b/src/repositories/customer/index.test.ts index af28f84..a6aa4f7 100644 --- a/src/repositories/customer/index.test.ts +++ b/src/repositories/customer/index.test.ts @@ -3,7 +3,7 @@ import { describe, expect, test } from "vitest"; import { InMemoryStorage } from "~src/storage"; import { CustomerRepository } from "./index"; -describe("Order repository", () => { +describe("Customer repository", () => { const storage = new InMemoryStorage(); const repository = new CustomerRepository(storage); @@ -111,4 +111,17 @@ describe("Order repository", () => { }, ]); }); + + test("adding customer without linked stores", async () => { + const result = repository.create( + { projectKey: "dummy" }, + { + email: "my-customer-without-stores@email.com", + stores: [], + }, + ); + + expect(result.email).toEqual("my-customer-without-stores@email.com"); + expect(result?.stores).toHaveLength(0); + }); }); diff --git a/src/repositories/customer/index.ts b/src/repositories/customer/index.ts index 313bf46..48dbc13 100644 --- a/src/repositories/customer/index.ts +++ b/src/repositories/customer/index.ts @@ -105,7 +105,7 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> { let storesForCustomer: StoreKeyReference[] = []; - if (draft.stores) { + if (draft.stores && draft.stores.length > 0) { const storeIds = draft.stores .map((storeReference) => storeReference.id) .filter(Boolean);