@labdigital/commercetools-mock 2.41.1 → 2.42.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 +1726 -1616
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +82 -74
- package/dist/index.d.ts +82 -74
- package/dist/index.js +1711 -1601
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/ctMock.ts +1 -5
- package/src/repositories/as-associate.ts +5 -0
- package/src/repositories/associate-role.ts +17 -0
- package/src/repositories/business-unit.ts +21 -3
- package/src/repositories/index.ts +8 -0
- package/src/repositories/product-selection.ts +18 -0
- package/src/services/as-associate-cart.ts +33 -0
- package/src/services/as-associate-order.test.ts +64 -0
- package/src/services/as-associate-order.ts +33 -0
- package/src/services/as-associate.ts +34 -0
- package/src/services/index.ts +2 -0
- package/src/types.ts +0 -5
package/package.json
CHANGED
package/src/ctMock.ts
CHANGED
|
@@ -12,7 +12,6 @@ import { OAuth2Server } from "./oauth/server";
|
|
|
12
12
|
import { ProjectAPI } from "./projectAPI";
|
|
13
13
|
import type { AbstractStorage } from "./storage";
|
|
14
14
|
import { InMemoryStorage } from "./storage";
|
|
15
|
-
import type { Services } from "./types";
|
|
16
15
|
|
|
17
16
|
// Services
|
|
18
17
|
import { warnDeprecation } from "./deprecation";
|
|
@@ -56,15 +55,12 @@ export class CommercetoolsMock {
|
|
|
56
55
|
|
|
57
56
|
private _mswServer: SetupServer | undefined = undefined;
|
|
58
57
|
|
|
59
|
-
private _services: Services | null;
|
|
60
|
-
|
|
61
58
|
private _repositories: RepositoryMap | null;
|
|
62
59
|
|
|
63
60
|
private _projectService?: ProjectService;
|
|
64
61
|
|
|
65
62
|
constructor(options: Partial<CommercetoolsMockOptions> = {}) {
|
|
66
63
|
this.options = { ...DEFAULT_OPTIONS, ...options };
|
|
67
|
-
this._services = null;
|
|
68
64
|
this._repositories = null;
|
|
69
65
|
this._projectService = undefined;
|
|
70
66
|
|
|
@@ -154,7 +150,7 @@ export class CommercetoolsMock {
|
|
|
154
150
|
}
|
|
155
151
|
|
|
156
152
|
// Register the rest api services in the router
|
|
157
|
-
|
|
153
|
+
createServices(projectRouter, this._repositories);
|
|
158
154
|
this._projectService = new ProjectService(
|
|
159
155
|
projectRouter,
|
|
160
156
|
this._repositories.project as ProjectRepository,
|
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
AssociateRoleDraft,
|
|
6
6
|
AssociateRoleRemovePermissionAction,
|
|
7
7
|
AssociateRoleSetCustomFieldAction,
|
|
8
|
+
AssociateRoleSetCustomTypeAction,
|
|
8
9
|
AssociateRoleSetNameAction,
|
|
9
10
|
AssociateRoleSetPermissionsAction,
|
|
10
11
|
AssociateRoleUpdateAction,
|
|
@@ -107,6 +108,22 @@ class AssociateRoleUpdateHandler
|
|
|
107
108
|
}
|
|
108
109
|
}
|
|
109
110
|
|
|
111
|
+
setCustomType(
|
|
112
|
+
context: RepositoryContext,
|
|
113
|
+
resource: Writable<AssociateRole>,
|
|
114
|
+
{ type, fields }: AssociateRoleSetCustomTypeAction,
|
|
115
|
+
) {
|
|
116
|
+
if (type) {
|
|
117
|
+
resource.custom = createCustomFields(
|
|
118
|
+
{ type, fields },
|
|
119
|
+
context.projectKey,
|
|
120
|
+
this._storage,
|
|
121
|
+
);
|
|
122
|
+
} else {
|
|
123
|
+
resource.custom = undefined;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
110
127
|
setName(
|
|
111
128
|
context: RepositoryContext,
|
|
112
129
|
resource: Writable<AssociateRole>,
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
BusinessUnitChangeApprovalRuleModeAction,
|
|
3
3
|
BusinessUnitChangeAssociateModeAction,
|
|
4
4
|
BusinessUnitChangeStatusAction,
|
|
5
|
+
BusinessUnitSetCustomTypeAction,
|
|
5
6
|
BusinessUnitUpdateAction,
|
|
6
7
|
CompanyDraft,
|
|
7
8
|
DivisionDraft,
|
|
@@ -91,9 +92,10 @@ export class BusinessUnitRepository extends AbstractResourceRepository<"business
|
|
|
91
92
|
associateMode: draft.associateMode,
|
|
92
93
|
approvalRuleMode: draft.approvalRuleMode,
|
|
93
94
|
|
|
94
|
-
associates:
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
associates:
|
|
96
|
+
draft.associates?.map((a) =>
|
|
97
|
+
createAssociate(a, context.projectKey, this._storage),
|
|
98
|
+
) ?? [],
|
|
97
99
|
};
|
|
98
100
|
|
|
99
101
|
if (this._isDivisionDraft(draft)) {
|
|
@@ -259,6 +261,22 @@ class BusinessUnitUpdateHandler
|
|
|
259
261
|
resource.contactEmail = contactEmail;
|
|
260
262
|
}
|
|
261
263
|
|
|
264
|
+
setCustomType(
|
|
265
|
+
context: RepositoryContext,
|
|
266
|
+
resource: Writable<BusinessUnit>,
|
|
267
|
+
{ type, fields }: BusinessUnitSetCustomTypeAction,
|
|
268
|
+
) {
|
|
269
|
+
if (type) {
|
|
270
|
+
resource.custom = createCustomFields(
|
|
271
|
+
{ type, fields },
|
|
272
|
+
context.projectKey,
|
|
273
|
+
this._storage,
|
|
274
|
+
);
|
|
275
|
+
} else {
|
|
276
|
+
resource.custom = undefined;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
262
280
|
setStoreMode(
|
|
263
281
|
context: RepositoryContext,
|
|
264
282
|
resource: Writable<BusinessUnit>,
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { ProductTailoringRepository } from "~src/repositories/product-tailoring";
|
|
2
2
|
import type { AbstractStorage } from "../storage";
|
|
3
|
+
import {
|
|
4
|
+
AsAssociateCartRepository,
|
|
5
|
+
AsAssociateOrderRepository,
|
|
6
|
+
} from "./as-associate";
|
|
3
7
|
import { AssociateRoleRepository } from "./associate-role";
|
|
4
8
|
import { AttributeGroupRepository } from "./attribute-group";
|
|
5
9
|
import { BusinessUnitRepository } from "./business-unit";
|
|
@@ -41,6 +45,10 @@ import { ZoneRepository } from "./zone";
|
|
|
41
45
|
export type RepositoryMap = ReturnType<typeof createRepositories>;
|
|
42
46
|
|
|
43
47
|
export const createRepositories = (storage: AbstractStorage) => ({
|
|
48
|
+
"as-associate": {
|
|
49
|
+
cart: new AsAssociateCartRepository(storage),
|
|
50
|
+
order: new AsAssociateOrderRepository(storage),
|
|
51
|
+
},
|
|
44
52
|
"associate-role": new AssociateRoleRepository(storage),
|
|
45
53
|
"attribute-group": new AttributeGroupRepository(storage),
|
|
46
54
|
"business-unit": new BusinessUnitRepository(storage),
|
|
@@ -2,8 +2,10 @@ import type {
|
|
|
2
2
|
ProductSelection,
|
|
3
3
|
ProductSelectionChangeNameAction,
|
|
4
4
|
ProductSelectionDraft,
|
|
5
|
+
ProductSelectionSetCustomTypeAction,
|
|
5
6
|
ProductSelectionUpdateAction,
|
|
6
7
|
} from "@commercetools/platform-sdk";
|
|
8
|
+
import { createCustomFields } from "~src/repositories/helpers";
|
|
7
9
|
import { getBaseResourceProperties } from "../helpers";
|
|
8
10
|
import type { AbstractStorage } from "../storage/abstract";
|
|
9
11
|
import type { Writable } from "../types";
|
|
@@ -45,4 +47,20 @@ class ProductSelectionUpdateHandler
|
|
|
45
47
|
) {
|
|
46
48
|
resource.name = name;
|
|
47
49
|
}
|
|
50
|
+
|
|
51
|
+
setCustomType(
|
|
52
|
+
context: RepositoryContext,
|
|
53
|
+
resource: Writable<ProductSelection>,
|
|
54
|
+
{ type, fields }: ProductSelectionSetCustomTypeAction,
|
|
55
|
+
) {
|
|
56
|
+
if (type) {
|
|
57
|
+
resource.custom = createCustomFields(
|
|
58
|
+
{ type, fields },
|
|
59
|
+
context.projectKey,
|
|
60
|
+
this._storage,
|
|
61
|
+
);
|
|
62
|
+
} else {
|
|
63
|
+
resource.custom = undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
48
66
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
import type { CartRepository } from "../repositories/cart";
|
|
3
|
+
import AbstractService from "./abstract";
|
|
4
|
+
|
|
5
|
+
export class AsAssociateCartService extends AbstractService {
|
|
6
|
+
public repository: CartRepository;
|
|
7
|
+
|
|
8
|
+
constructor(parent: Router, repository: CartRepository) {
|
|
9
|
+
super(parent);
|
|
10
|
+
this.repository = repository;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getBasePath() {
|
|
14
|
+
return "carts";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
registerRoutes(parent: Router) {
|
|
18
|
+
const basePath = this.getBasePath();
|
|
19
|
+
const router = Router({ mergeParams: true });
|
|
20
|
+
|
|
21
|
+
this.extraRoutes(router);
|
|
22
|
+
|
|
23
|
+
router.get("/", this.get.bind(this));
|
|
24
|
+
router.get("/:id", this.getWithId.bind(this));
|
|
25
|
+
|
|
26
|
+
router.delete("/:id", this.deleteWithId.bind(this));
|
|
27
|
+
|
|
28
|
+
router.post("/", this.post.bind(this));
|
|
29
|
+
router.post("/:id", this.postWithId.bind(this));
|
|
30
|
+
|
|
31
|
+
parent.use(`/${basePath}`, router);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Order } from "@commercetools/platform-sdk";
|
|
2
|
+
import assert from "assert";
|
|
3
|
+
import supertest from "supertest";
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, test } from "vitest";
|
|
5
|
+
import { CommercetoolsMock } from "../index";
|
|
6
|
+
|
|
7
|
+
describe("Order Query", () => {
|
|
8
|
+
const ctMock = new CommercetoolsMock();
|
|
9
|
+
let order: Order | undefined;
|
|
10
|
+
const projectKey = "dummy";
|
|
11
|
+
const customerId = "5fac8fca-2484-4b14-a1d1-cfdce2f8d3c4";
|
|
12
|
+
const businessUnitKey = "business-unit";
|
|
13
|
+
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
let response = await supertest(ctMock.app)
|
|
16
|
+
.post(
|
|
17
|
+
`/${projectKey}/as-associate/${customerId}/in-business-unit/key=${businessUnitKey}/carts`,
|
|
18
|
+
)
|
|
19
|
+
.send({
|
|
20
|
+
currency: "EUR",
|
|
21
|
+
custom: {
|
|
22
|
+
type: {
|
|
23
|
+
key: "my-cart",
|
|
24
|
+
},
|
|
25
|
+
fields: {
|
|
26
|
+
description: "example description",
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
expect(response.status).toBe(201);
|
|
31
|
+
const cart = response.body;
|
|
32
|
+
|
|
33
|
+
response = await supertest(ctMock.app)
|
|
34
|
+
.post(
|
|
35
|
+
`/${projectKey}/as-associate/${customerId}/in-business-unit/key=${businessUnitKey}/orders`,
|
|
36
|
+
)
|
|
37
|
+
.send({
|
|
38
|
+
cart: {
|
|
39
|
+
typeId: "cart",
|
|
40
|
+
id: cart.id,
|
|
41
|
+
},
|
|
42
|
+
orderNumber: "foobar",
|
|
43
|
+
});
|
|
44
|
+
expect(response.status).toBe(201);
|
|
45
|
+
order = response.body;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
afterEach(() => {
|
|
49
|
+
ctMock.clear();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("no filter", async () => {
|
|
53
|
+
assert(order, "order not created");
|
|
54
|
+
|
|
55
|
+
const response = await supertest(ctMock.app).get(
|
|
56
|
+
`/${projectKey}/as-associate/${customerId}/in-business-unit/key=${businessUnitKey}/orders`,
|
|
57
|
+
);
|
|
58
|
+
expect(response.status).toBe(200);
|
|
59
|
+
expect(response.body.count).toBe(1);
|
|
60
|
+
expect(response.body.total).toBe(1);
|
|
61
|
+
expect(response.body.offset).toBe(0);
|
|
62
|
+
expect(response.body.limit).toBe(20);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
import type { MyOrderRepository } from "../repositories/my-order";
|
|
3
|
+
import AbstractService from "./abstract";
|
|
4
|
+
|
|
5
|
+
export class AsAssociateOrderService extends AbstractService {
|
|
6
|
+
public repository: MyOrderRepository;
|
|
7
|
+
|
|
8
|
+
constructor(parent: Router, repository: MyOrderRepository) {
|
|
9
|
+
super(parent);
|
|
10
|
+
this.repository = repository;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getBasePath() {
|
|
14
|
+
return "orders";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
registerRoutes(parent: Router) {
|
|
18
|
+
const basePath = this.getBasePath();
|
|
19
|
+
const router = Router({ mergeParams: true });
|
|
20
|
+
|
|
21
|
+
this.extraRoutes(router);
|
|
22
|
+
|
|
23
|
+
router.get("/", this.get.bind(this));
|
|
24
|
+
router.get("/:id", this.getWithId.bind(this));
|
|
25
|
+
|
|
26
|
+
router.delete("/:id", this.deleteWithId.bind(this));
|
|
27
|
+
|
|
28
|
+
router.post("/", this.post.bind(this));
|
|
29
|
+
router.post("/:id", this.postWithId.bind(this));
|
|
30
|
+
|
|
31
|
+
parent.use(`/${basePath}`, router);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
import type {
|
|
3
|
+
AsAssociateCartRepository,
|
|
4
|
+
AsAssociateOrderRepository,
|
|
5
|
+
} from "~src/repositories/as-associate";
|
|
6
|
+
import { AsAssociateCartService } from "./as-associate-cart";
|
|
7
|
+
import { AsAssociateOrderService } from "./as-associate-order";
|
|
8
|
+
|
|
9
|
+
type Repositories = {
|
|
10
|
+
cart: AsAssociateCartRepository;
|
|
11
|
+
order: AsAssociateOrderRepository;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export class AsAssociateService {
|
|
15
|
+
router: Router;
|
|
16
|
+
|
|
17
|
+
subServices: {
|
|
18
|
+
cart: AsAssociateCartService;
|
|
19
|
+
order: AsAssociateOrderService;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
constructor(parent: Router, repositories: Repositories) {
|
|
23
|
+
this.router = Router({ mergeParams: true });
|
|
24
|
+
|
|
25
|
+
this.subServices = {
|
|
26
|
+
order: new AsAssociateOrderService(this.router, repositories.order),
|
|
27
|
+
cart: new AsAssociateCartService(this.router, repositories.cart),
|
|
28
|
+
};
|
|
29
|
+
parent.use(
|
|
30
|
+
"/as-associate/:associateId/in-business-unit/key=:businessUnitId",
|
|
31
|
+
this.router,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/services/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { createRepositories } from "../repositories";
|
|
2
|
+
import { AsAssociateService } from "./as-associate";
|
|
2
3
|
import { AssociateRoleServices } from "./associate-roles";
|
|
3
4
|
import { AttributeGroupService } from "./attribute-group";
|
|
4
5
|
import { BusinessUnitServices } from "./business-units";
|
|
@@ -41,6 +42,7 @@ export const createServices = (
|
|
|
41
42
|
repos: ReturnType<typeof createRepositories>,
|
|
42
43
|
) => ({
|
|
43
44
|
"associate-role": new AssociateRoleServices(router, repos["associate-role"]),
|
|
45
|
+
"as-associate": new AsAssociateService(router, repos["as-associate"]),
|
|
44
46
|
"business-unit": new BusinessUnitServices(router, repos["business-unit"]),
|
|
45
47
|
"category": new CategoryServices(router, repos["category"]),
|
|
46
48
|
"cart": new CartService(router, repos["cart"], repos["order"]),
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type * as ctp from "@commercetools/platform-sdk";
|
|
2
2
|
import type { RepositoryMap } from "./repositories";
|
|
3
|
-
import type AbstractService from "./services/abstract";
|
|
4
3
|
|
|
5
4
|
export const isType = <T>(x: T) => x;
|
|
6
5
|
|
|
@@ -17,10 +16,6 @@ export type ServiceTypes =
|
|
|
17
16
|
| "my-customer"
|
|
18
17
|
| "my-business-unit";
|
|
19
18
|
|
|
20
|
-
export type Services = Partial<{
|
|
21
|
-
[index in ServiceTypes]: AbstractService;
|
|
22
|
-
}>;
|
|
23
|
-
|
|
24
19
|
export type ResourceType = keyof ResourceMap & keyof RepositoryMap;
|
|
25
20
|
|
|
26
21
|
export type ResourceMap = {
|