Skip to content

Commit

Permalink
fix/allow creating customers with storekey reference (labd#254)
Browse files Browse the repository at this point in the history
This fixes a bug with setting the stores of a customer, it previously
would throw an error if a single store was set using a storekey.
  • Loading branch information
jsm1t authored Dec 6, 2024
1 parent 67de195 commit 424a375
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-kiwis-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": patch
---

Fix setting stores to customer by storekey
42 changes: 41 additions & 1 deletion src/repositories/customer/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("Customer repository", () => {
);
});

test("adding stores to customer", async () => {
test("adding multiple stores to customer", async () => {
const store1: Store = {
id: "d0016081-e9af-48a7-8133-1f04f340a335",
key: "store-1",
Expand Down Expand Up @@ -112,6 +112,46 @@ describe("Customer repository", () => {
]);
});

test("adding single store to customer", async () => {
const store1: Store = {
id: "58082253-fe4e-4714-941f-86ab596d42ed",
key: "store-1",
name: {
en: "Store 1",
},
version: 1,
createdAt: "2021-09-02T12:23:30.036Z",
lastModifiedAt: "2021-09-02T12:23:30.546Z",
languages: [],
distributionChannels: [],
countries: [],
supplyChannels: [],
productSelections: [],
};

storage.add("dummy", "store", store1);

const result = repository.create(
{ projectKey: "dummy" },
{
email: "[email protected]",
stores: [
{
typeId: "store",
key: store1.key,
},
],
},
);

expect(result?.stores).toEqual([
{
typeId: "store",
key: store1.key,
},
]);
});

test("adding customer without linked stores", async () => {
const result = repository.create(
{ projectKey: "dummy" },
Expand Down
59 changes: 37 additions & 22 deletions src/repositories/customer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import type {
InvalidInputError,
MyCustomerResetPassword,
ResourceNotFoundError,
Store,
StoreKeyReference,
StoreResourceIdentifier,
} from "@commercetools/platform-sdk";
import { CommercetoolsError } from "~src/exceptions";
import { generateRandomString, getBaseResourceProperties } from "~src/helpers";
Expand Down Expand Up @@ -106,28 +108,10 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
let storesForCustomer: StoreKeyReference[] = [];

if (draft.stores && draft.stores.length > 0) {
const storeIds = draft.stores
.map((storeReference) => storeReference.id)
.filter(Boolean);

const stores = this._storage.query(context.projectKey, "store", {
where: storeIds.map((id) => `id="${id}"`),
}).results;

if (storeIds.length !== stores.length) {
throw new CommercetoolsError<ResourceNotFoundError>({
code: "ResourceNotFound",
message: `Store with ID '${storeIds.find((id) => !stores.some((store) => store.id === id))}' was not found.`,
});
}

storesForCustomer = draft.stores.map((storeReference) => ({
typeId: "store",
key:
storeReference.key ??
(stores.find((store) => store.id === storeReference.id)
?.key as string),
}));
storesForCustomer = this.storeReferenceToStoreKeyReference(
draft.stores,
context.projectKey,
);
}

const resource: Customer = {
Expand Down Expand Up @@ -267,4 +251,35 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
value: token,
};
}

private storeReferenceToStoreKeyReference(
draftStores: StoreResourceIdentifier[],
projectKey: string,
): StoreKeyReference[] {
const storeIds = draftStores
.map((storeReference) => storeReference.id)
.filter(Boolean);

let stores: Store[] = [];

if (storeIds.length > 0) {
stores = this._storage.query(projectKey, "store", {
where: storeIds.map((id) => `id="${id}"`),
}).results;

if (storeIds.length !== stores.length) {
throw new CommercetoolsError<ResourceNotFoundError>({
code: "ResourceNotFound",
message: `Store with ID '${storeIds.find((id) => !stores.some((store) => store.id === id))}' was not found.`,
});
}
}

return draftStores.map((storeReference) => ({
typeId: "store",
key:
storeReference.key ??
(stores.find((store) => store.id === storeReference.id)?.key as string),
}));
}
}

0 comments on commit 424a375

Please sign in to comment.