@labdigital/commercetools-mock 2.19.0 → 2.20.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 +32 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -5
- package/dist/index.d.ts +8 -5
- package/dist/index.js +32 -11
- package/dist/index.js.map +1 -1
- package/package.json +4 -5
- package/src/repositories/abstract.ts +16 -9
- package/src/repositories/category/index.test.ts +82 -0
- package/src/repositories/category/index.ts +26 -2
- package/src/repositories/extension.ts +4 -1
- package/src/repositories/order/actions.ts +9 -0
- package/src/repositories/project.ts +2 -2
- package/src/services/category.test.ts +2 -0
- package/src/services/order.test.ts +35 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@labdigital/commercetools-mock",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.20.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Michael van Tellingen",
|
|
6
6
|
"type": "module",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"@types/uuid": "^9.0.8",
|
|
50
50
|
"@typescript-eslint/eslint-plugin": "^7.0.2",
|
|
51
51
|
"@typescript-eslint/parser": "^7.0.2",
|
|
52
|
-
"@vitest/coverage-v8": "^1.
|
|
52
|
+
"@vitest/coverage-v8": "^1.5.0",
|
|
53
53
|
"esbuild": "^0.20.1",
|
|
54
54
|
"eslint": "^8.57.0",
|
|
55
55
|
"eslint-plugin-sort-class-members": "^1.20.0",
|
|
@@ -65,12 +65,11 @@
|
|
|
65
65
|
"tslib": "^2.6.2",
|
|
66
66
|
"tsup": "^8.0.2",
|
|
67
67
|
"typescript": "^5.3.3",
|
|
68
|
-
"vitest": "^1.
|
|
68
|
+
"vitest": "^1.5.0"
|
|
69
69
|
},
|
|
70
|
-
"packageManager": "pnpm@8.6.5",
|
|
71
70
|
"engines": {
|
|
72
71
|
"node": ">=18",
|
|
73
|
-
"pnpm": ">=
|
|
72
|
+
"pnpm": ">=9.0.2"
|
|
74
73
|
},
|
|
75
74
|
"publishConfig": {
|
|
76
75
|
"access": "public"
|
|
@@ -53,7 +53,7 @@ export abstract class AbstractRepository<R extends BaseResource | Project> {
|
|
|
53
53
|
resource: R,
|
|
54
54
|
): void;
|
|
55
55
|
|
|
56
|
-
abstract postProcessResource(resource: any): any;
|
|
56
|
+
abstract postProcessResource(context: RepositoryContext, resource: any): any;
|
|
57
57
|
|
|
58
58
|
processUpdateActions(
|
|
59
59
|
context: RepositoryContext,
|
|
@@ -78,7 +78,7 @@ export abstract class AbstractRepository<R extends BaseResource | Project> {
|
|
|
78
78
|
this.saveUpdate(context, version, updatedResource);
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
const result = this.postProcessResource(updatedResource);
|
|
81
|
+
const result = this.postProcessResource(context, updatedResource);
|
|
82
82
|
if (!result) {
|
|
83
83
|
throw new Error("invalid post process action");
|
|
84
84
|
}
|
|
@@ -113,7 +113,7 @@ export abstract class AbstractResourceRepository<
|
|
|
113
113
|
id,
|
|
114
114
|
params,
|
|
115
115
|
);
|
|
116
|
-
return resource ? this.postProcessResource(resource) : null;
|
|
116
|
+
return resource ? this.postProcessResource(context, resource) : null;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
get(
|
|
@@ -127,7 +127,7 @@ export abstract class AbstractResourceRepository<
|
|
|
127
127
|
id,
|
|
128
128
|
params,
|
|
129
129
|
);
|
|
130
|
-
return resource ? this.postProcessResource(resource) : null;
|
|
130
|
+
return resource ? this.postProcessResource(context, resource) : null;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
getByKey(
|
|
@@ -141,10 +141,13 @@ export abstract class AbstractResourceRepository<
|
|
|
141
141
|
key,
|
|
142
142
|
params,
|
|
143
143
|
);
|
|
144
|
-
return resource ? this.postProcessResource(resource) : null;
|
|
144
|
+
return resource ? this.postProcessResource(context, resource) : null;
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
postProcessResource(
|
|
147
|
+
postProcessResource(
|
|
148
|
+
context: RepositoryContext,
|
|
149
|
+
resource: ResourceMap[T],
|
|
150
|
+
): ResourceMap[T] {
|
|
148
151
|
return resource;
|
|
149
152
|
}
|
|
150
153
|
|
|
@@ -153,9 +156,13 @@ export abstract class AbstractResourceRepository<
|
|
|
153
156
|
...params,
|
|
154
157
|
});
|
|
155
158
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
+
const data = result.results.map((r) =>
|
|
160
|
+
this.postProcessResource(context, r as ResourceMap[T]),
|
|
161
|
+
);
|
|
162
|
+
return {
|
|
163
|
+
...result,
|
|
164
|
+
results: data,
|
|
165
|
+
};
|
|
159
166
|
}
|
|
160
167
|
|
|
161
168
|
saveNew(
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { InMemoryStorage } from "~src/storage";
|
|
3
|
+
import { CategoryRepository } from "./index";
|
|
4
|
+
|
|
5
|
+
describe("Order repository", () => {
|
|
6
|
+
const storage = new InMemoryStorage();
|
|
7
|
+
const repository = new CategoryRepository(storage);
|
|
8
|
+
|
|
9
|
+
test("valid ancestors", async () => {
|
|
10
|
+
const root = repository.create(
|
|
11
|
+
{ projectKey: "dummy" },
|
|
12
|
+
{
|
|
13
|
+
key: "root",
|
|
14
|
+
slug: {
|
|
15
|
+
en: "root",
|
|
16
|
+
},
|
|
17
|
+
name: {
|
|
18
|
+
en: "root",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const level1 = repository.create(
|
|
24
|
+
{ projectKey: "dummy" },
|
|
25
|
+
{
|
|
26
|
+
key: "level-1",
|
|
27
|
+
slug: {
|
|
28
|
+
en: "level-1",
|
|
29
|
+
},
|
|
30
|
+
name: {
|
|
31
|
+
en: "level-1",
|
|
32
|
+
},
|
|
33
|
+
parent: {
|
|
34
|
+
id: root.id,
|
|
35
|
+
typeId: "category",
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const level2 = repository.create(
|
|
41
|
+
{ projectKey: "dummy" },
|
|
42
|
+
{
|
|
43
|
+
key: "level-2",
|
|
44
|
+
slug: {
|
|
45
|
+
en: "level-2",
|
|
46
|
+
},
|
|
47
|
+
name: {
|
|
48
|
+
en: "level-2",
|
|
49
|
+
},
|
|
50
|
+
parent: {
|
|
51
|
+
id: level1.id,
|
|
52
|
+
typeId: "category",
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const level3 = repository.create(
|
|
58
|
+
{ projectKey: "dummy" },
|
|
59
|
+
{
|
|
60
|
+
key: "level-3",
|
|
61
|
+
slug: {
|
|
62
|
+
en: "level-3",
|
|
63
|
+
},
|
|
64
|
+
name: {
|
|
65
|
+
en: "level-3",
|
|
66
|
+
},
|
|
67
|
+
parent: {
|
|
68
|
+
id: level2.id,
|
|
69
|
+
typeId: "category",
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const result = repository.get({ projectKey: "dummy" }, level3.id);
|
|
75
|
+
expect(result?.ancestors).toHaveLength(3);
|
|
76
|
+
expect(result?.ancestors).toEqual([
|
|
77
|
+
{ id: level2.id, typeId: "category" },
|
|
78
|
+
{ id: level1.id, typeId: "category" },
|
|
79
|
+
{ id: root.id, typeId: "category" },
|
|
80
|
+
]);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
Category,
|
|
3
|
+
CategoryDraft,
|
|
4
|
+
CategoryReference,
|
|
5
|
+
} from "@commercetools/platform-sdk";
|
|
2
6
|
import { v4 as uuidv4 } from "uuid";
|
|
3
7
|
import { getBaseResourceProperties } from "~src/helpers";
|
|
4
8
|
import { AbstractStorage } from "~src/storage/abstract";
|
|
9
|
+
import { Writable } from "~src/types";
|
|
5
10
|
import {
|
|
6
11
|
AbstractResourceRepository,
|
|
7
12
|
type RepositoryContext,
|
|
@@ -26,7 +31,7 @@ export class CategoryRepository extends AbstractResourceRepository<"category"> {
|
|
|
26
31
|
parent: draft.parent
|
|
27
32
|
? { typeId: "category", id: draft.parent.id! }
|
|
28
33
|
: undefined,
|
|
29
|
-
ancestors: [], //
|
|
34
|
+
ancestors: [], // Resolved at runtime
|
|
30
35
|
assets:
|
|
31
36
|
draft.assets?.map((d) => ({
|
|
32
37
|
id: uuidv4(),
|
|
@@ -49,4 +54,23 @@ export class CategoryRepository extends AbstractResourceRepository<"category"> {
|
|
|
49
54
|
};
|
|
50
55
|
return this.saveNew(context, resource);
|
|
51
56
|
}
|
|
57
|
+
|
|
58
|
+
postProcessResource(
|
|
59
|
+
context: RepositoryContext,
|
|
60
|
+
resource: Writable<Category>,
|
|
61
|
+
): Category {
|
|
62
|
+
let node: Category = resource;
|
|
63
|
+
const ancestors: CategoryReference[] = [];
|
|
64
|
+
|
|
65
|
+
while (node.parent) {
|
|
66
|
+
node = this._storage.getByResourceIdentifier<"category">(
|
|
67
|
+
context.projectKey,
|
|
68
|
+
node.parent,
|
|
69
|
+
);
|
|
70
|
+
ancestors.push({ typeId: "category", id: node.id });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
resource.ancestors = ancestors;
|
|
74
|
+
return resource;
|
|
75
|
+
}
|
|
52
76
|
}
|
|
@@ -35,7 +35,10 @@ export class ExtensionRepository extends AbstractResourceRepository<"extension">
|
|
|
35
35
|
return this.saveNew(context, resource);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
postProcessResource(
|
|
38
|
+
postProcessResource(
|
|
39
|
+
context: RepositoryContext,
|
|
40
|
+
resource: Extension,
|
|
41
|
+
): Extension {
|
|
39
42
|
if (resource) {
|
|
40
43
|
const extension = resource as Extension;
|
|
41
44
|
if (
|
|
@@ -10,6 +10,7 @@ import type {
|
|
|
10
10
|
OrderSetCustomFieldAction,
|
|
11
11
|
OrderSetCustomTypeAction,
|
|
12
12
|
OrderSetCustomerEmailAction,
|
|
13
|
+
OrderSetCustomerIdAction,
|
|
13
14
|
OrderSetLocaleAction,
|
|
14
15
|
OrderSetOrderNumberAction,
|
|
15
16
|
OrderSetShippingAddressAction,
|
|
@@ -134,6 +135,14 @@ export class OrderUpdateHandler
|
|
|
134
135
|
resource.customerEmail = email;
|
|
135
136
|
}
|
|
136
137
|
|
|
138
|
+
setCustomerId(
|
|
139
|
+
context: RepositoryContext,
|
|
140
|
+
resource: Writable<Order>,
|
|
141
|
+
{ customerId }: OrderSetCustomerIdAction,
|
|
142
|
+
) {
|
|
143
|
+
resource.customerId = customerId;
|
|
144
|
+
}
|
|
145
|
+
|
|
137
146
|
setCustomField(
|
|
138
147
|
context: RepositoryContext,
|
|
139
148
|
resource: Order,
|
|
@@ -31,10 +31,10 @@ export class ProjectRepository extends AbstractRepository<Project> {
|
|
|
31
31
|
|
|
32
32
|
get(context: RepositoryContext): Project | null {
|
|
33
33
|
const resource = this._storage.getProject(context.projectKey);
|
|
34
|
-
return this.postProcessResource(resource);
|
|
34
|
+
return this.postProcessResource(context, resource);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
postProcessResource(resource: Project): Project {
|
|
37
|
+
postProcessResource(context: RepositoryContext, resource: Project): Project {
|
|
38
38
|
if (resource) {
|
|
39
39
|
return maskSecretValue(resource, "externalOAuth.authorizationHeader");
|
|
40
40
|
}
|
|
@@ -144,6 +144,8 @@ describe("categories changeParent", () => {
|
|
|
144
144
|
typeId: "category",
|
|
145
145
|
id: category1?.id,
|
|
146
146
|
});
|
|
147
|
+
expect(changeNameResponse.body.ancestors).toHaveLength(1);
|
|
148
|
+
expect(changeNameResponse.body.ancestors[0].id).toEqual(category1?.id);
|
|
147
149
|
});
|
|
148
150
|
});
|
|
149
151
|
|
|
@@ -321,6 +321,41 @@ describe("Order Update Actions", () => {
|
|
|
321
321
|
expect(responseAgain.body.locale).toBe("nl-NL");
|
|
322
322
|
});
|
|
323
323
|
|
|
324
|
+
test("setCustomerEmail", async () => {
|
|
325
|
+
assert(order, "order not created");
|
|
326
|
+
|
|
327
|
+
const response = await supertest(ctMock.app)
|
|
328
|
+
.post(`/dummy/orders/${order.id}`)
|
|
329
|
+
.send({
|
|
330
|
+
version: 1,
|
|
331
|
+
actions: [{ action: "setCustomerEmail", email: "john@doe.com" }],
|
|
332
|
+
});
|
|
333
|
+
expect(response.status).toBe(200);
|
|
334
|
+
expect(response.body.version).toBe(2);
|
|
335
|
+
expect(response.body.customerEmail).toBe("john@doe.com");
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
test("setCustomerId", async () => {
|
|
339
|
+
assert(order, "order not created");
|
|
340
|
+
|
|
341
|
+
const response = await supertest(ctMock.app)
|
|
342
|
+
.post(`/dummy/orders/${order.id}`)
|
|
343
|
+
.send({
|
|
344
|
+
version: 1,
|
|
345
|
+
actions: [
|
|
346
|
+
{
|
|
347
|
+
action: "setCustomerId",
|
|
348
|
+
customerId: "9e3479fc-cc92-4d10-820a-a080b45ddcc1",
|
|
349
|
+
},
|
|
350
|
+
],
|
|
351
|
+
});
|
|
352
|
+
expect(response.status).toBe(200);
|
|
353
|
+
expect(response.body.version).toBe(2);
|
|
354
|
+
expect(response.body.customerId).toBe(
|
|
355
|
+
"9e3479fc-cc92-4d10-820a-a080b45ddcc1",
|
|
356
|
+
);
|
|
357
|
+
});
|
|
358
|
+
|
|
324
359
|
test("setOrderNumber", async () => {
|
|
325
360
|
assert(order, "order not created");
|
|
326
361
|
|