@labdigital/commercetools-mock 2.20.1 → 2.21.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.cjs +49 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +49 -9
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/helpers.ts +11 -0
- package/src/repositories/abstract.ts +13 -4
- package/src/repositories/category/index.test.ts +24 -0
- package/src/repositories/category/index.ts +18 -1
- package/src/repositories/customer/index.ts +33 -2
package/package.json
CHANGED
package/src/helpers.ts
CHANGED
|
@@ -77,3 +77,14 @@ export const mapHeaderType = (
|
|
|
77
77
|
}
|
|
78
78
|
return headersInit;
|
|
79
79
|
};
|
|
80
|
+
|
|
81
|
+
export const generateRandomString = (length: number) => {
|
|
82
|
+
const characters =
|
|
83
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
84
|
+
let result = "";
|
|
85
|
+
for (let i = 0; i < length; i++) {
|
|
86
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
87
|
+
result += characters[randomIndex];
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
};
|
|
@@ -113,7 +113,9 @@ export abstract class AbstractResourceRepository<
|
|
|
113
113
|
id,
|
|
114
114
|
params,
|
|
115
115
|
);
|
|
116
|
-
return resource
|
|
116
|
+
return resource
|
|
117
|
+
? this.postProcessResource(context, resource, params)
|
|
118
|
+
: null;
|
|
117
119
|
}
|
|
118
120
|
|
|
119
121
|
get(
|
|
@@ -127,7 +129,9 @@ export abstract class AbstractResourceRepository<
|
|
|
127
129
|
id,
|
|
128
130
|
params,
|
|
129
131
|
);
|
|
130
|
-
return resource
|
|
132
|
+
return resource
|
|
133
|
+
? this.postProcessResource(context, resource, params)
|
|
134
|
+
: null;
|
|
131
135
|
}
|
|
132
136
|
|
|
133
137
|
getByKey(
|
|
@@ -141,12 +145,15 @@ export abstract class AbstractResourceRepository<
|
|
|
141
145
|
key,
|
|
142
146
|
params,
|
|
143
147
|
);
|
|
144
|
-
return resource
|
|
148
|
+
return resource
|
|
149
|
+
? this.postProcessResource(context, resource, params)
|
|
150
|
+
: null;
|
|
145
151
|
}
|
|
146
152
|
|
|
147
153
|
postProcessResource(
|
|
148
154
|
context: RepositoryContext,
|
|
149
155
|
resource: ResourceMap[T],
|
|
156
|
+
params?: GetParams,
|
|
150
157
|
): ResourceMap[T] {
|
|
151
158
|
return resource;
|
|
152
159
|
}
|
|
@@ -157,7 +164,9 @@ export abstract class AbstractResourceRepository<
|
|
|
157
164
|
});
|
|
158
165
|
|
|
159
166
|
const data = result.results.map((r) =>
|
|
160
|
-
this.postProcessResource(context, r as ResourceMap[T]
|
|
167
|
+
this.postProcessResource(context, r as ResourceMap[T], {
|
|
168
|
+
expand: params.expand,
|
|
169
|
+
}),
|
|
161
170
|
);
|
|
162
171
|
return {
|
|
163
172
|
...result,
|
|
@@ -78,5 +78,29 @@ describe("Order repository", () => {
|
|
|
78
78
|
{ id: level1.id, typeId: "category" },
|
|
79
79
|
{ id: root.id, typeId: "category" },
|
|
80
80
|
]);
|
|
81
|
+
|
|
82
|
+
const expandResult = repository.get({ projectKey: "dummy" }, level3.id, {
|
|
83
|
+
expand: ["ancestors[*]"],
|
|
84
|
+
});
|
|
85
|
+
expect(expandResult?.ancestors).toHaveLength(3);
|
|
86
|
+
expect(expandResult?.ancestors).toEqual([
|
|
87
|
+
{ id: level2.id, typeId: "category", obj: level2 },
|
|
88
|
+
{ id: level1.id, typeId: "category", obj: level1 },
|
|
89
|
+
{ id: root.id, typeId: "category", obj: root },
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
const queryResult = repository.query(
|
|
93
|
+
{ projectKey: "dummy" },
|
|
94
|
+
{
|
|
95
|
+
where: [`id="${level3.id}"`],
|
|
96
|
+
expand: ["ancestors[*]"],
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
expect(queryResult.results[0].ancestors).toHaveLength(3);
|
|
100
|
+
expect(queryResult.results[0].ancestors).toEqual([
|
|
101
|
+
{ id: level2.id, typeId: "category", obj: level2 },
|
|
102
|
+
{ id: level1.id, typeId: "category", obj: level1 },
|
|
103
|
+
{ id: root.id, typeId: "category", obj: root },
|
|
104
|
+
]);
|
|
81
105
|
});
|
|
82
106
|
});
|
|
@@ -5,10 +5,12 @@ import type {
|
|
|
5
5
|
} from "@commercetools/platform-sdk";
|
|
6
6
|
import { v4 as uuidv4 } from "uuid";
|
|
7
7
|
import { getBaseResourceProperties } from "~src/helpers";
|
|
8
|
+
import { parseExpandClause } from "~src/lib/expandParser";
|
|
8
9
|
import { AbstractStorage } from "~src/storage/abstract";
|
|
9
10
|
import { Writable } from "~src/types";
|
|
10
11
|
import {
|
|
11
12
|
AbstractResourceRepository,
|
|
13
|
+
GetParams,
|
|
12
14
|
type RepositoryContext,
|
|
13
15
|
} from "../abstract";
|
|
14
16
|
import { createCustomFields } from "../helpers";
|
|
@@ -58,16 +60,31 @@ export class CategoryRepository extends AbstractResourceRepository<"category"> {
|
|
|
58
60
|
postProcessResource(
|
|
59
61
|
context: RepositoryContext,
|
|
60
62
|
resource: Writable<Category>,
|
|
63
|
+
params?: GetParams,
|
|
61
64
|
): Category {
|
|
62
65
|
let node: Category = resource;
|
|
63
66
|
const ancestors: CategoryReference[] = [];
|
|
64
67
|
|
|
68
|
+
// TODO: The expand clause here is a hack, the current expand architecture
|
|
69
|
+
// is not able to handle the case for 'dynamic' fields like ancestors which
|
|
70
|
+
// are resolved at runtime. We should do the expand resolution post query
|
|
71
|
+
// execution for all resources
|
|
72
|
+
|
|
73
|
+
const expandClauses = params?.expand?.map(parseExpandClause) ?? [];
|
|
74
|
+
const addExpand = expandClauses?.find(
|
|
75
|
+
(c) => c.element === "ancestors" && c.index === "*",
|
|
76
|
+
);
|
|
77
|
+
|
|
65
78
|
while (node.parent) {
|
|
66
79
|
node = this._storage.getByResourceIdentifier<"category">(
|
|
67
80
|
context.projectKey,
|
|
68
81
|
node.parent,
|
|
69
82
|
);
|
|
70
|
-
ancestors.push({
|
|
83
|
+
ancestors.push({
|
|
84
|
+
typeId: "category",
|
|
85
|
+
id: node.id,
|
|
86
|
+
obj: addExpand ? node : undefined,
|
|
87
|
+
});
|
|
71
88
|
}
|
|
72
89
|
|
|
73
90
|
resource.ancestors = ancestors;
|
|
@@ -6,13 +6,14 @@ import type {
|
|
|
6
6
|
ResourceNotFoundError,
|
|
7
7
|
} from "@commercetools/platform-sdk";
|
|
8
8
|
import { CommercetoolsError } from "~src/exceptions";
|
|
9
|
-
import { getBaseResourceProperties } from "~src/helpers";
|
|
9
|
+
import { generateRandomString, getBaseResourceProperties } from "~src/helpers";
|
|
10
10
|
import { createPasswordResetToken, hashPassword } from "~src/lib/password";
|
|
11
11
|
import { AbstractStorage } from "~src/storage/abstract";
|
|
12
12
|
import {
|
|
13
13
|
AbstractResourceRepository,
|
|
14
14
|
type RepositoryContext,
|
|
15
15
|
} from "../abstract";
|
|
16
|
+
import { createCustomFields } from "../helpers";
|
|
16
17
|
import { CustomerUpdateHandler } from "./actions";
|
|
17
18
|
|
|
18
19
|
export class CustomerRepository extends AbstractResourceRepository<"customer"> {
|
|
@@ -43,14 +44,44 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
|
|
|
43
44
|
});
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
const addresses =
|
|
48
|
+
draft.addresses?.map((address) => ({
|
|
49
|
+
...address,
|
|
50
|
+
id: generateRandomString(5),
|
|
51
|
+
})) ?? [];
|
|
52
|
+
|
|
53
|
+
const defaultBillingAddressId =
|
|
54
|
+
addresses.length > 0 && draft.defaultBillingAddress !== undefined
|
|
55
|
+
? addresses[draft.defaultBillingAddress].id
|
|
56
|
+
: undefined;
|
|
57
|
+
const defaultShippingAddressId =
|
|
58
|
+
addresses.length > 0 && draft.defaultShippingAddress !== undefined
|
|
59
|
+
? addresses[draft.defaultShippingAddress].id
|
|
60
|
+
: undefined;
|
|
61
|
+
|
|
46
62
|
const resource: Customer = {
|
|
47
63
|
...getBaseResourceProperties(),
|
|
64
|
+
key: draft.key,
|
|
48
65
|
authenticationMode: draft.authenticationMode || "Password",
|
|
66
|
+
firstName: draft.firstName,
|
|
67
|
+
lastName: draft.lastName,
|
|
68
|
+
middleName: draft.middleName,
|
|
69
|
+
title: draft.title,
|
|
70
|
+
dateOfBirth: draft.dateOfBirth,
|
|
71
|
+
companyName: draft.companyName,
|
|
49
72
|
email: draft.email.toLowerCase(),
|
|
50
73
|
password: draft.password ? hashPassword(draft.password) : undefined,
|
|
51
74
|
isEmailVerified: draft.isEmailVerified || false,
|
|
52
|
-
addresses:
|
|
75
|
+
addresses: addresses,
|
|
53
76
|
customerNumber: draft.customerNumber,
|
|
77
|
+
externalId: draft.externalId,
|
|
78
|
+
defaultBillingAddressId: defaultBillingAddressId,
|
|
79
|
+
defaultShippingAddressId: defaultShippingAddressId,
|
|
80
|
+
custom: createCustomFields(
|
|
81
|
+
draft.custom,
|
|
82
|
+
context.projectKey,
|
|
83
|
+
this._storage,
|
|
84
|
+
),
|
|
54
85
|
};
|
|
55
86
|
return this.saveNew(context, resource);
|
|
56
87
|
}
|