@reactionary/commercetools 0.6.5 → 0.6.7
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/capabilities/company-registration.capability.js +137 -0
- package/capabilities/company.capability.js +306 -0
- package/capabilities/employee-invitation.capability.js +398 -0
- package/capabilities/employee.capability.js +346 -0
- package/capabilities/index.js +3 -0
- package/capabilities/profile.capability.js +10 -0
- package/core/capability-descriptors.js +65 -1
- package/core/client.js +2 -2
- package/factories/checkout/checkout.factory.js +6 -1
- package/factories/company/company.factory.js +80 -0
- package/factories/company-registration/company-registration.factory.js +36 -0
- package/factories/employee/employee.factory.js +61 -0
- package/factories/employee-invitation/employee-invitation.factory.js +61 -0
- package/index.js +8 -0
- package/package.json +2 -2
- package/schema/capabilities.schema.js +4 -0
- package/schema/commercetools.schema.js +18 -1
- package/src/capabilities/company-registration.capability.d.ts +21 -0
- package/src/capabilities/company.capability.d.ts +30 -0
- package/src/capabilities/employee-invitation.capability.d.ts +34 -0
- package/src/capabilities/employee.capability.d.ts +33 -0
- package/src/capabilities/index.d.ts +3 -0
- package/src/core/capability-descriptors.d.ts +1 -1
- package/src/core/initialize.types.d.ts +20 -0
- package/src/factories/company/company.factory.d.ts +25 -0
- package/src/factories/company-registration/company-registration.factory.d.ts +25 -0
- package/src/factories/employee/employee.factory.d.ts +22 -0
- package/src/factories/employee-invitation/employee-invitation.factory.d.ts +94 -0
- package/src/index.d.ts +8 -0
- package/src/schema/capabilities.schema.d.ts +24 -1
- package/src/schema/commercetools.schema.d.ts +57 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
6
|
+
if (decorator = decorators[i])
|
|
7
|
+
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
+
if (kind && result)
|
|
9
|
+
__defProp(target, key, result);
|
|
10
|
+
return result;
|
|
11
|
+
};
|
|
12
|
+
import {
|
|
13
|
+
EmployeeCapability,
|
|
14
|
+
EmployeeMutationAssignRoleSchema,
|
|
15
|
+
EmployeeMutationUnassignRoleSchema,
|
|
16
|
+
EmployeeQueryByEmailSchema,
|
|
17
|
+
EmployeeQueryListSchema,
|
|
18
|
+
error,
|
|
19
|
+
EmployeePaginatedListSchema,
|
|
20
|
+
EmployeeSchema,
|
|
21
|
+
Reactionary,
|
|
22
|
+
success
|
|
23
|
+
} from "@reactionary/core";
|
|
24
|
+
import {} from "../factories/employee/employee.factory.js";
|
|
25
|
+
class CommercetoolsEmployeeCapability extends EmployeeCapability {
|
|
26
|
+
config;
|
|
27
|
+
commercetools;
|
|
28
|
+
factory;
|
|
29
|
+
constructor(config, cache, context, commercetools, factory) {
|
|
30
|
+
super(cache, context);
|
|
31
|
+
this.config = config;
|
|
32
|
+
this.commercetools = commercetools;
|
|
33
|
+
this.factory = factory;
|
|
34
|
+
}
|
|
35
|
+
async getClient() {
|
|
36
|
+
const client = await this.commercetools.getAdminClient();
|
|
37
|
+
if (this.context.session.identityContext.identity.type !== "Registered") {
|
|
38
|
+
throw new Error("Only registered users can access employees capabilities");
|
|
39
|
+
}
|
|
40
|
+
return client.withProjectKey({ projectKey: this.config.projectKey }).asAssociate().withAssociateIdValue({
|
|
41
|
+
associateId: this.context.session.identityContext.identity.id.userId
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async getAdminClient() {
|
|
45
|
+
const client = await this.commercetools.getAdminClient();
|
|
46
|
+
return client.withProjectKey({ projectKey: this.config.projectKey });
|
|
47
|
+
}
|
|
48
|
+
async getBusinessUnit(key, extraExpands = []) {
|
|
49
|
+
const client = await this.getClient();
|
|
50
|
+
const response = await client.businessUnits().withKey({ key }).get({
|
|
51
|
+
queryArgs: {
|
|
52
|
+
expand: ["associates[*].customer", ...extraExpands]
|
|
53
|
+
}
|
|
54
|
+
}).execute().catch((err) => {
|
|
55
|
+
if (err.statusCode === 404) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
throw err;
|
|
59
|
+
});
|
|
60
|
+
return response ? response.body : null;
|
|
61
|
+
}
|
|
62
|
+
async updateBusinessUnit(key, version, actions) {
|
|
63
|
+
const client = await this.getClient();
|
|
64
|
+
const response = await client.businessUnits().withKey({ key }).post({
|
|
65
|
+
body: {
|
|
66
|
+
version,
|
|
67
|
+
actions
|
|
68
|
+
},
|
|
69
|
+
queryArgs: {
|
|
70
|
+
expand: ["associates[*].customer"]
|
|
71
|
+
}
|
|
72
|
+
}).execute();
|
|
73
|
+
return response.body;
|
|
74
|
+
}
|
|
75
|
+
getAssociateRole(associate) {
|
|
76
|
+
return associate.associateRoleAssignments[0]?.associateRole.key;
|
|
77
|
+
}
|
|
78
|
+
getRoles(associate) {
|
|
79
|
+
return associate.associateRoleAssignments.map(
|
|
80
|
+
(assignment) => assignment.associateRole.key
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
findAssociateByCustomerId(businessUnit, customerId) {
|
|
84
|
+
return businessUnit.associates.find(
|
|
85
|
+
(associate) => associate.customer.id === customerId
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
findAssociateByEmail(businessUnit, email) {
|
|
89
|
+
return businessUnit.associates.find(
|
|
90
|
+
(associate) => associate.customer.obj?.email === email
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
async listEmployees(payload) {
|
|
94
|
+
const businessUnit = await this.getBusinessUnit(payload.search.company.taxIdentifier);
|
|
95
|
+
if (!businessUnit) {
|
|
96
|
+
return success(
|
|
97
|
+
this.factory.parseEmployeePaginatedList(
|
|
98
|
+
this.context,
|
|
99
|
+
{
|
|
100
|
+
items: [],
|
|
101
|
+
totalCount: 0
|
|
102
|
+
},
|
|
103
|
+
payload
|
|
104
|
+
)
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
const employees = businessUnit.associates.filter((associate) => {
|
|
108
|
+
const assocEmail = (associate.customer.obj?.email ?? "").toLowerCase();
|
|
109
|
+
const assocFirstName = (associate.customer.obj?.firstName ?? "").toLowerCase();
|
|
110
|
+
const assocLastName = (associate.customer.obj?.lastName ?? "").toLowerCase();
|
|
111
|
+
const assocRole = this.getAssociateRole(associate) ?? "";
|
|
112
|
+
if (payload.search.email && !assocEmail.startsWith(payload.search.email.toLowerCase())) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
if (payload.search.firstName && !assocFirstName.startsWith(payload.search.firstName.toLowerCase())) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (payload.search.lastName && !assocLastName.startsWith(payload.search.lastName.toLowerCase())) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
if (payload.search.role && assocRole !== this.factory.mapRole(payload.search.role)) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
return true;
|
|
125
|
+
});
|
|
126
|
+
const pageNumber = payload.search.paginationOptions.pageNumber;
|
|
127
|
+
const pageSize = payload.search.paginationOptions.pageSize;
|
|
128
|
+
const offset = (pageNumber - 1) * pageSize;
|
|
129
|
+
const paged = employees.slice(offset, offset + pageSize);
|
|
130
|
+
return success(
|
|
131
|
+
this.factory.parseEmployeePaginatedList(
|
|
132
|
+
this.context,
|
|
133
|
+
{
|
|
134
|
+
items: paged,
|
|
135
|
+
totalCount: employees.length
|
|
136
|
+
},
|
|
137
|
+
payload
|
|
138
|
+
)
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
async getByEmail(payload) {
|
|
142
|
+
const businessUnit = await this.getBusinessUnit(payload.company.taxIdentifier);
|
|
143
|
+
if (!businessUnit) {
|
|
144
|
+
return error({
|
|
145
|
+
type: "NotFound",
|
|
146
|
+
identifier: payload.company
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const customer = await this.findAssociateByEmail(businessUnit, payload.email);
|
|
150
|
+
if (!customer) {
|
|
151
|
+
return error({
|
|
152
|
+
type: "NotFound",
|
|
153
|
+
identifier: { email: payload.email }
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return success(this.factory.parseEmployee(this.context, {
|
|
157
|
+
company: payload.company,
|
|
158
|
+
associate: customer
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
161
|
+
assignRolePayload(payload) {
|
|
162
|
+
const nextRoles = Array.from(/* @__PURE__ */ new Set([this.factory.mapRole(payload.role)]));
|
|
163
|
+
return [
|
|
164
|
+
{
|
|
165
|
+
action: "changeAssociate",
|
|
166
|
+
associate: {
|
|
167
|
+
customer: {
|
|
168
|
+
typeId: "customer",
|
|
169
|
+
id: payload.employeeIdentifier.userId
|
|
170
|
+
},
|
|
171
|
+
associateRoleAssignments: nextRoles.map((roleKey) => ({
|
|
172
|
+
associateRole: {
|
|
173
|
+
typeId: "associate-role",
|
|
174
|
+
key: roleKey
|
|
175
|
+
}
|
|
176
|
+
}))
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
];
|
|
180
|
+
}
|
|
181
|
+
async assignRole(payload) {
|
|
182
|
+
const key = payload.company.taxIdentifier;
|
|
183
|
+
const businessUnit = await this.getBusinessUnit(key);
|
|
184
|
+
if (!businessUnit) {
|
|
185
|
+
return error({
|
|
186
|
+
type: "NotFound",
|
|
187
|
+
identifier: payload.company
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
const associate = this.findAssociateByCustomerId(
|
|
191
|
+
businessUnit,
|
|
192
|
+
payload.employeeIdentifier.userId
|
|
193
|
+
);
|
|
194
|
+
if (!associate) {
|
|
195
|
+
return error({
|
|
196
|
+
type: "NotFound",
|
|
197
|
+
identifier: payload.employeeIdentifier
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
const actions = this.assignRolePayload(payload);
|
|
201
|
+
const updated = await this.updateBusinessUnit(key, businessUnit.version, actions);
|
|
202
|
+
const updatedAssociate = this.findAssociateByCustomerId(
|
|
203
|
+
updated,
|
|
204
|
+
payload.employeeIdentifier.userId
|
|
205
|
+
);
|
|
206
|
+
if (!updatedAssociate) {
|
|
207
|
+
return error({
|
|
208
|
+
type: "NotFound",
|
|
209
|
+
identifier: payload.employeeIdentifier
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
const customersById = this.findAssociateByCustomerId(updated, payload.employeeIdentifier.userId);
|
|
213
|
+
if (!customersById) {
|
|
214
|
+
return error({
|
|
215
|
+
type: "NotFound",
|
|
216
|
+
identifier: payload.employeeIdentifier
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
return success(this.factory.parseEmployee(this.context, { company: payload.company, associate: customersById }));
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* The CT role assigned when you have no other roles in the org
|
|
223
|
+
* @returns
|
|
224
|
+
*/
|
|
225
|
+
getDefaultRole() {
|
|
226
|
+
return "buyer";
|
|
227
|
+
}
|
|
228
|
+
unassignRolePayload(payload, associate) {
|
|
229
|
+
const ctRole = this.factory.mapRole(payload.role);
|
|
230
|
+
const nextRoles = this.getRoles(associate).filter((role) => role !== ctRole);
|
|
231
|
+
if (nextRoles.length === 0) {
|
|
232
|
+
nextRoles.push(this.getDefaultRole());
|
|
233
|
+
}
|
|
234
|
+
return [
|
|
235
|
+
{
|
|
236
|
+
action: "changeAssociate",
|
|
237
|
+
associate: {
|
|
238
|
+
customer: {
|
|
239
|
+
typeId: "customer",
|
|
240
|
+
id: payload.employeeIdentifier.userId
|
|
241
|
+
},
|
|
242
|
+
associateRoleAssignments: nextRoles.map((roleKey) => ({
|
|
243
|
+
associateRole: {
|
|
244
|
+
typeId: "associate-role",
|
|
245
|
+
key: roleKey
|
|
246
|
+
}
|
|
247
|
+
}))
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
];
|
|
251
|
+
}
|
|
252
|
+
async unassignRole(payload) {
|
|
253
|
+
const key = payload.company.taxIdentifier;
|
|
254
|
+
const businessUnit = await this.getBusinessUnit(key);
|
|
255
|
+
if (!businessUnit) {
|
|
256
|
+
return error({
|
|
257
|
+
type: "NotFound",
|
|
258
|
+
identifier: payload.company
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
const associate = this.findAssociateByCustomerId(
|
|
262
|
+
businessUnit,
|
|
263
|
+
payload.employeeIdentifier.userId
|
|
264
|
+
);
|
|
265
|
+
if (!associate) {
|
|
266
|
+
return error({
|
|
267
|
+
type: "NotFound",
|
|
268
|
+
identifier: payload.employeeIdentifier
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
const actions = this.unassignRolePayload(payload, associate);
|
|
272
|
+
const updated = await this.updateBusinessUnit(key, businessUnit.version, actions);
|
|
273
|
+
const updatedAssociate = this.findAssociateByCustomerId(
|
|
274
|
+
updated,
|
|
275
|
+
payload.employeeIdentifier.userId
|
|
276
|
+
);
|
|
277
|
+
if (!updatedAssociate) {
|
|
278
|
+
return error({
|
|
279
|
+
type: "NotFound",
|
|
280
|
+
identifier: payload.employeeIdentifier
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
return success(this.factory.parseEmployee(this.context, { company: payload.company, associate: updatedAssociate }));
|
|
284
|
+
}
|
|
285
|
+
removeEmployeePayload(payload) {
|
|
286
|
+
return [
|
|
287
|
+
{
|
|
288
|
+
action: "removeAssociate",
|
|
289
|
+
customer: {
|
|
290
|
+
typeId: "customer",
|
|
291
|
+
id: payload.employeeIdentifier.userId
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
];
|
|
295
|
+
}
|
|
296
|
+
async removeEmployee(payload) {
|
|
297
|
+
const key = payload.company.taxIdentifier;
|
|
298
|
+
const businessUnit = await this.getBusinessUnit(key);
|
|
299
|
+
if (!businessUnit) {
|
|
300
|
+
return error({
|
|
301
|
+
type: "NotFound",
|
|
302
|
+
identifier: payload.company
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
const associate = this.findAssociateByCustomerId(
|
|
306
|
+
businessUnit,
|
|
307
|
+
payload.employeeIdentifier.userId
|
|
308
|
+
);
|
|
309
|
+
if (!associate) {
|
|
310
|
+
return error({
|
|
311
|
+
type: "NotFound",
|
|
312
|
+
identifier: payload.employeeIdentifier
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
const actions = this.removeEmployeePayload(payload);
|
|
316
|
+
await this.updateBusinessUnit(key, businessUnit.version, actions);
|
|
317
|
+
return success(void 0);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
__decorateClass([
|
|
321
|
+
Reactionary({
|
|
322
|
+
inputSchema: EmployeeQueryListSchema,
|
|
323
|
+
outputSchema: EmployeePaginatedListSchema
|
|
324
|
+
})
|
|
325
|
+
], CommercetoolsEmployeeCapability.prototype, "listEmployees", 1);
|
|
326
|
+
__decorateClass([
|
|
327
|
+
Reactionary({
|
|
328
|
+
inputSchema: EmployeeQueryByEmailSchema,
|
|
329
|
+
outputSchema: EmployeeSchema
|
|
330
|
+
})
|
|
331
|
+
], CommercetoolsEmployeeCapability.prototype, "getByEmail", 1);
|
|
332
|
+
__decorateClass([
|
|
333
|
+
Reactionary({
|
|
334
|
+
inputSchema: EmployeeMutationAssignRoleSchema,
|
|
335
|
+
outputSchema: EmployeeSchema
|
|
336
|
+
})
|
|
337
|
+
], CommercetoolsEmployeeCapability.prototype, "assignRole", 1);
|
|
338
|
+
__decorateClass([
|
|
339
|
+
Reactionary({
|
|
340
|
+
inputSchema: EmployeeMutationUnassignRoleSchema,
|
|
341
|
+
outputSchema: EmployeeSchema
|
|
342
|
+
})
|
|
343
|
+
], CommercetoolsEmployeeCapability.prototype, "unassignRole", 1);
|
|
344
|
+
export {
|
|
345
|
+
CommercetoolsEmployeeCapability
|
|
346
|
+
};
|
package/capabilities/index.js
CHANGED
|
@@ -13,3 +13,6 @@ export * from "./product-search.capability.js";
|
|
|
13
13
|
export * from "./store.capability.js";
|
|
14
14
|
export * from "./order.capability.js";
|
|
15
15
|
export * from "./checkout.capability.js";
|
|
16
|
+
export * from "./company-registration.capability.js";
|
|
17
|
+
export * from "./company.capability.js";
|
|
18
|
+
export * from "./employee.capability.js";
|
|
@@ -214,6 +214,16 @@ class CommercetoolsProfileCapability extends ProfileCapability {
|
|
|
214
214
|
address: this.createCTAddressDraft(payload.address)
|
|
215
215
|
});
|
|
216
216
|
}
|
|
217
|
+
updateActions.push(
|
|
218
|
+
{
|
|
219
|
+
action: "setFirstName",
|
|
220
|
+
firstName: payload.address.firstName
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
action: "setLastName",
|
|
224
|
+
lastName: payload.address.lastName
|
|
225
|
+
}
|
|
226
|
+
);
|
|
217
227
|
if (updateActions.length > 0) {
|
|
218
228
|
const updateResponse = await client.me().post({
|
|
219
229
|
body: {
|
|
@@ -4,6 +4,10 @@ import {
|
|
|
4
4
|
CategoryPaginatedResultSchema,
|
|
5
5
|
CategorySchema,
|
|
6
6
|
CheckoutSchema,
|
|
7
|
+
CompanySchema,
|
|
8
|
+
CompanyPaginatedListSchema,
|
|
9
|
+
EmployeeSchema,
|
|
10
|
+
EmployeePaginatedListSchema,
|
|
7
11
|
IdentitySchema,
|
|
8
12
|
InventorySchema,
|
|
9
13
|
OrderSearchResultSchema,
|
|
@@ -41,6 +45,7 @@ import { CommercetoolsProductReviewsFactory } from "../factories/product-reviews
|
|
|
41
45
|
import { CommercetoolsProductSearchFactory } from "../factories/product-search/product-search.factory.js";
|
|
42
46
|
import { CommercetoolsProfileFactory } from "../factories/profile/profile.factory.js";
|
|
43
47
|
import { CommercetoolsStoreFactory } from "../factories/store/store.factory.js";
|
|
48
|
+
import { CommercetoolsCompanyRegistrationFactory } from "../factories/company-registration/company-registration.factory.js";
|
|
44
49
|
import { CommercetoolsCartCapability } from "../capabilities/cart.capability.js";
|
|
45
50
|
import { CommercetoolsCategoryCapability } from "../capabilities/category.capability.js";
|
|
46
51
|
import { CommercetoolsCheckoutCapability } from "../capabilities/checkout.capability.js";
|
|
@@ -56,6 +61,13 @@ import { CommercetoolsProductReviewsCapability } from "../capabilities/product-r
|
|
|
56
61
|
import { CommercetoolsProductSearchCapability } from "../capabilities/product-search.capability.js";
|
|
57
62
|
import { CommercetoolsProfileCapability } from "../capabilities/profile.capability.js";
|
|
58
63
|
import { CommercetoolsStoreCapability } from "../capabilities/store.capability.js";
|
|
64
|
+
import { CommercetoolsCompanyRegistrationCapability } from "../capabilities/company-registration.capability.js";
|
|
65
|
+
import { CommercetoolsCompanyCapability } from "../capabilities/company.capability.js";
|
|
66
|
+
import { CommercetoolsCompanyFactory } from "../factories/company/company.factory.js";
|
|
67
|
+
import { CommercetoolsEmployeeFactory } from "../factories/employee/employee.factory.js";
|
|
68
|
+
import { CommercetoolsEmployeeCapability } from "../capabilities/employee.capability.js";
|
|
69
|
+
import { CommercetoolsEmployeeInvitationCapability } from "../capabilities/employee-invitation.capability.js";
|
|
70
|
+
import { CommercetoolsEmployeeInvitationFactory } from "../factories/employee-invitation/employee-invitation.factory.js";
|
|
59
71
|
const capabilityKeys = [
|
|
60
72
|
"product",
|
|
61
73
|
"profile",
|
|
@@ -71,7 +83,11 @@ const capabilityKeys = [
|
|
|
71
83
|
"checkout",
|
|
72
84
|
"store",
|
|
73
85
|
"order",
|
|
74
|
-
"orderSearch"
|
|
86
|
+
"orderSearch",
|
|
87
|
+
"companyRegistration",
|
|
88
|
+
"company",
|
|
89
|
+
"employee",
|
|
90
|
+
"employeeInvitation"
|
|
75
91
|
];
|
|
76
92
|
const capabilityDescriptors = {
|
|
77
93
|
product: {
|
|
@@ -266,6 +282,54 @@ const capabilityDescriptors = {
|
|
|
266
282
|
args.commercetoolsApi,
|
|
267
283
|
args.factory
|
|
268
284
|
)
|
|
285
|
+
},
|
|
286
|
+
companyRegistration: {
|
|
287
|
+
isEnabled: (caps) => caps.companyRegistration?.enabled,
|
|
288
|
+
getOverride: (caps) => caps.companyRegistration,
|
|
289
|
+
createDefaultFactory: () => new CommercetoolsCompanyRegistrationFactory(),
|
|
290
|
+
createDefaultCapability: (args) => new CommercetoolsCompanyRegistrationCapability(
|
|
291
|
+
args.config,
|
|
292
|
+
args.cache,
|
|
293
|
+
args.context,
|
|
294
|
+
args.commercetoolsApi,
|
|
295
|
+
args.factory
|
|
296
|
+
)
|
|
297
|
+
},
|
|
298
|
+
company: {
|
|
299
|
+
isEnabled: (caps) => caps.company?.enabled,
|
|
300
|
+
getOverride: (caps) => caps.company,
|
|
301
|
+
createDefaultFactory: () => new CommercetoolsCompanyFactory(CompanySchema, CompanyPaginatedListSchema),
|
|
302
|
+
createDefaultCapability: (args) => new CommercetoolsCompanyCapability(
|
|
303
|
+
args.config,
|
|
304
|
+
args.cache,
|
|
305
|
+
args.context,
|
|
306
|
+
args.commercetoolsApi,
|
|
307
|
+
args.factory
|
|
308
|
+
)
|
|
309
|
+
},
|
|
310
|
+
employee: {
|
|
311
|
+
isEnabled: (caps) => caps.employee?.enabled,
|
|
312
|
+
getOverride: (caps) => caps.employee,
|
|
313
|
+
createDefaultFactory: () => new CommercetoolsEmployeeFactory(EmployeeSchema, EmployeePaginatedListSchema),
|
|
314
|
+
createDefaultCapability: (args) => new CommercetoolsEmployeeCapability(
|
|
315
|
+
args.config,
|
|
316
|
+
args.cache,
|
|
317
|
+
args.context,
|
|
318
|
+
args.commercetoolsApi,
|
|
319
|
+
args.factory
|
|
320
|
+
)
|
|
321
|
+
},
|
|
322
|
+
employeeInvitation: {
|
|
323
|
+
isEnabled: (caps) => caps.employeeInvitation?.enabled,
|
|
324
|
+
getOverride: (caps) => caps.employeeInvitation,
|
|
325
|
+
createDefaultFactory: () => new CommercetoolsEmployeeInvitationFactory(),
|
|
326
|
+
createDefaultCapability: (args) => new CommercetoolsEmployeeInvitationCapability(
|
|
327
|
+
args.config,
|
|
328
|
+
args.cache,
|
|
329
|
+
args.context,
|
|
330
|
+
args.commercetoolsApi,
|
|
331
|
+
args.factory
|
|
332
|
+
)
|
|
269
333
|
}
|
|
270
334
|
};
|
|
271
335
|
export {
|
package/core/client.js
CHANGED
|
@@ -111,8 +111,8 @@ class CommercetoolsAPI {
|
|
|
111
111
|
let builder = this.createBaseClientBuilder();
|
|
112
112
|
builder = builder.withAnonymousSessionFlow({
|
|
113
113
|
credentials: {
|
|
114
|
-
clientId: this.config.clientId,
|
|
115
|
-
clientSecret: this.config.clientSecret
|
|
114
|
+
clientId: this.config.adminClientId || this.config.clientId,
|
|
115
|
+
clientSecret: this.config.adminClientSecret || this.config.clientSecret
|
|
116
116
|
},
|
|
117
117
|
host: this.config.authUrl,
|
|
118
118
|
projectKey: this.config.projectKey
|
|
@@ -70,6 +70,10 @@ class CommercetoolsCheckoutFactory {
|
|
|
70
70
|
for (const lineItem of data.lineItems) {
|
|
71
71
|
items.push(this.parseCheckoutItem(lineItem));
|
|
72
72
|
}
|
|
73
|
+
const pointOfContact = {
|
|
74
|
+
email: data.billingAddress?.email || "",
|
|
75
|
+
phone: data.billingAddress?.phone
|
|
76
|
+
};
|
|
73
77
|
const shippingInstruction = this.parseShippingInstruction(data);
|
|
74
78
|
const readyForFinalization = this.isReadyForFinalization(
|
|
75
79
|
price,
|
|
@@ -89,7 +93,8 @@ class CommercetoolsCheckoutFactory {
|
|
|
89
93
|
shippingInstruction,
|
|
90
94
|
paymentInstructions,
|
|
91
95
|
items,
|
|
92
|
-
price
|
|
96
|
+
price,
|
|
97
|
+
pointOfContact
|
|
93
98
|
};
|
|
94
99
|
return this.checkoutSchema.parse(result);
|
|
95
100
|
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
} from "@reactionary/core";
|
|
3
|
+
class CommercetoolsCompanyFactory {
|
|
4
|
+
companySchema;
|
|
5
|
+
companyPaginatedListSchema;
|
|
6
|
+
constructor(companySchema, companyPaginatedListSchema) {
|
|
7
|
+
this.companySchema = companySchema;
|
|
8
|
+
this.companyPaginatedListSchema = companyPaginatedListSchema;
|
|
9
|
+
}
|
|
10
|
+
parseCompanyPaginatedList(context, data, payload) {
|
|
11
|
+
const result = {
|
|
12
|
+
pageNumber: payload.search.paginationOptions.pageNumber,
|
|
13
|
+
pageSize: payload.search.paginationOptions.pageSize,
|
|
14
|
+
totalCount: data.total || 0,
|
|
15
|
+
totalPages: Math.ceil((data.total || 0) / payload.search.paginationOptions.pageSize),
|
|
16
|
+
items: data.results.map((bu) => this.parseCompany(context, bu)),
|
|
17
|
+
identifier: payload.search
|
|
18
|
+
};
|
|
19
|
+
return this.companyPaginatedListSchema.parse(result);
|
|
20
|
+
}
|
|
21
|
+
parseCompany(_context, data) {
|
|
22
|
+
const defaultShippingAddress = data.defaultShippingAddressId ? data.addresses.find((addr) => addr.id === data.defaultShippingAddressId) : void 0;
|
|
23
|
+
const defaultBillingAddress = data.defaultBillingAddressId ? data.addresses.find((addr) => addr.id === data.defaultBillingAddressId) : void 0;
|
|
24
|
+
const alternateShippingAddresses = data.addresses.filter((addr) => addr.id !== data.defaultShippingAddressId).filter((addr) => addr.id !== data.defaultBillingAddressId).map((addr) => this.parseAddress(addr));
|
|
25
|
+
const customFields = data.custom?.fields;
|
|
26
|
+
const result = {
|
|
27
|
+
identifier: {
|
|
28
|
+
taxIdentifier: data.key
|
|
29
|
+
},
|
|
30
|
+
name: data.name,
|
|
31
|
+
dunsIdentifier: customFields?.["dunsIdentifier"],
|
|
32
|
+
tinIdentifier: customFields?.["tinIdentifier"],
|
|
33
|
+
status: this.mapBusinessUnitStatus(data.status),
|
|
34
|
+
pointOfContact: {
|
|
35
|
+
email: data.contactEmail ?? "",
|
|
36
|
+
phone: customFields?.["pointOfContactPhone"]
|
|
37
|
+
},
|
|
38
|
+
shippingAddress: defaultShippingAddress ? this.parseAddress(defaultShippingAddress) : void 0,
|
|
39
|
+
billingAddress: defaultBillingAddress ? this.parseAddress(defaultBillingAddress) : {
|
|
40
|
+
identifier: { nickName: "" },
|
|
41
|
+
firstName: "",
|
|
42
|
+
lastName: "",
|
|
43
|
+
streetAddress: "",
|
|
44
|
+
streetNumber: "",
|
|
45
|
+
city: "",
|
|
46
|
+
region: "",
|
|
47
|
+
postalCode: "",
|
|
48
|
+
countryCode: ""
|
|
49
|
+
},
|
|
50
|
+
alternateShippingAddresses,
|
|
51
|
+
isCustomAddressesAllowed: customFields?.["isCustomAddressesAllowed"] ?? false,
|
|
52
|
+
isSelfManagementOfShippingAddressesAllowed: customFields?.["isSelfManagementOfShippingAddressesAllowed"] ?? true
|
|
53
|
+
};
|
|
54
|
+
return this.companySchema.parse(result);
|
|
55
|
+
}
|
|
56
|
+
mapBusinessUnitStatus(status) {
|
|
57
|
+
switch (status) {
|
|
58
|
+
case "Active":
|
|
59
|
+
return "active";
|
|
60
|
+
default:
|
|
61
|
+
return "blocked";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
parseAddress(address) {
|
|
65
|
+
return {
|
|
66
|
+
identifier: { nickName: address.key || "" },
|
|
67
|
+
firstName: address.firstName || "",
|
|
68
|
+
lastName: address.lastName || "",
|
|
69
|
+
streetAddress: address.streetName || "",
|
|
70
|
+
streetNumber: address.streetNumber || "",
|
|
71
|
+
city: address.city || "",
|
|
72
|
+
region: address.region || "",
|
|
73
|
+
postalCode: address.postalCode || "",
|
|
74
|
+
countryCode: address.country
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export {
|
|
79
|
+
CommercetoolsCompanyFactory
|
|
80
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CompanyRegistrationRequestSchema
|
|
3
|
+
} from "@reactionary/core";
|
|
4
|
+
class CommercetoolsCompanyRegistrationFactory {
|
|
5
|
+
companyRegistrationRequestSchema = CompanyRegistrationRequestSchema;
|
|
6
|
+
parseCompanyRegistrationRequest(_context, data) {
|
|
7
|
+
const status = this.mapBusinessUnitStatus(data.status);
|
|
8
|
+
const result = {
|
|
9
|
+
identifier: {
|
|
10
|
+
key: data.id
|
|
11
|
+
},
|
|
12
|
+
companyIdentifier: {
|
|
13
|
+
taxIdentifier: data.key
|
|
14
|
+
},
|
|
15
|
+
name: data.name,
|
|
16
|
+
pointOfContact: {
|
|
17
|
+
email: data.contactEmail ?? ""
|
|
18
|
+
},
|
|
19
|
+
status
|
|
20
|
+
};
|
|
21
|
+
return CompanyRegistrationRequestSchema.parse(result);
|
|
22
|
+
}
|
|
23
|
+
mapBusinessUnitStatus(status) {
|
|
24
|
+
switch (status) {
|
|
25
|
+
case "Active":
|
|
26
|
+
return "approved";
|
|
27
|
+
case "Inactive":
|
|
28
|
+
return "pending";
|
|
29
|
+
default:
|
|
30
|
+
return "pending";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
CommercetoolsCompanyRegistrationFactory
|
|
36
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {
|
|
2
|
+
} from "@reactionary/core";
|
|
3
|
+
class CommercetoolsEmployeeFactory {
|
|
4
|
+
employeeSchema;
|
|
5
|
+
employeePaginatedListSchema;
|
|
6
|
+
constructor(employeeSchema, employeePaginatedListSchema) {
|
|
7
|
+
this.employeeSchema = employeeSchema;
|
|
8
|
+
this.employeePaginatedListSchema = employeePaginatedListSchema;
|
|
9
|
+
}
|
|
10
|
+
parseEmployee(_context, input) {
|
|
11
|
+
const result = {
|
|
12
|
+
identifier: { userId: input.associate.customer.id },
|
|
13
|
+
company: input.company,
|
|
14
|
+
firstName: input.associate.customer.obj?.firstName,
|
|
15
|
+
lastName: input.associate.customer.obj?.lastName,
|
|
16
|
+
email: input.associate.customer.obj?.email || "",
|
|
17
|
+
role: this.parseRole(input.associate.associateRoleAssignments[0]?.associateRole.key)
|
|
18
|
+
};
|
|
19
|
+
return this.employeeSchema.parse(result);
|
|
20
|
+
}
|
|
21
|
+
parseEmployeePaginatedList(context, data, query) {
|
|
22
|
+
const totalPages = query.search.paginationOptions.pageSize > 0 ? Math.ceil(data.totalCount / query.search.paginationOptions.pageSize) : 0;
|
|
23
|
+
const result = {
|
|
24
|
+
identifier: query.search,
|
|
25
|
+
pageNumber: query.search.paginationOptions.pageNumber,
|
|
26
|
+
pageSize: query.search.paginationOptions.pageSize,
|
|
27
|
+
totalCount: data.totalCount,
|
|
28
|
+
totalPages,
|
|
29
|
+
items: data.items.map((associate) => this.parseEmployee(context, {
|
|
30
|
+
company: query.search.company,
|
|
31
|
+
associate
|
|
32
|
+
}))
|
|
33
|
+
};
|
|
34
|
+
return this.employeePaginatedListSchema.parse(result);
|
|
35
|
+
}
|
|
36
|
+
mapRole(role) {
|
|
37
|
+
switch (role) {
|
|
38
|
+
case "admin":
|
|
39
|
+
return "buyer-admin";
|
|
40
|
+
case "manager":
|
|
41
|
+
return "buyer-approver";
|
|
42
|
+
case "employee":
|
|
43
|
+
return "buyer";
|
|
44
|
+
}
|
|
45
|
+
return "buyer";
|
|
46
|
+
}
|
|
47
|
+
parseRole(role) {
|
|
48
|
+
switch (role) {
|
|
49
|
+
case "buyer-admin":
|
|
50
|
+
return "admin";
|
|
51
|
+
case "buyer-approver":
|
|
52
|
+
return "manager";
|
|
53
|
+
case "buyer":
|
|
54
|
+
return "employee";
|
|
55
|
+
}
|
|
56
|
+
return "employee";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
CommercetoolsEmployeeFactory
|
|
61
|
+
};
|