@labdigital/commercetools-mock 2.51.0 → 2.52.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/dist/index.d.ts +13 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +109 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/product-review-statistics.test.ts +349 -0
- package/src/lib/review-statistics.ts +58 -0
- package/src/product-projection-search.ts +17 -2
- package/src/repositories/as-associate.test.ts +126 -0
- package/src/repositories/attribute-group.test.ts +221 -0
- package/src/repositories/business-unit.test.ts +282 -0
- package/src/repositories/business-unit.ts +5 -1
- package/src/repositories/channel.test.ts +374 -0
- package/src/repositories/customer-group.test.ts +262 -0
- package/src/repositories/extension.test.ts +306 -0
- package/src/repositories/index.test.ts +17 -0
- package/src/repositories/product/index.ts +22 -1
- package/src/repositories/product-projection.ts +8 -2
- package/src/repositories/review.test.ts +636 -0
- package/src/repositories/review.ts +145 -4
- package/src/repositories/subscription.test.ts +207 -0
- package/src/repositories/zone.test.ts +278 -0
- package/src/services/as-associate-cart.test.ts +58 -0
- package/src/services/as-associate.test.ts +34 -0
- package/src/services/attribute-group.test.ts +114 -0
- package/src/services/channel.test.ts +90 -0
- package/src/services/customer-group.test.ts +85 -0
- package/src/services/discount-code.test.ts +120 -0
- package/src/services/extension.test.ts +130 -0
- package/src/services/my-business-unit.test.ts +113 -0
- package/src/services/my-business-unit.ts +6 -0
- package/src/services/my-customer.test.ts +24 -0
- package/src/services/order.test.ts +18 -0
- package/src/services/product-discount.test.ts +146 -0
- package/src/services/project.test.ts +17 -0
- package/src/services/reviews.test.ts +230 -0
- package/src/services/subscription.test.ts +151 -0
- package/src/services/type.test.ts +127 -0
- package/src/services/zone.test.ts +117 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Extension,
|
|
3
|
+
ExtensionChangeDestinationAction,
|
|
4
|
+
ExtensionChangeTriggersAction,
|
|
5
|
+
ExtensionDraft,
|
|
6
|
+
ExtensionSetKeyAction,
|
|
7
|
+
ExtensionSetTimeoutInMsAction,
|
|
8
|
+
} from "@commercetools/platform-sdk";
|
|
9
|
+
import { describe, expect, test } from "vitest";
|
|
10
|
+
import type { Config } from "~src/config";
|
|
11
|
+
import { InMemoryStorage } from "~src/storage";
|
|
12
|
+
import { ExtensionRepository } from "./extension";
|
|
13
|
+
|
|
14
|
+
describe("Extension Repository", () => {
|
|
15
|
+
const storage = new InMemoryStorage();
|
|
16
|
+
const config: Config = { storage, strict: false };
|
|
17
|
+
const repository = new ExtensionRepository(config);
|
|
18
|
+
|
|
19
|
+
test("create extension with HTTP destination", () => {
|
|
20
|
+
const draft: ExtensionDraft = {
|
|
21
|
+
key: "test-extension",
|
|
22
|
+
timeoutInMs: 2000,
|
|
23
|
+
destination: {
|
|
24
|
+
type: "HTTP",
|
|
25
|
+
url: "https://example.com/webhook",
|
|
26
|
+
authentication: {
|
|
27
|
+
type: "AuthorizationHeader",
|
|
28
|
+
headerValue: "Bearer secret-token",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
triggers: [
|
|
32
|
+
{
|
|
33
|
+
resourceTypeId: "cart",
|
|
34
|
+
actions: ["Create", "Update"],
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const ctx = { projectKey: "dummy" };
|
|
40
|
+
const result = repository.create(ctx, draft);
|
|
41
|
+
|
|
42
|
+
expect(result.id).toBeDefined();
|
|
43
|
+
expect(result.key).toBe(draft.key);
|
|
44
|
+
expect(result.timeoutInMs).toBe(draft.timeoutInMs);
|
|
45
|
+
expect(result.destination.type).toBe("HTTP");
|
|
46
|
+
expect(result.triggers).toEqual(draft.triggers);
|
|
47
|
+
|
|
48
|
+
// Test that the extension is stored
|
|
49
|
+
const items = repository.query(ctx);
|
|
50
|
+
expect(items.count).toBe(1);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("create extension with AWSLambda destination", () => {
|
|
54
|
+
const draft: ExtensionDraft = {
|
|
55
|
+
key: "aws-extension",
|
|
56
|
+
destination: {
|
|
57
|
+
type: "AWSLambda",
|
|
58
|
+
arn: "arn:aws:lambda:us-east-1:123456789012:function:MyFunction",
|
|
59
|
+
accessKey: "AKIAIOSFODNN7EXAMPLE",
|
|
60
|
+
accessSecret: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
|
61
|
+
},
|
|
62
|
+
triggers: [
|
|
63
|
+
{
|
|
64
|
+
resourceTypeId: "order",
|
|
65
|
+
actions: ["Create"],
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const ctx = { projectKey: "dummy" };
|
|
71
|
+
const result = repository.create(ctx, draft);
|
|
72
|
+
|
|
73
|
+
expect(result.id).toBeDefined();
|
|
74
|
+
expect(result.key).toBe(draft.key);
|
|
75
|
+
expect(result.destination.type).toBe("AWSLambda");
|
|
76
|
+
expect(result.triggers).toEqual(draft.triggers);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("postProcessResource masks HTTP authentication header", () => {
|
|
80
|
+
const extension: Extension = {
|
|
81
|
+
id: "test-id",
|
|
82
|
+
version: 1,
|
|
83
|
+
createdAt: "2023-01-01T00:00:00Z",
|
|
84
|
+
lastModifiedAt: "2023-01-01T00:00:00Z",
|
|
85
|
+
key: "test-extension",
|
|
86
|
+
destination: {
|
|
87
|
+
type: "HTTP",
|
|
88
|
+
url: "https://example.com/webhook",
|
|
89
|
+
authentication: {
|
|
90
|
+
type: "AuthorizationHeader",
|
|
91
|
+
headerValue: "Bearer secret-token",
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
triggers: [],
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const ctx = { projectKey: "dummy" };
|
|
98
|
+
const result = repository.postProcessResource(ctx, extension);
|
|
99
|
+
|
|
100
|
+
expect(result.destination.type).toBe("HTTP");
|
|
101
|
+
if (
|
|
102
|
+
result.destination.type === "HTTP" &&
|
|
103
|
+
result.destination.authentication?.type === "AuthorizationHeader"
|
|
104
|
+
) {
|
|
105
|
+
expect(result.destination.authentication.headerValue).toBe("****");
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("postProcessResource masks AWSLambda access secret", () => {
|
|
110
|
+
const extension: Extension = {
|
|
111
|
+
id: "test-id",
|
|
112
|
+
version: 1,
|
|
113
|
+
createdAt: "2023-01-01T00:00:00Z",
|
|
114
|
+
lastModifiedAt: "2023-01-01T00:00:00Z",
|
|
115
|
+
key: "aws-extension",
|
|
116
|
+
destination: {
|
|
117
|
+
type: "AWSLambda",
|
|
118
|
+
arn: "arn:aws:lambda:us-east-1:123456789012:function:MyFunction",
|
|
119
|
+
accessKey: "AKIAIOSFODNN7EXAMPLE",
|
|
120
|
+
accessSecret: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
|
121
|
+
},
|
|
122
|
+
triggers: [],
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const ctx = { projectKey: "dummy" };
|
|
126
|
+
const result = repository.postProcessResource(ctx, extension);
|
|
127
|
+
|
|
128
|
+
expect(result.destination.type).toBe("AWSLambda");
|
|
129
|
+
if (result.destination.type === "AWSLambda") {
|
|
130
|
+
expect(result.destination.accessSecret).toBe("****");
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("update extension - changeDestination", () => {
|
|
135
|
+
const draft: ExtensionDraft = {
|
|
136
|
+
key: "test-extension",
|
|
137
|
+
destination: {
|
|
138
|
+
type: "HTTP",
|
|
139
|
+
url: "https://example.com/webhook",
|
|
140
|
+
},
|
|
141
|
+
triggers: [
|
|
142
|
+
{
|
|
143
|
+
resourceTypeId: "cart",
|
|
144
|
+
actions: ["Create"],
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const ctx = { projectKey: "dummy" };
|
|
150
|
+
const extension = repository.create(ctx, draft);
|
|
151
|
+
|
|
152
|
+
const newDestination = {
|
|
153
|
+
type: "HTTP" as const,
|
|
154
|
+
url: "https://new-example.com/webhook",
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const result = repository.processUpdateActions(
|
|
158
|
+
ctx,
|
|
159
|
+
extension,
|
|
160
|
+
extension.version,
|
|
161
|
+
[
|
|
162
|
+
{
|
|
163
|
+
action: "changeDestination",
|
|
164
|
+
destination: newDestination,
|
|
165
|
+
} as ExtensionChangeDestinationAction,
|
|
166
|
+
],
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
expect((result.destination as any).url).toBe(
|
|
170
|
+
"https://new-example.com/webhook",
|
|
171
|
+
);
|
|
172
|
+
expect(result.version).toBe(extension.version + 1);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("update extension - changeTriggers", () => {
|
|
176
|
+
const draft: ExtensionDraft = {
|
|
177
|
+
key: "test-extension",
|
|
178
|
+
destination: {
|
|
179
|
+
type: "HTTP",
|
|
180
|
+
url: "https://example.com/webhook",
|
|
181
|
+
},
|
|
182
|
+
triggers: [
|
|
183
|
+
{
|
|
184
|
+
resourceTypeId: "cart",
|
|
185
|
+
actions: ["Create"],
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const ctx = { projectKey: "dummy" };
|
|
191
|
+
const extension = repository.create(ctx, draft);
|
|
192
|
+
|
|
193
|
+
const newTriggers = [
|
|
194
|
+
{
|
|
195
|
+
resourceTypeId: "order" as const,
|
|
196
|
+
actions: ["Create" as const, "Update" as const],
|
|
197
|
+
},
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
const result = repository.processUpdateActions(
|
|
201
|
+
ctx,
|
|
202
|
+
extension,
|
|
203
|
+
extension.version,
|
|
204
|
+
[
|
|
205
|
+
{
|
|
206
|
+
action: "changeTriggers",
|
|
207
|
+
triggers: newTriggers,
|
|
208
|
+
} as ExtensionChangeTriggersAction,
|
|
209
|
+
],
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
expect(result.triggers).toEqual(newTriggers);
|
|
213
|
+
expect(result.version).toBe(extension.version + 1);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("update extension - setKey", () => {
|
|
217
|
+
const draft: ExtensionDraft = {
|
|
218
|
+
key: "test-extension",
|
|
219
|
+
destination: {
|
|
220
|
+
type: "HTTP",
|
|
221
|
+
url: "https://example.com/webhook",
|
|
222
|
+
},
|
|
223
|
+
triggers: [],
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const ctx = { projectKey: "dummy" };
|
|
227
|
+
const extension = repository.create(ctx, draft);
|
|
228
|
+
|
|
229
|
+
const result = repository.processUpdateActions(
|
|
230
|
+
ctx,
|
|
231
|
+
extension,
|
|
232
|
+
extension.version,
|
|
233
|
+
[
|
|
234
|
+
{
|
|
235
|
+
action: "setKey",
|
|
236
|
+
key: "new-extension-key",
|
|
237
|
+
} as ExtensionSetKeyAction,
|
|
238
|
+
],
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
expect(result.key).toBe("new-extension-key");
|
|
242
|
+
expect(result.version).toBe(extension.version + 1);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test("update extension - setTimeoutInMs", () => {
|
|
246
|
+
const draft: ExtensionDraft = {
|
|
247
|
+
key: "test-extension",
|
|
248
|
+
destination: {
|
|
249
|
+
type: "HTTP",
|
|
250
|
+
url: "https://example.com/webhook",
|
|
251
|
+
},
|
|
252
|
+
triggers: [],
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const ctx = { projectKey: "dummy" };
|
|
256
|
+
const extension = repository.create(ctx, draft);
|
|
257
|
+
|
|
258
|
+
const result = repository.processUpdateActions(
|
|
259
|
+
ctx,
|
|
260
|
+
extension,
|
|
261
|
+
extension.version,
|
|
262
|
+
[
|
|
263
|
+
{
|
|
264
|
+
action: "setTimeoutInMs",
|
|
265
|
+
timeoutInMs: 5000,
|
|
266
|
+
} as ExtensionSetTimeoutInMsAction,
|
|
267
|
+
],
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
expect(result.timeoutInMs).toBe(5000);
|
|
271
|
+
expect(result.version).toBe(extension.version + 1);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test("get and delete extension", () => {
|
|
275
|
+
const draft: ExtensionDraft = {
|
|
276
|
+
key: "test-extension",
|
|
277
|
+
destination: {
|
|
278
|
+
type: "HTTP",
|
|
279
|
+
url: "https://example.com/webhook",
|
|
280
|
+
},
|
|
281
|
+
triggers: [],
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
const ctx = { projectKey: "dummy" };
|
|
285
|
+
const extension = repository.create(ctx, draft);
|
|
286
|
+
|
|
287
|
+
// Test get
|
|
288
|
+
const retrieved = repository.get(ctx, extension.id);
|
|
289
|
+
expect(retrieved).toBeDefined();
|
|
290
|
+
expect(retrieved?.id).toBe(extension.id);
|
|
291
|
+
|
|
292
|
+
// Test getByKey
|
|
293
|
+
const retrievedByKey = repository.getByKey(ctx, extension.key!);
|
|
294
|
+
expect(retrievedByKey).toBeDefined();
|
|
295
|
+
expect(retrievedByKey?.key).toBe(extension.key);
|
|
296
|
+
|
|
297
|
+
// Test delete
|
|
298
|
+
const deleted = repository.delete(ctx, extension.id);
|
|
299
|
+
expect(deleted).toBeDefined();
|
|
300
|
+
expect(deleted?.id).toBe(extension.id);
|
|
301
|
+
|
|
302
|
+
// Verify it's deleted
|
|
303
|
+
const notFound = repository.get(ctx, extension.id);
|
|
304
|
+
expect(notFound).toBeNull();
|
|
305
|
+
});
|
|
306
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import type { Config } from "~src/config";
|
|
3
|
+
import { InMemoryStorage } from "~src/storage";
|
|
4
|
+
import { createRepositories } from "./index";
|
|
5
|
+
|
|
6
|
+
describe("Repository Index", () => {
|
|
7
|
+
const storage = new InMemoryStorage();
|
|
8
|
+
const config: Config = { storage, strict: false };
|
|
9
|
+
|
|
10
|
+
test("createRepositories succeeds", () => {
|
|
11
|
+
const repositories = createRepositories(config);
|
|
12
|
+
|
|
13
|
+
// Test that createRepositories succeeded
|
|
14
|
+
expect(repositories).toBeDefined();
|
|
15
|
+
expect(typeof repositories).toBe("object");
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -13,8 +13,9 @@ import type {
|
|
|
13
13
|
import type { Config } from "~src/config";
|
|
14
14
|
import { CommercetoolsError } from "~src/exceptions";
|
|
15
15
|
import { getBaseResourceProperties } from "~src/helpers";
|
|
16
|
+
import { ReviewStatisticsService } from "~src/lib/review-statistics";
|
|
16
17
|
import { ProductSearch } from "~src/product-search";
|
|
17
|
-
import type { RepositoryContext } from "../abstract";
|
|
18
|
+
import type { GetParams, RepositoryContext } from "../abstract";
|
|
18
19
|
import { AbstractResourceRepository } from "../abstract";
|
|
19
20
|
import { getReferenceFromResourceIdentifier } from "../helpers";
|
|
20
21
|
import { ProductUpdateHandler } from "./actions";
|
|
@@ -22,11 +23,13 @@ import { variantFromDraft } from "./helpers";
|
|
|
22
23
|
|
|
23
24
|
export class ProductRepository extends AbstractResourceRepository<"product"> {
|
|
24
25
|
protected _searchService: ProductSearch;
|
|
26
|
+
protected _reviewStatisticsService: ReviewStatisticsService;
|
|
25
27
|
|
|
26
28
|
constructor(config: Config) {
|
|
27
29
|
super("product", config);
|
|
28
30
|
this.actions = new ProductUpdateHandler(config.storage);
|
|
29
31
|
this._searchService = new ProductSearch(config);
|
|
32
|
+
this._reviewStatisticsService = new ReviewStatisticsService(config.storage);
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
create(context: RepositoryContext, draft: ProductDraft): Product {
|
|
@@ -139,6 +142,24 @@ export class ProductRepository extends AbstractResourceRepository<"product"> {
|
|
|
139
142
|
return this.saveNew(context, resource);
|
|
140
143
|
}
|
|
141
144
|
|
|
145
|
+
postProcessResource(
|
|
146
|
+
context: RepositoryContext,
|
|
147
|
+
resource: Product,
|
|
148
|
+
params?: GetParams,
|
|
149
|
+
): Product {
|
|
150
|
+
// Add review statistics to the product
|
|
151
|
+
const reviewStatistics =
|
|
152
|
+
this._reviewStatisticsService.calculateProductReviewStatistics(
|
|
153
|
+
context.projectKey,
|
|
154
|
+
resource.id,
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
...resource,
|
|
159
|
+
reviewRatingStatistics: reviewStatistics,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
142
163
|
search(
|
|
143
164
|
context: RepositoryContext,
|
|
144
165
|
searchRequest: ProductSearchRequest,
|
|
@@ -53,7 +53,7 @@ export class ProductProjectionRepository extends AbstractResourceRepository<"pro
|
|
|
53
53
|
params,
|
|
54
54
|
);
|
|
55
55
|
if (resource) {
|
|
56
|
-
return this._searchService.transform(resource, false);
|
|
56
|
+
return this._searchService.transform(resource, false, context.projectKey);
|
|
57
57
|
}
|
|
58
58
|
return null;
|
|
59
59
|
}
|
|
@@ -61,7 +61,13 @@ export class ProductProjectionRepository extends AbstractResourceRepository<"pro
|
|
|
61
61
|
query(context: RepositoryContext, params: ProductProjectionQueryParams = {}) {
|
|
62
62
|
let resources = this._storage
|
|
63
63
|
.all(context.projectKey, "product")
|
|
64
|
-
.map((r) =>
|
|
64
|
+
.map((r) =>
|
|
65
|
+
this._searchService.transform(
|
|
66
|
+
r,
|
|
67
|
+
params.staged ?? false,
|
|
68
|
+
context.projectKey,
|
|
69
|
+
),
|
|
70
|
+
)
|
|
65
71
|
.filter((p) => {
|
|
66
72
|
if (!(params.staged ?? false)) {
|
|
67
73
|
return p.published;
|