@shushed/helpers 0.0.202-bug-erp-770-20251203154925 → 0.0.202-v2-20251208085030

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 (236) hide show
  1. package/dist/cjs/dist-dereferenced/asset.js +4 -0
  2. package/dist/cjs/dist-dereferenced/category.js +4 -0
  3. package/dist/cjs/dist-dereferenced/country.js +4 -0
  4. package/dist/cjs/dist-dereferenced/currency.js +4 -0
  5. package/dist/cjs/dist-dereferenced/customer-segment.js +4 -0
  6. package/dist/cjs/dist-dereferenced/development-colour.js +4 -0
  7. package/dist/cjs/dist-dereferenced/index.js +69 -0
  8. package/dist/cjs/dist-dereferenced/marketing-preferences.js +4 -0
  9. package/dist/cjs/dist-dereferenced/messages/ean-change.js +4 -0
  10. package/dist/cjs/dist-dereferenced/messages/index.js +50 -0
  11. package/dist/cjs/dist-dereferenced/messages/order/delivered.js +4 -0
  12. package/dist/cjs/dist-dereferenced/messages/order/index.js +18 -0
  13. package/dist/cjs/dist-dereferenced/messages/order/new.js +4 -0
  14. package/dist/cjs/dist-dereferenced/messages/order/processed.js +4 -0
  15. package/dist/cjs/dist-dereferenced/messages/order/return-initiated.js +4 -0
  16. package/dist/cjs/dist-dereferenced/messages/order/returned.js +4 -0
  17. package/dist/cjs/dist-dereferenced/messages/order/shipped.js +4 -0
  18. package/dist/cjs/dist-dereferenced/messages/price-change.js +4 -0
  19. package/dist/cjs/dist-dereferenced/messages/product-category.js +4 -0
  20. package/dist/cjs/dist-dereferenced/messages/product-draft.js +4 -0
  21. package/dist/cjs/dist-dereferenced/messages/product.js +4 -0
  22. package/dist/cjs/dist-dereferenced/money.js +4 -0
  23. package/dist/cjs/dist-dereferenced/order/address.js +4 -0
  24. package/dist/cjs/dist-dereferenced/order/customer.js +4 -0
  25. package/dist/cjs/dist-dereferenced/order/index.js +51 -0
  26. package/dist/cjs/dist-dereferenced/order/item.js +4 -0
  27. package/dist/cjs/dist-dereferenced/order/orderMain.js +4 -0
  28. package/dist/cjs/dist-dereferenced/order/payment.js +4 -0
  29. package/dist/cjs/dist-dereferenced/order/shipment/index.js +45 -0
  30. package/dist/cjs/dist-dereferenced/order/shipment/item/index.js +11 -0
  31. package/dist/cjs/dist-dereferenced/order/shipment/item/itemMain.js +4 -0
  32. package/dist/cjs/dist-dereferenced/order/shipment/item/returned.js +4 -0
  33. package/dist/cjs/dist-dereferenced/order/shipment/pos/index.js +13 -0
  34. package/dist/cjs/dist-dereferenced/order/shipment/pos/outbound.js +4 -0
  35. package/dist/cjs/dist-dereferenced/order/shipment/pos/posMain.js +4 -0
  36. package/dist/cjs/dist-dereferenced/order/shipment/pos/return.js +4 -0
  37. package/dist/cjs/dist-dereferenced/order/shipment/shipmentMain.js +4 -0
  38. package/dist/cjs/dist-dereferenced/order/shipment/shipped/index.js +13 -0
  39. package/dist/cjs/dist-dereferenced/order/shipment/shipped/outbound.js +4 -0
  40. package/dist/cjs/dist-dereferenced/order/shipment/shipped/return.js +4 -0
  41. package/dist/cjs/dist-dereferenced/order/shipment/shipped/shippedMain.js +4 -0
  42. package/dist/cjs/dist-dereferenced/price.js +4 -0
  43. package/dist/cjs/dist-dereferenced/product-category.js +4 -0
  44. package/dist/cjs/dist-dereferenced/product-draft.js +4 -0
  45. package/dist/cjs/dist-dereferenced/product.js +4 -0
  46. package/dist/cjs/dist-dereferenced/stock.js +4 -0
  47. package/dist/cjs/dist-dereferenced/total.js +4 -0
  48. package/dist/cjs/dist-types/asset.js +2 -0
  49. package/dist/cjs/dist-types/category.js +2 -0
  50. package/dist/cjs/dist-types/country.js +2 -0
  51. package/dist/cjs/dist-types/currency.js +2 -0
  52. package/dist/cjs/dist-types/customer-segment.js +2 -0
  53. package/dist/cjs/dist-types/development-colour.js +2 -0
  54. package/dist/cjs/dist-types/index.js +38 -0
  55. package/dist/cjs/dist-types/marketing-preferences.js +2 -0
  56. package/dist/cjs/dist-types/messages/ean-change.js +2 -0
  57. package/dist/cjs/dist-types/messages/index.js +37 -0
  58. package/dist/cjs/dist-types/messages/order/delivered.js +2 -0
  59. package/dist/cjs/dist-types/messages/order/index.js +2 -0
  60. package/dist/cjs/dist-types/messages/order/new.js +2 -0
  61. package/dist/cjs/dist-types/messages/order/processed.js +2 -0
  62. package/dist/cjs/dist-types/messages/order/return-initiated.js +2 -0
  63. package/dist/cjs/dist-types/messages/order/returned.js +2 -0
  64. package/dist/cjs/dist-types/messages/order/shipped.js +2 -0
  65. package/dist/cjs/dist-types/messages/price-change.js +2 -0
  66. package/dist/cjs/dist-types/messages/product-category.js +2 -0
  67. package/dist/cjs/dist-types/messages/product-draft.js +2 -0
  68. package/dist/cjs/dist-types/messages/product.js +2 -0
  69. package/dist/cjs/dist-types/money.js +2 -0
  70. package/dist/cjs/dist-types/order/address.js +2 -0
  71. package/dist/cjs/dist-types/order/customer.js +2 -0
  72. package/dist/cjs/dist-types/order/index.js +37 -0
  73. package/dist/cjs/dist-types/order/item.js +2 -0
  74. package/dist/cjs/dist-types/order/orderMain.js +2 -0
  75. package/dist/cjs/dist-types/order/payment.js +2 -0
  76. package/dist/cjs/dist-types/order/shipment/index.js +39 -0
  77. package/dist/cjs/dist-types/order/shipment/item/index.js +2 -0
  78. package/dist/cjs/dist-types/order/shipment/item/itemMain.js +2 -0
  79. package/dist/cjs/dist-types/order/shipment/item/returned.js +2 -0
  80. package/dist/cjs/dist-types/order/shipment/pos/index.js +2 -0
  81. package/dist/cjs/dist-types/order/shipment/pos/outbound.js +2 -0
  82. package/dist/cjs/dist-types/order/shipment/pos/posMain.js +2 -0
  83. package/dist/cjs/dist-types/order/shipment/pos/return.js +2 -0
  84. package/dist/cjs/dist-types/order/shipment/shipmentMain.js +2 -0
  85. package/dist/cjs/dist-types/order/shipment/shipped/index.js +2 -0
  86. package/dist/cjs/dist-types/order/shipment/shipped/outbound.js +2 -0
  87. package/dist/cjs/dist-types/order/shipment/shipped/return.js +2 -0
  88. package/dist/cjs/dist-types/order/shipment/shipped/shippedMain.js +2 -0
  89. package/dist/cjs/dist-types/price.js +2 -0
  90. package/dist/cjs/dist-types/product-category.js +2 -0
  91. package/dist/cjs/dist-types/product-draft.js +2 -0
  92. package/dist/cjs/dist-types/product.js +2 -0
  93. package/dist/cjs/dist-types/stock.js +2 -0
  94. package/dist/cjs/dist-types/total.js +2 -0
  95. package/dist/cjs/index.js +39 -0
  96. package/dist/cjs/src-public/airtable.js +625 -0
  97. package/dist/cjs/src-public/bigquery.js +59 -0
  98. package/dist/cjs/src-public/centra.js +1053 -0
  99. package/dist/cjs/src-public/cloudtasks.js +207 -0
  100. package/dist/cjs/src-public/dato.js +680 -0
  101. package/dist/cjs/src-public/env.js +900 -0
  102. package/dist/cjs/src-public/getEventTime.js +28 -0
  103. package/dist/cjs/src-public/index.js +52 -0
  104. package/dist/cjs/src-public/ipValidation.js +167 -0
  105. package/dist/cjs/src-public/jwks.js +108 -0
  106. package/dist/cjs/src-public/pubsub.js +423 -0
  107. package/dist/cjs/src-public/rateLimit.js +140 -0
  108. package/dist/cjs/src-public/redisClient.js +44 -0
  109. package/dist/cjs/src-public/runtime.js +145 -0
  110. package/dist/cjs/src-public/sanitize.js +25 -0
  111. package/dist/cjs/src-public/scheduler.js +247 -0
  112. package/dist/cjs/src-public/secret.js +16 -0
  113. package/dist/cjs/src-public/setHeaders.js +16 -0
  114. package/dist/cjs/src-public/types.js +2 -0
  115. package/dist/cjs/src-public/utils.js +457 -0
  116. package/dist/cjs/src-public/validate.js +40 -0
  117. package/dist/package.json +6 -3
  118. package/dist/types/dist-dereferenced/asset.d.ts +51 -0
  119. package/dist/types/dist-dereferenced/category.d.ts +58 -0
  120. package/dist/types/dist-dereferenced/country.d.ts +8 -0
  121. package/dist/types/dist-dereferenced/currency.d.ts +8 -0
  122. package/dist/types/dist-dereferenced/customer-segment.d.ts +26 -0
  123. package/dist/types/dist-dereferenced/development-colour.d.ts +114 -0
  124. package/dist/types/dist-dereferenced/index.d.ts +16 -0
  125. package/dist/types/dist-dereferenced/marketing-preferences.d.ts +31 -0
  126. package/dist/types/dist-dereferenced/messages/ean-change.d.ts +17 -0
  127. package/dist/types/dist-dereferenced/messages/index.d.ts +6 -0
  128. package/dist/types/dist-dereferenced/messages/order/delivered.d.ts +3414 -0
  129. package/dist/types/dist-dereferenced/messages/order/index.d.ts +6 -0
  130. package/dist/types/dist-dereferenced/messages/order/new.d.ts +3414 -0
  131. package/dist/types/dist-dereferenced/messages/order/processed.d.ts +3408 -0
  132. package/dist/types/dist-dereferenced/messages/order/return-initiated.d.ts +3414 -0
  133. package/dist/types/dist-dereferenced/messages/order/returned.d.ts +3413 -0
  134. package/dist/types/dist-dereferenced/messages/order/shipped.d.ts +3412 -0
  135. package/dist/types/dist-dereferenced/messages/price-change.d.ts +67 -0
  136. package/dist/types/dist-dereferenced/messages/product-category.d.ts +33 -0
  137. package/dist/types/dist-dereferenced/messages/product-draft.d.ts +589 -0
  138. package/dist/types/dist-dereferenced/messages/product.d.ts +628 -0
  139. package/dist/types/dist-dereferenced/money.d.ts +41 -0
  140. package/dist/types/dist-dereferenced/order/address.d.ts +98 -0
  141. package/dist/types/dist-dereferenced/order/customer.d.ts +309 -0
  142. package/dist/types/dist-dereferenced/order/index.d.ts +3404 -0
  143. package/dist/types/dist-dereferenced/order/item.d.ts +336 -0
  144. package/dist/types/dist-dereferenced/order/orderMain.d.ts +3399 -0
  145. package/dist/types/dist-dereferenced/order/payment.d.ts +94 -0
  146. package/dist/types/dist-dereferenced/order/shipment/index.d.ts +253 -0
  147. package/dist/types/dist-dereferenced/order/shipment/item/index.d.ts +19 -0
  148. package/dist/types/dist-dereferenced/order/shipment/item/itemMain.d.ts +18 -0
  149. package/dist/types/dist-dereferenced/order/shipment/item/returned.d.ts +34 -0
  150. package/dist/types/dist-dereferenced/order/shipment/pos/index.d.ts +282 -0
  151. package/dist/types/dist-dereferenced/order/shipment/pos/outbound.d.ts +320 -0
  152. package/dist/types/dist-dereferenced/order/shipment/pos/posMain.d.ts +280 -0
  153. package/dist/types/dist-dereferenced/order/shipment/pos/return.d.ts +331 -0
  154. package/dist/types/dist-dereferenced/order/shipment/shipmentMain.d.ts +250 -0
  155. package/dist/types/dist-dereferenced/order/shipment/shipped/index.d.ts +288 -0
  156. package/dist/types/dist-dereferenced/order/shipment/shipped/outbound.d.ts +286 -0
  157. package/dist/types/dist-dereferenced/order/shipment/shipped/return.d.ts +287 -0
  158. package/dist/types/dist-dereferenced/order/shipment/shipped/shippedMain.d.ts +286 -0
  159. package/dist/types/dist-dereferenced/price.d.ts +58 -0
  160. package/dist/types/dist-dereferenced/product-category.d.ts +24 -0
  161. package/dist/types/dist-dereferenced/product-draft.d.ts +580 -0
  162. package/dist/types/dist-dereferenced/product.d.ts +619 -0
  163. package/dist/types/dist-dereferenced/stock.d.ts +74 -0
  164. package/dist/types/dist-dereferenced/total.d.ts +172 -0
  165. package/dist/types/dist-types/asset.d.ts +16 -0
  166. package/dist/types/dist-types/category.d.ts +20 -0
  167. package/dist/types/dist-types/country.d.ts +2 -0
  168. package/dist/types/dist-types/currency.d.ts +2 -0
  169. package/dist/types/dist-types/customer-segment.d.ts +6 -0
  170. package/dist/types/dist-types/development-colour.d.ts +31 -0
  171. package/dist/types/dist-types/index.d.ts +16 -0
  172. package/dist/types/dist-types/marketing-preferences.d.ts +9 -0
  173. package/dist/types/dist-types/messages/ean-change.d.ts +5 -0
  174. package/dist/types/dist-types/messages/index.d.ts +6 -0
  175. package/dist/types/dist-types/messages/order/delivered.d.ts +292 -0
  176. package/dist/types/dist-types/messages/order/index.d.ts +6 -0
  177. package/dist/types/dist-types/messages/order/new.d.ts +292 -0
  178. package/dist/types/dist-types/messages/order/processed.d.ts +292 -0
  179. package/dist/types/dist-types/messages/order/return-initiated.d.ts +292 -0
  180. package/dist/types/dist-types/messages/order/returned.d.ts +293 -0
  181. package/dist/types/dist-types/messages/order/shipped.d.ts +293 -0
  182. package/dist/types/dist-types/messages/price-change.d.ts +16 -0
  183. package/dist/types/dist-types/messages/product-category.d.ts +7 -0
  184. package/dist/types/dist-types/messages/product-draft.d.ts +136 -0
  185. package/dist/types/dist-types/messages/product.d.ts +144 -0
  186. package/dist/types/dist-types/money.d.ts +11 -0
  187. package/dist/types/dist-types/order/address.d.ts +27 -0
  188. package/dist/types/dist-types/order/customer.d.ts +81 -0
  189. package/dist/types/dist-types/order/index.d.ts +8 -0
  190. package/dist/types/dist-types/order/item.d.ts +85 -0
  191. package/dist/types/dist-types/order/orderMain.d.ts +289 -0
  192. package/dist/types/dist-types/order/payment.d.ts +26 -0
  193. package/dist/types/dist-types/order/shipment/index.d.ts +6 -0
  194. package/dist/types/dist-types/order/shipment/item/index.d.ts +4 -0
  195. package/dist/types/dist-types/order/shipment/item/itemMain.d.ts +6 -0
  196. package/dist/types/dist-types/order/shipment/item/returned.d.ts +11 -0
  197. package/dist/types/dist-types/order/shipment/pos/index.d.ts +5 -0
  198. package/dist/types/dist-types/order/shipment/pos/outbound.d.ts +76 -0
  199. package/dist/types/dist-types/order/shipment/pos/posMain.d.ts +71 -0
  200. package/dist/types/dist-types/order/shipment/pos/return.d.ts +75 -0
  201. package/dist/types/dist-types/order/shipment/shipmentMain.d.ts +62 -0
  202. package/dist/types/dist-types/order/shipment/shipped/index.d.ts +5 -0
  203. package/dist/types/dist-types/order/shipment/shipped/outbound.d.ts +68 -0
  204. package/dist/types/dist-types/order/shipment/shipped/return.d.ts +68 -0
  205. package/dist/types/dist-types/order/shipment/shipped/shippedMain.d.ts +69 -0
  206. package/dist/types/dist-types/price.d.ts +15 -0
  207. package/dist/types/dist-types/product-category.d.ts +6 -0
  208. package/dist/types/dist-types/product-draft.d.ts +135 -0
  209. package/dist/types/dist-types/product.d.ts +143 -0
  210. package/dist/types/dist-types/stock.d.ts +19 -0
  211. package/dist/types/dist-types/total.d.ts +44 -0
  212. package/dist/types/index.d.ts +3 -0
  213. package/dist/types/src-public/airtable.d.ts +227 -0
  214. package/dist/types/src-public/bigquery.d.ts +17 -0
  215. package/dist/types/src-public/centra.d.ts +176 -0
  216. package/dist/types/src-public/cloudtasks.d.ts +74 -0
  217. package/dist/types/src-public/dato.d.ts +95 -0
  218. package/dist/types/src-public/env.d.ts +144 -0
  219. package/dist/types/src-public/getEventTime.d.ts +1 -0
  220. package/dist/types/src-public/index.d.ts +18 -0
  221. package/dist/types/src-public/ipValidation.d.ts +15 -0
  222. package/dist/types/src-public/jwks.d.ts +15 -0
  223. package/dist/types/src-public/pubsub.d.ts +95 -0
  224. package/dist/types/src-public/rateLimit.d.ts +21 -0
  225. package/dist/types/src-public/redisClient.d.ts +6 -0
  226. package/dist/types/src-public/runtime.d.ts +58 -0
  227. package/dist/types/src-public/sanitize.d.ts +4 -0
  228. package/dist/types/src-public/scheduler.d.ts +71 -0
  229. package/dist/types/src-public/secret.d.ts +6 -0
  230. package/dist/types/src-public/setHeaders.d.ts +13 -0
  231. package/dist/types/src-public/types.d.ts +264 -0
  232. package/dist/types/src-public/utils.d.ts +101 -0
  233. package/dist/types/src-public/validate.d.ts +9 -0
  234. package/package.json +7 -4
  235. package/dist/index.d.ts +0 -34970
  236. package/dist/index.js +0 -113296
@@ -0,0 +1,1053 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const crypto_1 = __importDefault(require("crypto"));
7
+ const env_1 = __importDefault(require("./env"));
8
+ const utils_1 = require("./utils");
9
+ const CACHE_EXPIRATION_MS = 1000 * 60 * 60 * 3;
10
+ class CentraHelper extends env_1.default {
11
+ opts;
12
+ shaToken;
13
+ graphqlUrl;
14
+ constructor(options, opts) {
15
+ super(options);
16
+ this.opts = opts;
17
+ this.shaToken = crypto_1.default.createHash('sha256').update(opts.accessToken).digest('hex').slice(0, 8);
18
+ const parsedUrl = new URL(opts.centraApiBase);
19
+ if (!parsedUrl.pathname.includes('/graphql')) {
20
+ this.graphqlUrl = `${opts.centraApiBase}/graphql`;
21
+ }
22
+ else {
23
+ this.graphqlUrl = opts.centraApiBase;
24
+ }
25
+ }
26
+ static isCentraErrors(data) {
27
+ return typeof data === 'object' && data !== null && 'errors' in data;
28
+ }
29
+ async fetch(arg1, arg2) {
30
+ let request;
31
+ if (typeof arg1 === 'string' || arg1 instanceof URL) {
32
+ request = new Request(arg1, arg2);
33
+ }
34
+ else if (arg1 instanceof Request) {
35
+ request = arg1;
36
+ }
37
+ else {
38
+ throw new Error('Invalid argument type');
39
+ }
40
+ request.headers.set('Authorization', `Bearer ${this.opts.accessToken}`);
41
+ request.headers.set('Content-Type', 'application/json');
42
+ const errors = [];
43
+ let response;
44
+ try {
45
+ response = await fetch(request);
46
+ }
47
+ catch (error) {
48
+ errors.push({ message: `Failed to fetch: ${error instanceof Error ? error.message : String(error)}`, path: 'global' });
49
+ return { errors };
50
+ }
51
+ if (response.status === 429) {
52
+ if (this.workflowRoot != null) {
53
+ (0, utils_1.setRetryAfterVariable)(response, { root: this.workflowRoot });
54
+ }
55
+ else {
56
+ this.logging.log('Warning: setRetryAfterVariable is not available because the runtime has not been initialized with a full options object');
57
+ }
58
+ }
59
+ let resp;
60
+ try {
61
+ resp = await response.json();
62
+ }
63
+ catch (error) {
64
+ errors.push({ message: `Failed to parse response: ${error instanceof Error ? error.message : String(error)}`, path: 'global' });
65
+ return { errors, limitExceeded: response.status === 429 };
66
+ }
67
+ if (resp.errors) {
68
+ errors.push(resp.errors.map((x) => x.message).join('\n'));
69
+ }
70
+ else if (resp.data) {
71
+ const userErrors = resp.data?.userErrors;
72
+ if (userErrors) {
73
+ errors.push(...userErrors.map((x) => ({ message: x.message, path: x.path })));
74
+ return { errors, limitExceeded: response.status === 429 };
75
+ }
76
+ }
77
+ if (response.status === 429) {
78
+ resp.limitExceeded = true;
79
+ }
80
+ return resp;
81
+ }
82
+ getCacheKeyForStyleId(styleId) {
83
+ return `centra_${this.shaToken}_style_id_${styleId}`;
84
+ }
85
+ getCacheKeyForCountryCode(countryCode) {
86
+ return `centra_${this.shaToken}_country_code_${countryCode}`;
87
+ }
88
+ getCacheKeyForMarkets() {
89
+ return `centra_${this.shaToken}_market_external_id`;
90
+ }
91
+ getCacheKeyForMarket(marketExternalId) {
92
+ return `${this.getCacheKeyForMarkets()}_${marketExternalId}`;
93
+ }
94
+ getCacheKeyForSizeCharts() {
95
+ return `centra_${this.shaToken}_size_chart_external_id`;
96
+ }
97
+ getCacheKeyForCountries() {
98
+ return `centra_${this.shaToken}_country_iso2_code`;
99
+ }
100
+ getCacheKeyForCountry(countryIso2Code) {
101
+ return `${this.getCacheKeyForCountries()}_${countryIso2Code}`;
102
+ }
103
+ getCacheKeyForSizeChart(sizeChartExternalId) {
104
+ return `${this.getCacheKeyForSizeCharts()}_${sizeChartExternalId}`;
105
+ }
106
+ getCacheKeyForCentraProduct(productExternalId) {
107
+ return `centra_${this.shaToken}_product_${productExternalId}`;
108
+ }
109
+ getCacheKeyForCentraVariant(variantExternalId) {
110
+ return `centra_${this.shaToken}_variant_${variantExternalId}`;
111
+ }
112
+ getCacheKeyForCentraDisplayByVariantId(centraVariantId) {
113
+ return `centra_${this.shaToken}_display_variant_${centraVariantId}`;
114
+ }
115
+ getCacheKeyForCentraDisplayByProductId(centraProductId) {
116
+ return `centra_${this.shaToken}_display_product_${centraProductId}`;
117
+ }
118
+ getCacheKeyForCentraWarehouses() {
119
+ return `centra_${this.shaToken}_warehouse_external_id`;
120
+ }
121
+ getCacheKeyForCentraWarehouse(warehouseExternalId) {
122
+ return `${this.getCacheKeyForCentraWarehouses()}_${warehouseExternalId}`;
123
+ }
124
+ getCacheKeyForCentraPricelists() {
125
+ return `centra_${this.shaToken}_pricelist_external_id`;
126
+ }
127
+ getCacheKeyForCentraPricelist(pricelistExternalId) {
128
+ return `${this.getCacheKeyForCentraPricelists()}_${pricelistExternalId}`;
129
+ }
130
+ async fetchCountries(iso2Codes) {
131
+ const result = [];
132
+ const limit = 200;
133
+ let page = 0;
134
+ let hasNextPage = true;
135
+ do {
136
+ const data = await this.fetch(this.graphqlUrl, {
137
+ method: 'POST',
138
+ body: JSON.stringify({
139
+ query: `
140
+ query countries($page: Int, $limit: Int, $iso2Codes: [String!]) {
141
+ countries(limit: $limit, page: $page${iso2Codes ? `, where: { ISO2: $iso2Codes }` : ''} ) {
142
+ id
143
+ name
144
+ code
145
+ }
146
+ }
147
+ `,
148
+ variables: { page: page, limit: limit, iso2Codes: iso2Codes }
149
+ })
150
+ });
151
+ if (CentraHelper.isCentraErrors(data)) {
152
+ return data;
153
+ }
154
+ if ((data.data?.countries?.length || 0) < limit) {
155
+ hasNextPage = false;
156
+ }
157
+ else {
158
+ page++;
159
+ }
160
+ for (const country of data.data?.countries || []) {
161
+ result.push({
162
+ id: country.id,
163
+ name: country.name,
164
+ code: country.code
165
+ });
166
+ }
167
+ } while (hasNextPage);
168
+ return result;
169
+ }
170
+ async fetchCentraWarehouses(externalIds) {
171
+ const result = [];
172
+ const limit = 200;
173
+ let page = 0;
174
+ let hasNextPage = true;
175
+ do {
176
+ const data = await this.fetch(this.graphqlUrl, {
177
+ method: 'POST',
178
+ body: JSON.stringify({
179
+ query: `
180
+ query GetWarehouses($limit: Int, $page: Int${externalIds ? `, $externalIds: WarehousesFilter` : ''}) {
181
+ warehouses(${externalIds ? `where: $externalIds, ` : ''}limit: $limit, page: $page) {
182
+ id
183
+ externalId
184
+ name
185
+ status
186
+ }
187
+ }
188
+ `,
189
+ variables: { page: page, limit: limit, externalIds: { externalId: externalIds } }
190
+ })
191
+ });
192
+ if (CentraHelper.isCentraErrors(data)) {
193
+ return data;
194
+ }
195
+ if ((data.data?.warehouses?.length || 0) < limit) {
196
+ hasNextPage = false;
197
+ }
198
+ else {
199
+ page++;
200
+ }
201
+ for (const warehouse of data.data?.warehouses || []) {
202
+ result.push({
203
+ id: warehouse.id,
204
+ name: warehouse.name,
205
+ status: warehouse.status,
206
+ externalId: warehouse.externalId
207
+ });
208
+ }
209
+ } while (hasNextPage);
210
+ return result;
211
+ }
212
+ async fetchCentraCampaigns(names) {
213
+ const limit = (names?.length || 0) > 200 ? 200 : (names?.length || 0);
214
+ let nextCursor = null;
215
+ const result = {};
216
+ do {
217
+ const data = await this.fetch(`${this.graphqlUrl}`, {
218
+ method: 'POST',
219
+ body: JSON.stringify({
220
+ query: `
221
+ query campaignConnection (${names ? `$input: CampaignFilter, ` : ''}$limit: Int, $after: String) {
222
+ campaignConnection(${names ? `where: $input, ` : ''}first: $limit, after: $after) {
223
+ pageInfo {
224
+ endCursor
225
+ hasNextPage
226
+ }
227
+ edges {
228
+ node {
229
+ id
230
+ name
231
+ }
232
+ }
233
+ }
234
+ }
235
+ `,
236
+ variables: {
237
+ input: {
238
+ name: {
239
+ equals: names,
240
+ }
241
+ },
242
+ after: nextCursor,
243
+ limit: limit,
244
+ }
245
+ })
246
+ });
247
+ if (CentraHelper.isCentraErrors(data)) {
248
+ if (!names) {
249
+ return data;
250
+ }
251
+ for (let i = 0; i < (names?.length || 0); i++) {
252
+ const name = names[i];
253
+ result[name] = new Error(data.errors.map((x, idx) => `${idx + 1}. ${x.message}`).join('\n'));
254
+ }
255
+ }
256
+ else {
257
+ const campaignConnection = data.data?.campaignConnection;
258
+ if (campaignConnection && campaignConnection.pageInfo?.hasNextPage && campaignConnection.pageInfo.endCursor && campaignConnection.pageInfo.endCursor !== nextCursor) {
259
+ nextCursor = campaignConnection.pageInfo?.endCursor;
260
+ }
261
+ else {
262
+ nextCursor = null;
263
+ }
264
+ if (campaignConnection && campaignConnection.edges?.length) {
265
+ for (let i = campaignConnection.edges.length; i < (names?.length || 0); i++) {
266
+ const { node } = campaignConnection.edges[i];
267
+ result[node.name] = node;
268
+ }
269
+ }
270
+ }
271
+ } while (nextCursor !== null);
272
+ return result;
273
+ }
274
+ async fetchPricelists(names) {
275
+ const result = [];
276
+ const limit = 200;
277
+ let page = 0;
278
+ let hasNextPage = true;
279
+ do {
280
+ const data = await this.fetch(this.graphqlUrl, {
281
+ method: 'POST',
282
+ body: JSON.stringify({
283
+ query: `
284
+ query GetPricelists($limit: Int, $page: Int${names ? `, $input: PricelistFilter` : ''}) {
285
+ pricelists(${names ? `where: $input, ` : ''}limit: $limit, page: $page) {
286
+ id
287
+ externalId
288
+ name,
289
+ campaigns {
290
+ id
291
+ name
292
+ status
293
+ }
294
+ currency {
295
+ id
296
+ name
297
+ code
298
+ }
299
+ }
300
+ }
301
+ `,
302
+ variables: {
303
+ page: page, limit: limit, input: {
304
+ name: {
305
+ equals: names
306
+ }
307
+ }
308
+ }
309
+ })
310
+ });
311
+ if (CentraHelper.isCentraErrors(data)) {
312
+ return data;
313
+ }
314
+ if ((data.data?.pricelists?.length || 0) < limit) {
315
+ hasNextPage = false;
316
+ }
317
+ else {
318
+ page++;
319
+ }
320
+ for (const pricelist of data.data?.pricelists || []) {
321
+ result.push(pricelist);
322
+ }
323
+ } while (hasNextPage);
324
+ return result;
325
+ }
326
+ async fetchCentraMarkets(externalIds) {
327
+ const result = [];
328
+ const limit = 200;
329
+ let page = 0;
330
+ let hasNextPage = true;
331
+ do {
332
+ const data = await this.fetch(this.graphqlUrl, {
333
+ method: 'POST',
334
+ body: JSON.stringify({
335
+ query: `
336
+ query GetMarkets($limit: Int, $page: Int${externalIds ? `, $externalIds: MarketsFilter` : ''}) {
337
+ markets(${externalIds ? `where: $externalIds, ` : ''}limit: $limit, page: $page) {
338
+ id
339
+ externalId
340
+ name
341
+ }
342
+ }
343
+ `,
344
+ variables: { page: page, limit: limit, externalIds: { externalId: externalIds } }
345
+ })
346
+ });
347
+ if (CentraHelper.isCentraErrors(data)) {
348
+ return data;
349
+ }
350
+ if ((data.data?.markets?.length || 0) < limit) {
351
+ hasNextPage = false;
352
+ }
353
+ else {
354
+ page++;
355
+ }
356
+ for (const market of data.data?.markets || []) {
357
+ result.push(market);
358
+ }
359
+ } while (hasNextPage);
360
+ return result;
361
+ }
362
+ async fetchCentraSizeCharts(externalIds) {
363
+ const result = [];
364
+ const limit = 200;
365
+ let page = 0;
366
+ let hasNextPage = true;
367
+ do {
368
+ const data = await this.fetch(this.graphqlUrl, {
369
+ method: 'POST',
370
+ body: JSON.stringify({
371
+ query: `
372
+ query sizeCharts($page: Int, $limit: Int${externalIds ? `, $externalIds: SizeChartFilter` : ''}) {
373
+ sizeCharts(${externalIds ? `where: $externalIds, ` : ''}limit: $limit, page: $page) {
374
+ id
375
+ name
376
+ externalId
377
+ sizes {
378
+ id
379
+ name
380
+ externalId
381
+ localized(where: { name: { equals: "System" } }) {
382
+ localizedName
383
+ }
384
+ }
385
+ }
386
+ }
387
+ `,
388
+ variables: { page: page, limit: limit, externalIds: { externalId: externalIds } }
389
+ })
390
+ });
391
+ if (CentraHelper.isCentraErrors(data)) {
392
+ return data;
393
+ }
394
+ if ((data.data?.sizeCharts?.length || 0) < limit) {
395
+ hasNextPage = false;
396
+ }
397
+ else {
398
+ page++;
399
+ }
400
+ for (const sizeChart of data.data?.sizeCharts || []) {
401
+ result.push({
402
+ id: sizeChart.id,
403
+ name: sizeChart.name,
404
+ externalId: sizeChart.externalId,
405
+ sizes: sizeChart.sizes.map((x) => ({
406
+ id: x.id,
407
+ name: x.name,
408
+ externalId: x.externalId,
409
+ localizedName: x.localized[0]?.localizedName || undefined
410
+ }))
411
+ });
412
+ }
413
+ } while (hasNextPage);
414
+ return result;
415
+ }
416
+ async fetchCentraProducts(styleIds) {
417
+ const limit = styleIds.length > 200 ? 200 : styleIds.length;
418
+ let nextCursor = null;
419
+ const result = {};
420
+ do {
421
+ const data = await this.fetch(`${this.graphqlUrl}`, {
422
+ method: 'POST',
423
+ body: JSON.stringify({
424
+ query: `
425
+ query productConnection ($input: ProductFilter, $limit: Int, $after: String) {
426
+ productConnection(where: $input, first: $limit, after: $after) {
427
+ pageInfo {
428
+ endCursor
429
+ hasNextPage
430
+ }
431
+ edges {
432
+ node {
433
+ id
434
+ externalId
435
+ productNumber
436
+ name
437
+ status
438
+ prices {
439
+ price {
440
+ value
441
+ }
442
+ pricelist {
443
+ id
444
+ name
445
+ }
446
+ }
447
+ }
448
+ }
449
+ }
450
+ }
451
+ `,
452
+ variables: {
453
+ input: {
454
+ externalId: styleIds,
455
+ },
456
+ after: nextCursor,
457
+ limit: limit,
458
+ }
459
+ })
460
+ });
461
+ if (data && data.errors) {
462
+ for (let i = 0; i < styleIds.length; i++) {
463
+ const styleId = styleIds[i];
464
+ result[styleId] = new Error(data.errors.map((x, idx) => `${idx + 1}. ${x.message}`).join('\n'));
465
+ }
466
+ }
467
+ if (data && data.data?.productConnection?.pageInfo?.hasNextPage && data.data.productConnection.pageInfo.endCursor && data.data.productConnection.pageInfo.endCursor !== nextCursor) {
468
+ nextCursor = data.data.productConnection?.pageInfo?.endCursor;
469
+ }
470
+ else {
471
+ nextCursor = null;
472
+ }
473
+ if (data && data.data?.productConnection?.edges?.length) {
474
+ for (let i = data.data.productConnection.edges.length; i < styleIds.length; i++) {
475
+ const { node } = data.data.productConnection.edges[i];
476
+ result[node.externalId] = node;
477
+ }
478
+ }
479
+ } while (nextCursor !== null);
480
+ return result;
481
+ }
482
+ async fetchCentraDisplays(inputIds, inputIdType) {
483
+ const limit = inputIds.length > 200 ? 200 : inputIds.length;
484
+ let nextCursor = null;
485
+ const result = {};
486
+ do {
487
+ const data = await this.fetch(`${this.graphqlUrl}`, {
488
+ method: 'POST',
489
+ body: JSON.stringify({
490
+ query: `
491
+ query displayConnection ($input: DisplayFilter, $limit: Int, $after: String) {
492
+ displayConnection(where: $input, first: $limit, after: $after) {
493
+ pageInfo {
494
+ endCursor,
495
+ hasNextPage
496
+ }
497
+ edges {
498
+ node {
499
+ id
500
+ externalId
501
+ status
502
+ url
503
+ metaTitle
504
+ metaDescription
505
+ name
506
+ categories {
507
+ id
508
+ name
509
+ url
510
+ isTopCategory
511
+ }
512
+ markets {
513
+ id
514
+ name
515
+ }
516
+ product {
517
+ id
518
+ externalId
519
+ status
520
+ }
521
+ productVariant {
522
+ id
523
+ externalId
524
+ status
525
+ variantNumber
526
+ }
527
+ }
528
+ }
529
+ }
530
+ }
531
+ `,
532
+ variables: {
533
+ input: {
534
+ ...(inputIdType === 'variant' ? { productVariantId: inputIds } : { productId: inputIds }),
535
+ },
536
+ after: nextCursor,
537
+ limit: limit,
538
+ }
539
+ })
540
+ });
541
+ if (data.limitExceeded) {
542
+ for (let i = 0; i < inputIds.length; i++) {
543
+ const inputId = inputIds[i];
544
+ if (!result[inputId]) {
545
+ result[inputId] = new Error(`Rate limit exceeded for product ID ${inputId}`);
546
+ }
547
+ }
548
+ return result;
549
+ }
550
+ if (data?.errors) {
551
+ nextCursor = null;
552
+ for (let i = 0; i < inputIds.length; i++) {
553
+ const inputId = inputIds[i];
554
+ result[inputId] = new Error(data.errors.map((x, idx) => `${idx + 1}. ${x.message}`).join('\n'));
555
+ }
556
+ }
557
+ if (data.data?.displayConnection?.pageInfo?.hasNextPage && data.data.displayConnection.pageInfo.endCursor && data.data.displayConnection.pageInfo.endCursor !== nextCursor) {
558
+ nextCursor = data.data.displayConnection?.pageInfo?.endCursor;
559
+ }
560
+ else {
561
+ nextCursor = null;
562
+ }
563
+ const edges = data.data?.displayConnection?.edges || [];
564
+ if (edges.length) {
565
+ for (let i = 0; i < edges.length; i++) {
566
+ const { node } = edges[i];
567
+ if (inputIdType === 'variant') {
568
+ result[+node.productVariant[0].id] = [];
569
+ result[+node.productVariant[0].id].push(node);
570
+ }
571
+ else {
572
+ result[+node.product.id] = [];
573
+ result[+node.product.id].push(node);
574
+ }
575
+ }
576
+ }
577
+ } while (nextCursor !== null);
578
+ return result;
579
+ }
580
+ async fetchCentraVariants(productIds) {
581
+ const limit = productIds.length > 200 ? 200 : productIds.length;
582
+ let nextCursor = null;
583
+ const result = {};
584
+ do {
585
+ const data = await this.fetch(`${this.graphqlUrl}`, {
586
+ method: 'POST',
587
+ body: JSON.stringify({
588
+ query: `
589
+ query productVariantConnection ($input: ProductVariantFilter, $limit: Int, $after: String) {
590
+ productVariantConnection(where: $input, first: $limit, after: $after) {
591
+ pageInfo {
592
+ endCursor,
593
+ hasNextPage
594
+ }
595
+ edges {
596
+ node {
597
+ id
598
+ externalId
599
+ status
600
+ name
601
+ variantNumber
602
+ product {
603
+ status
604
+ externalId
605
+ id
606
+ name
607
+ productNumber
608
+ prices {
609
+ price {
610
+ value
611
+ }
612
+ pricelist {
613
+ id
614
+ name
615
+ }
616
+ }
617
+ }
618
+ sizeChart {
619
+ externalId
620
+ id
621
+ }
622
+ productSizes {
623
+ id
624
+ EAN
625
+ SKU
626
+ isActive
627
+ sizeNumber
628
+ size {
629
+ id
630
+ name
631
+ }
632
+ }
633
+ }
634
+ }
635
+ }
636
+ }
637
+ `,
638
+ variables: {
639
+ input: {
640
+ externalId: productIds,
641
+ },
642
+ after: nextCursor,
643
+ limit: limit,
644
+ }
645
+ })
646
+ });
647
+ if (data.limitExceeded) {
648
+ for (let i = 0; i < productIds.length; i++) {
649
+ const productId = productIds[i];
650
+ if (!result[productId]) {
651
+ result[productId] = new Error(`Rate limit exceeded for product ID ${productId}`);
652
+ }
653
+ }
654
+ return result;
655
+ }
656
+ if (data?.errors) {
657
+ nextCursor = null;
658
+ for (let i = 0; i < productIds.length; i++) {
659
+ const productId = productIds[i];
660
+ result[productId] = new Error(data.errors.map((x, idx) => `${idx + 1}. ${x.message}`).join('\n'));
661
+ }
662
+ }
663
+ if (data.data?.productVariantConnection?.pageInfo?.hasNextPage && data.data.productVariantConnection.pageInfo.endCursor && data.data.productVariantConnection.pageInfo.endCursor !== nextCursor) {
664
+ nextCursor = data.data.productVariantConnection?.pageInfo?.endCursor;
665
+ }
666
+ else {
667
+ nextCursor = null;
668
+ }
669
+ const edges = data.data?.productVariantConnection?.edges || [];
670
+ if (edges.length) {
671
+ for (let i = 0; i < edges.length; i++) {
672
+ const { node } = edges[i];
673
+ result[node.externalId] = node;
674
+ }
675
+ }
676
+ } while (nextCursor !== null);
677
+ return result;
678
+ }
679
+ async getCentraWarehouses(externalIds, alwaysFetch = false) {
680
+ if (externalIds && !externalIds.length) {
681
+ return {};
682
+ }
683
+ let warehouseInCache = {};
684
+ let dedupedExternalIds = [];
685
+ let warehousesToFetch = null;
686
+ if (!alwaysFetch) {
687
+ if (!externalIds) {
688
+ externalIds = (await this.get(this.getCacheKeyForCentraWarehouses(), 'env', {
689
+ isEphemeral: true,
690
+ encrypted: false,
691
+ }).then(x => x ? JSON.parse(x) : null));
692
+ }
693
+ if (externalIds) {
694
+ dedupedExternalIds = externalIds.filter((x, index, self) => self.indexOf(x) === index);
695
+ warehouseInCache = Object.fromEntries(Object.entries(await this.get(dedupedExternalIds.map(x => this.getCacheKeyForCentraWarehouse(x)), 'env', {
696
+ isEphemeral: true,
697
+ encrypted: false,
698
+ })).map(([key, value]) => [key, value ? JSON.parse(value || 'null') : undefined]).filter(([_, value]) => value));
699
+ warehousesToFetch = dedupedExternalIds.filter(x => !warehouseInCache[x]);
700
+ }
701
+ }
702
+ const warehouseToSet = {};
703
+ if (!warehousesToFetch || warehousesToFetch.length) {
704
+ const warehouses = await this.fetchCentraWarehouses(warehousesToFetch);
705
+ if (CentraHelper.isCentraErrors(warehouses)) {
706
+ if (warehousesToFetch) {
707
+ for (const warehouseExternalId of warehousesToFetch) {
708
+ warehouseToSet[warehouseExternalId] = new Error(`Failed to fetch warehouse ${warehouseExternalId}: ${warehouses.errors.map((x) => x.message).join(', ')}`);
709
+ }
710
+ }
711
+ else {
712
+ return new Error(`Failed to fetch warehouses: ${warehouses.errors.map((x) => x.message).join(', ')}`);
713
+ }
714
+ }
715
+ else {
716
+ for (const warehouse of warehouses) {
717
+ warehouseToSet[warehouse.externalId] = warehouse;
718
+ }
719
+ await this.set(Object.entries(warehouseToSet).filter(([_, value]) => !(value instanceof Error)).map(([key, value]) => ({ name: this.getCacheKeyForCentraWarehouse(key), value: JSON.stringify(value) })), 'env', {
720
+ ephemeralMs: CACHE_EXPIRATION_MS,
721
+ encrypted: false,
722
+ });
723
+ if (!warehousesToFetch) {
724
+ await this.set([{
725
+ name: this.getCacheKeyForCentraWarehouses(),
726
+ value: JSON.stringify(Object.keys(warehouseToSet)),
727
+ }], 'env', {
728
+ ephemeralMs: CACHE_EXPIRATION_MS,
729
+ encrypted: false,
730
+ });
731
+ }
732
+ }
733
+ }
734
+ return Object.assign({}, warehouseInCache, warehouseToSet);
735
+ }
736
+ async getCentraPricelists(names, alwaysFetch = false) {
737
+ if (names && !names.length) {
738
+ return {};
739
+ }
740
+ let pricelistInCache = {};
741
+ let dedupedNames = [];
742
+ let pricelistsToFetch = null;
743
+ if (!alwaysFetch) {
744
+ if (!names) {
745
+ names = (await this.get(this.getCacheKeyForCentraPricelists(), 'env', {
746
+ isEphemeral: true,
747
+ encrypted: false,
748
+ }).then(x => x ? JSON.parse(x) : null));
749
+ }
750
+ if (names) {
751
+ dedupedNames = names.filter((x, index, self) => self.indexOf(x) === index);
752
+ pricelistInCache = Object.fromEntries(Object.entries(await this.get(dedupedNames.map(x => this.getCacheKeyForCentraPricelist(x)), 'env', {
753
+ isEphemeral: true,
754
+ encrypted: false,
755
+ })).map(([key, value]) => [key, value ? JSON.parse(value || 'null') : undefined]).filter(([_, value]) => value !== undefined));
756
+ pricelistsToFetch = dedupedNames.filter(x => !pricelistInCache[x]);
757
+ }
758
+ }
759
+ const pricelistToSet = {};
760
+ if (!pricelistsToFetch || pricelistsToFetch.length) {
761
+ const pricelists = await this.fetchPricelists(pricelistsToFetch);
762
+ if (CentraHelper.isCentraErrors(pricelists)) {
763
+ if (pricelistsToFetch) {
764
+ for (const priceListName of pricelistsToFetch) {
765
+ pricelistToSet[priceListName] = new Error(`Failed to fetch pricelist ${priceListName}: ${pricelists.errors.map((x) => x.message).join(', ')}`);
766
+ }
767
+ }
768
+ else {
769
+ return new Error(`Failed to fetch pricelists: ${pricelists.errors.map((x) => x.message).join(', ')}`);
770
+ }
771
+ }
772
+ else {
773
+ for (const pricelist of pricelists) {
774
+ pricelistToSet[pricelist.name] = pricelist;
775
+ }
776
+ await this.set(Object.entries(pricelistToSet).filter(([_, value]) => !(value instanceof Error)).map(([key, value]) => ({ name: this.getCacheKeyForCentraPricelist(key), value: JSON.stringify(value) })), 'env', {
777
+ ephemeralMs: CACHE_EXPIRATION_MS,
778
+ encrypted: false,
779
+ });
780
+ if (!pricelistsToFetch) {
781
+ await this.set([{
782
+ name: this.getCacheKeyForCentraPricelists(),
783
+ value: JSON.stringify(Object.keys(pricelistToSet)),
784
+ }], 'env', {
785
+ ephemeralMs: CACHE_EXPIRATION_MS,
786
+ encrypted: false,
787
+ });
788
+ }
789
+ }
790
+ }
791
+ return Object.assign({}, pricelistInCache, pricelistToSet);
792
+ }
793
+ async getCentraMarkets(externalIds, alwaysFetch = false) {
794
+ if (externalIds && !externalIds.length) {
795
+ return {};
796
+ }
797
+ let marketInCache = {};
798
+ let dedupedExternalIds = [];
799
+ let marketsToFetch = null;
800
+ if (!alwaysFetch) {
801
+ if (!externalIds) {
802
+ externalIds = (await this.get(this.getCacheKeyForMarkets(), 'env', {
803
+ isEphemeral: true,
804
+ encrypted: false,
805
+ }).then(x => x ? JSON.parse(x) : null));
806
+ }
807
+ if (externalIds) {
808
+ dedupedExternalIds = externalIds.filter((x, index, self) => self.indexOf(x) === index);
809
+ marketInCache = Object.fromEntries(Object.entries(await this.get(dedupedExternalIds.map(x => this.getCacheKeyForMarket(x)), 'env', {
810
+ isEphemeral: true,
811
+ encrypted: false,
812
+ })).map(([key, value]) => [key, value ? JSON.parse(value || 'null') : undefined]).filter(([_, value]) => value));
813
+ marketsToFetch = dedupedExternalIds.filter(x => !marketInCache[x]);
814
+ }
815
+ }
816
+ const marketToSet = {};
817
+ if (!marketsToFetch || marketsToFetch.length) {
818
+ const markets = await this.fetchCentraMarkets(marketsToFetch);
819
+ if (CentraHelper.isCentraErrors(markets)) {
820
+ if (marketsToFetch) {
821
+ for (const marketExternalId of marketsToFetch) {
822
+ marketToSet[marketExternalId] = new Error(`Failed to fetch market ${marketExternalId}: ${markets.errors.map((x) => x.message).join(', ')}`);
823
+ }
824
+ }
825
+ else {
826
+ return new Error(`Failed to fetch markets: ${markets.errors.map((x) => x.message).join(', ')}`);
827
+ }
828
+ }
829
+ else {
830
+ for (const market of markets) {
831
+ marketToSet[market.externalId] = market;
832
+ }
833
+ await this.set(Object.entries(marketToSet).filter(([_, value]) => !(value instanceof Error)).map(([key, value]) => ({ name: this.getCacheKeyForMarket(key), value: JSON.stringify(value) })), 'env', {
834
+ ephemeralMs: CACHE_EXPIRATION_MS,
835
+ encrypted: false,
836
+ });
837
+ if (!marketsToFetch) {
838
+ await this.set([{
839
+ name: this.getCacheKeyForMarkets(),
840
+ value: JSON.stringify(Object.keys(marketToSet)),
841
+ }], 'env', {
842
+ ephemeralMs: CACHE_EXPIRATION_MS,
843
+ encrypted: false,
844
+ });
845
+ }
846
+ }
847
+ }
848
+ return Object.assign({}, marketInCache, marketToSet);
849
+ }
850
+ async getCentraCountries(iso2Codes, alwaysFetch = false) {
851
+ if (iso2Codes && !iso2Codes.length) {
852
+ return {};
853
+ }
854
+ let countryInCache = {};
855
+ let dedupedIso2Codes = [];
856
+ let countriesToFetch = null;
857
+ if (!alwaysFetch) {
858
+ if (!iso2Codes) {
859
+ iso2Codes = (await this.get(this.getCacheKeyForCountries(), 'env', {
860
+ isEphemeral: true,
861
+ encrypted: false,
862
+ }).then(x => x ? JSON.parse(x) : null));
863
+ }
864
+ if (iso2Codes) {
865
+ dedupedIso2Codes = iso2Codes.filter((x, index, self) => self.indexOf(x) === index);
866
+ countryInCache = Object.fromEntries(Object.entries(await this.get(dedupedIso2Codes.map(x => this.getCacheKeyForCountry(x)), 'env', {
867
+ isEphemeral: true,
868
+ encrypted: false,
869
+ })).map(([key, value]) => [key, value ? JSON.parse(value || 'null') : undefined]).filter(([_, value]) => value));
870
+ countriesToFetch = dedupedIso2Codes.filter(x => !countryInCache[x]);
871
+ }
872
+ }
873
+ const countryToSet = {};
874
+ if (!countriesToFetch || countriesToFetch.length) {
875
+ const countries = await this.fetchCountries(countriesToFetch);
876
+ if (CentraHelper.isCentraErrors(countries)) {
877
+ if (countriesToFetch) {
878
+ for (const countryIso2Code of countriesToFetch) {
879
+ countryToSet[countryIso2Code] = new Error(`Failed to fetch country ${countryIso2Code}: ${countries.errors.map((x) => x.message).join(', ')}`);
880
+ }
881
+ }
882
+ else {
883
+ return new Error(`Failed to fetch countries: ${countries.errors.map((x) => x.message).join(', ')}`);
884
+ }
885
+ }
886
+ else {
887
+ for (const country of countries) {
888
+ countryToSet[country.code] = country;
889
+ }
890
+ await this.set(Object.entries(countryToSet).filter(([_, value]) => !(value instanceof Error)).map(([key, value]) => ({ name: this.getCacheKeyForCountry(key), value: JSON.stringify(value) })), 'env', {
891
+ ephemeralMs: CACHE_EXPIRATION_MS,
892
+ encrypted: false,
893
+ });
894
+ if (!countriesToFetch) {
895
+ await this.set([{
896
+ name: this.getCacheKeyForCountries(),
897
+ value: JSON.stringify(Object.keys(countryToSet)),
898
+ }], 'env', {
899
+ ephemeralMs: CACHE_EXPIRATION_MS,
900
+ encrypted: false,
901
+ });
902
+ }
903
+ }
904
+ }
905
+ return Object.assign({}, countryInCache, countryToSet);
906
+ }
907
+ async getCentraSizeCharts(externalIds, alwaysFetch = false) {
908
+ if (externalIds && !externalIds.length) {
909
+ return {};
910
+ }
911
+ let sizeChartInCache = {};
912
+ let dedupedExternalIds = [];
913
+ let sizeChartsToFetch = null;
914
+ if (!alwaysFetch) {
915
+ if (!externalIds) {
916
+ externalIds = (await this.get(this.getCacheKeyForSizeCharts(), 'env', {
917
+ isEphemeral: true,
918
+ encrypted: false,
919
+ }).then(x => x ? JSON.parse(x) : null));
920
+ }
921
+ if (externalIds) {
922
+ dedupedExternalIds = externalIds.filter((x, index, self) => self.indexOf(x) === index);
923
+ sizeChartInCache = Object.fromEntries(Object.entries(await this.get(dedupedExternalIds.map(x => this.getCacheKeyForSizeChart(x)), 'env', {
924
+ isEphemeral: true,
925
+ encrypted: false,
926
+ })).map(([key, value]) => [key, value ? JSON.parse(value || 'null') : undefined]).filter(([_, value]) => value));
927
+ sizeChartsToFetch = dedupedExternalIds.filter(x => !sizeChartInCache[x]);
928
+ }
929
+ }
930
+ const sizeChartToSet = {};
931
+ if (!sizeChartsToFetch || sizeChartsToFetch.length) {
932
+ const sizeCharts = await this.fetchCentraSizeCharts(sizeChartsToFetch);
933
+ if (CentraHelper.isCentraErrors(sizeCharts)) {
934
+ if (sizeChartsToFetch) {
935
+ for (const sizeChartExternalId of sizeChartsToFetch) {
936
+ sizeChartToSet[sizeChartExternalId] = new Error(`Failed to fetch size chart ${sizeChartExternalId}: ${sizeCharts.errors.map((x) => x.message).join(', ')}`);
937
+ }
938
+ }
939
+ else {
940
+ return new Error(`Failed to fetch size charts: ${sizeCharts.errors.map((x) => x.message).join(', ')}`);
941
+ }
942
+ }
943
+ else {
944
+ for (const sizeChart of sizeCharts) {
945
+ sizeChartToSet[sizeChart.externalId] = sizeChart;
946
+ }
947
+ await this.set(Object.entries(sizeChartToSet).filter(([_, value]) => !(value instanceof Error)).map(([key, value]) => ({ name: this.getCacheKeyForSizeChart(key), value: JSON.stringify(value) })), 'env', {
948
+ ephemeralMs: CACHE_EXPIRATION_MS,
949
+ encrypted: false,
950
+ });
951
+ if (!sizeChartsToFetch) {
952
+ await this.set([{
953
+ name: this.getCacheKeyForSizeCharts(),
954
+ value: JSON.stringify(Object.keys(sizeChartToSet)),
955
+ }], 'env', {
956
+ ephemeralMs: CACHE_EXPIRATION_MS,
957
+ encrypted: false,
958
+ });
959
+ }
960
+ }
961
+ }
962
+ return Object.assign({}, sizeChartInCache, sizeChartToSet);
963
+ }
964
+ async getCentraProducts(styleIds) {
965
+ const dedupedStyleIds = styleIds.filter((x, index, self) => self.indexOf(x) === index);
966
+ const productIdsInCache = await this.get(dedupedStyleIds.map(x => this.getCacheKeyForStyleId(x)), 'env', {
967
+ isEphemeral: true,
968
+ encrypted: false,
969
+ });
970
+ const productsToFetch = dedupedStyleIds.filter(x => !productIdsInCache[this.getCacheKeyForCentraProduct(x)]);
971
+ const products = await this.fetchCentraProducts(productsToFetch);
972
+ const productsToSet = {};
973
+ for (const styleId of dedupedStyleIds) {
974
+ const product = products[styleId];
975
+ productsToSet[styleId] = product;
976
+ }
977
+ this.set(Object.entries(productsToSet).filter(([_, value]) => !(value instanceof Error)).map(([key, value]) => ({ name: this.getCacheKeyForCentraProduct(key), value: JSON.stringify(value) })), 'env', {
978
+ ephemeralMs: CACHE_EXPIRATION_MS,
979
+ encrypted: false,
980
+ });
981
+ return Object.assign({}, productIdsInCache, productsToSet);
982
+ }
983
+ async getCentraDisplaysByVariantIds(centraVariantIds) {
984
+ const dedupedCentraVariantIds = centraVariantIds.filter((x, index, self) => self.indexOf(x) === index);
985
+ const displaysInCache = Object.fromEntries(Object.entries(await this.get(dedupedCentraVariantIds.map(x => this.getCacheKeyForCentraDisplayByVariantId(x)), 'env', {
986
+ isEphemeral: true,
987
+ encrypted: false,
988
+ })).map(([key, value]) => [key, value ? JSON.parse(value || 'null') : undefined]));
989
+ const displaysToFetch = dedupedCentraVariantIds.filter(x => !displaysInCache[this.getCacheKeyForCentraDisplayByVariantId(x)]);
990
+ const displays = await this.fetchCentraDisplays(displaysToFetch, 'variant');
991
+ const displaysToSet = {};
992
+ for (const centraVariantId of dedupedCentraVariantIds) {
993
+ const display = displays[centraVariantId];
994
+ displaysToSet[centraVariantId] = display;
995
+ }
996
+ this.set(Object.entries(displaysToSet).filter(([_, value]) => !(value instanceof Error)).map(([key, value]) => ({ name: this.getCacheKeyForCentraDisplayByVariantId(+key), value: JSON.stringify(value) })), 'env', {
997
+ ephemeralMs: CACHE_EXPIRATION_MS,
998
+ encrypted: false,
999
+ });
1000
+ return Object.assign({}, displaysInCache, displaysToSet);
1001
+ }
1002
+ async getCentraDisplaysByProductIds(centraProductIds) {
1003
+ const dedupedCentraProductIds = centraProductIds.filter((x, index, self) => self.indexOf(x) === index);
1004
+ const displaysInCache = Object.fromEntries(Object.entries(await this.get(dedupedCentraProductIds.map(x => this.getCacheKeyForCentraDisplayByProductId(x)), 'env', {
1005
+ isEphemeral: true,
1006
+ encrypted: false,
1007
+ })).map(([key, value]) => [key, value ? JSON.parse(value || 'null') : undefined]));
1008
+ const displaysToFetch = dedupedCentraProductIds.filter(x => !displaysInCache[this.getCacheKeyForCentraDisplayByProductId(x)]);
1009
+ const displays = await this.fetchCentraDisplays(displaysToFetch, 'product');
1010
+ const displaysToSet = {};
1011
+ for (const centraProductId of dedupedCentraProductIds) {
1012
+ const display = displays[centraProductId];
1013
+ displaysToSet[centraProductId] = display;
1014
+ }
1015
+ this.set(Object.entries(displaysToSet).filter(([_, value]) => !(value instanceof Error)).map(([key, value]) => ({ name: this.getCacheKeyForCentraDisplayByProductId(+key), value: JSON.stringify(value) })), 'env', {
1016
+ ephemeralMs: CACHE_EXPIRATION_MS,
1017
+ encrypted: false,
1018
+ });
1019
+ return Object.assign({}, displaysInCache, displaysToSet);
1020
+ }
1021
+ async getCentraVariants(productIds) {
1022
+ const dedupedProductIds = productIds.filter((x, index, self) => self.indexOf(x) === index);
1023
+ const variantsInCache = Object.fromEntries(Object.entries(await this.get(dedupedProductIds.map(x => this.getCacheKeyForCentraVariant(x)), 'env', {
1024
+ isEphemeral: true,
1025
+ encrypted: false,
1026
+ })).map(([key, value]) => [key, value ? JSON.parse(value || 'null') : undefined]));
1027
+ const variantsToFetch = dedupedProductIds.filter(x => !variantsInCache[this.getCacheKeyForCentraVariant(x)]);
1028
+ const variants = await this.fetchCentraVariants(variantsToFetch);
1029
+ const variantsToSet = {};
1030
+ for (const productId of dedupedProductIds) {
1031
+ const variant = variants[productId];
1032
+ variantsToSet[productId] = variant;
1033
+ }
1034
+ this.set(Object.entries(variantsToSet).filter(([_, value]) => !(value instanceof Error)).map(([key, value]) => ({ name: this.getCacheKeyForCentraVariant(key), value: JSON.stringify(value) })), 'env', {
1035
+ ephemeralMs: CACHE_EXPIRATION_MS,
1036
+ encrypted: false,
1037
+ });
1038
+ return Object.assign({}, variantsInCache, variantsToSet);
1039
+ }
1040
+ static centraSizeNameToSize(centraSizeName) {
1041
+ if (centraSizeName.endsWith('xS') && !centraSizeName.toLowerCase().endsWith('xxs')) {
1042
+ return centraSizeName.slice(0, -2) + 'S';
1043
+ }
1044
+ if (centraSizeName.endsWith('xT')) {
1045
+ return centraSizeName.slice(0, -2) + 'T';
1046
+ }
1047
+ if (centraSizeName.endsWith('xR')) {
1048
+ return centraSizeName.slice(0, -2);
1049
+ }
1050
+ return centraSizeName;
1051
+ }
1052
+ }
1053
+ exports.default = CentraHelper;