@labdigital/commercetools-mock 2.41.0 → 2.41.1
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/dist/index.cjs +23 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +23 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/repositories/customer/index.test.ts +41 -1
- package/src/repositories/customer/index.ts +37 -22
package/package.json
CHANGED
|
@@ -46,7 +46,7 @@ describe("Customer repository", () => {
|
|
|
46
46
|
);
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
-
test("adding stores to customer", async () => {
|
|
49
|
+
test("adding multiple stores to customer", async () => {
|
|
50
50
|
const store1: Store = {
|
|
51
51
|
id: "d0016081-e9af-48a7-8133-1f04f340a335",
|
|
52
52
|
key: "store-1",
|
|
@@ -112,6 +112,46 @@ describe("Customer repository", () => {
|
|
|
112
112
|
]);
|
|
113
113
|
});
|
|
114
114
|
|
|
115
|
+
test("adding single store to customer", async () => {
|
|
116
|
+
const store1: Store = {
|
|
117
|
+
id: "58082253-fe4e-4714-941f-86ab596d42ed",
|
|
118
|
+
key: "store-1",
|
|
119
|
+
name: {
|
|
120
|
+
en: "Store 1",
|
|
121
|
+
},
|
|
122
|
+
version: 1,
|
|
123
|
+
createdAt: "2021-09-02T12:23:30.036Z",
|
|
124
|
+
lastModifiedAt: "2021-09-02T12:23:30.546Z",
|
|
125
|
+
languages: [],
|
|
126
|
+
distributionChannels: [],
|
|
127
|
+
countries: [],
|
|
128
|
+
supplyChannels: [],
|
|
129
|
+
productSelections: [],
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
storage.add("dummy", "store", store1);
|
|
133
|
+
|
|
134
|
+
const result = repository.create(
|
|
135
|
+
{ projectKey: "dummy" },
|
|
136
|
+
{
|
|
137
|
+
email: "my-customer2@email.com",
|
|
138
|
+
stores: [
|
|
139
|
+
{
|
|
140
|
+
typeId: "store",
|
|
141
|
+
key: store1.key,
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
},
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
expect(result?.stores).toEqual([
|
|
148
|
+
{
|
|
149
|
+
typeId: "store",
|
|
150
|
+
key: store1.key,
|
|
151
|
+
},
|
|
152
|
+
]);
|
|
153
|
+
});
|
|
154
|
+
|
|
115
155
|
test("adding customer without linked stores", async () => {
|
|
116
156
|
const result = repository.create(
|
|
117
157
|
{ projectKey: "dummy" },
|
|
@@ -9,7 +9,9 @@ import type {
|
|
|
9
9
|
InvalidInputError,
|
|
10
10
|
MyCustomerResetPassword,
|
|
11
11
|
ResourceNotFoundError,
|
|
12
|
+
Store,
|
|
12
13
|
StoreKeyReference,
|
|
14
|
+
StoreResourceIdentifier,
|
|
13
15
|
} from "@commercetools/platform-sdk";
|
|
14
16
|
import { CommercetoolsError } from "~src/exceptions";
|
|
15
17
|
import { generateRandomString, getBaseResourceProperties } from "~src/helpers";
|
|
@@ -106,28 +108,10 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
|
|
|
106
108
|
let storesForCustomer: StoreKeyReference[] = [];
|
|
107
109
|
|
|
108
110
|
if (draft.stores && draft.stores.length > 0) {
|
|
109
|
-
|
|
110
|
-
.
|
|
111
|
-
.
|
|
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
|
-
}));
|
|
111
|
+
storesForCustomer = this.storeReferenceToStoreKeyReference(
|
|
112
|
+
draft.stores,
|
|
113
|
+
context.projectKey,
|
|
114
|
+
);
|
|
131
115
|
}
|
|
132
116
|
|
|
133
117
|
const resource: Customer = {
|
|
@@ -267,4 +251,35 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
|
|
|
267
251
|
value: token,
|
|
268
252
|
};
|
|
269
253
|
}
|
|
254
|
+
|
|
255
|
+
private storeReferenceToStoreKeyReference(
|
|
256
|
+
draftStores: StoreResourceIdentifier[],
|
|
257
|
+
projectKey: string,
|
|
258
|
+
): StoreKeyReference[] {
|
|
259
|
+
const storeIds = draftStores
|
|
260
|
+
.map((storeReference) => storeReference.id)
|
|
261
|
+
.filter(Boolean);
|
|
262
|
+
|
|
263
|
+
let stores: Store[] = [];
|
|
264
|
+
|
|
265
|
+
if (storeIds.length > 0) {
|
|
266
|
+
stores = this._storage.query(projectKey, "store", {
|
|
267
|
+
where: storeIds.map((id) => `id="${id}"`),
|
|
268
|
+
}).results;
|
|
269
|
+
|
|
270
|
+
if (storeIds.length !== stores.length) {
|
|
271
|
+
throw new CommercetoolsError<ResourceNotFoundError>({
|
|
272
|
+
code: "ResourceNotFound",
|
|
273
|
+
message: `Store with ID '${storeIds.find((id) => !stores.some((store) => store.id === id))}' was not found.`,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return draftStores.map((storeReference) => ({
|
|
279
|
+
typeId: "store",
|
|
280
|
+
key:
|
|
281
|
+
storeReference.key ??
|
|
282
|
+
(stores.find((store) => store.id === storeReference.id)?.key as string),
|
|
283
|
+
}));
|
|
284
|
+
}
|
|
270
285
|
}
|