@reactionary/hcl 0.9.12 → 0.9.14
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/analytics.capability.js +98 -0
- package/capabilities/company.capability.js +307 -0
- package/capabilities/personalization-profile.capability.js +85 -0
- package/capabilities/product-associations.capability.js +117 -0
- package/capabilities/product-list.capability.js +665 -0
- package/capabilities/product-recommendations.capability.js +143 -0
- package/capabilities/store.capability.js +72 -0
- package/core/initialize.js +148 -2
- package/factories/company/company.factory.js +77 -0
- package/factories/index.js +6 -0
- package/factories/personalization-profile/personalization-profile.factory.js +16 -0
- package/factories/product-associations/product-associations.factory.js +16 -0
- package/factories/product-list/product-list.factory.js +158 -0
- package/factories/product-recommendations/product-recommendations.factory.js +17 -0
- package/factories/store/store.factory.js +18 -0
- package/index.js +7 -0
- package/package.json +2 -2
- package/schema/capabilities.schema.js +16 -2
- package/schema/configuration.schema.js +37 -1
- package/src/capabilities/analytics.capability.d.ts +17 -0
- package/src/capabilities/company.capability.d.ts +26 -0
- package/src/capabilities/personalization-profile.capability.d.ts +19 -0
- package/src/capabilities/product-associations.capability.d.ts +22 -0
- package/src/capabilities/product-list.capability.d.ts +42 -0
- package/src/capabilities/product-recommendations.capability.d.ts +29 -0
- package/src/capabilities/store.capability.d.ts +13 -0
- package/src/core/initialize.types.d.ts +7 -1
- package/src/factories/company/company.factory.d.ts +11 -0
- package/src/factories/index.d.ts +6 -0
- package/src/factories/personalization-profile/personalization-profile.factory.d.ts +11 -0
- package/src/factories/product-associations/product-associations.factory.d.ts +8 -0
- package/src/factories/product-list/product-list.factory.d.ts +40 -0
- package/src/factories/product-recommendations/product-recommendations.factory.d.ts +12 -0
- package/src/factories/store/store.factory.d.ts +8 -0
- package/src/index.d.ts +7 -0
- package/src/schema/capabilities.schema.d.ts +45 -1
- package/src/schema/configuration.schema.d.ts +24 -0
- package/src/schema/hcl.schema.d.ts +277 -1
- package/src/test/test-utils.d.ts +4 -1
|
@@ -0,0 +1,98 @@
|
|
|
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
|
+
AnalyticsCapability,
|
|
14
|
+
AnalyticsMutationSchema,
|
|
15
|
+
AnalyticsResultSchema,
|
|
16
|
+
Reactionary
|
|
17
|
+
} from "@reactionary/core";
|
|
18
|
+
import { SESSION_KEY_PERSONALIZATION_ID } from "../core/session-keys.js";
|
|
19
|
+
class HclAnalyticsCapability extends AnalyticsCapability {
|
|
20
|
+
constructor(cache, context, config, client) {
|
|
21
|
+
super(cache, context);
|
|
22
|
+
this.config = config;
|
|
23
|
+
this.client = client;
|
|
24
|
+
}
|
|
25
|
+
async track(event) {
|
|
26
|
+
return super.track(event);
|
|
27
|
+
}
|
|
28
|
+
async processProductSummaryView(event) {
|
|
29
|
+
try {
|
|
30
|
+
for (const product of event.products) {
|
|
31
|
+
await this.fireEvent({ productId: product.key });
|
|
32
|
+
}
|
|
33
|
+
return this.accepted();
|
|
34
|
+
} catch {
|
|
35
|
+
return this.rejected();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async processProductSummaryClick(event) {
|
|
39
|
+
try {
|
|
40
|
+
await this.fireEvent({ productId: event.product.key });
|
|
41
|
+
return this.accepted();
|
|
42
|
+
} catch {
|
|
43
|
+
return this.rejected();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async processProductDetailsView(event) {
|
|
47
|
+
try {
|
|
48
|
+
await this.fireEvent({ productId: event.product.key });
|
|
49
|
+
return this.accepted();
|
|
50
|
+
} catch {
|
|
51
|
+
return this.rejected();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async processProductAddToCart(event) {
|
|
55
|
+
try {
|
|
56
|
+
await this.fireEvent({ productId: event.product.key });
|
|
57
|
+
return this.accepted();
|
|
58
|
+
} catch {
|
|
59
|
+
return this.rejected();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async processPurchase(event) {
|
|
63
|
+
try {
|
|
64
|
+
for (const item of event.order.items) {
|
|
65
|
+
await this.fireEvent({ productId: item.variant.sku });
|
|
66
|
+
}
|
|
67
|
+
return this.accepted();
|
|
68
|
+
} catch {
|
|
69
|
+
return this.rejected();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async fireEvent(extra) {
|
|
73
|
+
await this.client.callPost(
|
|
74
|
+
this.getEventUrl(),
|
|
75
|
+
this.getEventBody(extra)
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
getEventUrl() {
|
|
79
|
+
return `${this.client.transactionBaseUrl}/event`;
|
|
80
|
+
}
|
|
81
|
+
getEventBody(extra) {
|
|
82
|
+
const personalizationID = this.context.session[SESSION_KEY_PERSONALIZATION_ID];
|
|
83
|
+
const body = { ...extra };
|
|
84
|
+
if (personalizationID) {
|
|
85
|
+
body["personalizationID"] = personalizationID;
|
|
86
|
+
}
|
|
87
|
+
return body;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
__decorateClass([
|
|
91
|
+
Reactionary({
|
|
92
|
+
inputSchema: AnalyticsMutationSchema,
|
|
93
|
+
outputSchema: AnalyticsResultSchema
|
|
94
|
+
})
|
|
95
|
+
], HclAnalyticsCapability.prototype, "track", 1);
|
|
96
|
+
export {
|
|
97
|
+
HclAnalyticsCapability
|
|
98
|
+
};
|
|
@@ -0,0 +1,307 @@
|
|
|
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
|
+
CompanyCapability,
|
|
14
|
+
CompanyMutationAddShippingAddressSchema,
|
|
15
|
+
CompanyMutationMakeShippingAddressDefaultSchema,
|
|
16
|
+
CompanyMutationRemoveShippingAddressSchema,
|
|
17
|
+
CompanyMutationUpdateShippingAddressSchema,
|
|
18
|
+
CompanyPaginatedListSchema,
|
|
19
|
+
CompanyQueryByIdSchema,
|
|
20
|
+
CompanyQueryListSchema,
|
|
21
|
+
Reactionary,
|
|
22
|
+
error,
|
|
23
|
+
success
|
|
24
|
+
} from "@reactionary/core";
|
|
25
|
+
import createDebug from "debug";
|
|
26
|
+
const debug = createDebug("reactionary:hcl:company");
|
|
27
|
+
class HclCompanyCapability extends CompanyCapability {
|
|
28
|
+
config;
|
|
29
|
+
client;
|
|
30
|
+
factory;
|
|
31
|
+
constructor(cache, context, config, client, factory) {
|
|
32
|
+
super(cache, context);
|
|
33
|
+
this.config = config;
|
|
34
|
+
this.client = client;
|
|
35
|
+
this.factory = factory;
|
|
36
|
+
}
|
|
37
|
+
async getById(payload) {
|
|
38
|
+
debug("getById %s", payload.identifier.taxIdentifier);
|
|
39
|
+
const raw = await this.client.callGet(
|
|
40
|
+
this.getOrganizationUrl(payload.identifier.taxIdentifier),
|
|
41
|
+
this.getOrganizationDetailsParams(),
|
|
42
|
+
{ allowUndefined: true }
|
|
43
|
+
);
|
|
44
|
+
if (!raw) {
|
|
45
|
+
return error({
|
|
46
|
+
type: "NotFound",
|
|
47
|
+
identifier: payload.identifier
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return success(
|
|
51
|
+
this.factory.parseCompany(
|
|
52
|
+
this.context,
|
|
53
|
+
raw
|
|
54
|
+
)
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
async listCompanies(payload) {
|
|
58
|
+
debug(
|
|
59
|
+
"listCompanies page=%d size=%d",
|
|
60
|
+
payload.search.paginationOptions.pageNumber,
|
|
61
|
+
payload.search.paginationOptions.pageSize
|
|
62
|
+
);
|
|
63
|
+
const response = await this.client.callGet(
|
|
64
|
+
this.getOrganizationListUrl(),
|
|
65
|
+
this.getOrganizationListParams(payload)
|
|
66
|
+
);
|
|
67
|
+
return success(
|
|
68
|
+
this.factory.parseCompanyPaginatedList(this.context, response, payload)
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
async addShippingAddress(payload) {
|
|
72
|
+
debug(
|
|
73
|
+
"addShippingAddress %s \u2192 %s",
|
|
74
|
+
payload.company.taxIdentifier,
|
|
75
|
+
payload.address.identifier.nickName
|
|
76
|
+
);
|
|
77
|
+
const org = await this.fetchOrganization(payload.company.taxIdentifier);
|
|
78
|
+
if (!org) {
|
|
79
|
+
return error({
|
|
80
|
+
type: "NotFound",
|
|
81
|
+
identifier: payload.company
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const addressBook = [
|
|
85
|
+
...org.addressBook ?? [],
|
|
86
|
+
this.toOrgContact(payload.address)
|
|
87
|
+
];
|
|
88
|
+
await this.client.callPut(
|
|
89
|
+
this.getUpdateOrganizationUrl(payload.company.taxIdentifier),
|
|
90
|
+
this.getUpdateOrganizationBody(org, addressBook)
|
|
91
|
+
);
|
|
92
|
+
return success(
|
|
93
|
+
this.factory.parseCompany(
|
|
94
|
+
this.context,
|
|
95
|
+
await this.fetchOrganizationOrThrow(payload.company.taxIdentifier)
|
|
96
|
+
)
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
async updateShippingAddress(payload) {
|
|
100
|
+
debug(
|
|
101
|
+
"updateShippingAddress %s \u2192 %s",
|
|
102
|
+
payload.company.taxIdentifier,
|
|
103
|
+
payload.address.identifier.nickName
|
|
104
|
+
);
|
|
105
|
+
const org = await this.fetchOrganization(payload.company.taxIdentifier);
|
|
106
|
+
if (!org) {
|
|
107
|
+
return error({
|
|
108
|
+
type: "NotFound",
|
|
109
|
+
identifier: payload.company
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
const existing = (org.addressBook ?? []).find(
|
|
113
|
+
(a) => a.nickName === payload.address.identifier.nickName
|
|
114
|
+
);
|
|
115
|
+
if (!existing) {
|
|
116
|
+
return error({
|
|
117
|
+
type: "NotFound",
|
|
118
|
+
identifier: payload.address.identifier
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
const addressBook = (org.addressBook ?? []).map(
|
|
122
|
+
(a) => a.nickName === payload.address.identifier.nickName ? { ...this.toOrgContact(payload.address), primary: existing.primary } : a
|
|
123
|
+
);
|
|
124
|
+
await this.client.callPut(
|
|
125
|
+
this.getUpdateOrganizationUrl(payload.company.taxIdentifier),
|
|
126
|
+
this.getUpdateOrganizationBody(org, addressBook)
|
|
127
|
+
);
|
|
128
|
+
return success(
|
|
129
|
+
this.factory.parseCompany(
|
|
130
|
+
this.context,
|
|
131
|
+
await this.fetchOrganizationOrThrow(payload.company.taxIdentifier)
|
|
132
|
+
)
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
async removeShippingAddress(payload) {
|
|
136
|
+
debug(
|
|
137
|
+
"removeShippingAddress %s \u2192 %s",
|
|
138
|
+
payload.company.taxIdentifier,
|
|
139
|
+
payload.addressIdentifier.nickName
|
|
140
|
+
);
|
|
141
|
+
const org = await this.fetchOrganization(payload.company.taxIdentifier);
|
|
142
|
+
if (!org) {
|
|
143
|
+
return error({
|
|
144
|
+
type: "NotFound",
|
|
145
|
+
identifier: payload.company
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
const existing = (org.addressBook ?? []).find(
|
|
149
|
+
(a) => a.nickName === payload.addressIdentifier.nickName
|
|
150
|
+
);
|
|
151
|
+
if (!existing) {
|
|
152
|
+
return error({
|
|
153
|
+
type: "NotFound",
|
|
154
|
+
identifier: payload.addressIdentifier
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const addressBook = (org.addressBook ?? []).filter(
|
|
158
|
+
(a) => a.nickName !== payload.addressIdentifier.nickName
|
|
159
|
+
);
|
|
160
|
+
await this.client.callPut(
|
|
161
|
+
this.getUpdateOrganizationUrl(payload.company.taxIdentifier),
|
|
162
|
+
this.getUpdateOrganizationBody(org, addressBook)
|
|
163
|
+
);
|
|
164
|
+
return success(
|
|
165
|
+
this.factory.parseCompany(
|
|
166
|
+
this.context,
|
|
167
|
+
await this.fetchOrganizationOrThrow(payload.company.taxIdentifier)
|
|
168
|
+
)
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
async makeShippingAddressDefault(payload) {
|
|
172
|
+
debug(
|
|
173
|
+
"makeShippingAddressDefault %s \u2192 %s",
|
|
174
|
+
payload.company.taxIdentifier,
|
|
175
|
+
payload.addressIdentifier.nickName
|
|
176
|
+
);
|
|
177
|
+
const org = await this.fetchOrganization(payload.company.taxIdentifier);
|
|
178
|
+
if (!org) {
|
|
179
|
+
return error({
|
|
180
|
+
type: "NotFound",
|
|
181
|
+
identifier: payload.company
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
const existing = (org.addressBook ?? []).find(
|
|
185
|
+
(a) => a.nickName === payload.addressIdentifier.nickName
|
|
186
|
+
);
|
|
187
|
+
if (!existing) {
|
|
188
|
+
return error({
|
|
189
|
+
type: "NotFound",
|
|
190
|
+
identifier: payload.addressIdentifier
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
const addressBook = (org.addressBook ?? []).map((a) => ({
|
|
194
|
+
...a,
|
|
195
|
+
primary: a.nickName === payload.addressIdentifier.nickName ? "1" : void 0
|
|
196
|
+
}));
|
|
197
|
+
await this.client.callPut(
|
|
198
|
+
this.getUpdateOrganizationUrl(payload.company.taxIdentifier),
|
|
199
|
+
this.getUpdateOrganizationBody(org, addressBook)
|
|
200
|
+
);
|
|
201
|
+
return success(
|
|
202
|
+
this.factory.parseCompany(
|
|
203
|
+
this.context,
|
|
204
|
+
await this.fetchOrganizationOrThrow(payload.company.taxIdentifier)
|
|
205
|
+
)
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
// ---------------------------------------------------------------------------
|
|
209
|
+
// Extension points
|
|
210
|
+
// ---------------------------------------------------------------------------
|
|
211
|
+
getOrganizationUrl(orgId) {
|
|
212
|
+
return `${this.client.transactionBaseUrl}/organization/${encodeURIComponent(orgId)}`;
|
|
213
|
+
}
|
|
214
|
+
getOrganizationDetailsParams() {
|
|
215
|
+
const params = new URLSearchParams();
|
|
216
|
+
params.set("profileName", "IBM_Organization_Details");
|
|
217
|
+
return params;
|
|
218
|
+
}
|
|
219
|
+
getOrganizationListUrl() {
|
|
220
|
+
return `${this.client.transactionBaseUrl}/organization`;
|
|
221
|
+
}
|
|
222
|
+
getOrganizationListParams(query) {
|
|
223
|
+
const { pageSize, pageNumber } = query.search.paginationOptions;
|
|
224
|
+
const params = new URLSearchParams();
|
|
225
|
+
params.set("q", "organizationsICanAdmin");
|
|
226
|
+
params.set("profileName", "IBM_Organization_List_Summary");
|
|
227
|
+
params.set("maxResults", String(pageSize));
|
|
228
|
+
params.set("startIndex", String((pageNumber - 1) * pageSize));
|
|
229
|
+
return params;
|
|
230
|
+
}
|
|
231
|
+
getUpdateOrganizationUrl(orgId) {
|
|
232
|
+
return `${this.client.transactionBaseUrl}/organization/${encodeURIComponent(orgId)}`;
|
|
233
|
+
}
|
|
234
|
+
getUpdateOrganizationBody(org, addressBook) {
|
|
235
|
+
return { addressBook };
|
|
236
|
+
}
|
|
237
|
+
// ---------------------------------------------------------------------------
|
|
238
|
+
// Internal helpers
|
|
239
|
+
// ---------------------------------------------------------------------------
|
|
240
|
+
async fetchOrganization(orgId) {
|
|
241
|
+
return this.client.callGet(
|
|
242
|
+
this.getOrganizationUrl(orgId),
|
|
243
|
+
this.getOrganizationDetailsParams(),
|
|
244
|
+
{ allowUndefined: true }
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
async fetchOrganizationOrThrow(orgId) {
|
|
248
|
+
const org = await this.fetchOrganization(orgId);
|
|
249
|
+
if (!org)
|
|
250
|
+
throw new Error(`Organization not found: ${orgId}`);
|
|
251
|
+
return org;
|
|
252
|
+
}
|
|
253
|
+
toOrgContact(address) {
|
|
254
|
+
const lines = [address.streetAddress, address.streetNumber].filter(
|
|
255
|
+
Boolean
|
|
256
|
+
);
|
|
257
|
+
return {
|
|
258
|
+
nickName: address.identifier.nickName,
|
|
259
|
+
firstName: address.firstName,
|
|
260
|
+
lastName: address.lastName,
|
|
261
|
+
addressLine: lines.length ? lines : void 0,
|
|
262
|
+
city: address.city,
|
|
263
|
+
stateOrProvinceName: address.region,
|
|
264
|
+
zipCode: address.postalCode,
|
|
265
|
+
country: address.countryCode
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
__decorateClass([
|
|
270
|
+
Reactionary({
|
|
271
|
+
inputSchema: CompanyQueryByIdSchema,
|
|
272
|
+
outputSchema: CompanyPaginatedListSchema
|
|
273
|
+
})
|
|
274
|
+
], HclCompanyCapability.prototype, "getById", 1);
|
|
275
|
+
__decorateClass([
|
|
276
|
+
Reactionary({
|
|
277
|
+
inputSchema: CompanyQueryListSchema,
|
|
278
|
+
outputSchema: CompanyPaginatedListSchema
|
|
279
|
+
})
|
|
280
|
+
], HclCompanyCapability.prototype, "listCompanies", 1);
|
|
281
|
+
__decorateClass([
|
|
282
|
+
Reactionary({
|
|
283
|
+
inputSchema: CompanyMutationAddShippingAddressSchema,
|
|
284
|
+
outputSchema: CompanyPaginatedListSchema
|
|
285
|
+
})
|
|
286
|
+
], HclCompanyCapability.prototype, "addShippingAddress", 1);
|
|
287
|
+
__decorateClass([
|
|
288
|
+
Reactionary({
|
|
289
|
+
inputSchema: CompanyMutationUpdateShippingAddressSchema,
|
|
290
|
+
outputSchema: CompanyPaginatedListSchema
|
|
291
|
+
})
|
|
292
|
+
], HclCompanyCapability.prototype, "updateShippingAddress", 1);
|
|
293
|
+
__decorateClass([
|
|
294
|
+
Reactionary({
|
|
295
|
+
inputSchema: CompanyMutationRemoveShippingAddressSchema,
|
|
296
|
+
outputSchema: CompanyPaginatedListSchema
|
|
297
|
+
})
|
|
298
|
+
], HclCompanyCapability.prototype, "removeShippingAddress", 1);
|
|
299
|
+
__decorateClass([
|
|
300
|
+
Reactionary({
|
|
301
|
+
inputSchema: CompanyMutationMakeShippingAddressDefaultSchema,
|
|
302
|
+
outputSchema: CompanyPaginatedListSchema
|
|
303
|
+
})
|
|
304
|
+
], HclCompanyCapability.prototype, "makeShippingAddressDefault", 1);
|
|
305
|
+
export {
|
|
306
|
+
HclCompanyCapability
|
|
307
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
PersonalizationProfileCapability,
|
|
14
|
+
PersonalizationProfileQueryGetProfileSchema,
|
|
15
|
+
PersonalizationProfileSchema,
|
|
16
|
+
Reactionary,
|
|
17
|
+
success
|
|
18
|
+
} from "@reactionary/core";
|
|
19
|
+
import { SESSION_KEY_PERSONALIZATION_ID } from "../core/session-keys.js";
|
|
20
|
+
class HclPersonalizationProfileCapability extends PersonalizationProfileCapability {
|
|
21
|
+
constructor(cache, context, config, client, factory) {
|
|
22
|
+
super(cache, context);
|
|
23
|
+
this.config = config;
|
|
24
|
+
this.client = client;
|
|
25
|
+
this.factory = factory;
|
|
26
|
+
}
|
|
27
|
+
async getPersonalizationProfile(_payload) {
|
|
28
|
+
const personalizationId = this.context.session[SESSION_KEY_PERSONALIZATION_ID];
|
|
29
|
+
if (!personalizationId) {
|
|
30
|
+
return success(
|
|
31
|
+
this.factory.parsePersonalizationProfile(this.context, {
|
|
32
|
+
personalizationId: "anonymous",
|
|
33
|
+
segments: []
|
|
34
|
+
})
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
const segments = await this.fetchSegments(personalizationId);
|
|
38
|
+
return success(
|
|
39
|
+
this.factory.parsePersonalizationProfile(this.context, {
|
|
40
|
+
personalizationId,
|
|
41
|
+
segments
|
|
42
|
+
})
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Calls the WCS segment API to retrieve the customer segments for the given
|
|
47
|
+
* personalization ID. Returns an empty array when the API call fails or
|
|
48
|
+
* returns no segment data.
|
|
49
|
+
*/
|
|
50
|
+
async fetchSegments(personalizationId) {
|
|
51
|
+
try {
|
|
52
|
+
const response = await this.client.callGet(
|
|
53
|
+
this.getSegmentsUrl(),
|
|
54
|
+
this.getSegmentsParams(personalizationId),
|
|
55
|
+
{ allowUndefined: true }
|
|
56
|
+
);
|
|
57
|
+
if (!response?.MemberGroup) {
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
return response.MemberGroup.map((g) => g.displayName?.value).filter(
|
|
61
|
+
(name) => name !== void 0 && name.length > 0
|
|
62
|
+
);
|
|
63
|
+
} catch {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
getSegmentsUrl() {
|
|
68
|
+
return `${this.client.transactionBaseUrl}/segment`;
|
|
69
|
+
}
|
|
70
|
+
getSegmentsParams(personalizationId) {
|
|
71
|
+
const params = new URLSearchParams();
|
|
72
|
+
params.set("q", "byPersonalizationId");
|
|
73
|
+
params.set("qPersonalizationId", personalizationId);
|
|
74
|
+
return params;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
__decorateClass([
|
|
78
|
+
Reactionary({
|
|
79
|
+
inputSchema: PersonalizationProfileQueryGetProfileSchema,
|
|
80
|
+
outputSchema: PersonalizationProfileSchema
|
|
81
|
+
})
|
|
82
|
+
], HclPersonalizationProfileCapability.prototype, "getPersonalizationProfile", 1);
|
|
83
|
+
export {
|
|
84
|
+
HclPersonalizationProfileCapability
|
|
85
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
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
|
+
ProductAssociationsCapability,
|
|
14
|
+
ProductAssociationSchema,
|
|
15
|
+
ProductAssociationsGetAccessoriesQuerySchema,
|
|
16
|
+
ProductAssociationsGetSparepartsQuerySchema,
|
|
17
|
+
ProductAssociationsGetReplacementsQuerySchema,
|
|
18
|
+
Reactionary,
|
|
19
|
+
success
|
|
20
|
+
} from "@reactionary/core";
|
|
21
|
+
import * as z from "zod";
|
|
22
|
+
class HclProductAssociationsCapability extends ProductAssociationsCapability {
|
|
23
|
+
constructor(cache, context, config, client, factory) {
|
|
24
|
+
super(cache, context);
|
|
25
|
+
this.config = config;
|
|
26
|
+
this.client = client;
|
|
27
|
+
this.factory = factory;
|
|
28
|
+
}
|
|
29
|
+
async getAccessories(query) {
|
|
30
|
+
const associations = await this.fetchAssociations(
|
|
31
|
+
query.forProduct.key,
|
|
32
|
+
this.config.associationTypes.accessories,
|
|
33
|
+
query.numberOfAccessories
|
|
34
|
+
);
|
|
35
|
+
return success(
|
|
36
|
+
associations.map((a) => this.factory.parseAssociation(this.context, a))
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
async getSpareparts(query) {
|
|
40
|
+
const associations = await this.fetchAssociations(
|
|
41
|
+
query.forProduct.key,
|
|
42
|
+
this.config.associationTypes.spareparts,
|
|
43
|
+
query.numberOfSpareparts
|
|
44
|
+
);
|
|
45
|
+
return success(
|
|
46
|
+
associations.map((a) => this.factory.parseAssociation(this.context, a))
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
async getReplacements(query) {
|
|
50
|
+
const associations = await this.fetchAssociations(
|
|
51
|
+
query.forProduct.key,
|
|
52
|
+
this.config.associationTypes.replacements,
|
|
53
|
+
query.numberOfReplacements
|
|
54
|
+
);
|
|
55
|
+
return success(
|
|
56
|
+
associations.map((a) => this.factory.parseAssociation(this.context, a))
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Fetches the product detail for the given part number and returns the
|
|
61
|
+
* `merchandisingAssociations` filtered to the requested association types,
|
|
62
|
+
* capped at `maxCount`.
|
|
63
|
+
*/
|
|
64
|
+
async fetchAssociations(partNumber, types, maxCount) {
|
|
65
|
+
const response = await this.client.callGet(
|
|
66
|
+
this.getProductsUrl(),
|
|
67
|
+
this.getProductsParams(partNumber)
|
|
68
|
+
);
|
|
69
|
+
const product = (response.contents ?? response.catalogEntryView ?? [])[0];
|
|
70
|
+
if (!product?.merchandisingAssociations) {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
return product.merchandisingAssociations.filter(
|
|
74
|
+
(a) => a.associationCodeType && types.includes(a.associationCodeType)
|
|
75
|
+
).slice(0, maxCount);
|
|
76
|
+
}
|
|
77
|
+
getProductsUrl() {
|
|
78
|
+
return `${this.client.catalogBaseUrl}/api/v2/products`;
|
|
79
|
+
}
|
|
80
|
+
getProductsParams(partNumber) {
|
|
81
|
+
const params = new URLSearchParams();
|
|
82
|
+
params.set("storeId", this.config.storeId);
|
|
83
|
+
params.append("partNumber", partNumber);
|
|
84
|
+
params.set("profileName", this.config.profiles.product);
|
|
85
|
+
return params;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
__decorateClass([
|
|
89
|
+
Reactionary({
|
|
90
|
+
inputSchema: ProductAssociationsGetAccessoriesQuerySchema,
|
|
91
|
+
outputSchema: z.array(ProductAssociationSchema),
|
|
92
|
+
cache: true,
|
|
93
|
+
cacheTimeToLiveInSeconds: 300,
|
|
94
|
+
currencyDependentCaching: false
|
|
95
|
+
})
|
|
96
|
+
], HclProductAssociationsCapability.prototype, "getAccessories", 1);
|
|
97
|
+
__decorateClass([
|
|
98
|
+
Reactionary({
|
|
99
|
+
inputSchema: ProductAssociationsGetSparepartsQuerySchema,
|
|
100
|
+
outputSchema: z.array(ProductAssociationSchema),
|
|
101
|
+
cache: true,
|
|
102
|
+
cacheTimeToLiveInSeconds: 300,
|
|
103
|
+
currencyDependentCaching: false
|
|
104
|
+
})
|
|
105
|
+
], HclProductAssociationsCapability.prototype, "getSpareparts", 1);
|
|
106
|
+
__decorateClass([
|
|
107
|
+
Reactionary({
|
|
108
|
+
inputSchema: ProductAssociationsGetReplacementsQuerySchema,
|
|
109
|
+
outputSchema: z.array(ProductAssociationSchema),
|
|
110
|
+
cache: true,
|
|
111
|
+
cacheTimeToLiveInSeconds: 300,
|
|
112
|
+
currencyDependentCaching: false
|
|
113
|
+
})
|
|
114
|
+
], HclProductAssociationsCapability.prototype, "getReplacements", 1);
|
|
115
|
+
export {
|
|
116
|
+
HclProductAssociationsCapability
|
|
117
|
+
};
|