@reactionary/hcl 0.9.7 → 0.9.11
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/cart.capability.js +411 -0
- package/capabilities/category.capability.js +111 -35
- package/capabilities/checkout.capability.js +337 -0
- package/capabilities/identity.capability.js +214 -0
- package/capabilities/inventory.capability.js +75 -0
- package/capabilities/price.capability.js +122 -0
- package/capabilities/product-search.capability.js +64 -13
- package/capabilities/product.capability.js +114 -28
- package/core/client.js +80 -89
- package/core/initialize.js +111 -5
- package/core/session-keys.js +14 -0
- package/factories/cart/cart.factory.js +135 -0
- package/factories/checkout/checkout.factory.js +131 -0
- package/factories/identity/identity.factory.js +12 -0
- package/factories/index.js +5 -0
- package/factories/inventory/inventory.factory.js +38 -0
- package/factories/price/price.factory.js +39 -0
- package/index.js +5 -0
- package/package.json +8 -5
- package/schema/capabilities.schema.js +2 -0
- package/schema/configuration.schema.js +12 -1
- package/src/capabilities/cart.capability.d.ts +69 -0
- package/src/capabilities/category.capability.d.ts +20 -1
- package/src/capabilities/checkout.capability.d.ts +41 -0
- package/src/capabilities/identity.capability.d.ts +29 -0
- package/src/capabilities/inventory.capability.d.ts +13 -0
- package/src/capabilities/price.capability.d.ts +16 -0
- package/src/capabilities/product-search.capability.d.ts +7 -2
- package/src/capabilities/product.capability.d.ts +14 -2
- package/src/core/client.d.ts +41 -14
- package/src/core/initialize.types.d.ts +11 -5
- package/src/core/session-keys.d.ts +14 -0
- package/src/factories/cart/cart.factory.d.ts +11 -0
- package/src/factories/checkout/checkout.factory.d.ts +11 -0
- package/src/factories/identity/identity.factory.d.ts +7 -0
- package/src/factories/index.d.ts +5 -0
- package/src/factories/inventory/inventory.factory.d.ts +29 -0
- package/src/factories/price/price.factory.d.ts +26 -0
- package/src/index.d.ts +5 -0
- package/src/schema/capabilities.schema.d.ts +7 -1
- package/src/schema/configuration.schema.d.ts +7 -0
- package/src/schema/hcl.schema.d.ts +321 -0
- package/src/test/test-utils.d.ts +2 -1
- package/test/test-utils.js +6 -1
|
@@ -0,0 +1,337 @@
|
|
|
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
|
+
CheckoutCapability,
|
|
14
|
+
CheckoutMutationAddPaymentInstructionSchema,
|
|
15
|
+
CheckoutMutationFinalizeCheckoutSchema,
|
|
16
|
+
CheckoutMutationInitiateCheckoutSchema,
|
|
17
|
+
CheckoutMutationRemovePaymentInstructionSchema,
|
|
18
|
+
CheckoutMutationSetShippingAddressSchema,
|
|
19
|
+
CheckoutMutationSetShippingInstructionSchema,
|
|
20
|
+
CheckoutQueryByIdSchema,
|
|
21
|
+
CheckoutQueryForAvailablePaymentMethodsSchema,
|
|
22
|
+
CheckoutQueryForAvailableShippingMethodsSchema,
|
|
23
|
+
CheckoutSchema,
|
|
24
|
+
PaymentMethodSchema,
|
|
25
|
+
ShippingMethodSchema,
|
|
26
|
+
Reactionary,
|
|
27
|
+
error,
|
|
28
|
+
success
|
|
29
|
+
} from "@reactionary/core";
|
|
30
|
+
import { HclCartNotFoundError } from "../core/client.js";
|
|
31
|
+
import createDebug from "debug";
|
|
32
|
+
const debug = createDebug("reactionary:hcl:checkout");
|
|
33
|
+
class HclCheckoutCapability extends CheckoutCapability {
|
|
34
|
+
config;
|
|
35
|
+
client;
|
|
36
|
+
factory;
|
|
37
|
+
constructor(cache, context, config, client, factory) {
|
|
38
|
+
super(cache, context);
|
|
39
|
+
this.config = config;
|
|
40
|
+
this.client = client;
|
|
41
|
+
this.factory = factory;
|
|
42
|
+
}
|
|
43
|
+
cartUrl() {
|
|
44
|
+
return `${this.client.transactionBaseUrl}/cart/@self`;
|
|
45
|
+
}
|
|
46
|
+
/** Fetch the raw WCS cart response. Throws HclCartNotFoundError on 404. */
|
|
47
|
+
async fetchWcsCart() {
|
|
48
|
+
const data = await this.client.callGet(
|
|
49
|
+
this.cartUrl(),
|
|
50
|
+
void 0,
|
|
51
|
+
{ allowUndefined: true }
|
|
52
|
+
);
|
|
53
|
+
if (!data)
|
|
54
|
+
throw new HclCartNotFoundError();
|
|
55
|
+
return data;
|
|
56
|
+
}
|
|
57
|
+
/** Fetch and parse the active cart as a Checkout. */
|
|
58
|
+
async fetchCheckout(extra) {
|
|
59
|
+
const data = await this.fetchWcsCart();
|
|
60
|
+
return this.factory.parseCheckout(this.context, { ...data, ...extra });
|
|
61
|
+
}
|
|
62
|
+
paymentMethodsUrl() {
|
|
63
|
+
return `${this.client.transactionBaseUrl}/cart/@self/payment_instruction/eligible_payment_info`;
|
|
64
|
+
}
|
|
65
|
+
addPaymentInstructionUrl() {
|
|
66
|
+
return `${this.client.transactionBaseUrl}/cart/@self/payment_instruction`;
|
|
67
|
+
}
|
|
68
|
+
deletePaymentInstructionUrl(piId) {
|
|
69
|
+
return `${this.client.transactionBaseUrl}/cart/@self/payment_instruction/${encodeURIComponent(piId)}`;
|
|
70
|
+
}
|
|
71
|
+
shippingModesUrl() {
|
|
72
|
+
return `${this.client.transactionBaseUrl}/cart/@self/shipping_info`;
|
|
73
|
+
}
|
|
74
|
+
shippingModesParams() {
|
|
75
|
+
const params = new URLSearchParams();
|
|
76
|
+
params.set("profileName", "IBM_usableShippingMode");
|
|
77
|
+
return params;
|
|
78
|
+
}
|
|
79
|
+
shippingInfoUrl() {
|
|
80
|
+
return `${this.client.transactionBaseUrl}/cart/@self/shipping_info`;
|
|
81
|
+
}
|
|
82
|
+
setShippingInfoPayload(body) {
|
|
83
|
+
return {
|
|
84
|
+
...body,
|
|
85
|
+
x_calculateOrder: "1",
|
|
86
|
+
x_calculationUsage: "-1,-2,-3,-4,-5,-6,-7"
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
setShippingAddressForItemsPayload(orderItemIds, addr) {
|
|
90
|
+
return {
|
|
91
|
+
x_calculateOrder: "1",
|
|
92
|
+
x_calculationUsage: "-1,-2,-3,-4,-5,-6,-7",
|
|
93
|
+
orderItem: orderItemIds.map((orderItemId) => ({
|
|
94
|
+
orderItemId,
|
|
95
|
+
x_addr: addr
|
|
96
|
+
}))
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
precheckoutUrl() {
|
|
100
|
+
return `${this.client.transactionBaseUrl}/cart/@self/precheckout`;
|
|
101
|
+
}
|
|
102
|
+
submitCheckoutUrl() {
|
|
103
|
+
return `${this.client.transactionBaseUrl}/cart/@self/checkout`;
|
|
104
|
+
}
|
|
105
|
+
async initiateCheckoutForCart(payload) {
|
|
106
|
+
debug("initiateCheckoutForCart orderId=%s", payload.cart.identifier.key);
|
|
107
|
+
if (payload.notificationEmail) {
|
|
108
|
+
this.context.session["hcl.notificationEmail"] = payload.notificationEmail;
|
|
109
|
+
}
|
|
110
|
+
if (payload.billingAddress) {
|
|
111
|
+
const addr = payload.billingAddress;
|
|
112
|
+
const methodsResp = await this.client.callGet(
|
|
113
|
+
this.paymentMethodsUrl(),
|
|
114
|
+
void 0,
|
|
115
|
+
{ allowUndefined: true }
|
|
116
|
+
) ?? { usablePaymentInformation: [] };
|
|
117
|
+
const firstMethod = methodsResp.usablePaymentInformation?.[0];
|
|
118
|
+
if (firstMethod) {
|
|
119
|
+
const cartData = await this.fetchWcsCart();
|
|
120
|
+
for (const pi of cartData.paymentInstruction ?? []) {
|
|
121
|
+
await this.client.callDelete(
|
|
122
|
+
this.deletePaymentInstructionUrl(pi.piId),
|
|
123
|
+
{
|
|
124
|
+
ignore404: true
|
|
125
|
+
}
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
await this.client.callPost(
|
|
129
|
+
this.addPaymentInstructionUrl(),
|
|
130
|
+
{
|
|
131
|
+
payMethodId: firstMethod.paymentMethodName,
|
|
132
|
+
billto_firstName: addr.firstName,
|
|
133
|
+
billto_lastName: addr.lastName,
|
|
134
|
+
billto_address1: `${addr.streetAddress} ${addr.streetNumber}`.trim(),
|
|
135
|
+
billto_city: addr.city,
|
|
136
|
+
billto_state: addr.region,
|
|
137
|
+
billto_zipCode: addr.postalCode,
|
|
138
|
+
billto_country: addr.countryCode,
|
|
139
|
+
billto_phone1: payload.notificationPhone,
|
|
140
|
+
billto_email1: payload.notificationEmail
|
|
141
|
+
}
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return success(await this.fetchCheckout());
|
|
146
|
+
}
|
|
147
|
+
async getById(_payload) {
|
|
148
|
+
debug("getById");
|
|
149
|
+
try {
|
|
150
|
+
return success(await this.fetchCheckout());
|
|
151
|
+
} catch (err) {
|
|
152
|
+
if (err instanceof HclCartNotFoundError) {
|
|
153
|
+
return error({ type: "NotFound", identifier: "" });
|
|
154
|
+
}
|
|
155
|
+
throw err;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async setShippingAddress(payload) {
|
|
159
|
+
debug("setShippingAddress");
|
|
160
|
+
const cartData = await this.fetchWcsCart();
|
|
161
|
+
const orderItemIds = (cartData.orderItem ?? []).map((i) => i.orderItemId);
|
|
162
|
+
const addr = payload.shippingAddress;
|
|
163
|
+
await this.client.callPut(
|
|
164
|
+
this.shippingInfoUrl(),
|
|
165
|
+
this.setShippingAddressForItemsPayload(orderItemIds, {
|
|
166
|
+
firstName: addr.firstName,
|
|
167
|
+
lastName: addr.lastName,
|
|
168
|
+
address1: `${addr.streetAddress} ${addr.streetNumber}`.trim(),
|
|
169
|
+
city: addr.city,
|
|
170
|
+
state: addr.region,
|
|
171
|
+
zipCode: addr.postalCode,
|
|
172
|
+
country: addr.countryCode
|
|
173
|
+
})
|
|
174
|
+
);
|
|
175
|
+
return success(await this.fetchCheckout());
|
|
176
|
+
}
|
|
177
|
+
async getAvailableShippingMethods(_payload) {
|
|
178
|
+
debug("getAvailableShippingMethods");
|
|
179
|
+
const response = await this.client.callGet(
|
|
180
|
+
this.shippingModesUrl(),
|
|
181
|
+
this.shippingModesParams()
|
|
182
|
+
);
|
|
183
|
+
let modes;
|
|
184
|
+
if (response.usableShippingMode && response.usableShippingMode.length > 0) {
|
|
185
|
+
modes = response.usableShippingMode.map(
|
|
186
|
+
(mode) => this.factory.parseShippingMethod(this.context, mode)
|
|
187
|
+
);
|
|
188
|
+
} else {
|
|
189
|
+
const seen = /* @__PURE__ */ new Set();
|
|
190
|
+
modes = [];
|
|
191
|
+
for (const item of response.orderItem ?? []) {
|
|
192
|
+
if (item.shipModeId && !seen.has(item.shipModeId)) {
|
|
193
|
+
seen.add(item.shipModeId);
|
|
194
|
+
modes.push(
|
|
195
|
+
this.factory.parseShippingMethod(this.context, {
|
|
196
|
+
shipModeId: item.shipModeId,
|
|
197
|
+
carrier: item.carrier ?? "",
|
|
198
|
+
shipModeCode: item.shipModeCode ?? "",
|
|
199
|
+
shipModeDescription: item.shipModeDescription ?? item.description ?? item.shipModeCode ?? ""
|
|
200
|
+
})
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return success(modes);
|
|
206
|
+
}
|
|
207
|
+
async getAvailablePaymentMethods(_payload) {
|
|
208
|
+
debug("getAvailablePaymentMethods");
|
|
209
|
+
const response = await this.client.callGet(
|
|
210
|
+
this.paymentMethodsUrl(),
|
|
211
|
+
void 0,
|
|
212
|
+
{ allowUndefined: true }
|
|
213
|
+
) ?? { usablePaymentInformation: [] };
|
|
214
|
+
const methods = (response.usablePaymentInformation ?? []).map(
|
|
215
|
+
(method) => this.factory.parsePaymentMethod(this.context, method)
|
|
216
|
+
);
|
|
217
|
+
return success(methods);
|
|
218
|
+
}
|
|
219
|
+
async addPaymentInstruction(payload) {
|
|
220
|
+
debug(
|
|
221
|
+
"addPaymentInstruction payMethodId=%s",
|
|
222
|
+
payload.paymentInstruction.paymentMethod.method
|
|
223
|
+
);
|
|
224
|
+
await this.client.callPost(
|
|
225
|
+
this.addPaymentInstructionUrl(),
|
|
226
|
+
{
|
|
227
|
+
payMethodId: payload.paymentInstruction.paymentMethod.method,
|
|
228
|
+
piAmount: payload.paymentInstruction.amount ? String(payload.paymentInstruction.amount.value) : void 0,
|
|
229
|
+
protocolData: payload.paymentInstruction.protocolData.map((pd) => ({
|
|
230
|
+
name: pd.key,
|
|
231
|
+
value: pd.value
|
|
232
|
+
}))
|
|
233
|
+
}
|
|
234
|
+
);
|
|
235
|
+
return success(await this.fetchCheckout());
|
|
236
|
+
}
|
|
237
|
+
async removePaymentInstruction(payload) {
|
|
238
|
+
debug("removePaymentInstruction piId=%s", payload.paymentInstruction.key);
|
|
239
|
+
await this.client.callDelete(
|
|
240
|
+
this.deletePaymentInstructionUrl(payload.paymentInstruction.key),
|
|
241
|
+
{
|
|
242
|
+
ignore404: true
|
|
243
|
+
}
|
|
244
|
+
);
|
|
245
|
+
return success(await this.fetchCheckout());
|
|
246
|
+
}
|
|
247
|
+
async setShippingInstruction(payload) {
|
|
248
|
+
debug(
|
|
249
|
+
"setShippingInstruction shipModeId=%s",
|
|
250
|
+
payload.shippingInstruction.shippingMethod.key
|
|
251
|
+
);
|
|
252
|
+
const cartData = await this.fetchWcsCart();
|
|
253
|
+
const orderItemIds = (cartData.orderItem ?? []).map((i) => i.orderItemId);
|
|
254
|
+
await this.client.callPut(
|
|
255
|
+
this.shippingInfoUrl(),
|
|
256
|
+
this.setShippingInfoPayload({
|
|
257
|
+
shipModeId: payload.shippingInstruction.shippingMethod.key,
|
|
258
|
+
orderItemId: orderItemIds
|
|
259
|
+
})
|
|
260
|
+
);
|
|
261
|
+
return success(await this.fetchCheckout());
|
|
262
|
+
}
|
|
263
|
+
async finalizeCheckout(_payload) {
|
|
264
|
+
debug("finalizeCheckout");
|
|
265
|
+
const cartBefore = await this.fetchWcsCart();
|
|
266
|
+
await this.client.callPut(
|
|
267
|
+
this.precheckoutUrl(),
|
|
268
|
+
{}
|
|
269
|
+
);
|
|
270
|
+
const result = await this.client.callPost(
|
|
271
|
+
this.submitCheckoutUrl(),
|
|
272
|
+
{}
|
|
273
|
+
);
|
|
274
|
+
const checkout = this.factory.parseCheckout(this.context, {
|
|
275
|
+
...cartBefore,
|
|
276
|
+
resultingOrderKey: result.orderId
|
|
277
|
+
});
|
|
278
|
+
return success(checkout);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
__decorateClass([
|
|
282
|
+
Reactionary({
|
|
283
|
+
inputSchema: CheckoutMutationInitiateCheckoutSchema,
|
|
284
|
+
outputSchema: CheckoutSchema
|
|
285
|
+
})
|
|
286
|
+
], HclCheckoutCapability.prototype, "initiateCheckoutForCart", 1);
|
|
287
|
+
__decorateClass([
|
|
288
|
+
Reactionary({
|
|
289
|
+
inputSchema: CheckoutQueryByIdSchema,
|
|
290
|
+
outputSchema: CheckoutSchema
|
|
291
|
+
})
|
|
292
|
+
], HclCheckoutCapability.prototype, "getById", 1);
|
|
293
|
+
__decorateClass([
|
|
294
|
+
Reactionary({
|
|
295
|
+
inputSchema: CheckoutMutationSetShippingAddressSchema,
|
|
296
|
+
outputSchema: CheckoutSchema
|
|
297
|
+
})
|
|
298
|
+
], HclCheckoutCapability.prototype, "setShippingAddress", 1);
|
|
299
|
+
__decorateClass([
|
|
300
|
+
Reactionary({
|
|
301
|
+
inputSchema: CheckoutQueryForAvailableShippingMethodsSchema,
|
|
302
|
+
outputSchema: ShippingMethodSchema.array()
|
|
303
|
+
})
|
|
304
|
+
], HclCheckoutCapability.prototype, "getAvailableShippingMethods", 1);
|
|
305
|
+
__decorateClass([
|
|
306
|
+
Reactionary({
|
|
307
|
+
inputSchema: CheckoutQueryForAvailablePaymentMethodsSchema,
|
|
308
|
+
outputSchema: PaymentMethodSchema.array()
|
|
309
|
+
})
|
|
310
|
+
], HclCheckoutCapability.prototype, "getAvailablePaymentMethods", 1);
|
|
311
|
+
__decorateClass([
|
|
312
|
+
Reactionary({
|
|
313
|
+
inputSchema: CheckoutMutationAddPaymentInstructionSchema,
|
|
314
|
+
outputSchema: CheckoutSchema
|
|
315
|
+
})
|
|
316
|
+
], HclCheckoutCapability.prototype, "addPaymentInstruction", 1);
|
|
317
|
+
__decorateClass([
|
|
318
|
+
Reactionary({
|
|
319
|
+
inputSchema: CheckoutMutationRemovePaymentInstructionSchema,
|
|
320
|
+
outputSchema: CheckoutSchema
|
|
321
|
+
})
|
|
322
|
+
], HclCheckoutCapability.prototype, "removePaymentInstruction", 1);
|
|
323
|
+
__decorateClass([
|
|
324
|
+
Reactionary({
|
|
325
|
+
inputSchema: CheckoutMutationSetShippingInstructionSchema,
|
|
326
|
+
outputSchema: CheckoutSchema
|
|
327
|
+
})
|
|
328
|
+
], HclCheckoutCapability.prototype, "setShippingInstruction", 1);
|
|
329
|
+
__decorateClass([
|
|
330
|
+
Reactionary({
|
|
331
|
+
inputSchema: CheckoutMutationFinalizeCheckoutSchema,
|
|
332
|
+
outputSchema: CheckoutSchema
|
|
333
|
+
})
|
|
334
|
+
], HclCheckoutCapability.prototype, "finalizeCheckout", 1);
|
|
335
|
+
export {
|
|
336
|
+
HclCheckoutCapability
|
|
337
|
+
};
|
|
@@ -0,0 +1,214 @@
|
|
|
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
|
+
IdentityMutationLoginSchema,
|
|
14
|
+
IdentityMutationLogoutSchema,
|
|
15
|
+
IdentityMutationRegisterSchema,
|
|
16
|
+
IdentityQuerySelfSchema,
|
|
17
|
+
IdentityCapability,
|
|
18
|
+
IdentitySchema,
|
|
19
|
+
Reactionary,
|
|
20
|
+
success
|
|
21
|
+
} from "@reactionary/core";
|
|
22
|
+
import {
|
|
23
|
+
SESSION_KEY_WC_TOKEN,
|
|
24
|
+
SESSION_KEY_WC_TRUSTED_TOKEN,
|
|
25
|
+
SESSION_KEY_USER_ID,
|
|
26
|
+
SESSION_KEY_IDENTITY_TYPE,
|
|
27
|
+
SESSION_KEY_PERSONALIZATION_ID
|
|
28
|
+
} from "../core/session-keys.js";
|
|
29
|
+
import createDebug from "debug";
|
|
30
|
+
const debug = createDebug("reactionary:hcl:identity");
|
|
31
|
+
class HclIdentityCapability extends IdentityCapability {
|
|
32
|
+
config;
|
|
33
|
+
client;
|
|
34
|
+
factory;
|
|
35
|
+
constructor(cache, context, config, client, factory) {
|
|
36
|
+
super(cache, context);
|
|
37
|
+
this.config = config;
|
|
38
|
+
this.client = client;
|
|
39
|
+
this.factory = factory;
|
|
40
|
+
}
|
|
41
|
+
async getSelf(_payload) {
|
|
42
|
+
const wcToken = this.context.session[SESSION_KEY_WC_TOKEN];
|
|
43
|
+
if (!wcToken) {
|
|
44
|
+
debug("No WCS session token \u2014 returning anonymous identity");
|
|
45
|
+
if (!this.context.session[SESSION_KEY_PERSONALIZATION_ID]) {
|
|
46
|
+
this.context.session[SESSION_KEY_PERSONALIZATION_ID] = crypto.randomUUID().replace(/-/g, "").slice(0, 30);
|
|
47
|
+
}
|
|
48
|
+
const parsed2 = this.factory.parseIdentity(this.context, {
|
|
49
|
+
type: "Anonymous"
|
|
50
|
+
});
|
|
51
|
+
this.updateIdentityContext(parsed2);
|
|
52
|
+
return success(parsed2);
|
|
53
|
+
}
|
|
54
|
+
const identityType = this.context.session[SESSION_KEY_IDENTITY_TYPE];
|
|
55
|
+
const userId = this.context.session[SESSION_KEY_USER_ID];
|
|
56
|
+
if (identityType === "registered" && userId) {
|
|
57
|
+
debug("Registered session found for userId %s", userId);
|
|
58
|
+
const parsed2 = this.factory.parseIdentity(this.context, {
|
|
59
|
+
type: "Registered",
|
|
60
|
+
id: { userId }
|
|
61
|
+
});
|
|
62
|
+
this.updateIdentityContext(parsed2);
|
|
63
|
+
return success(parsed2);
|
|
64
|
+
}
|
|
65
|
+
if (identityType === "guest" && userId) {
|
|
66
|
+
debug("Guest session found for userId %s", userId);
|
|
67
|
+
const parsed2 = this.factory.parseIdentity(this.context, {
|
|
68
|
+
type: "Guest",
|
|
69
|
+
id: { userId }
|
|
70
|
+
});
|
|
71
|
+
this.updateIdentityContext(parsed2);
|
|
72
|
+
return success(parsed2);
|
|
73
|
+
}
|
|
74
|
+
debug("WCS token present but identity type unknown \u2014 returning anonymous");
|
|
75
|
+
const parsed = this.factory.parseIdentity(this.context, {
|
|
76
|
+
type: "Anonymous"
|
|
77
|
+
});
|
|
78
|
+
this.updateIdentityContext(parsed);
|
|
79
|
+
return success(parsed);
|
|
80
|
+
}
|
|
81
|
+
async login(payload) {
|
|
82
|
+
debug("Attempting login for user: %s", payload.username);
|
|
83
|
+
const response = await this.client.callPost(
|
|
84
|
+
this.loginIdentityUrl(),
|
|
85
|
+
this.loginIdentityPayload(payload.username, payload.password)
|
|
86
|
+
);
|
|
87
|
+
this.storeSessionTokens(
|
|
88
|
+
response.WCToken,
|
|
89
|
+
response.WCTrustedToken,
|
|
90
|
+
response.userId,
|
|
91
|
+
"registered",
|
|
92
|
+
response.personalizationID
|
|
93
|
+
);
|
|
94
|
+
const loginParsed = this.factory.parseIdentity(this.context, {
|
|
95
|
+
type: "Registered",
|
|
96
|
+
id: { userId: response.userId }
|
|
97
|
+
});
|
|
98
|
+
this.updateIdentityContext(loginParsed);
|
|
99
|
+
return success(loginParsed);
|
|
100
|
+
}
|
|
101
|
+
async logout(_payload) {
|
|
102
|
+
debug("Logging out current session");
|
|
103
|
+
const wcToken = this.context.session[SESSION_KEY_WC_TOKEN];
|
|
104
|
+
const userId = this.context.session[SESSION_KEY_USER_ID];
|
|
105
|
+
if (wcToken && userId) {
|
|
106
|
+
try {
|
|
107
|
+
await this.client.callDelete(this.deleteLoginIdentityUrl(userId), {
|
|
108
|
+
ignore404: true
|
|
109
|
+
});
|
|
110
|
+
} catch (err) {
|
|
111
|
+
debug("Server-side logout failed (non-fatal): %o", err);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
this.clearSessionTokens();
|
|
115
|
+
const logoutParsed = this.factory.parseIdentity(this.context, {
|
|
116
|
+
type: "Anonymous"
|
|
117
|
+
});
|
|
118
|
+
this.updateIdentityContext(logoutParsed);
|
|
119
|
+
return success(logoutParsed);
|
|
120
|
+
}
|
|
121
|
+
async register(payload) {
|
|
122
|
+
debug("Registering new user: %s", payload.username);
|
|
123
|
+
const guest = await this.client.callPost(
|
|
124
|
+
this.createGuestIdentityUrl()
|
|
125
|
+
);
|
|
126
|
+
this.storeSessionTokens(
|
|
127
|
+
guest.WCToken,
|
|
128
|
+
guest.WCTrustedToken,
|
|
129
|
+
guest.userId,
|
|
130
|
+
"guest",
|
|
131
|
+
guest.personalizationID
|
|
132
|
+
);
|
|
133
|
+
const registered = await this.client.callPut(
|
|
134
|
+
this.registerPersonUrl(),
|
|
135
|
+
this.registerPersonPayload(payload.username, payload.password)
|
|
136
|
+
);
|
|
137
|
+
this.storeSessionTokens(
|
|
138
|
+
registered.WCToken,
|
|
139
|
+
registered.WCTrustedToken,
|
|
140
|
+
registered.userId,
|
|
141
|
+
"registered",
|
|
142
|
+
registered.personalizationID
|
|
143
|
+
);
|
|
144
|
+
const registerParsed = this.factory.parseIdentity(this.context, {
|
|
145
|
+
type: "Registered",
|
|
146
|
+
id: { userId: registered.userId }
|
|
147
|
+
});
|
|
148
|
+
this.updateIdentityContext(registerParsed);
|
|
149
|
+
return success(registerParsed);
|
|
150
|
+
}
|
|
151
|
+
storeSessionTokens(wcToken, wcTrustedToken, userId, identityType, personalizationID) {
|
|
152
|
+
this.context.session[SESSION_KEY_WC_TOKEN] = wcToken;
|
|
153
|
+
this.context.session[SESSION_KEY_WC_TRUSTED_TOKEN] = wcTrustedToken;
|
|
154
|
+
this.context.session[SESSION_KEY_USER_ID] = userId;
|
|
155
|
+
this.context.session[SESSION_KEY_IDENTITY_TYPE] = identityType;
|
|
156
|
+
if (personalizationID) {
|
|
157
|
+
this.context.session[SESSION_KEY_PERSONALIZATION_ID] = personalizationID;
|
|
158
|
+
} else {
|
|
159
|
+
delete this.context.session[SESSION_KEY_PERSONALIZATION_ID];
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
clearSessionTokens() {
|
|
163
|
+
delete this.context.session[SESSION_KEY_WC_TOKEN];
|
|
164
|
+
delete this.context.session[SESSION_KEY_WC_TRUSTED_TOKEN];
|
|
165
|
+
delete this.context.session[SESSION_KEY_USER_ID];
|
|
166
|
+
delete this.context.session[SESSION_KEY_IDENTITY_TYPE];
|
|
167
|
+
delete this.context.session[SESSION_KEY_PERSONALIZATION_ID];
|
|
168
|
+
}
|
|
169
|
+
loginIdentityUrl() {
|
|
170
|
+
return `${this.client.transactionBaseUrl}/loginidentity`;
|
|
171
|
+
}
|
|
172
|
+
loginIdentityPayload(logonId, logonPassword) {
|
|
173
|
+
return { logonId, logonPassword };
|
|
174
|
+
}
|
|
175
|
+
deleteLoginIdentityUrl(userId) {
|
|
176
|
+
return `${this.client.transactionBaseUrl}/loginidentity/${encodeURIComponent(userId)}`;
|
|
177
|
+
}
|
|
178
|
+
createGuestIdentityUrl() {
|
|
179
|
+
return `${this.client.transactionBaseUrl}/guestidentity`;
|
|
180
|
+
}
|
|
181
|
+
registerPersonUrl() {
|
|
182
|
+
return `${this.client.transactionBaseUrl}/person/@self`;
|
|
183
|
+
}
|
|
184
|
+
registerPersonPayload(logonId, logonPassword) {
|
|
185
|
+
return { logonId, logonPassword, logonPasswordVerify: logonPassword };
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
__decorateClass([
|
|
189
|
+
Reactionary({
|
|
190
|
+
inputSchema: IdentityQuerySelfSchema,
|
|
191
|
+
outputSchema: IdentitySchema
|
|
192
|
+
})
|
|
193
|
+
], HclIdentityCapability.prototype, "getSelf", 1);
|
|
194
|
+
__decorateClass([
|
|
195
|
+
Reactionary({
|
|
196
|
+
inputSchema: IdentityMutationLoginSchema,
|
|
197
|
+
outputSchema: IdentitySchema
|
|
198
|
+
})
|
|
199
|
+
], HclIdentityCapability.prototype, "login", 1);
|
|
200
|
+
__decorateClass([
|
|
201
|
+
Reactionary({
|
|
202
|
+
inputSchema: IdentityMutationLogoutSchema,
|
|
203
|
+
outputSchema: IdentitySchema
|
|
204
|
+
})
|
|
205
|
+
], HclIdentityCapability.prototype, "logout", 1);
|
|
206
|
+
__decorateClass([
|
|
207
|
+
Reactionary({
|
|
208
|
+
inputSchema: IdentityMutationRegisterSchema,
|
|
209
|
+
outputSchema: IdentitySchema
|
|
210
|
+
})
|
|
211
|
+
], HclIdentityCapability.prototype, "register", 1);
|
|
212
|
+
export {
|
|
213
|
+
HclIdentityCapability
|
|
214
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
InventoryCapability,
|
|
14
|
+
InventoryQueryBySKUSchema,
|
|
15
|
+
InventorySchema,
|
|
16
|
+
Reactionary,
|
|
17
|
+
error,
|
|
18
|
+
success
|
|
19
|
+
} from "@reactionary/core";
|
|
20
|
+
class HclInventoryCapability extends InventoryCapability {
|
|
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 getBySKU(payload) {
|
|
28
|
+
const { sku } = payload.variant;
|
|
29
|
+
const centreKey = payload.fulfilmentCenter.key;
|
|
30
|
+
const response = await this.client.callGet(
|
|
31
|
+
this.getBySKUUrl(payload),
|
|
32
|
+
this.getBySKUPayload(payload)
|
|
33
|
+
);
|
|
34
|
+
const items = response.InventoryAvailability ?? [];
|
|
35
|
+
let item = items.find(
|
|
36
|
+
(i) => centreKey ? i.physicalStoreName === centreKey : !i.physicalStoreId
|
|
37
|
+
);
|
|
38
|
+
if (!item && items.length > 0) {
|
|
39
|
+
item = items[0];
|
|
40
|
+
}
|
|
41
|
+
if (!item) {
|
|
42
|
+
const notFound = {
|
|
43
|
+
type: "NotFound",
|
|
44
|
+
identifier: { variant: { sku }, fulfillmentCenter: { key: centreKey } }
|
|
45
|
+
};
|
|
46
|
+
return error(notFound);
|
|
47
|
+
}
|
|
48
|
+
return success(
|
|
49
|
+
this.factory.parseInventory(this.context, {
|
|
50
|
+
item,
|
|
51
|
+
sku,
|
|
52
|
+
fulfilmentCenterKey: centreKey
|
|
53
|
+
})
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
getBySKUUrl(payload) {
|
|
57
|
+
return `${this.client.transactionBaseUrl}/inventoryavailability/byPartNumber/${encodeURIComponent(payload.variant.sku)}`;
|
|
58
|
+
}
|
|
59
|
+
getBySKUPayload(payload) {
|
|
60
|
+
const params = new URLSearchParams();
|
|
61
|
+
const centreKey = payload.fulfilmentCenter.key;
|
|
62
|
+
if (centreKey)
|
|
63
|
+
params.set("physicalStoreName", centreKey);
|
|
64
|
+
return params;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
__decorateClass([
|
|
68
|
+
Reactionary({
|
|
69
|
+
inputSchema: InventoryQueryBySKUSchema,
|
|
70
|
+
outputSchema: InventorySchema
|
|
71
|
+
})
|
|
72
|
+
], HclInventoryCapability.prototype, "getBySKU", 1);
|
|
73
|
+
export {
|
|
74
|
+
HclInventoryCapability
|
|
75
|
+
};
|