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

fix(customer): add setting of stores on customer #249

Merged
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/curly-carpets-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": minor
---

Add support for setting stores on customers
75 changes: 75 additions & 0 deletions src/repositories/customer/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type { Store } from "@commercetools/platform-sdk";
import { describe, expect, test } from "vitest";
import { InMemoryStorage } from "~src/storage";
import { CustomerRepository } from "./index";

describe("Order repository", () => {
const storage = new InMemoryStorage();
const repository = new CustomerRepository(storage);

test("adding stores to customer", async () => {
const store1: Store = {
id: "d0016081-e9af-48a7-8133-1f04f340a335",
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: [],
};

const store2: Store = {
id: "6dac7d6d-2a48-4705-aa8b-17b0124a499a",
key: "store-2",
name: {
en: "Store 2",
},
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);
storage.add("dummy", "store", store2);

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

expect(result?.stores).toHaveLength(2);
expect(result?.stores).toEqual([
{
typeId: "store",
key: store1.key,
},
{
typeId: "store",
key: store2.key,
},
]);
});
});
30 changes: 29 additions & 1 deletion src/repositories/customer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
InvalidInputError,
MyCustomerResetPassword,
ResourceNotFoundError,
StoreKeyReference,
} from "@commercetools/platform-sdk";
import { CommercetoolsError } from "~src/exceptions";
import { generateRandomString, getBaseResourceProperties } from "~src/helpers";
Expand Down Expand Up @@ -102,6 +103,33 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
lookupAdressId(addresses, addressId),
) ?? [];

let storesForCustomer: StoreKeyReference[] = [];

if (draft.stores) {
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),
}));
}

const resource: Customer = {
...getBaseResourceProperties(),
key: draft.key,
Expand All @@ -127,7 +155,7 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
context.projectKey,
this._storage,
),
stores: [],
stores: storesForCustomer,
};
return this.saveNew(context, resource);
}
Expand Down