@reactionary/magento 0.0.1 → 0.10.19

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 (62) hide show
  1. package/LICENSE +21 -0
  2. package/capabilities/cart.capability.js +22 -22
  3. package/capabilities/category.capability.js +10 -9
  4. package/capabilities/checkout.capability.js +308 -0
  5. package/capabilities/identity.capability.js +31 -16
  6. package/capabilities/inventory.capability.js +2 -2
  7. package/capabilities/order-search.capability.js +150 -0
  8. package/capabilities/order.capability.js +51 -0
  9. package/capabilities/price.capability.js +2 -2
  10. package/capabilities/product-associations.capability.js +118 -0
  11. package/capabilities/product-recommendations.capability.js +148 -0
  12. package/capabilities/product-reviews.capability.js +182 -0
  13. package/capabilities/product-search.capability.js +2 -2
  14. package/capabilities/product.capability.js +6 -5
  15. package/capabilities/profile.capability.js +283 -0
  16. package/core/client.js +324 -24
  17. package/core/initialize.js +150 -2
  18. package/core/product-reviews.graphql.js +72 -0
  19. package/factories/category/category.factory.js +1 -1
  20. package/factories/checkout/checkout.factory.js +203 -0
  21. package/factories/index.js +6 -0
  22. package/factories/order/order.factory.js +115 -0
  23. package/factories/order-search/order-search.factory.js +79 -0
  24. package/factories/product/product.factory.js +23 -18
  25. package/factories/product-associations/product-associations.factory.js +25 -0
  26. package/factories/product-reviews/product-reviews.factory.js +65 -0
  27. package/factories/product-search/product-search.factory.js +3 -4
  28. package/factories/profile/profile.factory.js +61 -0
  29. package/index.js +9 -0
  30. package/package.json +11 -6
  31. package/schema/capabilities.schema.js +16 -2
  32. package/schema/configuration.schema.js +5 -1
  33. package/src/capabilities/cart.capability.d.ts +5 -0
  34. package/src/capabilities/checkout.capability.d.ts +30 -0
  35. package/src/capabilities/order-search.capability.d.ts +16 -0
  36. package/src/capabilities/order.capability.d.ts +12 -0
  37. package/src/capabilities/product-associations.capability.d.ts +23 -0
  38. package/src/capabilities/product-recommendations.capability.d.ts +24 -0
  39. package/src/capabilities/product-reviews.capability.d.ts +35 -0
  40. package/src/capabilities/profile.capability.d.ts +24 -0
  41. package/src/core/client.d.ts +77 -2
  42. package/src/core/initialize.types.d.ts +40 -7
  43. package/src/core/product-reviews.graphql.d.ts +10 -0
  44. package/src/factories/category/category.factory.d.ts +2 -2
  45. package/src/factories/checkout/checkout.factory.d.ts +32 -0
  46. package/src/factories/index.d.ts +6 -0
  47. package/src/factories/order/order.factory.d.ts +77 -0
  48. package/src/factories/order-search/order-search.factory.d.ts +43 -0
  49. package/src/factories/product/product.factory.d.ts +2 -1
  50. package/src/factories/product-associations/product-associations.factory.d.ts +15 -0
  51. package/src/factories/product-reviews/product-reviews.factory.d.ts +40 -0
  52. package/src/factories/profile/profile.factory.d.ts +43 -0
  53. package/src/index.d.ts +9 -0
  54. package/src/schema/capabilities.schema.d.ts +50 -2
  55. package/src/schema/configuration.schema.d.ts +7 -0
  56. package/src/schema/magento.types.d.ts +219 -0
  57. package/src/utils/magento-product-lookup.d.ts +15 -0
  58. package/src/utils/magento-product.d.ts +36 -0
  59. package/src/utils/magento-review.d.ts +20 -0
  60. package/utils/magento-product-lookup.js +76 -0
  61. package/utils/magento-product.js +110 -0
  62. package/utils/magento-review.js +34 -0
@@ -5,8 +5,7 @@ var __decorateClass = (decorators, target, key, kind) => {
5
5
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
6
  if (decorator = decorators[i])
7
7
  result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
- if (kind && result)
9
- __defProp(target, key, result);
8
+ if (kind && result) __defProp(target, key, result);
10
9
  return result;
11
10
  };
12
11
  import {
@@ -32,7 +31,7 @@ function buildProductsSearchUrl(baseUrl, storeCode, field, value, condition = "e
32
31
  params.set("searchCriteria[filterGroups][0][filters][0][condition_type]", condition);
33
32
  params.set("searchCriteria[pageSize]", String(pageSize));
34
33
  params.set("searchCriteria[currentPage]", String(currentPage));
35
- return `${b}/${storeCode}/rest/V1/products?${params.toString()}`;
34
+ return `${b}/rest/${storeCode}/V1/products?${params.toString()}`;
36
35
  }
37
36
  async function adminSearchProducts(config, field, value) {
38
37
  const token = config.adminApiKey;
@@ -42,6 +41,7 @@ async function adminSearchProducts(config, field, value) {
42
41
  );
43
42
  }
44
43
  const url = buildProductsSearchUrl(config.baseUrl, config.storeCode, field, value, "eq", 1, 1);
44
+ const t = `Bearer ${token}`;
45
45
  const res = await fetch(url, {
46
46
  method: "GET",
47
47
  headers: {
@@ -65,6 +65,7 @@ class MagentoProductCapability extends ProductCapability {
65
65
  this.config = config;
66
66
  this.factory = factory;
67
67
  }
68
+ magentoApi;
68
69
  config;
69
70
  factory;
70
71
  async getById(payload) {
@@ -73,8 +74,8 @@ class MagentoProductCapability extends ProductCapability {
73
74
  debug(`Fetching product by ID/key: ${key}`);
74
75
  }
75
76
  try {
76
- if (/^\d+$/.test(key)) {
77
- const result = await adminSearchProducts(this.config, "entity_id", Number(key));
77
+ if (key.length > 0) {
78
+ const result = await adminSearchProducts(this.config, "external_id", key);
78
79
  const product2 = result.items?.[0];
79
80
  if (!product2) {
80
81
  return success(this.createEmptyProduct(key));
@@ -0,0 +1,283 @@
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) __defProp(target, key, result);
9
+ return result;
10
+ };
11
+ import {
12
+ ProfileCapability,
13
+ ProfileMutationAddShippingAddressSchema,
14
+ ProfileMutationMakeShippingAddressDefaultSchema,
15
+ ProfileMutationRemoveShippingAddressSchema,
16
+ ProfileMutationSetBillingAddressSchema,
17
+ ProfileMutationUpdateSchema,
18
+ ProfileMutationUpdateShippingAddressSchema,
19
+ ProfileQueryByIdSchema,
20
+ ProfileSchema,
21
+ Reactionary,
22
+ error,
23
+ success
24
+ } from "@reactionary/core";
25
+ import createDebug from "debug";
26
+ const debug = createDebug("reactionary:magento:profile");
27
+ class MagentoProfileCapability extends ProfileCapability {
28
+ constructor(config, cache, context, magentoApi, factory) {
29
+ super(cache, context);
30
+ this.magentoApi = magentoApi;
31
+ this.config = config;
32
+ this.factory = factory;
33
+ }
34
+ magentoApi;
35
+ config;
36
+ factory;
37
+ async fetchCustomer() {
38
+ try {
39
+ return await this.magentoApi.getMe();
40
+ } catch (err) {
41
+ debug("fetchCustomer failed: %O", err);
42
+ return null;
43
+ }
44
+ }
45
+ buildMagentoAddress(address, flags = {}, existing) {
46
+ return {
47
+ ...existing?.id !== void 0 ? { id: existing.id } : {},
48
+ company: address.identifier.nickName,
49
+ firstname: address.firstName,
50
+ lastname: address.lastName,
51
+ street: [address.streetAddress, address.streetNumber],
52
+ city: address.city,
53
+ region: { region: address.region },
54
+ postcode: address.postalCode,
55
+ country_id: address.countryCode,
56
+ telephone: existing?.telephone || "000",
57
+ default_billing: flags.default_billing ?? false,
58
+ default_shipping: flags.default_shipping ?? false
59
+ };
60
+ }
61
+ findByNickName(customer, nickName) {
62
+ return (customer.addresses ?? []).find((a) => a.company === nickName);
63
+ }
64
+ async getById(payload) {
65
+ const customer = await this.fetchCustomer();
66
+ if (!customer) {
67
+ return error({
68
+ type: "NotFound",
69
+ identifier: payload.identifier
70
+ });
71
+ }
72
+ return success(this.factory.parseProfile(this.context, customer));
73
+ }
74
+ async update(payload) {
75
+ const customer = await this.fetchCustomer();
76
+ if (!customer) {
77
+ return error({
78
+ type: "NotFound",
79
+ identifier: payload.identifier
80
+ });
81
+ }
82
+ const customAttributes = (customer.custom_attributes ?? []).filter(
83
+ (a) => a.attribute_code !== "phone"
84
+ );
85
+ if (payload.phone !== void 0) {
86
+ customAttributes.push({ attribute_code: "phone", value: payload.phone });
87
+ }
88
+ const updated = await this.magentoApi.updateMe({
89
+ ...customer,
90
+ email: payload.email ?? customer.email,
91
+ custom_attributes: customAttributes
92
+ });
93
+ return success(this.factory.parseProfile(this.context, updated));
94
+ }
95
+ async addShippingAddress(payload) {
96
+ const customer = await this.fetchCustomer();
97
+ if (!customer) {
98
+ return error({
99
+ type: "NotFound",
100
+ identifier: payload.identifier
101
+ });
102
+ }
103
+ if (this.findByNickName(customer, payload.address.identifier.nickName)) {
104
+ return error({
105
+ type: "InvalidInput",
106
+ error: "Address with the same nickname already exists"
107
+ });
108
+ }
109
+ const addresses = this.preserveAddresses(customer);
110
+ addresses.push(this.buildMagentoAddress(payload.address));
111
+ const updated = await this.magentoApi.updateMe({ ...customer, addresses });
112
+ return success(this.factory.parseProfile(this.context, updated));
113
+ }
114
+ async updateShippingAddress(payload) {
115
+ const customer = await this.fetchCustomer();
116
+ if (!customer) {
117
+ return error({
118
+ type: "NotFound",
119
+ identifier: payload.identifier
120
+ });
121
+ }
122
+ const existing = this.findByNickName(
123
+ customer,
124
+ payload.address.identifier.nickName
125
+ );
126
+ if (!existing) {
127
+ return error({
128
+ type: "NotFound",
129
+ identifier: payload.address.identifier
130
+ });
131
+ }
132
+ const isBilling = String(existing.id) === customer.default_billing;
133
+ const isShipping = String(existing.id) === customer.default_shipping;
134
+ const addresses = this.preserveAddresses(customer).map(
135
+ (a) => a.id === existing.id ? this.buildMagentoAddress(
136
+ payload.address,
137
+ { default_billing: isBilling, default_shipping: isShipping },
138
+ existing
139
+ ) : a
140
+ );
141
+ const updated = await this.magentoApi.updateMe({ ...customer, addresses });
142
+ return success(this.factory.parseProfile(this.context, updated));
143
+ }
144
+ async removeShippingAddress(payload) {
145
+ const customer = await this.fetchCustomer();
146
+ if (!customer) {
147
+ return error({
148
+ type: "NotFound",
149
+ identifier: payload.identifier
150
+ });
151
+ }
152
+ const existing = this.findByNickName(
153
+ customer,
154
+ payload.addressIdentifier.nickName
155
+ );
156
+ if (!existing) {
157
+ return error({
158
+ type: "NotFound",
159
+ identifier: payload.addressIdentifier
160
+ });
161
+ }
162
+ const addresses = this.preserveAddresses(customer).filter(
163
+ (a) => a.id !== existing.id
164
+ );
165
+ const updated = await this.magentoApi.updateMe({ ...customer, addresses });
166
+ return success(this.factory.parseProfile(this.context, updated));
167
+ }
168
+ async makeShippingAddressDefault(payload) {
169
+ const customer = await this.fetchCustomer();
170
+ if (!customer) {
171
+ return error({
172
+ type: "NotFound",
173
+ identifier: payload.identifier
174
+ });
175
+ }
176
+ const existing = this.findByNickName(
177
+ customer,
178
+ payload.addressIdentifier.nickName
179
+ );
180
+ if (!existing) {
181
+ return error({
182
+ type: "NotFound",
183
+ identifier: payload.addressIdentifier
184
+ });
185
+ }
186
+ const addresses = this.preserveAddresses(customer).map((a) => ({
187
+ ...a,
188
+ default_shipping: a.id === existing.id
189
+ }));
190
+ const updated = await this.magentoApi.updateMe({ ...customer, addresses });
191
+ return success(this.factory.parseProfile(this.context, updated));
192
+ }
193
+ async setBillingAddress(payload) {
194
+ const customer = await this.fetchCustomer();
195
+ if (!customer) {
196
+ return error({
197
+ type: "NotFound",
198
+ identifier: payload.identifier
199
+ });
200
+ }
201
+ const currentBilling = (customer.addresses ?? []).find(
202
+ (a) => String(a.id) === customer.default_billing
203
+ );
204
+ const collision = this.findByNickName(
205
+ customer,
206
+ payload.address.identifier.nickName
207
+ );
208
+ if (collision && collision.id !== currentBilling?.id) {
209
+ return error({
210
+ type: "InvalidInput",
211
+ error: "Another address with the same nickname already exists"
212
+ });
213
+ }
214
+ let addresses = this.preserveAddresses(customer);
215
+ if (currentBilling) {
216
+ addresses = addresses.map(
217
+ (a) => a.id === currentBilling.id ? this.buildMagentoAddress(
218
+ payload.address,
219
+ { default_billing: true },
220
+ currentBilling
221
+ ) : a
222
+ );
223
+ } else {
224
+ addresses.push(
225
+ this.buildMagentoAddress(payload.address, { default_billing: true })
226
+ );
227
+ }
228
+ const updated = await this.magentoApi.updateMe({ ...customer, addresses });
229
+ return success(this.factory.parseProfile(this.context, updated));
230
+ }
231
+ preserveAddresses(customer) {
232
+ return (customer.addresses ?? []).map((a) => ({
233
+ ...a,
234
+ default_billing: String(a.id) === customer.default_billing,
235
+ default_shipping: String(a.id) === customer.default_shipping
236
+ }));
237
+ }
238
+ }
239
+ __decorateClass([
240
+ Reactionary({
241
+ inputSchema: ProfileQueryByIdSchema,
242
+ outputSchema: ProfileSchema
243
+ })
244
+ ], MagentoProfileCapability.prototype, "getById", 1);
245
+ __decorateClass([
246
+ Reactionary({
247
+ inputSchema: ProfileMutationUpdateSchema,
248
+ outputSchema: ProfileSchema
249
+ })
250
+ ], MagentoProfileCapability.prototype, "update", 1);
251
+ __decorateClass([
252
+ Reactionary({
253
+ inputSchema: ProfileMutationAddShippingAddressSchema,
254
+ outputSchema: ProfileSchema
255
+ })
256
+ ], MagentoProfileCapability.prototype, "addShippingAddress", 1);
257
+ __decorateClass([
258
+ Reactionary({
259
+ inputSchema: ProfileMutationUpdateShippingAddressSchema,
260
+ outputSchema: ProfileSchema
261
+ })
262
+ ], MagentoProfileCapability.prototype, "updateShippingAddress", 1);
263
+ __decorateClass([
264
+ Reactionary({
265
+ inputSchema: ProfileMutationRemoveShippingAddressSchema,
266
+ outputSchema: ProfileSchema
267
+ })
268
+ ], MagentoProfileCapability.prototype, "removeShippingAddress", 1);
269
+ __decorateClass([
270
+ Reactionary({
271
+ inputSchema: ProfileMutationMakeShippingAddressDefaultSchema,
272
+ outputSchema: ProfileSchema
273
+ })
274
+ ], MagentoProfileCapability.prototype, "makeShippingAddressDefault", 1);
275
+ __decorateClass([
276
+ Reactionary({
277
+ inputSchema: ProfileMutationSetBillingAddressSchema,
278
+ outputSchema: ProfileSchema
279
+ })
280
+ ], MagentoProfileCapability.prototype, "setBillingAddress", 1);
281
+ export {
282
+ MagentoProfileCapability
283
+ };