@reactionary/commercetools 0.6.3

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.
Files changed (104) hide show
  1. package/README.md +11 -0
  2. package/capabilities/cart.capability.js +324 -0
  3. package/capabilities/category.capability.js +198 -0
  4. package/capabilities/checkout.capability.js +374 -0
  5. package/capabilities/identity.capability.js +95 -0
  6. package/capabilities/index.js +15 -0
  7. package/capabilities/inventory.capability.js +63 -0
  8. package/capabilities/order-search.capability.js +106 -0
  9. package/capabilities/order.capability.js +48 -0
  10. package/capabilities/price.capability.js +114 -0
  11. package/capabilities/product-associations.capability.js +120 -0
  12. package/capabilities/product-list.capability.js +393 -0
  13. package/capabilities/product-reviews.capability.js +193 -0
  14. package/capabilities/product-search.capability.js +297 -0
  15. package/capabilities/product.capability.js +103 -0
  16. package/capabilities/profile.capability.js +337 -0
  17. package/capabilities/store.capability.js +51 -0
  18. package/core/capability-descriptors.js +274 -0
  19. package/core/client.js +318 -0
  20. package/core/initialize.js +47 -0
  21. package/core/initialize.types.js +8 -0
  22. package/core/token-cache.js +36 -0
  23. package/factories/cart/cart.factory.js +110 -0
  24. package/factories/category/category.factory.js +40 -0
  25. package/factories/checkout/checkout-initializer-overrides.example.js +67 -0
  26. package/factories/checkout/checkout.factory.js +235 -0
  27. package/factories/identity/identity.factory.js +14 -0
  28. package/factories/inventory/inventory.factory.js +30 -0
  29. package/factories/order/order.factory.js +114 -0
  30. package/factories/order-search/order-search.factory.js +68 -0
  31. package/factories/price/price.factory.js +52 -0
  32. package/factories/product/product-capability-custom-method-only.example.js +38 -0
  33. package/factories/product/product-capability-schema-signature-extension.example.js +46 -0
  34. package/factories/product/product-factory-baseline.example.js +10 -0
  35. package/factories/product/product-factory-schema-and-parse-extension.example.js +25 -0
  36. package/factories/product/product-factory-schema-extension.example.js +18 -0
  37. package/factories/product/product-initializer-factory-extension.example.js +33 -0
  38. package/factories/product/product.factory.js +151 -0
  39. package/factories/product/utils.example.js +8 -0
  40. package/factories/product-associations/product-associations.factory.js +63 -0
  41. package/factories/product-list/product-list.factory.js +65 -0
  42. package/factories/product-reviews/product-reviews.factory.js +42 -0
  43. package/factories/product-search/product-search.factory.js +114 -0
  44. package/factories/profile/profile.factory.js +62 -0
  45. package/factories/store/store.factory.js +29 -0
  46. package/index.js +28 -0
  47. package/package.json +17 -0
  48. package/schema/capabilities.schema.js +46 -0
  49. package/schema/commercetools.schema.js +30 -0
  50. package/schema/configuration.schema.js +19 -0
  51. package/schema/session.schema.js +9 -0
  52. package/src/capabilities/cart.capability.d.ts +34 -0
  53. package/src/capabilities/category.capability.d.ts +47 -0
  54. package/src/capabilities/checkout.capability.d.ts +39 -0
  55. package/src/capabilities/identity.capability.d.ts +14 -0
  56. package/src/capabilities/index.d.ts +15 -0
  57. package/src/capabilities/inventory.capability.d.ts +13 -0
  58. package/src/capabilities/order-search.capability.d.ts +13 -0
  59. package/src/capabilities/order.capability.d.ts +13 -0
  60. package/src/capabilities/price.capability.d.ts +18 -0
  61. package/src/capabilities/product-associations.capability.d.ts +17 -0
  62. package/src/capabilities/product-list.capability.d.ts +29 -0
  63. package/src/capabilities/product-reviews.capability.d.ts +18 -0
  64. package/src/capabilities/product-search.capability.d.ts +82 -0
  65. package/src/capabilities/product.capability.d.ts +17 -0
  66. package/src/capabilities/profile.capability.d.ts +30 -0
  67. package/src/capabilities/store.capability.d.ts +13 -0
  68. package/src/core/capability-descriptors.d.ts +16 -0
  69. package/src/core/client.d.ts +55 -0
  70. package/src/core/initialize.d.ts +5 -0
  71. package/src/core/initialize.types.d.ts +118 -0
  72. package/src/core/token-cache.d.ts +9 -0
  73. package/src/factories/cart/cart.factory.d.ts +14 -0
  74. package/src/factories/category/category.factory.d.ts +11 -0
  75. package/src/factories/checkout/checkout-initializer-overrides.example.d.ts +1 -0
  76. package/src/factories/checkout/checkout.factory.d.ts +18 -0
  77. package/src/factories/identity/identity.factory.d.ts +8 -0
  78. package/src/factories/inventory/inventory.factory.d.ts +9 -0
  79. package/src/factories/order/order.factory.d.ts +9 -0
  80. package/src/factories/order-search/order-search.factory.d.ts +11 -0
  81. package/src/factories/price/price.factory.d.ts +11 -0
  82. package/src/factories/product/product-capability-custom-method-only.example.d.ts +1 -0
  83. package/src/factories/product/product-capability-schema-signature-extension.example.d.ts +1 -0
  84. package/src/factories/product/product-factory-baseline.example.d.ts +1 -0
  85. package/src/factories/product/product-factory-schema-and-parse-extension.example.d.ts +1 -0
  86. package/src/factories/product/product-factory-schema-extension.example.d.ts +1 -0
  87. package/src/factories/product/product-initializer-factory-extension.example.d.ts +1 -0
  88. package/src/factories/product/product.factory.d.ts +18 -0
  89. package/src/factories/product/utils.example.d.ts +4 -0
  90. package/src/factories/product-associations/product-associations.factory.d.ts +15 -0
  91. package/src/factories/product-list/product-list.factory.d.ts +17 -0
  92. package/src/factories/product-reviews/product-reviews.factory.d.ts +14 -0
  93. package/src/factories/product-search/product-search.factory.d.ts +13 -0
  94. package/src/factories/profile/profile.factory.d.ts +11 -0
  95. package/src/factories/store/store.factory.d.ts +9 -0
  96. package/src/index.d.ts +28 -0
  97. package/src/schema/capabilities.schema.d.ts +104 -0
  98. package/src/schema/commercetools.schema.d.ts +30 -0
  99. package/src/schema/configuration.schema.d.ts +30 -0
  100. package/src/schema/session.schema.d.ts +7 -0
  101. package/src/test/client-builder-merge-extensions.example.d.ts +1 -0
  102. package/src/test/test-utils.d.ts +27 -0
  103. package/test/client-builder-merge-extensions.example.js +94 -0
  104. package/test/test-utils.js +27 -0
@@ -0,0 +1,374 @@
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
+ CheckoutMutationAddPaymentInstructionSchema,
14
+ CheckoutMutationFinalizeCheckoutSchema,
15
+ CheckoutMutationInitiateCheckoutSchema,
16
+ CheckoutMutationRemovePaymentInstructionSchema,
17
+ CheckoutMutationSetShippingAddressSchema,
18
+ CheckoutMutationSetShippingInstructionSchema,
19
+ CheckoutCapability,
20
+ CheckoutQueryByIdSchema,
21
+ CheckoutQueryForAvailablePaymentMethodsSchema,
22
+ CheckoutQueryForAvailableShippingMethodsSchema,
23
+ CheckoutSchema,
24
+ PaymentMethodSchema,
25
+ Reactionary,
26
+ ShippingMethodSchema,
27
+ success,
28
+ error,
29
+ unwrapValue
30
+ } from "@reactionary/core";
31
+ import * as z from "zod";
32
+ import {} from "../schema/commercetools.schema.js";
33
+ class CheckoutNotReadyForFinalizationError extends Error {
34
+ constructor(checkoutIdentifier) {
35
+ super(
36
+ "Checkout is not ready for finalization. Ensure all required fields are set and valid. " + (checkoutIdentifier ? `Checkout ID: ${JSON.stringify(checkoutIdentifier)}` : "")
37
+ );
38
+ this.checkoutIdentifier = checkoutIdentifier;
39
+ this.name = "CheckoutNotReadyForFinalizationError";
40
+ }
41
+ }
42
+ class CommercetoolsCheckoutCapability extends CheckoutCapability {
43
+ config;
44
+ commercetools;
45
+ factory;
46
+ constructor(config, cache, context, commercetools, factory) {
47
+ super(cache, context);
48
+ this.config = config;
49
+ this.commercetools = commercetools;
50
+ this.factory = factory;
51
+ }
52
+ async getClient() {
53
+ const client = await this.commercetools.getClient();
54
+ return {
55
+ payments: client.withProjectKey({ projectKey: this.config.projectKey }).me().payments(),
56
+ carts: client.withProjectKey({ projectKey: this.config.projectKey }).me().carts(),
57
+ shippingMethods: client.withProjectKey({ projectKey: this.config.projectKey }).shippingMethods(),
58
+ orders: client.withProjectKey({ projectKey: this.config.projectKey }).me().orders()
59
+ };
60
+ }
61
+ async initiateCheckoutForCart(payload) {
62
+ const client = await this.getClient();
63
+ const cart = await client.carts.withId({ ID: payload.cart.identifier.key }).get().execute();
64
+ const replicationResponse = await client.carts.replicate().post({
65
+ body: {
66
+ reference: {
67
+ typeId: "cart",
68
+ id: cart.body.id
69
+ }
70
+ }
71
+ }).execute();
72
+ const actions = [
73
+ {
74
+ action: "setCustomType",
75
+ type: {
76
+ typeId: "type",
77
+ key: "reactionaryCheckout"
78
+ },
79
+ fields: {
80
+ commerceToolsCartId: payload.cart.identifier.key
81
+ }
82
+ }
83
+ ];
84
+ if (payload.billingAddress) {
85
+ actions.push({
86
+ action: "setBillingAddress",
87
+ address: {
88
+ country: payload.billingAddress.countryCode,
89
+ firstName: payload.billingAddress.firstName || "",
90
+ lastName: payload.billingAddress.lastName || "",
91
+ streetName: payload.billingAddress.streetAddress || "",
92
+ streetNumber: payload.billingAddress.streetNumber || "",
93
+ postalCode: payload.billingAddress.postalCode || "",
94
+ city: payload.billingAddress.city || "",
95
+ email: payload.notificationEmail || "",
96
+ phone: payload.notificationPhone || ""
97
+ }
98
+ });
99
+ actions.push({
100
+ action: "setShippingAddress",
101
+ address: {
102
+ country: payload.billingAddress.countryCode,
103
+ firstName: payload.billingAddress.firstName || "",
104
+ lastName: payload.billingAddress.lastName || "",
105
+ streetName: payload.billingAddress.streetAddress || "",
106
+ streetNumber: payload.billingAddress.streetNumber || "",
107
+ postalCode: payload.billingAddress.postalCode || "",
108
+ city: payload.billingAddress.city || ""
109
+ }
110
+ });
111
+ }
112
+ const checkoutResponse = await client.carts.withId({ ID: replicationResponse.body.id }).post({
113
+ body: {
114
+ version: replicationResponse.body.version || 0,
115
+ actions: [...actions]
116
+ }
117
+ }).execute();
118
+ return success(this.factory.parseCheckout(this.context, checkoutResponse.body));
119
+ }
120
+ async getById(payload) {
121
+ const client = await this.getClient();
122
+ const checkoutResponse = await client.carts.withId({ ID: payload.identifier.key }).get({
123
+ queryArgs: {
124
+ expand: ["paymentInfo.payments[*]", "shippingInfo.shippingMethod"]
125
+ }
126
+ }).execute();
127
+ const checkout = this.factory.parseCheckout(this.context, checkoutResponse.body);
128
+ if (checkoutResponse.body.cartState === "Ordered") {
129
+ const order = await client.orders.get({
130
+ queryArgs: {
131
+ where: `cart(id="${checkout.identifier.key}")`
132
+ }
133
+ }).execute();
134
+ checkout.resultingOrder = {
135
+ key: order.body.results[0].id
136
+ };
137
+ }
138
+ return success(checkout);
139
+ }
140
+ async setShippingAddress(payload) {
141
+ const client = await this.getClient();
142
+ const version = payload.checkout.version;
143
+ const checkoutResponse = await client.carts.withId({ ID: payload.checkout.key }).post({
144
+ body: {
145
+ version,
146
+ actions: [
147
+ {
148
+ action: "setShippingAddress",
149
+ address: {
150
+ country: payload.shippingAddress.countryCode,
151
+ firstName: payload.shippingAddress.firstName || "",
152
+ lastName: payload.shippingAddress.lastName || "",
153
+ streetName: payload.shippingAddress.streetAddress || "",
154
+ streetNumber: payload.shippingAddress.streetNumber || "",
155
+ postalCode: payload.shippingAddress.postalCode || "",
156
+ city: payload.shippingAddress.city || ""
157
+ }
158
+ }
159
+ ]
160
+ }
161
+ }).execute();
162
+ return success(this.factory.parseCheckout(this.context, checkoutResponse.body));
163
+ }
164
+ async getAvailableShippingMethods(payload) {
165
+ const client = await this.getClient();
166
+ const shippingMethodsResponse = await client.shippingMethods.matchingCart().get({
167
+ queryArgs: {
168
+ cartId: payload.checkout.key
169
+ }
170
+ }).execute();
171
+ const result = shippingMethodsResponse.body.results.map(
172
+ (shippingMethod) => this.factory.parseShippingMethod(this.context, shippingMethod)
173
+ );
174
+ return success(result);
175
+ }
176
+ async getAvailablePaymentMethods(payload) {
177
+ const staticMethods = this.getStaticPaymentMethods(payload.checkout);
178
+ const dynamicMethods = [];
179
+ const result = [...staticMethods, ...dynamicMethods].map(
180
+ (paymentMethod) => this.factory.parsePaymentMethod(this.context, paymentMethod)
181
+ );
182
+ return success(result);
183
+ }
184
+ async addPaymentInstruction(payload) {
185
+ const client = await this.getClient();
186
+ const response = await client.payments.post({
187
+ body: {
188
+ amountPlanned: {
189
+ centAmount: Math.round(
190
+ payload.paymentInstruction.amount.value * 100
191
+ ),
192
+ currencyCode: payload.paymentInstruction.amount.currency
193
+ },
194
+ paymentMethodInfo: {
195
+ method: payload.paymentInstruction.paymentMethod.method,
196
+ name: {
197
+ [this.context.languageContext.locale]: payload.paymentInstruction.paymentMethod.name
198
+ },
199
+ paymentInterface: payload.paymentInstruction.paymentMethod.paymentProcessor
200
+ },
201
+ custom: {
202
+ type: {
203
+ typeId: "type",
204
+ key: "reactionaryPaymentCustomFields"
205
+ },
206
+ fields: {
207
+ commerceToolsCartId: payload.checkout.key
208
+ }
209
+ }
210
+ }
211
+ }).execute();
212
+ const version = payload.checkout.version;
213
+ const actions = [
214
+ {
215
+ action: "addPayment",
216
+ payment: {
217
+ typeId: "payment",
218
+ id: response.body.id
219
+ }
220
+ }
221
+ ];
222
+ const result = await this.applyActions(
223
+ payload.checkout,
224
+ actions
225
+ );
226
+ return success(result);
227
+ }
228
+ async removePaymentInstruction(payload) {
229
+ const client = await this.getClient();
230
+ const checkout = unwrapValue(await this.getById({ identifier: payload.checkout }));
231
+ return success(checkout);
232
+ }
233
+ async setShippingInstruction(payload) {
234
+ const actions = [];
235
+ actions.push({
236
+ action: "setShippingMethod",
237
+ shippingMethod: {
238
+ typeId: "shipping-method",
239
+ key: payload.shippingInstruction.shippingMethod.key
240
+ }
241
+ });
242
+ actions.push({
243
+ action: "setCustomField",
244
+ name: "shippingInstruction",
245
+ value: payload.shippingInstruction.instructions
246
+ });
247
+ actions.push({
248
+ action: "setCustomField",
249
+ name: "consentForUnattendedDelivery",
250
+ value: payload.shippingInstruction.consentForUnattendedDelivery + ""
251
+ });
252
+ actions.push({
253
+ action: "setCustomField",
254
+ name: "pickupPointId",
255
+ value: payload.shippingInstruction.pickupPoint
256
+ });
257
+ const result = await this.applyActions(
258
+ payload.checkout,
259
+ actions
260
+ );
261
+ return success(result);
262
+ }
263
+ async finalizeCheckout(payload) {
264
+ const checkout = await this.getById({ identifier: payload.checkout });
265
+ if (!checkout.success || !checkout.value.readyForFinalization) {
266
+ throw new CheckoutNotReadyForFinalizationError(payload.checkout);
267
+ }
268
+ const client = await this.getClient();
269
+ const ctId = payload.checkout;
270
+ await client.orders.post({
271
+ body: {
272
+ id: ctId.key,
273
+ version: ctId.version
274
+ }
275
+ }).execute();
276
+ const result = await this.getById({
277
+ identifier: payload.checkout
278
+ });
279
+ if (!result.success) {
280
+ throw new Error("Unable to fetch checkout after finalization.");
281
+ }
282
+ return success(result.value);
283
+ }
284
+ async applyActions(checkout, actions) {
285
+ const client = await this.getClient();
286
+ const ctId = checkout;
287
+ try {
288
+ const response = await client.carts.withId({ ID: ctId.key }).post({
289
+ queryArgs: {
290
+ expand: ["paymentInfo.payments[*]", "shippingInfo.shippingMethod"]
291
+ },
292
+ body: {
293
+ version: ctId.version,
294
+ actions
295
+ }
296
+ }).execute();
297
+ if (response.error) {
298
+ console.error(response.error);
299
+ }
300
+ const p = this.factory.parseCheckout(this.context, response.body);
301
+ return p;
302
+ } catch (e) {
303
+ console.error("Error applying actions to cart:", e);
304
+ throw e;
305
+ }
306
+ }
307
+ /**
308
+ * Extension point, to allow filtering the options, or adding new ones, like invoicing for b2b.
309
+ *
310
+ * Usecase: Override this, if you need to change the payment options based on the request context.
311
+ * @returns
312
+ */
313
+ getStaticPaymentMethods(_checkout) {
314
+ return this.config.paymentMethods || [];
315
+ }
316
+ }
317
+ __decorateClass([
318
+ Reactionary({
319
+ inputSchema: CheckoutMutationInitiateCheckoutSchema,
320
+ outputSchema: CheckoutSchema
321
+ })
322
+ ], CommercetoolsCheckoutCapability.prototype, "initiateCheckoutForCart", 1);
323
+ __decorateClass([
324
+ Reactionary({
325
+ inputSchema: CheckoutQueryByIdSchema,
326
+ outputSchema: CheckoutSchema.nullable()
327
+ })
328
+ ], CommercetoolsCheckoutCapability.prototype, "getById", 1);
329
+ __decorateClass([
330
+ Reactionary({
331
+ inputSchema: CheckoutMutationSetShippingAddressSchema,
332
+ outputSchema: CheckoutSchema
333
+ })
334
+ ], CommercetoolsCheckoutCapability.prototype, "setShippingAddress", 1);
335
+ __decorateClass([
336
+ Reactionary({
337
+ inputSchema: CheckoutQueryForAvailableShippingMethodsSchema,
338
+ outputSchema: z.array(ShippingMethodSchema)
339
+ })
340
+ ], CommercetoolsCheckoutCapability.prototype, "getAvailableShippingMethods", 1);
341
+ __decorateClass([
342
+ Reactionary({
343
+ inputSchema: CheckoutQueryForAvailablePaymentMethodsSchema,
344
+ outputSchema: z.array(PaymentMethodSchema)
345
+ })
346
+ ], CommercetoolsCheckoutCapability.prototype, "getAvailablePaymentMethods", 1);
347
+ __decorateClass([
348
+ Reactionary({
349
+ inputSchema: CheckoutMutationAddPaymentInstructionSchema,
350
+ outputSchema: CheckoutSchema
351
+ })
352
+ ], CommercetoolsCheckoutCapability.prototype, "addPaymentInstruction", 1);
353
+ __decorateClass([
354
+ Reactionary({
355
+ inputSchema: CheckoutMutationRemovePaymentInstructionSchema,
356
+ outputSchema: CheckoutSchema
357
+ })
358
+ ], CommercetoolsCheckoutCapability.prototype, "removePaymentInstruction", 1);
359
+ __decorateClass([
360
+ Reactionary({
361
+ inputSchema: CheckoutMutationSetShippingInstructionSchema,
362
+ outputSchema: CheckoutSchema
363
+ })
364
+ ], CommercetoolsCheckoutCapability.prototype, "setShippingInstruction", 1);
365
+ __decorateClass([
366
+ Reactionary({
367
+ inputSchema: CheckoutMutationFinalizeCheckoutSchema,
368
+ outputSchema: CheckoutSchema
369
+ })
370
+ ], CommercetoolsCheckoutCapability.prototype, "finalizeCheckout", 1);
371
+ export {
372
+ CheckoutNotReadyForFinalizationError,
373
+ CommercetoolsCheckoutCapability
374
+ };
@@ -0,0 +1,95 @@
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
+ IdentityCapability,
14
+ Reactionary,
15
+ IdentityQuerySelfSchema,
16
+ IdentitySchema,
17
+ IdentityMutationRegisterSchema,
18
+ IdentityMutationLoginSchema,
19
+ success
20
+ } from "@reactionary/core";
21
+ class CommercetoolsIdentityCapability extends IdentityCapability {
22
+ config;
23
+ commercetools;
24
+ factory;
25
+ constructor(config, cache, context, commercetools, factory) {
26
+ super(cache, context);
27
+ this.config = config;
28
+ this.commercetools = commercetools;
29
+ this.factory = factory;
30
+ }
31
+ async getSelf(payload) {
32
+ const identity = this.factory.parseIdentity(
33
+ this.context,
34
+ await this.commercetools.introspect()
35
+ );
36
+ this.updateIdentityContext(identity);
37
+ return success(identity);
38
+ }
39
+ async login(payload) {
40
+ const identity = this.factory.parseIdentity(
41
+ this.context,
42
+ await this.commercetools.login(
43
+ payload.username,
44
+ payload.password
45
+ )
46
+ );
47
+ this.updateIdentityContext(identity);
48
+ return success(identity);
49
+ }
50
+ async logout(payload) {
51
+ const identity = this.factory.parseIdentity(
52
+ this.context,
53
+ await this.commercetools.logout()
54
+ );
55
+ this.updateIdentityContext(identity);
56
+ return success(identity);
57
+ }
58
+ async register(payload) {
59
+ const identity = this.factory.parseIdentity(
60
+ this.context,
61
+ await this.commercetools.register(
62
+ payload.username,
63
+ payload.password
64
+ )
65
+ );
66
+ this.updateIdentityContext(identity);
67
+ return success(identity);
68
+ }
69
+ }
70
+ __decorateClass([
71
+ Reactionary({
72
+ inputSchema: IdentityQuerySelfSchema,
73
+ outputSchema: IdentitySchema
74
+ })
75
+ ], CommercetoolsIdentityCapability.prototype, "getSelf", 1);
76
+ __decorateClass([
77
+ Reactionary({
78
+ inputSchema: IdentityMutationLoginSchema,
79
+ outputSchema: IdentitySchema
80
+ })
81
+ ], CommercetoolsIdentityCapability.prototype, "login", 1);
82
+ __decorateClass([
83
+ Reactionary({
84
+ outputSchema: IdentitySchema
85
+ })
86
+ ], CommercetoolsIdentityCapability.prototype, "logout", 1);
87
+ __decorateClass([
88
+ Reactionary({
89
+ inputSchema: IdentityMutationRegisterSchema,
90
+ outputSchema: IdentitySchema
91
+ })
92
+ ], CommercetoolsIdentityCapability.prototype, "register", 1);
93
+ export {
94
+ CommercetoolsIdentityCapability
95
+ };
@@ -0,0 +1,15 @@
1
+ export * from "./cart.capability.js";
2
+ export * from "./category.capability.js";
3
+ export * from "./identity.capability.js";
4
+ export * from "./inventory.capability.js";
5
+ export * from "./order-search.capability.js";
6
+ export * from "./price.capability.js";
7
+ export * from "./product-associations.capability.js";
8
+ export * from "./product-list.capability.js";
9
+ export * from "./product.capability.js";
10
+ export * from "./product-reviews.capability.js";
11
+ export * from "./profile.capability.js";
12
+ export * from "./product-search.capability.js";
13
+ export * from "./store.capability.js";
14
+ export * from "./order.capability.js";
15
+ export * from "./checkout.capability.js";
@@ -0,0 +1,63 @@
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 { InventoryCapability, InventoryQueryBySKUSchema, InventorySchema, Reactionary, success, error } from "@reactionary/core";
13
+ class CommercetoolsInventoryCapability extends InventoryCapability {
14
+ config;
15
+ commercetools;
16
+ factory;
17
+ constructor(config, cache, context, commercetools, factory) {
18
+ super(cache, context);
19
+ this.config = config;
20
+ this.commercetools = commercetools;
21
+ this.factory = factory;
22
+ }
23
+ async getClient() {
24
+ const client = await this.commercetools.getClient();
25
+ return client.withProjectKey({ projectKey: this.config.projectKey });
26
+ }
27
+ async getBySKU(payload) {
28
+ const client = await this.getClient();
29
+ try {
30
+ const channelId = await this.commercetools.resolveChannelIdByKey(payload.fulfilmentCenter.key);
31
+ const remote = await client.inventory().get({
32
+ queryArgs: {
33
+ where: "sku=:sku AND supplyChannel(id=:channel)",
34
+ "var.sku": payload.variant.sku,
35
+ "var.channel": channelId,
36
+ expand: "supplyChannel"
37
+ }
38
+ }).execute();
39
+ const result = remote.body.results[0];
40
+ const model = this.factory.parseInventory(this.context, result);
41
+ return success(model);
42
+ } catch (err) {
43
+ console.error("Error fetching inventory by SKU and Fulfillment Center:", error, payload);
44
+ return error({
45
+ type: "NotFound",
46
+ identifier: payload
47
+ });
48
+ }
49
+ }
50
+ }
51
+ __decorateClass([
52
+ Reactionary({
53
+ cache: true,
54
+ cacheTimeToLiveInSeconds: 300,
55
+ currencyDependentCaching: false,
56
+ localeDependentCaching: false,
57
+ inputSchema: InventoryQueryBySKUSchema,
58
+ outputSchema: InventorySchema
59
+ })
60
+ ], CommercetoolsInventoryCapability.prototype, "getBySKU", 1);
61
+ export {
62
+ CommercetoolsInventoryCapability
63
+ };
@@ -0,0 +1,106 @@
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
+ OrderSearchCapability,
14
+ OrderSearchQueryByTermSchema,
15
+ OrderSearchResultSchema,
16
+ Reactionary,
17
+ success
18
+ } from "@reactionary/core";
19
+ import createDebug from "debug";
20
+ const debug = createDebug("reactionary:commercetools:order-search");
21
+ class CommercetoolsOrderSearchCapability extends OrderSearchCapability {
22
+ config;
23
+ commercetools;
24
+ factory;
25
+ constructor(config, cache, context, commercetools, factory) {
26
+ super(cache, context);
27
+ this.config = config;
28
+ this.commercetools = commercetools;
29
+ this.factory = factory;
30
+ }
31
+ async getClient() {
32
+ const client = await this.commercetools.getClient();
33
+ return client.withProjectKey({ projectKey: this.config.projectKey }).me().orders();
34
+ }
35
+ async queryByTerm(payload) {
36
+ debug("queryByTerm", payload);
37
+ const client = await this.getClient();
38
+ const where = [];
39
+ if (payload.search) {
40
+ if (payload.search.term) {
41
+ debug("Search by term is not implemented yet in CommercetoolsOrderSearchCapability");
42
+ }
43
+ if (payload.search.partNumber) {
44
+ for (const partNumber of payload.search.partNumber) {
45
+ where.push(`lineItems(variant(sku="${partNumber}"))`);
46
+ }
47
+ }
48
+ if (payload.search.user && payload.search.user.userId) {
49
+ }
50
+ if (payload.search.orderStatus) {
51
+ const orderStatusWhere = payload.search.orderStatus.map((x) => {
52
+ let mappedStatus = "Open";
53
+ if (x === "AwaitingPayment") {
54
+ mappedStatus = `Open`;
55
+ }
56
+ if (x === "ReleasedToFulfillment") {
57
+ mappedStatus = `Confirmed`;
58
+ }
59
+ if (x === "Shipped") {
60
+ mappedStatus = `Complete`;
61
+ }
62
+ if (x === "Cancelled") {
63
+ mappedStatus = `Cancelled`;
64
+ }
65
+ return `orderState="${mappedStatus}"`;
66
+ }).join(" OR ");
67
+ where.push(orderStatusWhere);
68
+ }
69
+ if (payload.search.startDate) {
70
+ where.push(`createdAt >= "${payload.search.startDate}"`);
71
+ }
72
+ if (payload.search.endDate) {
73
+ where.push(`createdAt <= "${payload.search.endDate}"`);
74
+ }
75
+ }
76
+ const response = await client.get({
77
+ queryArgs: {
78
+ where,
79
+ withTotal: true,
80
+ limit: payload.search.paginationOptions.pageSize,
81
+ offset: (payload.search.paginationOptions.pageNumber - 1) * payload.search.paginationOptions.pageSize
82
+ }
83
+ }).execute();
84
+ const responseBody = response.body;
85
+ const result = this.factory.parseOrderSearchResult(
86
+ this.context,
87
+ responseBody,
88
+ payload
89
+ );
90
+ if (debug.enabled) {
91
+ debug(
92
+ `Search for term "${payload.search.term}" returned ${responseBody.results.length} orders (page ${payload.search.paginationOptions.pageNumber} of ${result.totalPages})`
93
+ );
94
+ }
95
+ return success(result);
96
+ }
97
+ }
98
+ __decorateClass([
99
+ Reactionary({
100
+ inputSchema: OrderSearchQueryByTermSchema,
101
+ outputSchema: OrderSearchResultSchema
102
+ })
103
+ ], CommercetoolsOrderSearchCapability.prototype, "queryByTerm", 1);
104
+ export {
105
+ CommercetoolsOrderSearchCapability
106
+ };
@@ -0,0 +1,48 @@
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 { OrderCapability, OrderQueryByIdSchema, OrderSchema, Reactionary, success, error } from "@reactionary/core";
13
+ class CommercetoolsOrderCapability extends OrderCapability {
14
+ config;
15
+ commercetools;
16
+ factory;
17
+ constructor(config, cache, context, commercetools, factory) {
18
+ super(cache, context);
19
+ this.config = config;
20
+ this.commercetools = commercetools;
21
+ this.factory = factory;
22
+ }
23
+ async getClient() {
24
+ const client = await this.commercetools.getClient();
25
+ return client.withProjectKey({ projectKey: this.config.projectKey }).me().orders();
26
+ }
27
+ async getById(payload) {
28
+ const client = await this.getClient();
29
+ try {
30
+ const remote = await client.withId({ ID: payload.order.key }).get().execute();
31
+ return success(this.factory.parseOrder(this.context, remote.body));
32
+ } catch (e) {
33
+ return error({
34
+ type: "NotFound",
35
+ identifier: payload
36
+ });
37
+ }
38
+ }
39
+ }
40
+ __decorateClass([
41
+ Reactionary({
42
+ inputSchema: OrderQueryByIdSchema,
43
+ outputSchema: OrderSchema
44
+ })
45
+ ], CommercetoolsOrderCapability.prototype, "getById", 1);
46
+ export {
47
+ CommercetoolsOrderCapability
48
+ };