@labdigital/commercetools-mock 2.38.0 → 2.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
- "version": "2.38.0",
3
+ "version": "2.40.0",
4
4
  "license": "MIT",
5
5
  "author": "Michael van Tellingen",
6
6
  "type": "module",
@@ -7,6 +7,7 @@ import type {
7
7
  CartDiscountSetCustomTypeAction,
8
8
  CartDiscountSetDescriptionAction,
9
9
  CartDiscountSetKeyAction,
10
+ CartDiscountSetStoresAction,
10
11
  CartDiscountSetValidFromAction,
11
12
  CartDiscountSetValidFromAndUntilAction,
12
13
  CartDiscountSetValidUntilAction,
@@ -18,6 +19,7 @@ import type { UpdateHandlerInterface } from "../abstract";
18
19
  import { AbstractUpdateHandler, type RepositoryContext } from "../abstract";
19
20
 
20
21
  import { CommercetoolsError } from "~src/exceptions";
22
+ import { getStoreKeyReference } from "~src/repositories/helpers";
21
23
 
22
24
  export class CartDiscountUpdateHandler
23
25
  extends AbstractUpdateHandler
@@ -118,6 +120,16 @@ export class CartDiscountUpdateHandler
118
120
  resource.key = key;
119
121
  }
120
122
 
123
+ setStores(
124
+ context: RepositoryContext,
125
+ resource: Writable<CartDiscount>,
126
+ { stores }: CartDiscountSetStoresAction,
127
+ ) {
128
+ resource.stores = stores?.map((s) =>
129
+ getStoreKeyReference(s, context.projectKey, this._storage),
130
+ );
131
+ }
132
+
121
133
  setValidFrom(
122
134
  context: RepositoryContext,
123
135
  resource: Writable<CartDiscount>,
@@ -0,0 +1,75 @@
1
+ import type { Store } from "@commercetools/platform-sdk";
2
+ import { describe, expect, test } from "vitest";
3
+ import { InMemoryStorage } from "~src/storage";
4
+ import { CustomerRepository } from "./index";
5
+
6
+ describe("Order repository", () => {
7
+ const storage = new InMemoryStorage();
8
+ const repository = new CustomerRepository(storage);
9
+
10
+ test("adding stores to customer", async () => {
11
+ const store1: Store = {
12
+ id: "d0016081-e9af-48a7-8133-1f04f340a335",
13
+ key: "store-1",
14
+ name: {
15
+ en: "Store 1",
16
+ },
17
+ version: 1,
18
+ createdAt: "2021-09-02T12:23:30.036Z",
19
+ lastModifiedAt: "2021-09-02T12:23:30.546Z",
20
+ languages: [],
21
+ distributionChannels: [],
22
+ countries: [],
23
+ supplyChannels: [],
24
+ productSelections: [],
25
+ };
26
+
27
+ const store2: Store = {
28
+ id: "6dac7d6d-2a48-4705-aa8b-17b0124a499a",
29
+ key: "store-2",
30
+ name: {
31
+ en: "Store 2",
32
+ },
33
+ version: 1,
34
+ createdAt: "2021-09-02T12:23:30.036Z",
35
+ lastModifiedAt: "2021-09-02T12:23:30.546Z",
36
+ languages: [],
37
+ distributionChannels: [],
38
+ countries: [],
39
+ supplyChannels: [],
40
+ productSelections: [],
41
+ };
42
+
43
+ storage.add("dummy", "store", store1);
44
+ storage.add("dummy", "store", store2);
45
+
46
+ const result = repository.create(
47
+ { projectKey: "dummy" },
48
+ {
49
+ email: "my-customer@email.com",
50
+ stores: [
51
+ {
52
+ typeId: "store",
53
+ id: store1.id,
54
+ },
55
+ {
56
+ typeId: "store",
57
+ key: store2.key,
58
+ },
59
+ ],
60
+ },
61
+ );
62
+
63
+ expect(result?.stores).toHaveLength(2);
64
+ expect(result?.stores).toEqual([
65
+ {
66
+ typeId: "store",
67
+ key: store1.key,
68
+ },
69
+ {
70
+ typeId: "store",
71
+ key: store2.key,
72
+ },
73
+ ]);
74
+ });
75
+ });
@@ -9,6 +9,7 @@ import type {
9
9
  InvalidInputError,
10
10
  MyCustomerResetPassword,
11
11
  ResourceNotFoundError,
12
+ StoreKeyReference,
12
13
  } from "@commercetools/platform-sdk";
13
14
  import { CommercetoolsError } from "~src/exceptions";
14
15
  import { generateRandomString, getBaseResourceProperties } from "~src/helpers";
@@ -102,6 +103,33 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
102
103
  lookupAdressId(addresses, addressId),
103
104
  ) ?? [];
104
105
 
106
+ let storesForCustomer: StoreKeyReference[] = [];
107
+
108
+ if (draft.stores) {
109
+ const storeIds = draft.stores
110
+ .map((storeReference) => storeReference.id)
111
+ .filter(Boolean);
112
+
113
+ const stores = this._storage.query(context.projectKey, "store", {
114
+ where: storeIds.map((id) => `id="${id}"`),
115
+ }).results;
116
+
117
+ if (storeIds.length !== stores.length) {
118
+ throw new CommercetoolsError<ResourceNotFoundError>({
119
+ code: "ResourceNotFound",
120
+ message: `Store with ID '${storeIds.find((id) => !stores.some((store) => store.id === id))}' was not found.`,
121
+ });
122
+ }
123
+
124
+ storesForCustomer = draft.stores.map((storeReference) => ({
125
+ typeId: "store",
126
+ key:
127
+ storeReference.key ??
128
+ (stores.find((store) => store.id === storeReference.id)
129
+ ?.key as string),
130
+ }));
131
+ }
132
+
105
133
  const resource: Customer = {
106
134
  ...getBaseResourceProperties(),
107
135
  key: draft.key,
@@ -127,7 +155,7 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
127
155
  context.projectKey,
128
156
  this._storage,
129
157
  ),
130
- stores: [],
158
+ stores: storesForCustomer,
131
159
  };
132
160
  return this.saveNew(context, resource);
133
161
  }