@fluentcommerce/fc-connect-sdk 0.1.54 → 0.1.55

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 (475) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/clients/fluent-client.js +13 -6
  3. package/dist/cjs/utils/pagination-helpers.js +38 -2
  4. package/dist/cjs/versori/fluent-versori-client.js +11 -5
  5. package/dist/esm/clients/fluent-client.js +13 -6
  6. package/dist/esm/utils/pagination-helpers.js +38 -2
  7. package/dist/esm/versori/fluent-versori-client.js +11 -5
  8. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  9. package/dist/tsconfig.tsbuildinfo +1 -1
  10. package/dist/tsconfig.types.tsbuildinfo +1 -1
  11. package/docs/00-START-HERE/EXPORT-VALIDATION.md +158 -158
  12. package/docs/00-START-HERE/cli-analyze-source-structure-guide.md +655 -655
  13. package/docs/00-START-HERE/cli-documentation-index.md +202 -202
  14. package/docs/00-START-HERE/cli-quick-reference.md +252 -252
  15. package/docs/00-START-HERE/decision-tree.md +552 -552
  16. package/docs/00-START-HERE/getting-started.md +1070 -1070
  17. package/docs/00-START-HERE/mapper-quick-decision-guide.md +235 -235
  18. package/docs/00-START-HERE/readme.md +237 -237
  19. package/docs/00-START-HERE/retailerid-configuration.md +404 -404
  20. package/docs/00-START-HERE/sdk-philosophy.md +794 -794
  21. package/docs/00-START-HERE/troubleshooting-quick-reference.md +1086 -1086
  22. package/docs/01-TEMPLATES/faq.md +686 -686
  23. package/docs/01-TEMPLATES/patterns/pattern-templates-guide.md +68 -68
  24. package/docs/01-TEMPLATES/patterns/patterns-csv-schema-validation-and-rejection-report.md +233 -233
  25. package/docs/01-TEMPLATES/patterns/patterns-custom-resolvers.md +407 -407
  26. package/docs/01-TEMPLATES/patterns/patterns-error-handling-retry.md +511 -511
  27. package/docs/01-TEMPLATES/patterns/patterns-field-mapping-universal.md +701 -701
  28. package/docs/01-TEMPLATES/patterns/patterns-large-file-splitting.md +1430 -1430
  29. package/docs/01-TEMPLATES/patterns/patterns-master-data-etl.md +2399 -2399
  30. package/docs/01-TEMPLATES/patterns/patterns-pagination-streaming.md +447 -447
  31. package/docs/01-TEMPLATES/patterns/patterns-state-duplicate-prevention.md +385 -385
  32. package/docs/01-TEMPLATES/readme.md +957 -957
  33. package/docs/01-TEMPLATES/standalone/standalone-asn-inbound-processing.md +1209 -1209
  34. package/docs/01-TEMPLATES/standalone/standalone-graphql-query-export.md +1140 -1140
  35. package/docs/01-TEMPLATES/standalone/standalone-graphql-to-parquet-partitioned-s3.md +432 -432
  36. package/docs/01-TEMPLATES/standalone/standalone-multi-channel-inventory-sync.md +1185 -1185
  37. package/docs/01-TEMPLATES/standalone/standalone-multi-source-aggregation.md +1462 -1462
  38. package/docs/01-TEMPLATES/standalone/standalone-s3-csv-batch-api.md +1390 -1390
  39. package/docs/01-TEMPLATES/standalone/standalone-s3-csv-inventory-to-batch.md +330 -330
  40. package/docs/01-TEMPLATES/standalone/standalone-scripts-guide.md +87 -87
  41. package/docs/01-TEMPLATES/standalone/standalone-sftp-xml-graphql.md +1444 -1444
  42. package/docs/01-TEMPLATES/standalone/standalone-webhook-payload-processing.md +688 -688
  43. package/docs/01-TEMPLATES/versori/business-examples/business-examples-dropship-order-routing.md +193 -193
  44. package/docs/01-TEMPLATES/versori/business-examples/business-examples-graphql-parquet-extraction.md +518 -518
  45. package/docs/01-TEMPLATES/versori/business-examples/business-examples-inter-location-transfers.md +2162 -2162
  46. package/docs/01-TEMPLATES/versori/business-examples/business-examples-pre-order-allocation.md +2226 -2226
  47. package/docs/01-TEMPLATES/versori/business-examples/business-scenarios-guide.md +87 -87
  48. package/docs/01-TEMPLATES/versori/patterns/versori-patterns-connection-validation-pattern.md +656 -656
  49. package/docs/01-TEMPLATES/versori/patterns/versori-patterns-dual-workflow-connector.md +835 -835
  50. package/docs/01-TEMPLATES/versori/patterns/versori-patterns-guide.md +108 -108
  51. package/docs/01-TEMPLATES/versori/patterns/versori-patterns-kv-state-management.md +1533 -1533
  52. package/docs/01-TEMPLATES/versori/patterns/versori-patterns-xml-response-patterns.md +1160 -1160
  53. package/docs/01-TEMPLATES/versori/versori-platform-guide.md +201 -201
  54. package/docs/01-TEMPLATES/versori/webhooks/template-webhook-asn-purchase-order.md +1906 -1906
  55. package/docs/01-TEMPLATES/versori/webhooks/template-webhook-dropship-routing.md +1074 -1074
  56. package/docs/01-TEMPLATES/versori/webhooks/template-webhook-flash-sale-reserve.md +1395 -1395
  57. package/docs/01-TEMPLATES/versori/webhooks/template-webhook-generic-xml-order.md +888 -888
  58. package/docs/01-TEMPLATES/versori/webhooks/template-webhook-payment-gateway-integration.md +2478 -2478
  59. package/docs/01-TEMPLATES/versori/webhooks/template-webhook-rma-returns-comprehensive.md +2240 -2240
  60. package/docs/01-TEMPLATES/versori/webhooks/template-webhook-xml-order-ingestion.md +2029 -2029
  61. package/docs/01-TEMPLATES/versori/webhooks/webhook-templates-guide.md +140 -140
  62. package/docs/01-TEMPLATES/versori/workflows/_examples/sample-data/inventory-mapping.json +20 -20
  63. package/docs/01-TEMPLATES/versori/workflows/_examples/sample-data/products_2025-01-22.csv +11 -11
  64. package/docs/01-TEMPLATES/versori/workflows/_examples/sample-data/sample-data-guide.md +34 -34
  65. package/docs/01-TEMPLATES/versori/workflows/_examples/workflow-examples-guide.md +36 -36
  66. package/docs/01-TEMPLATES/versori/workflows/extraction/extraction-modes-guide.md +1038 -1038
  67. package/docs/01-TEMPLATES/versori/workflows/extraction/extraction-workflows-guide.md +138 -138
  68. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/graphql-extraction-guide.md +63 -63
  69. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-fulfillments-to-sftp-csv.md +2062 -2062
  70. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-fulfillments-to-sftp-xml.md +2294 -2294
  71. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-inventory-positions-to-s3-csv.md +2461 -2461
  72. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-inventory-positions-to-sftp-xml.md +2529 -2529
  73. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-inventory-quantities-to-s3-csv.md +2464 -2464
  74. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-inventory-quantities-to-s3-json.md +1959 -1959
  75. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-orders-to-s3-csv.md +1953 -1953
  76. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-orders-to-sftp-xml.md +2541 -2541
  77. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-products-to-s3-json.md +2384 -2384
  78. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-products-to-sftp-xml.md +2445 -2445
  79. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-virtual-positions-to-s3-csv.md +2355 -2355
  80. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-virtual-positions-to-s3-json.md +2042 -2042
  81. package/docs/01-TEMPLATES/versori/workflows/extraction/graphql-queries/template-extraction-virtual-positions-to-sftp-xml.md +2726 -2726
  82. package/docs/01-TEMPLATES/versori/workflows/ingestion/batch-api/batch-api-guide.md +206 -206
  83. package/docs/01-TEMPLATES/versori/workflows/ingestion/batch-api/template-ingestion-cycle-count-reconciliation.md +2030 -2030
  84. package/docs/01-TEMPLATES/versori/workflows/ingestion/batch-api/template-ingestion-multi-channel-inventory-sync.md +1882 -1882
  85. package/docs/01-TEMPLATES/versori/workflows/ingestion/batch-api/template-ingestion-s3-csv-inventory-batch.md +2827 -2827
  86. package/docs/01-TEMPLATES/versori/workflows/ingestion/batch-api/template-ingestion-s3-json-inventory-batch.md +1952 -1952
  87. package/docs/01-TEMPLATES/versori/workflows/ingestion/batch-api/template-ingestion-s3-xml-inventory-batch.md +3289 -3289
  88. package/docs/01-TEMPLATES/versori/workflows/ingestion/batch-api/template-ingestion-sftp-csv-inventory-batch.md +3064 -3064
  89. package/docs/01-TEMPLATES/versori/workflows/ingestion/batch-api/template-ingestion-sftp-json-inventory-batch.md +3238 -3238
  90. package/docs/01-TEMPLATES/versori/workflows/ingestion/batch-api/template-ingestion-sftp-xml-inventory-batch.md +2977 -2977
  91. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/event-api-guide.md +321 -321
  92. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/template-ingestion-payload-json-order-cancel-event.md +959 -959
  93. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/template-ingestion-payload-xml-order-cancel-event.md +1170 -1170
  94. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/template-ingestion-s3-csv-product-event.md +2312 -2312
  95. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/template-ingestion-s3-json-product-event.md +2999 -2999
  96. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/template-ingestion-s3-parquet-product-event.md +2836 -2836
  97. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/template-ingestion-s3-xml-product-event.md +2395 -2395
  98. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/template-ingestion-sftp-csv-product-event.md +2295 -2295
  99. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/template-ingestion-sftp-json-product-event.md +2602 -2602
  100. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/template-ingestion-sftp-parquet-product-event.md +2589 -2589
  101. package/docs/01-TEMPLATES/versori/workflows/ingestion/event-api/template-ingestion-sftp-xml-product-event.md +3578 -3578
  102. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/graphql-mutations-guide.md +93 -93
  103. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/template-ingestion-payload-json-order-update-graphql.md +1260 -1260
  104. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/template-ingestion-payload-xml-order-update-graphql.md +1472 -1472
  105. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/template-ingestion-s3-csv-control-graphql.md +2417 -2417
  106. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/template-ingestion-s3-csv-location-graphql.md +2811 -2811
  107. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/template-ingestion-s3-csv-price-graphql.md +2619 -2619
  108. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/template-ingestion-s3-json-location-graphql.md +2807 -2807
  109. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/template-ingestion-s3-xml-location-graphql.md +2373 -2373
  110. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/template-ingestion-sftp-csv-control-graphql.md +2740 -2740
  111. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/template-ingestion-sftp-csv-location-graphql.md +2760 -2760
  112. package/docs/01-TEMPLATES/versori/workflows/ingestion/graphql-mutations/template-ingestion-sftp-json-location-graphql.md +1710 -1710
  113. package/docs/01-TEMPLATES/versori/workflows/ingestion/ingestion-workflows-guide.md +136 -136
  114. package/docs/01-TEMPLATES/versori/workflows/rubix-webhooks/rubix-webhooks-guide.md +520 -520
  115. package/docs/01-TEMPLATES/versori/workflows/rubix-webhooks/template-webhook-rubix-fulfilment-to-sftp-xml-inline.md +1418 -1418
  116. package/docs/01-TEMPLATES/versori/workflows/rubix-webhooks/template-webhook-rubix-fulfilment-to-sftp-xml-universal-mapper.md +1785 -1785
  117. package/docs/01-TEMPLATES/versori/workflows/rubix-webhooks/template-webhook-rubix-order-attribute-update.md +824 -824
  118. package/docs/01-TEMPLATES/versori/workflows/workflows-overview-guide.md +646 -646
  119. package/docs/02-CORE-GUIDES/advanced-services/advanced-services-batch-archival.md +724 -724
  120. package/docs/02-CORE-GUIDES/advanced-services/advanced-services-job-tracker.md +627 -627
  121. package/docs/02-CORE-GUIDES/advanced-services/advanced-services-partial-batch-recovery.md +561 -561
  122. package/docs/02-CORE-GUIDES/advanced-services/advanced-services-quick-reference.md +367 -367
  123. package/docs/02-CORE-GUIDES/advanced-services/advanced-services-readme.md +407 -407
  124. package/docs/02-CORE-GUIDES/advanced-services/readme.md +49 -49
  125. package/docs/02-CORE-GUIDES/api-reference/api-reference-quick-reference.md +548 -548
  126. package/docs/02-CORE-GUIDES/api-reference/event-api-input-output-reference.md +702 -1171
  127. package/docs/02-CORE-GUIDES/api-reference/examples/client-initialization.ts +286 -286
  128. package/docs/02-CORE-GUIDES/api-reference/graphql-error-classification.md +337 -337
  129. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-01-client-api.md +399 -520
  130. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-03-authentication.md +199 -199
  131. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-04-graphql-mapping.md +925 -925
  132. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-05-services.md +1198 -1198
  133. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-06-data-sources.md +1083 -1083
  134. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-07-parsers.md +1097 -1097
  135. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-08-pagination.md +513 -513
  136. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-08-types.md +545 -597
  137. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-09-error-handling.md +527 -527
  138. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-09-webhook-validation.md +514 -514
  139. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-10-extraction.md +557 -557
  140. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-10-utilities.md +412 -412
  141. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-11-cli-tools.md +423 -423
  142. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-11-error-handling.md +716 -716
  143. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-12-analyze-source-structure.md +518 -518
  144. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-12-partial-responses.md +212 -212
  145. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-12-testing.md +300 -300
  146. package/docs/02-CORE-GUIDES/api-reference/modules/api-reference-13-resolver-builder.md +322 -322
  147. package/docs/02-CORE-GUIDES/api-reference/readme.md +279 -279
  148. package/docs/02-CORE-GUIDES/auto-pagination/auto-pagination-quick-reference.md +351 -351
  149. package/docs/02-CORE-GUIDES/auto-pagination/auto-pagination-readme.md +277 -277
  150. package/docs/02-CORE-GUIDES/auto-pagination/examples/auto-pagination-readme.md +178 -178
  151. package/docs/02-CORE-GUIDES/auto-pagination/examples/common-patterns.ts +351 -351
  152. package/docs/02-CORE-GUIDES/auto-pagination/examples/paginate-products.ts +384 -384
  153. package/docs/02-CORE-GUIDES/auto-pagination/examples/paginate-virtual-positions.ts +308 -308
  154. package/docs/02-CORE-GUIDES/auto-pagination/modules/auto-pagination-01-foundations.md +470 -470
  155. package/docs/02-CORE-GUIDES/auto-pagination/modules/auto-pagination-02-quick-start.md +713 -713
  156. package/docs/02-CORE-GUIDES/auto-pagination/modules/auto-pagination-03-configuration.md +754 -754
  157. package/docs/02-CORE-GUIDES/auto-pagination/modules/auto-pagination-04-advanced-patterns.md +732 -732
  158. package/docs/02-CORE-GUIDES/auto-pagination/modules/auto-pagination-05-sdk-integration.md +847 -847
  159. package/docs/02-CORE-GUIDES/auto-pagination/modules/auto-pagination-06-troubleshooting.md +359 -359
  160. package/docs/02-CORE-GUIDES/auto-pagination/modules/auto-pagination-07-api-reference.md +462 -462
  161. package/docs/02-CORE-GUIDES/auto-pagination/readme.md +54 -54
  162. package/docs/02-CORE-GUIDES/data-sources/data-sources-file-operations-error-handling.md +1487 -1487
  163. package/docs/02-CORE-GUIDES/data-sources/data-sources-quick-reference.md +836 -836
  164. package/docs/02-CORE-GUIDES/data-sources/data-sources-readme.md +276 -276
  165. package/docs/02-CORE-GUIDES/data-sources/data-sources-sftp-credential-access-security.md +553 -553
  166. package/docs/02-CORE-GUIDES/data-sources/examples/common-patterns.ts +409 -409
  167. package/docs/02-CORE-GUIDES/data-sources/examples/data-sources-readme.md +178 -178
  168. package/docs/02-CORE-GUIDES/data-sources/examples/s3-operations.ts +308 -308
  169. package/docs/02-CORE-GUIDES/data-sources/examples/sftp-operations.ts +371 -371
  170. package/docs/02-CORE-GUIDES/data-sources/modules/data-sources-01-foundations.md +735 -735
  171. package/docs/02-CORE-GUIDES/data-sources/modules/data-sources-02-s3-operations.md +1302 -1302
  172. package/docs/02-CORE-GUIDES/data-sources/modules/data-sources-03-sftp-operations.md +1379 -1379
  173. package/docs/02-CORE-GUIDES/data-sources/modules/data-sources-04-file-patterns.md +941 -941
  174. package/docs/02-CORE-GUIDES/data-sources/modules/data-sources-05-advanced-topics.md +813 -813
  175. package/docs/02-CORE-GUIDES/data-sources/modules/data-sources-06-integration-patterns.md +486 -486
  176. package/docs/02-CORE-GUIDES/data-sources/modules/data-sources-07-troubleshooting.md +387 -387
  177. package/docs/02-CORE-GUIDES/data-sources/modules/data-sources-08-api-reference.md +417 -417
  178. package/docs/02-CORE-GUIDES/data-sources/readme.md +77 -77
  179. package/docs/02-CORE-GUIDES/error-handling-guide.md +936 -936
  180. package/docs/02-CORE-GUIDES/extraction/examples/02-core-guides-extraction-readme.md +116 -116
  181. package/docs/02-CORE-GUIDES/extraction/examples/common-patterns.ts +428 -428
  182. package/docs/02-CORE-GUIDES/extraction/examples/extract-inventory-basic.ts +187 -187
  183. package/docs/02-CORE-GUIDES/extraction/extraction-quick-reference.md +596 -596
  184. package/docs/02-CORE-GUIDES/extraction/modules/02-core-guides-extraction-01-foundations.md +514 -514
  185. package/docs/02-CORE-GUIDES/extraction/modules/02-core-guides-extraction-02-basic-extraction.md +823 -823
  186. package/docs/02-CORE-GUIDES/extraction/modules/02-core-guides-extraction-03-parquet-processing.md +507 -507
  187. package/docs/02-CORE-GUIDES/extraction/modules/02-core-guides-extraction-04-data-enrichment.md +546 -546
  188. package/docs/02-CORE-GUIDES/extraction/modules/02-core-guides-extraction-05-transformation.md +494 -494
  189. package/docs/02-CORE-GUIDES/extraction/modules/02-core-guides-extraction-06-export-formats.md +458 -458
  190. package/docs/02-CORE-GUIDES/extraction/modules/02-core-guides-extraction-06-performance.md +138 -138
  191. package/docs/02-CORE-GUIDES/extraction/modules/02-core-guides-extraction-07-api-reference.md +148 -148
  192. package/docs/02-CORE-GUIDES/extraction/modules/02-core-guides-extraction-07-optimization.md +692 -692
  193. package/docs/02-CORE-GUIDES/extraction/modules/02-core-guides-extraction-08-extraction-orchestrator.md +1008 -1008
  194. package/docs/02-CORE-GUIDES/extraction/readme.md +151 -151
  195. package/docs/02-CORE-GUIDES/ingestion/examples/_simple-kv-store.ts +40 -40
  196. package/docs/02-CORE-GUIDES/ingestion/examples/error-recovery.ts +728 -728
  197. package/docs/02-CORE-GUIDES/ingestion/examples/event-driven.ts +501 -501
  198. package/docs/02-CORE-GUIDES/ingestion/examples/local-file-ingestion.ts +88 -88
  199. package/docs/02-CORE-GUIDES/ingestion/examples/parquet-ingestion.ts +117 -117
  200. package/docs/02-CORE-GUIDES/ingestion/examples/performance-optimized.ts +647 -647
  201. package/docs/02-CORE-GUIDES/ingestion/examples/s3-csv-ingestion.ts +169 -169
  202. package/docs/02-CORE-GUIDES/ingestion/examples/sftp-csv-ingestion.ts +134 -134
  203. package/docs/02-CORE-GUIDES/ingestion/ingestion-quick-reference.md +546 -546
  204. package/docs/02-CORE-GUIDES/ingestion/modules/02-core-guides-ingestion-01-introduction.md +626 -626
  205. package/docs/02-CORE-GUIDES/ingestion/modules/02-core-guides-ingestion-02-quick-start.md +658 -658
  206. package/docs/02-CORE-GUIDES/ingestion/modules/02-core-guides-ingestion-03-data-sources.md +1052 -1052
  207. package/docs/02-CORE-GUIDES/ingestion/modules/02-core-guides-ingestion-04-field-mapping.md +763 -763
  208. package/docs/02-CORE-GUIDES/ingestion/modules/02-core-guides-ingestion-05-advanced-parsers.md +676 -676
  209. package/docs/02-CORE-GUIDES/ingestion/modules/02-core-guides-ingestion-06-batch-api.md +1295 -1295
  210. package/docs/02-CORE-GUIDES/ingestion/modules/02-core-guides-ingestion-07-api-reference.md +138 -138
  211. package/docs/02-CORE-GUIDES/ingestion/modules/02-core-guides-ingestion-07-state-management.md +1037 -1037
  212. package/docs/02-CORE-GUIDES/ingestion/modules/02-core-guides-ingestion-08-performance-optimization.md +1349 -1349
  213. package/docs/02-CORE-GUIDES/ingestion/modules/02-core-guides-ingestion-09-best-practices.md +1893 -1893
  214. package/docs/02-CORE-GUIDES/ingestion/readme.md +160 -160
  215. package/docs/02-CORE-GUIDES/logging-guide.md +585 -585
  216. package/docs/02-CORE-GUIDES/mapping/error-handling-patterns.md +401 -401
  217. package/docs/02-CORE-GUIDES/mapping/examples/02-core-guides-mapping-readme.md +128 -128
  218. package/docs/02-CORE-GUIDES/mapping/examples/common-patterns.ts +273 -273
  219. package/docs/02-CORE-GUIDES/mapping/examples/csv-location-ingestion.json +36 -36
  220. package/docs/02-CORE-GUIDES/mapping/examples/csv-mapping.ts +242 -242
  221. package/docs/02-CORE-GUIDES/mapping/examples/graphql-to-parquet-extraction.json +36 -36
  222. package/docs/02-CORE-GUIDES/mapping/examples/json-mapping.ts +213 -213
  223. package/docs/02-CORE-GUIDES/mapping/examples/json-product-to-mutation.json +48 -48
  224. package/docs/02-CORE-GUIDES/mapping/examples/xml-mapping.ts +291 -291
  225. package/docs/02-CORE-GUIDES/mapping/examples/xml-order-to-mutation.json +45 -45
  226. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/graphql-mutation-mapping-quick-reference.md +463 -463
  227. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/graphql-mutation-mapping-readme.md +227 -227
  228. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-01-introduction.md +222 -222
  229. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-02-quick-start.md +351 -351
  230. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-03-schema-validation.md +569 -569
  231. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-04-mapping-patterns.md +471 -471
  232. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-05-configuration-reference.md +611 -611
  233. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-06-advanced-xpath.md +148 -148
  234. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-06-path-syntax.md +464 -464
  235. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-07-api-reference.md +94 -94
  236. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-07-array-handling.md +307 -307
  237. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-08-custom-resolvers.md +544 -544
  238. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-09-advanced-patterns.md +427 -427
  239. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-10-hooks-and-variables.md +336 -336
  240. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-11-error-handling.md +488 -488
  241. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-12-arguments-vs-nodes.md +383 -383
  242. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/modules/graphql-mutation-mapping-13-best-practices.md +477 -477
  243. package/docs/02-CORE-GUIDES/mapping/graphql-mutation-mapping/readme.md +62 -62
  244. package/docs/02-CORE-GUIDES/mapping/mapping-format-decision-tree.md +480 -480
  245. package/docs/02-CORE-GUIDES/mapping/mapping-graphql-alias-batching-guide.md +820 -820
  246. package/docs/02-CORE-GUIDES/mapping/mapping-javascript-objects.md +2369 -2369
  247. package/docs/02-CORE-GUIDES/mapping/mapping-mapper-comparison-guide.md +682 -682
  248. package/docs/02-CORE-GUIDES/mapping/modules/02-core-guides-mapping-07-api-reference.md +1327 -1327
  249. package/docs/02-CORE-GUIDES/mapping/modules/02-core-guides-mapping-08-error-handling.md +1142 -1142
  250. package/docs/02-CORE-GUIDES/mapping/modules/mapping-04-use-cases.md +891 -891
  251. package/docs/02-CORE-GUIDES/mapping/modules/mapping-06-helpers-resolvers.md +1126 -1126
  252. package/docs/02-CORE-GUIDES/mapping/modules/mapping-06-sdk-resolvers.md +199 -199
  253. package/docs/02-CORE-GUIDES/mapping/modules/mapping-07-api-reference.md +1319 -1319
  254. package/docs/02-CORE-GUIDES/mapping/readme.md +178 -178
  255. package/docs/02-CORE-GUIDES/mapping/resolver-registration.md +410 -410
  256. package/docs/02-CORE-GUIDES/mapping/resolvers/examples/common-patterns.ts +226 -226
  257. package/docs/02-CORE-GUIDES/mapping/resolvers/examples/custom-resolvers.ts +227 -227
  258. package/docs/02-CORE-GUIDES/mapping/resolvers/examples/sdk-resolvers-usage.ts +203 -203
  259. package/docs/02-CORE-GUIDES/mapping/resolvers/mapping-resolvers-readme.md +274 -274
  260. package/docs/02-CORE-GUIDES/mapping/resolvers/mapping-resolvers-resolver-api-reference.md +679 -679
  261. package/docs/02-CORE-GUIDES/mapping/resolvers/mapping-resolvers-resolver-cookbook.md +826 -826
  262. package/docs/02-CORE-GUIDES/mapping/resolvers/mapping-resolvers-resolver-guide.md +1330 -1330
  263. package/docs/02-CORE-GUIDES/mapping/resolvers/mapping-resolvers-resolver-helpers-reference.md +1437 -1437
  264. package/docs/02-CORE-GUIDES/mapping/resolvers/mapping-resolvers-resolver-parameters-reference.md +553 -553
  265. package/docs/02-CORE-GUIDES/mapping/resolvers/mapping-resolvers-resolver-troubleshooting.md +854 -854
  266. package/docs/02-CORE-GUIDES/mapping/resolvers/readme.md +75 -75
  267. package/docs/02-CORE-GUIDES/parsers/examples/02-core-guides-parsers-readme.md +161 -161
  268. package/docs/02-CORE-GUIDES/parsers/examples/csv-parser-examples.ts +110 -110
  269. package/docs/02-CORE-GUIDES/parsers/examples/json-parser-examples.ts +33 -33
  270. package/docs/02-CORE-GUIDES/parsers/examples/parquet-parser-examples.ts +47 -47
  271. package/docs/02-CORE-GUIDES/parsers/examples/xml-parser-examples.ts +38 -38
  272. package/docs/02-CORE-GUIDES/parsers/modules/02-core-guides-parsers-01-foundations.md +355 -355
  273. package/docs/02-CORE-GUIDES/parsers/modules/02-core-guides-parsers-02-csv-parser.md +772 -772
  274. package/docs/02-CORE-GUIDES/parsers/modules/02-core-guides-parsers-03-json-parser.md +789 -789
  275. package/docs/02-CORE-GUIDES/parsers/modules/02-core-guides-parsers-04-xml-parser.md +857 -857
  276. package/docs/02-CORE-GUIDES/parsers/modules/02-core-guides-parsers-05-parquet-parser.md +603 -603
  277. package/docs/02-CORE-GUIDES/parsers/modules/02-core-guides-parsers-06-integration-patterns.md +702 -702
  278. package/docs/02-CORE-GUIDES/parsers/modules/02-core-guides-parsers-06-streaming.md +121 -121
  279. package/docs/02-CORE-GUIDES/parsers/modules/02-core-guides-parsers-07-api-reference.md +89 -89
  280. package/docs/02-CORE-GUIDES/parsers/modules/02-core-guides-parsers-07-troubleshooting.md +727 -727
  281. package/docs/02-CORE-GUIDES/parsers/parsers-quick-reference.md +482 -482
  282. package/docs/02-CORE-GUIDES/parsers/parsers-readme.md +258 -258
  283. package/docs/02-CORE-GUIDES/parsers/readme.md +65 -65
  284. package/docs/02-CORE-GUIDES/readme.md +194 -194
  285. package/docs/02-CORE-GUIDES/webhook-validation/examples/basic-validation.ts +108 -108
  286. package/docs/02-CORE-GUIDES/webhook-validation/examples/common-patterns.ts +316 -316
  287. package/docs/02-CORE-GUIDES/webhook-validation/examples/webhook-validation-readme.md +61 -61
  288. package/docs/02-CORE-GUIDES/webhook-validation/modules/webhook-validation-01-foundations.md +440 -440
  289. package/docs/02-CORE-GUIDES/webhook-validation/modules/webhook-validation-02-quick-start.md +525 -525
  290. package/docs/02-CORE-GUIDES/webhook-validation/modules/webhook-validation-03-versori-integration.md +741 -741
  291. package/docs/02-CORE-GUIDES/webhook-validation/modules/webhook-validation-04-platform-integration.md +629 -629
  292. package/docs/02-CORE-GUIDES/webhook-validation/modules/webhook-validation-05-configuration.md +535 -535
  293. package/docs/02-CORE-GUIDES/webhook-validation/modules/webhook-validation-06-error-handling.md +611 -611
  294. package/docs/02-CORE-GUIDES/webhook-validation/modules/webhook-validation-06-troubleshooting.md +124 -124
  295. package/docs/02-CORE-GUIDES/webhook-validation/modules/webhook-validation-07-api-reference.md +511 -511
  296. package/docs/02-CORE-GUIDES/webhook-validation/modules/webhook-validation-08-rubix-webhooks.md +590 -590
  297. package/docs/02-CORE-GUIDES/webhook-validation/modules/webhook-validation-09-rubix-event-vs-http-call.md +432 -432
  298. package/docs/02-CORE-GUIDES/webhook-validation/readme.md +239 -239
  299. package/docs/02-CORE-GUIDES/webhook-validation/webhook-validation-quick-reference.md +392 -392
  300. package/docs/03-PATTERN-GUIDES/connector-scenarios/connector-scenarios-quick-reference.md +498 -498
  301. package/docs/03-PATTERN-GUIDES/connector-scenarios/connector-scenarios-readme.md +313 -313
  302. package/docs/03-PATTERN-GUIDES/connector-scenarios/examples/common-patterns.ts +612 -612
  303. package/docs/03-PATTERN-GUIDES/connector-scenarios/examples/connector-scenarios-readme.md +253 -253
  304. package/docs/03-PATTERN-GUIDES/connector-scenarios/modules/connector-scenarios-01-foundations.md +452 -452
  305. package/docs/03-PATTERN-GUIDES/connector-scenarios/modules/connector-scenarios-02-simple-scenarios.md +681 -681
  306. package/docs/03-PATTERN-GUIDES/connector-scenarios/modules/connector-scenarios-03-intermediate-scenarios.md +637 -637
  307. package/docs/03-PATTERN-GUIDES/connector-scenarios/modules/connector-scenarios-04-advanced-scenarios.md +650 -650
  308. package/docs/03-PATTERN-GUIDES/connector-scenarios/modules/connector-scenarios-05-bidirectional-sync.md +233 -233
  309. package/docs/03-PATTERN-GUIDES/connector-scenarios/modules/connector-scenarios-06-production-patterns.md +442 -442
  310. package/docs/03-PATTERN-GUIDES/connector-scenarios/modules/connector-scenarios-07-reference.md +445 -445
  311. package/docs/03-PATTERN-GUIDES/connector-scenarios/readme.md +31 -31
  312. package/docs/03-PATTERN-GUIDES/enterprise-integration-patterns.md +1528 -1528
  313. package/docs/03-PATTERN-GUIDES/error-handling/comprehensive-error-handling-guide.md +1437 -1437
  314. package/docs/03-PATTERN-GUIDES/error-handling/error-handling-quick-reference.md +390 -390
  315. package/docs/03-PATTERN-GUIDES/error-handling/examples/common-patterns.ts +438 -438
  316. package/docs/03-PATTERN-GUIDES/error-handling/modules/error-handling-01-foundations.md +362 -362
  317. package/docs/03-PATTERN-GUIDES/error-handling/modules/error-handling-02-error-types.md +850 -850
  318. package/docs/03-PATTERN-GUIDES/error-handling/modules/error-handling-03-utf8-handling.md +456 -456
  319. package/docs/03-PATTERN-GUIDES/error-handling/modules/error-handling-04-error-scenarios.md +658 -658
  320. package/docs/03-PATTERN-GUIDES/error-handling/modules/error-handling-05-calling-patterns.md +671 -671
  321. package/docs/03-PATTERN-GUIDES/error-handling/modules/error-handling-06-retry-strategies.md +1034 -1034
  322. package/docs/03-PATTERN-GUIDES/error-handling/modules/error-handling-07-monitoring.md +653 -653
  323. package/docs/03-PATTERN-GUIDES/error-handling/modules/error-handling-08-api-reference.md +847 -847
  324. package/docs/03-PATTERN-GUIDES/error-handling/readme.md +36 -36
  325. package/docs/03-PATTERN-GUIDES/examples/__tests__/readme.md +40 -40
  326. package/docs/03-PATTERN-GUIDES/examples/__tests__/resolver-examples.test.js +282 -282
  327. package/docs/03-PATTERN-GUIDES/examples/test-data/03-pattern-guides-readme.md +110 -110
  328. package/docs/03-PATTERN-GUIDES/examples/test-data/canonical-inventory.json +123 -123
  329. package/docs/03-PATTERN-GUIDES/examples/test-data/canonical-order.json +171 -171
  330. package/docs/03-PATTERN-GUIDES/examples/test-data/readme.md +28 -28
  331. package/docs/03-PATTERN-GUIDES/extraction/extraction-readme.md +15 -15
  332. package/docs/03-PATTERN-GUIDES/extraction/readme.md +25 -25
  333. package/docs/03-PATTERN-GUIDES/file-operations/examples/common-patterns.ts +407 -407
  334. package/docs/03-PATTERN-GUIDES/file-operations/examples/file-operations-readme.md +142 -142
  335. package/docs/03-PATTERN-GUIDES/file-operations/file-operations-quick-reference.md +462 -462
  336. package/docs/03-PATTERN-GUIDES/file-operations/file-operations-readme.md +379 -379
  337. package/docs/03-PATTERN-GUIDES/file-operations/modules/file-operations-01-foundations.md +430 -430
  338. package/docs/03-PATTERN-GUIDES/file-operations/modules/file-operations-02-quick-start.md +484 -484
  339. package/docs/03-PATTERN-GUIDES/file-operations/modules/file-operations-03-s3-operations.md +507 -507
  340. package/docs/03-PATTERN-GUIDES/file-operations/modules/file-operations-04-sftp-operations.md +963 -963
  341. package/docs/03-PATTERN-GUIDES/file-operations/modules/file-operations-05-streaming-performance.md +503 -503
  342. package/docs/03-PATTERN-GUIDES/file-operations/modules/file-operations-06-archive-patterns.md +386 -386
  343. package/docs/03-PATTERN-GUIDES/file-operations/modules/file-operations-06-error-handling.md +117 -117
  344. package/docs/03-PATTERN-GUIDES/file-operations/modules/file-operations-07-api-reference.md +78 -78
  345. package/docs/03-PATTERN-GUIDES/file-operations/modules/file-operations-07-testing-troubleshooting.md +567 -567
  346. package/docs/03-PATTERN-GUIDES/file-operations/modules/file-operations-08-api-reference.md +1055 -1055
  347. package/docs/03-PATTERN-GUIDES/file-operations/readme.md +32 -32
  348. package/docs/03-PATTERN-GUIDES/ingestion/ingestion-readme.md +15 -15
  349. package/docs/03-PATTERN-GUIDES/ingestion/readme.md +25 -25
  350. package/docs/03-PATTERN-GUIDES/integration-patterns/examples/batch-processing.ts +130 -130
  351. package/docs/03-PATTERN-GUIDES/integration-patterns/examples/common-patterns.ts +360 -360
  352. package/docs/03-PATTERN-GUIDES/integration-patterns/examples/delta-sync.ts +130 -130
  353. package/docs/03-PATTERN-GUIDES/integration-patterns/examples/integration-patterns-readme.md +100 -100
  354. package/docs/03-PATTERN-GUIDES/integration-patterns/examples/real-time-webhook.ts +398 -398
  355. package/docs/03-PATTERN-GUIDES/integration-patterns/integration-patterns-quick-reference.md +962 -962
  356. package/docs/03-PATTERN-GUIDES/integration-patterns/integration-patterns-readme.md +134 -134
  357. package/docs/03-PATTERN-GUIDES/integration-patterns/modules/integration-patterns-01-real-time-processing.md +991 -991
  358. package/docs/03-PATTERN-GUIDES/integration-patterns/modules/integration-patterns-02-batch-processing.md +1547 -1547
  359. package/docs/03-PATTERN-GUIDES/integration-patterns/modules/integration-patterns-03-delta-sync.md +1108 -1108
  360. package/docs/03-PATTERN-GUIDES/integration-patterns/modules/integration-patterns-04-webhook-patterns.md +1181 -1181
  361. package/docs/03-PATTERN-GUIDES/integration-patterns/modules/integration-patterns-05-error-handling.md +1061 -1061
  362. package/docs/03-PATTERN-GUIDES/integration-patterns/modules/integration-patterns-06-advanced-integration-services.md +1547 -1547
  363. package/docs/03-PATTERN-GUIDES/integration-patterns/modules/integration-patterns-06-performance.md +109 -109
  364. package/docs/03-PATTERN-GUIDES/integration-patterns/modules/integration-patterns-07-api-reference.md +34 -34
  365. package/docs/03-PATTERN-GUIDES/integration-patterns/readme.md +30 -30
  366. package/docs/03-PATTERN-GUIDES/logging-minimal-mode.md +128 -128
  367. package/docs/03-PATTERN-GUIDES/multiple-connections/examples/common-patterns.ts +380 -380
  368. package/docs/03-PATTERN-GUIDES/multiple-connections/examples/multiple-connections-readme.md +139 -139
  369. package/docs/03-PATTERN-GUIDES/multiple-connections/examples/parallel-root-connections.ts +149 -149
  370. package/docs/03-PATTERN-GUIDES/multiple-connections/examples/real-world-scenarios.ts +405 -405
  371. package/docs/03-PATTERN-GUIDES/multiple-connections/modules/multiple-connections-01-foundations.md +378 -378
  372. package/docs/03-PATTERN-GUIDES/multiple-connections/modules/multiple-connections-02-quick-start.md +566 -566
  373. package/docs/03-PATTERN-GUIDES/multiple-connections/modules/multiple-connections-03-targeting-connections.md +659 -659
  374. package/docs/03-PATTERN-GUIDES/multiple-connections/modules/multiple-connections-04-parallel-queries.md +656 -656
  375. package/docs/03-PATTERN-GUIDES/multiple-connections/modules/multiple-connections-05-best-practices.md +624 -624
  376. package/docs/03-PATTERN-GUIDES/multiple-connections/modules/multiple-connections-06-api-reference.md +824 -824
  377. package/docs/03-PATTERN-GUIDES/multiple-connections/modules/multiple-connections-06-versori.md +119 -119
  378. package/docs/03-PATTERN-GUIDES/multiple-connections/modules/multiple-connections-07-api-reference.md +87 -87
  379. package/docs/03-PATTERN-GUIDES/multiple-connections/multiple-connections-quick-reference.md +353 -353
  380. package/docs/03-PATTERN-GUIDES/multiple-connections/multiple-connections-readme.md +270 -270
  381. package/docs/03-PATTERN-GUIDES/multiple-connections/readme.md +30 -30
  382. package/docs/03-PATTERN-GUIDES/pagination/pagination-readme.md +14 -14
  383. package/docs/03-PATTERN-GUIDES/pagination/readme.md +24 -24
  384. package/docs/03-PATTERN-GUIDES/parquet/examples/common-patterns.ts +180 -180
  385. package/docs/03-PATTERN-GUIDES/parquet/examples/read-parquet.ts +48 -48
  386. package/docs/03-PATTERN-GUIDES/parquet/examples/write-parquet.ts +65 -65
  387. package/docs/03-PATTERN-GUIDES/parquet/modules/03-pattern-guides-parquet-01-introduction.md +393 -393
  388. package/docs/03-PATTERN-GUIDES/parquet/modules/03-pattern-guides-parquet-02-quick-start.md +572 -572
  389. package/docs/03-PATTERN-GUIDES/parquet/modules/03-pattern-guides-parquet-03-reading-parquet.md +525 -525
  390. package/docs/03-PATTERN-GUIDES/parquet/modules/03-pattern-guides-parquet-04-writing-parquet.md +554 -554
  391. package/docs/03-PATTERN-GUIDES/parquet/modules/03-pattern-guides-parquet-05-graphql-extraction.md +405 -405
  392. package/docs/03-PATTERN-GUIDES/parquet/modules/03-pattern-guides-parquet-06-performance.md +104 -104
  393. package/docs/03-PATTERN-GUIDES/parquet/modules/03-pattern-guides-parquet-06-s3-integration.md +511 -511
  394. package/docs/03-PATTERN-GUIDES/parquet/modules/03-pattern-guides-parquet-07-api-reference.md +90 -90
  395. package/docs/03-PATTERN-GUIDES/parquet/modules/03-pattern-guides-parquet-07-performance-optimization.md +525 -525
  396. package/docs/03-PATTERN-GUIDES/parquet/modules/03-pattern-guides-parquet-08-best-practices.md +712 -712
  397. package/docs/03-PATTERN-GUIDES/parquet/parquet-quick-reference.md +683 -683
  398. package/docs/03-PATTERN-GUIDES/parquet/parquet-readme.md +248 -248
  399. package/docs/03-PATTERN-GUIDES/parquet/readme.md +32 -32
  400. package/docs/03-PATTERN-GUIDES/parsers/parsers-readme.md +12 -12
  401. package/docs/03-PATTERN-GUIDES/parsers/readme.md +24 -24
  402. package/docs/03-PATTERN-GUIDES/readme.md +159 -159
  403. package/docs/03-PATTERN-GUIDES/webhooks/readme.md +24 -24
  404. package/docs/03-PATTERN-GUIDES/webhooks/webhooks-readme.md +8 -8
  405. package/docs/04-REFERENCE/architecture/architecture-01-overview.md +427 -427
  406. package/docs/04-REFERENCE/architecture/architecture-02-client-architecture.md +424 -424
  407. package/docs/04-REFERENCE/architecture/architecture-03-data-flow.md +690 -690
  408. package/docs/04-REFERENCE/architecture/architecture-04-service-layer.md +834 -834
  409. package/docs/04-REFERENCE/architecture/architecture-05-integration-architecture.md +655 -655
  410. package/docs/04-REFERENCE/architecture/architecture-06-state-management.md +653 -653
  411. package/docs/04-REFERENCE/architecture/architecture-adding-new-data-sources.md +686 -686
  412. package/docs/04-REFERENCE/architecture/readme.md +279 -279
  413. package/docs/04-REFERENCE/platforms/deno/readme.md +117 -117
  414. package/docs/04-REFERENCE/platforms/nodejs/readme.md +146 -146
  415. package/docs/04-REFERENCE/platforms/readme.md +135 -135
  416. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-01-introduction.md +398 -398
  417. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-02-quick-start.md +560 -560
  418. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-03-authentication.md +757 -757
  419. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-04-workflows.md +2476 -2476
  420. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-05-connections.md +1167 -1167
  421. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-06-kv-storage.md +990 -990
  422. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-06-state-management.md +121 -121
  423. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-07-api-reference.md +68 -68
  424. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-07-deployment.md +731 -731
  425. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-08-best-practices.md +1111 -1111
  426. package/docs/04-REFERENCE/platforms/versori/modules/platforms-versori-09-signature-reference.md +766 -766
  427. package/docs/04-REFERENCE/platforms/versori/platforms-versori-readme.md +299 -299
  428. package/docs/04-REFERENCE/platforms/versori/platforms-versori-s3-sftp-configuration-guide.md +1425 -1425
  429. package/docs/04-REFERENCE/platforms/versori/platforms-versori-webhook-api-key-security.md +816 -816
  430. package/docs/04-REFERENCE/platforms/versori/platforms-versori-webhook-connection-security.md +681 -681
  431. package/docs/04-REFERENCE/platforms/versori/platforms-versori-workflow-task-types.md +708 -708
  432. package/docs/04-REFERENCE/platforms/versori/readme.md +108 -108
  433. package/docs/04-REFERENCE/readme.md +148 -148
  434. package/docs/04-REFERENCE/resolver-signature/examples/advanced-resolvers.ts +482 -482
  435. package/docs/04-REFERENCE/resolver-signature/examples/async-resolvers.ts +496 -496
  436. package/docs/04-REFERENCE/resolver-signature/examples/basic-resolvers.ts +343 -343
  437. package/docs/04-REFERENCE/resolver-signature/examples/resolver-signature-readme.md +188 -188
  438. package/docs/04-REFERENCE/resolver-signature/examples/testing-resolvers.ts +463 -463
  439. package/docs/04-REFERENCE/resolver-signature/modules/resolver-signature-01-foundations.md +286 -286
  440. package/docs/04-REFERENCE/resolver-signature/modules/resolver-signature-02-parameter-reference.md +643 -643
  441. package/docs/04-REFERENCE/resolver-signature/modules/resolver-signature-03-basic-examples.md +521 -521
  442. package/docs/04-REFERENCE/resolver-signature/modules/resolver-signature-04-advanced-patterns.md +739 -739
  443. package/docs/04-REFERENCE/resolver-signature/modules/resolver-signature-05-sdk-resolvers.md +531 -531
  444. package/docs/04-REFERENCE/resolver-signature/modules/resolver-signature-06-migration-guide.md +650 -650
  445. package/docs/04-REFERENCE/resolver-signature/modules/resolver-signature-06-testing.md +125 -125
  446. package/docs/04-REFERENCE/resolver-signature/modules/resolver-signature-07-api-reference.md +794 -794
  447. package/docs/04-REFERENCE/resolver-signature/readme.md +64 -64
  448. package/docs/04-REFERENCE/resolver-signature/resolver-signature-quick-reference.md +270 -270
  449. package/docs/04-REFERENCE/resolver-signature/resolver-signature-readme.md +351 -351
  450. package/docs/04-REFERENCE/schema/fluent-commerce-schema.json +764 -764
  451. package/docs/04-REFERENCE/schema/readme.md +141 -141
  452. package/docs/04-REFERENCE/testing/examples/04-reference-testing-readme.md +158 -158
  453. package/docs/04-REFERENCE/testing/examples/fluent-testing.ts +62 -62
  454. package/docs/04-REFERENCE/testing/examples/health-check.ts +155 -155
  455. package/docs/04-REFERENCE/testing/examples/integration-test.ts +119 -119
  456. package/docs/04-REFERENCE/testing/examples/performance-test.ts +183 -183
  457. package/docs/04-REFERENCE/testing/examples/s3-testing.ts +127 -127
  458. package/docs/04-REFERENCE/testing/modules/04-reference-testing-01-foundations.md +267 -267
  459. package/docs/04-REFERENCE/testing/modules/04-reference-testing-02-s3-testing.md +599 -599
  460. package/docs/04-REFERENCE/testing/modules/04-reference-testing-03-fluent-testing.md +589 -589
  461. package/docs/04-REFERENCE/testing/modules/04-reference-testing-04-integration-testing.md +699 -699
  462. package/docs/04-REFERENCE/testing/modules/04-reference-testing-05-debugging.md +478 -478
  463. package/docs/04-REFERENCE/testing/modules/04-reference-testing-06-cicd-integration.md +463 -463
  464. package/docs/04-REFERENCE/testing/modules/04-reference-testing-06-preflight-validation.md +131 -131
  465. package/docs/04-REFERENCE/testing/modules/04-reference-testing-07-best-practices.md +499 -499
  466. package/docs/04-REFERENCE/testing/modules/04-reference-testing-07-coverage-ci.md +165 -165
  467. package/docs/04-REFERENCE/testing/modules/04-reference-testing-08-api-reference.md +634 -634
  468. package/docs/04-REFERENCE/testing/readme.md +86 -86
  469. package/docs/04-REFERENCE/testing/testing-quick-reference.md +667 -667
  470. package/docs/04-REFERENCE/testing/testing-readme.md +286 -286
  471. package/docs/04-REFERENCE/troubleshooting/readme.md +144 -144
  472. package/docs/04-REFERENCE/troubleshooting/troubleshooting-deno-sftp-compatibility.md +392 -392
  473. package/docs/template-loading-matrix.md +242 -242
  474. package/package.json +5 -3
  475. package/docs/02-CORE-GUIDES/api-reference/cli-profile-integration.md +0 -377
@@ -1,2445 +1,2445 @@
1
- ---
2
- template_id: tpl-extract-products-to-sftp-xml
3
- canonical_filename: template-extraction-products-to-sftp-xml.md
4
- version: 2.0.0
5
- sdk_version: ^0.1.39
6
- runtime: versori
7
- direction: extraction
8
- source: fluent-graphql
9
- destination: sftp-xml
10
- entity: products
11
- format: xml
12
- logging: versori
13
- status: stable
14
- features:
15
- - memory-management
16
- - enhanced-logging
17
- - pagination-progress
18
- - dispose-finally
19
- ---
20
-
21
- # Template: Extraction - Products to SFTP XML
22
-
23
- **Template Version:** 2.0.0
24
- **SDK Version:** @fluentcommerce/fc-connect-sdk@^0.1.39
25
- **Last Updated:** 2025-01-24
26
- **Deployment Target:** Versori Platform
27
-
28
- **🆕 Version 2.0.0 Enhancements:**
29
- - ✅ **Memory Management** - Clear large result sets after processing batches
30
- - ✅ **Enhanced Logging** - Pagination progress tracking with emoji indicators (📊, 📥, ✅)
31
- - ✅ **Pagination Progress** - Real-time page-by-page progress logging with metrics
32
- - ✅ **Resource Cleanup** - SFTP dispose in finally blocks prevents connection leaks
33
-
34
- ## Installation
35
-
36
- ```bash
37
- npm install @fluentcommerce/fc-connect-sdk@latest
38
- ```
39
-
40
- Use the latest SDK version to ensure compatibility with this template.
41
-
42
- ---
43
-
44
- ## 📚 STEP 1: Load These Docs (Human Checklist)
45
-
46
- 1. REQUIRED (load all)
47
- - [ ] fc-connect-sdk/docs/02-CORE-GUIDES/api-reference/
48
- - [ ] fc-connect-sdk/docs/02-CORE-GUIDES/mapping/
49
- - [ ] fc-connect-sdk/docs/03-PATTERN-GUIDES/data-sources/
50
- - [ ] fc-connect-sdk/docs/03-PATTERN-GUIDES/parsers/
51
- - [ ] fc-connect-sdk/docs/03-PATTERN-GUIDES/extraction/
52
- - [ ] fc-connect-sdk/docs/04-REFERENCE/platforms/versori/
53
-
54
- Copy-paste list (open these):
55
- fc-connect-sdk/docs/02-CORE-GUIDES/api-reference/
56
- fc-connect-sdk/docs/02-CORE-GUIDES/mapping/
57
- fc-connect-sdk/docs/03-PATTERN-GUIDES/data-sources/
58
- fc-connect-sdk/docs/03-PATTERN-GUIDES/parsers/
59
- fc-connect-sdk/docs/03-PATTERN-GUIDES/extraction/
60
- fc-connect-sdk/docs/04-REFERENCE/platforms/versori/
61
-
62
- ---
63
-
64
- ## 📋 Implementation Prompt
65
-
66
- ```
67
- Create a Versori scheduled extractor for products that uses ExtractionOrchestrator + JobTracker, incremental updatedOn with a 60s overlap buffer, transforms via UniversalMapper, generates XML with XMLBuilder, uploads to SFTP using SftpDataSource with dispose(). Include 3 workflows: scheduled, ad-hoc webhook, and job-status query with native Versori logging.
68
- ```
69
-
70
- ---
71
-
72
- ## 📦 SDK Imports (Verified - Versori Optimized)
73
-
74
- ```typescript
75
- import { Buffer } from 'node:buffer';
76
- import {
77
- createClient,
78
- ExtractionOrchestrator,
79
- JobTracker,
80
- UniversalMapper,
81
- XMLBuilder,
82
- SftpDataSource,
83
- VersoriKVAdapter,
84
- } from '@fluentcommerce/fc-connect-sdk';
85
-
86
- import { schedule, webhook, http, fn } from '@versori/run';
87
- ```
88
-
89
- ---
90
-
91
- # Versori Scheduled: Products Extraction to SFTP XML (Incremental)
92
-
93
- **FC Connect SDK Use Case Guide**
94
-
95
- > SDK: [@fluentcommerce/fc-connect-sdk](https://www.npmjs.com/package/@fluentcommerce/fc-connect-sdk)
96
- > Version: Use ^0.1.39 - `npm install @fluentcommerce/fc-connect-sdk@^0.1.39`
97
-
98
- Context: Scheduled Versori workflow that extracts product catalog from Fluent Commerce via GraphQL query with **ExtractionOrchestrator**, **JobTracker**, and **incremental timestamp tracking**, transforms with `UniversalMapper`, and writes **XML files** to partner SFTP server for marketplace/partner integrations (Amazon, eBay, distributors).
99
-
100
- **Pattern**: EXTRACTION (Fluent → SFTP XML)
101
- **Complexity**: High | Runtime: Versori Platform (Scheduled)
102
-
103
- ---
104
-
105
- ## ⚠️ IMPORTANT: Production-Ready Base Template
106
-
107
- > **📋 BASE TEMPLATE - Ready for Production (Customize for Your Needs)**
108
- >
109
- > This is a **production-ready base template** demonstrating FC Connect SDK best practices for product extraction workflows with XML output.
110
- >
111
- > **✅ INCLUDED FEATURES:**
112
- >
113
- > - ✅ Comprehensive error handling with retry logic
114
- > - ✅ SFTP upload with exponential backoff (3 attempts)
115
- > - ✅ State management with overlap buffer (prevents missed records)
116
- > - ✅ Job tracking with lifecycle management
117
- > - ✅ Security (credential masking in logs)
118
- > - ✅ UTC time enforcement (prevents timezone bugs)
119
- > - ✅ Incremental extraction (safe, efficient, production-ready)
120
- > - ✅ Natural rate limiting via timestamps
121
- >
122
- > **📝 BEFORE DEPLOYING:**
123
- >
124
- > 1. Review and customize activation variables for your environment
125
- > 2. Test with sample data in your Versori workspace
126
- > 3. Adjust safety limits (pageSize, maxRecords) if needed
127
- > 4. Configure monitoring alerts for extraction failures
128
- > 5. Verify SFTP credentials and paths
129
- >
130
- > **This base template follows SDK best practices - tweak specific to your needs.**
131
-
132
- ---
133
-
134
- ## What You'll Build
135
-
136
- - **Incremental extraction** using `updatedOn >= (lastRunTime - buffer)` with **overlap buffer**
137
- - **ExtractionOrchestrator** for auto-pagination and path-based extraction
138
- - **JobTracker** for lifecycle management and status tracking
139
- - **State management** with VersoriKV to track last successful run
140
- - **Safety buffer** (60 seconds) to handle clock skew and race conditions
141
- - GraphQL query for product catalog (SKU, title, description, pricing)
142
- - `UniversalMapper` transformation for partner schema
143
- - XML file generation with product catalog data
144
- - **SFTP upload** to partner server (with `dispose()` cleanup)
145
- - **3 workflow patterns**: scheduled, ad-hoc webhook, job status query
146
- - **Failure recovery** with timestamp tracking
147
-
148
- ## Business Use Case
149
-
150
- **Daily product catalog feed to marketplace/partner:**
151
-
152
- - Extract new and updated products since last run
153
- - Generate XML file with product data for partner consumption
154
- - Upload to partner SFTP server for marketplace integration
155
- - Run daily to keep product catalog synchronized
156
- - Support product updates (price changes, inventory status)
157
- - Standard XML format for EDI/ERP integration
158
-
159
- ## SDK Methods Used
160
-
161
- ```typescript
162
- import { Buffer } from 'node:buffer';
163
- import {
164
- createClient,
165
- ExtractionOrchestrator,
166
- JobTracker,
167
- UniversalMapper,
168
- XMLBuilder,
169
- SftpDataSource,
170
- VersoriKVAdapter,
171
- } from '@fluentcommerce/fc-connect-sdk';
172
-
173
- await createClient(ctx); // Versori-aware client
174
- const orchestrator = new ExtractionOrchestrator(client, log); // Auto-pagination
175
- const tracker = new JobTracker(kv, log); // Job lifecycle tracking
176
- await orchestrator.extract({ query, resultPath, variables, pageSize, maxRecords }); // Extract
177
- new VersoriKVAdapter(ctx.openKv(':project:')); // State management
178
- new UniversalMapper(exportMapping); // Field transformation
179
- new XMLBuilder(options); // XML generation with auto-escaping
180
- await sftp.uploadFile(remotePath, buffer); // SFTP upload
181
- await sftp.dispose(); // CRITICAL: Connection cleanup
182
- ```
183
-
184
- ## SFTP Connection Setup & Credential Access
185
-
186
- ### Method 1: Versori Connections (Recommended)
187
-
188
- **✅ BEST PRACTICE:** Store SFTP credentials in a Versori connection object with Basic Auth:
189
-
190
- **Connection Configuration:**
191
-
192
- 1. In Versori platform, create a connection named `versori_ftp_server`
193
- 2. Set **Authentication Type**: `Basic Auth`
194
- 3. Enter **Username**: Your SFTP username
195
- 4. Enter **Password**: Your SFTP password
196
-
197
- **Access Method: `activation.connections` (Recommended)**
198
-
199
- ```typescript
200
- import { Buffer } from 'node:buffer'; // ⚠️ CRITICAL: Required for Deno/Versori runtime
201
-
202
- // Get SFTP credentials from Versori connection (Basic Auth)
203
- // RECOMMENDED: Use activation.connections (already decoded)
204
- const allConnections = ctx.activation.connections || [];
205
- const sftpConn = allConnections.find(c => c.name === 'versori_ftp_server');
206
-
207
- if (!sftpConn) {
208
- throw new Error('SFTP connection "versori_ftp_server" not found');
209
- }
210
-
211
- const credential = sftpConn.credentials[0]?.credential;
212
- if (!credential?.data?.basicAuth) {
213
- throw new Error('SFTP connection not configured with Basic Authentication');
214
- }
215
-
216
- const { username, password } = credential.data.basicAuth;
217
- // ✅ Already decoded - no Buffer.from() needed!
218
- ```
219
-
220
- **Alternative: `credentials().getAccessToken()` (Explicit)**
221
-
222
- ```typescript
223
- import { Buffer } from 'node:buffer'; // ⚠️ CRITICAL: Required for Deno/Versori runtime
224
-
225
- const sftpCred = await ctx.credentials().getAccessToken('versori_ftp_server');
226
- const rawAccessToken = sftpCred.accessToken;
227
- const rawBasicAuth = Buffer.from(rawAccessToken, 'base64').toString('utf-8');
228
- const [username, password] = rawBasicAuth.split(':');
229
- ```
230
-
231
- **Why use connections instead of activation variables?**
232
-
233
- - ✅ Credentials stored securely in Versori vault
234
- - ✅ Connection can be reused across workflows
235
- - ✅ No need to manage sensitive data in activation variables
236
- - ✅ Easier credential rotation
237
- - ✅ Centralized credential management across projects
238
-
239
- **🔍– Complete Guide:** See `docs/02-CORE-GUIDES/data-sources/sftp-credential-access-security.md` for comprehensive security patterns and credential management best practices.
240
-
241
- ### Method 2: Activation Variables (Alternative)
242
-
243
- Store credentials directly in activation variables (less secure):
244
-
245
- ```typescript
246
- const sftpUsername = ctx.activation?.getVariable('sftpUsername');
247
- const sftpPassword = ctx.activation?.getVariable('sftpPassword');
248
- ```
249
-
250
- **When to use activation variables:**
251
-
252
- - Quick prototyping or testing
253
- - Non-production environments
254
- - Single-use credentials
255
-
256
- **⚠️ Security Warning:** Activation variables are less secure than Versori connections. Always prefer connection-based credential storage for production.
257
-
258
- ### Buffer Import for Deno/Versori (CRITICAL)
259
-
260
- **⚠️ ALWAYS import Buffer** when using SFTP operations in Versori/Deno runtime:
261
-
262
- ```typescript
263
- import { Buffer } from 'node:buffer';
264
- ```
265
-
266
- **Why?** Unlike Node.js where `Buffer` is global, Deno requires explicit imports from Node.js built-ins using the `node:` prefix.
267
-
268
- **Common use cases:**
269
-
270
- - SFTP uploads: `Buffer.from(content, 'utf8')`
271
- - Base64 decoding: `Buffer.from(str, 'base64').toString('utf-8')`
272
- - Binary data: `Buffer.from(data)`
273
-
274
- **Error:** `Buffer is not defined` →' Add `import { Buffer } from 'node:buffer';`
275
-
276
- ## Activation Variables
277
-
278
- **Configuration is driven by activation variables - modify these instead of code:**
279
-
280
- ```json
281
- {
282
- "retailerId": "your-retailer-id",
283
- "sftpHost": "sftp.partner.com",
284
- "sftpPort": 22,
285
- "sftpPrivateKey": "-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----",
286
- "sftpRemotePath": "/incoming/products/",
287
- "pageSize": 200,
288
- "maxRecords": 50000,
289
- "fallbackStartDate": "2024-01-01T00:00:00Z",
290
- "overlapBufferSeconds": "60",
291
- "productStatus": "ACTIVE"
292
- }
293
- ```
294
-
295
- > **Note:** `sftpUsername` and `sftpPassword` are fetched from the `versori_ftp_server` Basic Auth connection (see SFTP Connection Setup above).
296
-
297
- ## Export Mapping Configuration
298
-
299
- **IMPORTANT**: Fields match CSV version exactly for consistency.
300
-
301
- Create file: `./config/products.export.xml.json`
302
-
303
- ```json
304
- {
305
- "name": "products.export.xml",
306
- "version": "1.0.0",
307
- "description": "Fluent Products → Partner SFTP XML Export",
308
- "fields": {
309
- "sku": { "source": "ref", "required": true, "resolver": "sdk.trim" },
310
- "title": { "source": "name", "required": true, "resolver": "sdk.trim" },
311
- "description": { "source": "summary", "required": false, "resolver": "sdk.trim" },
312
- "gtin": { "source": "gtin", "required": false, "resolver": "sdk.trim" },
313
- "type": { "source": "type", "required": false, "resolver": "sdk.uppercase" },
314
- "status": { "source": "status", "required": true, "resolver": "sdk.uppercase" },
315
- "price": { "source": "price", "required": false, "resolver": "sdk.parseFloat" },
316
- "catalogue_ref": { "source": "catalogue.ref", "required": false, "resolver": "sdk.trim" },
317
- "catalogue_name": { "source": "catalogue.name", "required": false, "resolver": "sdk.trim" },
318
- "created_on": { "source": "createdOn", "required": true, "resolver": "sdk.toString" },
319
- "updated_on": { "source": "updatedOn", "required": true, "resolver": "sdk.toString" }
320
- }
321
- }
322
- ```
323
-
324
- ## Mapping & Resolvers Explained
325
-
326
- ### SDK Resolvers Used
327
-
328
- The export mapping uses **SDK resolvers** to transform GraphQL data into the target XML format:
329
-
330
- | Field | Resolver | Why? | Example Transformation |
331
- | --------------- | ---------------- | -------------------------------------------- | --------------------------------------- |
332
- | `sku` | `sdk.trim` | Remove leading/trailing whitespace from SKUs | `" ABC-123 "` → `"ABC-123"` |
333
- | `title` | `sdk.trim` | Clean product names | `"Widget "` → `"Widget"` |
334
- | `description` | `sdk.trim` | Clean descriptions | `" Description"` → `"Description"` |
335
- | `gtin` | `sdk.trim` | Clean barcode numbers | `" 012345678901"` → `"012345678901"` |
336
- | `type` | `sdk.uppercase` | Normalize product type codes | `"standard"` → `"STANDARD"` |
337
- | `status` | `sdk.uppercase` | Normalize status values | `"active"` → `"ACTIVE"` |
338
- | `price` | `sdk.parseFloat` | Parse price as decimal | `"29.99"` → `29.99` |
339
- | `catalogue_ref` | `sdk.trim` | Clean catalogue references | `" CAT-001 "` → `"CAT-001"` |
340
- | `created_on` | `sdk.toString` | Ensure timestamp is string | `Date` → `"2025-01-21T10:30:00Z"` |
341
- | `updated_on` | `sdk.toString` | Ensure timestamp is string | `Date` → `"2025-01-21T10:30:00Z"` |
342
-
343
- ### Transformation Flow
344
-
345
- ```typescript
346
- // 1. GraphQL Response (from Fluent API)
347
- {
348
- ref: " SKU-001 ", // → Has whitespace
349
- name: "Premium Widget ", // → Has trailing space
350
- type: "standard", // → Lowercase
351
- status: "active", // → Lowercase
352
- price: "29.99", // → String
353
- catalogue: {
354
- ref: " CAT-001 ",
355
- name: " Default Catalogue "
356
- },
357
- updatedOn: "2025-01-21T10:30:00Z"
358
- }
359
-
360
- // 2. UniversalMapper applies resolvers
361
- const mapper = new UniversalMapper(productsExportMapping);
362
- const result = await mapper.map(node);
363
-
364
- // 3. Transformed Output (clean, normalized)
365
- result.data = {
366
- sku: "SKU-001", // ✅ Trimmed
367
- title: "Premium Widget", // ✅ Trimmed
368
- type: "STANDARD", // ✅ Uppercased
369
- status: "ACTIVE", // ✅ Uppercased
370
- price: 29.99, // ✅ Float
371
- catalogue_ref: "CAT-001", // ✅ Trimmed from nested object
372
- catalogue_name: "Default Catalogue", // ✅ Trimmed from nested object
373
- updated_on: "2025-01-21T10:30:00Z"
374
- }
375
- ```
376
-
377
- ### Custom Resolvers for Product-Specific Logic
378
-
379
- You can add **custom resolvers** for business-specific transformations:
380
-
381
- ```typescript
382
- const productsExportMapping = {
383
- name: 'products.export.xml',
384
- version: '1.0.0',
385
- fields: {
386
- sku: { source: 'ref', required: true, resolver: 'sdk.trim' },
387
- title: { source: 'name', required: true, resolver: 'sdk.trim' },
388
-
389
- // Custom resolver: Calculate display price with tax
390
- display_price: {
391
- source: 'price',
392
- resolver: 'custom.calculateDisplayPrice',
393
- },
394
-
395
- // Custom resolver: Map internal categories to partner categories
396
- partner_category: {
397
- source: 'category',
398
- resolver: 'custom.mapCategory',
399
- },
400
-
401
- // Custom resolver: Generate SEO-friendly URL slug
402
- url_slug: {
403
- source: 'name',
404
- resolver: 'custom.generateSlug',
405
- },
406
- },
407
- };
408
-
409
- // Custom resolver implementations
410
- const customResolvers = {
411
- 'custom.calculateDisplayPrice': (price: number) => {
412
- const TAX_RATE = 0.1;
413
- return (price * (1 + TAX_RATE)).toFixed(2);
414
- },
415
-
416
- 'custom.mapCategory': (category: string) => {
417
- const categoryMap: Record<string, string> = {
418
- ELECTRONICS: 'Electronics & Gadgets',
419
- APPAREL: 'Clothing & Fashion',
420
- HOME: 'Home & Living',
421
- };
422
- return categoryMap[category] || 'General';
423
- },
424
-
425
- 'custom.generateSlug': (name: string) => {
426
- return name
427
- .toLowerCase()
428
- .replace(/[^\w\s-]/g, '')
429
- .replace(/\s+/g, '-');
430
- },
431
- };
432
-
433
- // Use with UniversalMapper
434
- const mapper = new UniversalMapper(productsExportMapping, { customResolvers });
435
- ```
436
-
437
- ### Available SDK Resolvers
438
-
439
- **String Transformations:**
440
-
441
- - `sdk.trim` - Remove whitespace
442
- - `sdk.uppercase` - Convert to uppercase
443
- - `sdk.lowercase` - Convert to lowercase
444
- - `sdk.toString` - Convert to string
445
-
446
- **Number Transformations:**
447
-
448
- - `sdk.parseInt` - Parse integer
449
- - `sdk.parseFloat` - Parse decimal
450
- - `sdk.number` - Generic number conversion
451
-
452
- **Date Transformations:**
453
-
454
- - `sdk.formatDate` - ISO 8601 format (`2025-01-22T14:30:00Z`)
455
- - `sdk.formatDateShort` - Short date format (`2025-01-22`)
456
- - `sdk.parseDate` - Parse date string
457
-
458
- **Type Conversions:**
459
-
460
- - `sdk.boolean` - Convert to boolean
461
- - `sdk.parseJson` - Parse JSON string
462
- - `sdk.toJson` - Convert to JSON string
463
-
464
- **Utility:**
465
-
466
- - `sdk.identity` - Pass through unchanged
467
- - `sdk.coalesce` - Return first non-null value
468
-
469
- See [Universal Mapping Guide](../../../../../02-CORE-GUIDES/advanced-services/advanced-services-readme.md) for complete resolver documentation.
470
-
471
- ## GraphQL Query
472
-
473
- ```graphql
474
- query GetProducts(
475
- $catalogues: [ProductCatalogueKey]
476
- $dateRangeFilter: DateRange
477
- $first: Int!
478
- $after: String
479
- ) {
480
- products(catalogueRef: $catalogues, updatedOn: $dateRangeFilter, first: $first, after: $after) {
481
- edges {
482
- node {
483
- id
484
- ref
485
- name
486
- type
487
- status
488
- gtin
489
- price
490
- attributes
491
- catalogue {
492
- ref
493
- name
494
- }
495
- createdOn
496
- updatedOn
497
- }
498
- cursor
499
- }
500
- pageInfo {
501
- hasNextPage
502
- }
503
- }
504
- }
505
- ```
506
-
507
- ## Expected XML Output
508
-
509
- **IMPORTANT**: XML structure with same fields as CSV version for consistency.
510
-
511
- ```xml
512
- <?xml version="1.0" encoding="UTF-8"?>
513
- <Products>
514
- <Product>
515
- <sku>SKU-001</sku>
516
- <title>Premium Widget</title>
517
- <description>High-quality widget for all purposes</description>
518
- <gtin>012345678901</gtin>
519
- <type>STANDARD</type>
520
- <status>ACTIVE</status>
521
- <price>29.99</price>
522
- <catalogue_ref>CAT-001</catalogue_ref>
523
- <catalogue_name>Default Catalogue</catalogue_name>
524
- <created_on>2025-01-21T10:30:00Z</created_on>
525
- <updated_on>2025-01-21T10:30:00Z</updated_on>
526
- </Product>
527
- <Product>
528
- <sku>SKU-002</sku>
529
- <title>Deluxe Gadget</title>
530
- <description>Advanced gadget with premium features</description>
531
- <gtin>012345678902</gtin>
532
- <type>STANDARD</type>
533
- <status>ACTIVE</status>
534
- <price>49.99</price>
535
- <catalogue_ref>CAT-001</catalogue_ref>
536
- <catalogue_name>Default Catalogue</catalogue_name>
537
- <created_on>2025-01-21T14:15:00Z</created_on>
538
- <updated_on>2025-01-21T14:15:00Z</updated_on>
539
- </Product>
540
- </Products>
541
- ```
542
-
543
- **Note**: XML preserves hierarchical structure unlike CSV which flattens to rows.
544
-
545
- ## Production Safety & Guardrails
546
-
547
- ### Overview
548
-
549
- Product catalogs require strict guardrails even with incremental extraction:
550
-
551
- - **Large initial extractions**: First run can include entire catalog (100k+ products)
552
- - **Bulk updates**: Marketing campaigns can update thousands of products at once
553
- - **XML overhead**: 2-3x larger than JSON/CSV for same data
554
- - **SFTP limits**: Partner servers may reject large files
555
- - **Memory pressure**: Product records are larger (descriptions, attributes)
556
-
557
- ### Hard Limits
558
-
559
- ```typescript
560
- const SAFETY_LIMITS = {
561
- MAX_RECORDS_PER_RUN: 50000, // 50k products per run (XML overhead)
562
- MAX_RECORDS_PER_FILE: 10000, // 10k per XML file (SFTP-friendly)
563
- MAX_FILE_SIZE_MB: 150, // 150MB per file
564
- MAX_XML_SIZE_MB: 300, // Total extraction size
565
- CHUNK_SIZE: 5000, // Process in chunks
566
- ESTIMATED_BYTES_PER_PRODUCT_XML: 3000, // 3KB per product in XML (conservative)
567
- };
568
- ```
569
-
570
- **Why different from JSON?**
571
-
572
- - XML has 2-3x size overhead (tags, attributes, whitespace)
573
- - Products have rich text content (descriptions, attributes)
574
- - Partner SFTP servers often have smaller file size limits than S3
575
- - 10k products per file = ~30MB (manageable for most SFTP systems)
576
-
577
- ### Runtime Validation Function
578
-
579
- ```typescript
580
- /**
581
- * Validate extraction safety limits before processing
582
- * CRITICAL: Account for XML size overhead vs CSV
583
- */
584
- function validateExtractionLimits(productCount: number) {
585
- const MAX_PRODUCTS_PER_RUN = 50000;
586
- const ESTIMATED_BYTES_PER_PRODUCT_XML = 3000; // Full XML product element
587
- const estimatedSizeMB = (productCount * ESTIMATED_BYTES_PER_PRODUCT_XML) / (1024 * 1024);
588
- const MAX_XML_SIZE_MB = 300;
589
-
590
- if (productCount > MAX_PRODUCTS_PER_RUN) {
591
- return {
592
- valid: false,
593
- error: `Extraction limit exceeded: ${productCount} products (max: ${MAX_PRODUCTS_PER_RUN})`,
594
- recommendation: `Too many products for single extraction. Consider:
595
- 1. Increase extraction frequency (daily → hourly)
596
- 2. Add product status filters (ACTIVE only)
597
- 3. Split by catalogue
598
- 4. Contact support if consistently exceeding limits`,
599
- productCount,
600
- maxAllowed: MAX_PRODUCTS_PER_RUN,
601
- };
602
- }
603
-
604
- if (estimatedSizeMB > MAX_XML_SIZE_MB) {
605
- return {
606
- valid: false,
607
- error: `XML size limit exceeded: ${estimatedSizeMB}MB (max: ${MAX_XML_SIZE_MB}MB)`,
608
- recommendation:
609
- 'File splitting required. Increase extraction frequency to reduce batch size.',
610
- estimatedSizeMB,
611
- maxAllowed: MAX_XML_SIZE_MB,
612
- };
613
- }
614
-
615
- return { valid: true };
616
- }
617
- ```
618
-
619
- ---
620
-
621
- ## Versori Workflows Structure
622
-
623
- **Key Concept**: Versori workflows are organized by **trigger type** at the first level, then by **specific workflow** with descriptive file names.
624
-
625
- **Trigger Types:**
626
- - **`schedule()`** → Time-based triggers (cron expressions) - NOT exposed as HTTP endpoints
627
- - **`webhook()`** → HTTP-based triggers (event-driven) - Creates HTTP endpoints
628
- - **`workflow()`** → Durable workflows (advanced, rarely used)
629
-
630
- **Execution Steps (chained to triggers):**
631
- - **`http()`** → External API calls (chained from schedule/webhook)
632
- - **`fn()`** → Internal processing (chained from schedule/webhook)
633
-
634
- ### Recommended Project Structure
635
-
636
- ```
637
- products-extraction/
638
- ├── index.ts # Entry point - exports all workflows
639
- └── src/
640
- ├── workflows/
641
- │ ├── scheduled/
642
- │ │ └── daily-products-extraction.ts # Scheduled: Daily products extraction
643
- │ │
644
- │ └── webhook/
645
- │ ├── adhoc-products-extraction.ts # Webhook: Manual trigger
646
- │ └── job-status-check.ts # Webhook: Status query
647
-
648
- ├── services/
649
- │ └── products-extraction.service.ts # Shared orchestration logic (reusable)
650
-
651
- └── config/
652
- └── products.export.xml.json # Mapping configuration
653
- ```
654
-
655
- ---
656
-
657
- ## Complete Workflow Code
658
-
659
- The code below demonstrates the implementation of each component in the modular structure.
660
-
661
- ### 1. Entry Point (`index.ts`)
662
-
663
- ```typescript
664
- /**
665
- * Entry point - Export all workflows for Versori platform
666
- *
667
- * This file exports all workflows to be registered with Versori.
668
- * Each workflow is defined in its own file for better organization.
669
- */
670
-
671
- // Scheduled workflows
672
- export { dailyProductsExtraction } from './src/workflows/scheduled/daily-products-extraction';
673
-
674
- // Webhook workflows
675
- export { adhocProductsExtraction } from './src/workflows/webhook/adhoc-products-extraction';
676
- export { productsExtractionJobStatus } from './src/workflows/webhook/job-status-check';
677
- ```
678
-
679
- ### 2. Workflows (src/workflows/products-extraction.ts)
680
-
681
- ```typescript
682
- // ⚠️ IMPORTANT: Do NOT import openKv - access it from context!
683
- import { schedule, webhook, http, fn } from '@versori/run';
684
- import {
685
- executeProductExtraction,
686
- getJobStatus,
687
- generateJobId,
688
- } from '../services/products-extraction.service';
689
-
690
- // 
691
- // WORKFLOW 1: Scheduled Extraction (Daily at 2 AM)
692
- // 
693
-
694
- export const scheduledProductsExtraction = schedule('products-extract-xml-daily', '0 2 * * *').then(
695
- http('execute-scheduled-extraction', { connection: 'fluent_commerce' }, async ctx => {
696
- const jobId = generateJobId('SCHED', 'PRODUCTS');
697
-
698
- const result = await executeProductExtraction(ctx, {
699
- jobId,
700
- triggeredBy: 'schedule',
701
- updateState: true, // Always update state for scheduled runs
702
- });
703
-
704
- return result;
705
- })
706
- );
707
-
708
- // 
709
- // WORKFLOW 2: Ad-hoc Webhook Extraction
710
- // 
711
-
712
- export const adhocProductsExtraction = webhook('products-adhoc', {
713
- connection: 'products-adhoc',
714
- response: { mode: 'sync' }, // ✅ Sync mode: response sent when handler returns
715
- }).then(
716
- http('execute-adhoc-extraction', { connection: 'fluent_commerce' }, async ctx => {
717
- // Security is enforced by the 'products-adhoc' connection
718
- const { log } = ctx;
719
- const jobId = generateJobId('ADHOC', 'PRODUCTS');
720
-
721
- log.info('🚀 [WEBHOOK] Adhoc products extraction triggered', {
722
- jobId,
723
- fromDate: ctx.data.fromDate,
724
- toDate: ctx.data.toDate,
725
- updateState: ctx.data.updateState,
726
- });
727
-
728
- // ✅ Fire-and-forget: Start background processing WITHOUT await
729
- // The promise continues execution after we return the response
730
- executeProductExtraction(ctx, {
731
- jobId,
732
- triggeredBy: 'webhook',
733
- fromDate: ctx.data.fromDate,
734
- toDate: ctx.data.toDate,
735
- updateState: ctx.data.updateState === true,
736
- })
737
- .then((result) => {
738
- log.info('✅ [BACKGROUND] Products extraction completed successfully', {
739
- jobId,
740
- recordCount: result.recordCount,
741
- fileName: result.fileName,
742
- });
743
- })
744
- .catch((error: unknown) => {
745
- const errorMessage = error instanceof Error ? error.message : String(error);
746
- log.error('❌ [BACKGROUND] Products extraction failed', {
747
- jobId,
748
- error: errorMessage,
749
- stack: error instanceof Error ? error.stack : undefined,
750
- });
751
- });
752
-
753
- // Return immediately with jobId (response sent with this return value)
754
- return {
755
- success: true,
756
- jobId,
757
- message: 'Products extraction started in background',
758
- statusEndpoint: `https://{workspace}.versori.run/products-job-status`,
759
- note: 'Poll the status endpoint with jobId to check progress',
760
- };
761
- })
762
- );
763
-
764
- // 
765
- // WORKFLOW 3: Job Status Query
766
- // 
767
-
768
- export const productsJobStatus = webhook('products-job-status', {
769
- connection: 'products-job-status',
770
- response: { mode: 'sync' },
771
- }).then(
772
- fn('query-job-status', async ctx => {
773
- const { data, log, openKv } = ctx;
774
- // Security is enforced by the 'products-job-status' connection
775
-
776
- const jobId = data.jobId;
777
- if (!jobId) {
778
- return { success: false, error: 'Job ID required' };
779
- }
780
-
781
- const status = await getJobStatus(openKv(':project:'), jobId, log);
782
- return status
783
- ? { success: true, jobId, ...status }
784
- : { success: false, error: 'Job not found', jobId };
785
- })
786
- );
787
- ```
788
-
789
- ### 3. Main Orchestration Service (`src/services/products-extraction.service.ts`)
790
-
791
- **Note:** This service file should be renamed from `extraction-orchestration.ts` to `products-extraction.service.ts` to match the new workflow structure.
792
-
793
- ```typescript
794
- import { Buffer } from 'node:buffer';
795
- import {
796
- createClient,
797
- ExtractionOrchestrator,
798
- JobTracker,
799
- UniversalMapper,
800
- XMLBuilder,
801
- SftpDataSource,
802
- VersoriKVAdapter,
803
- } from '@fluentcommerce/fc-connect-sdk';
804
- import productsExportMapping from '../../config/products.export.xml.json' with { type: 'json' };
805
-
806
- const PRODUCTS_EXTRACTION_QUERY = `
807
- query GetProducts(
808
- $catalogues: [ProductCatalogueKey]
809
- $dateRangeFilter: DateRange
810
- $first: Int!
811
- $after: String
812
- ) {
813
- products(
814
- catalogueRef: $catalogues
815
- updatedOn: $dateRangeFilter
816
- first: $first
817
- after: $after
818
- ) {
819
- edges {
820
- node {
821
- id
822
- ref
823
- name
824
- type
825
- status
826
- gtin
827
- price
828
- attributes
829
- catalogue {
830
- ref
831
- name
832
- }
833
- createdOn
834
- updatedOn
835
- }
836
- cursor
837
- }
838
- pageInfo {
839
- hasNextPage
840
- }
841
- }
842
- }
843
- `;
844
-
845
- // Initialize XMLBuilder for products
846
- const xmlBuilder = new XMLBuilder({
847
- rootElement: 'Products',
848
- prettyPrint: true,
849
- indent: ' ',
850
- xmlDeclaration: true,
851
- encoding: 'UTF-8',
852
- });
853
-
854
- function buildProductsXML(products: any[]): string {
855
- // Transform to XMLBuilder format
856
- const productsForXml = products.map(p => ({
857
- sku: p.sku,
858
- title: p.title,
859
- description: p.description || '',
860
- gtin: p.gtin || '',
861
- type: p.type || '',
862
- status: p.status,
863
- price: String(p.price || ''),
864
- catalogue_ref: p.catalogue_ref || '',
865
- catalogue_name: p.catalogue_name || '',
866
- created_on: p.created_on,
867
- updated_on: p.updated_on,
868
- }));
869
-
870
- return xmlBuilder.build({ Product: productsForXml });
871
- }
872
-
873
- interface ProductExtractionParams {
874
- jobId: string;
875
- triggeredBy: 'schedule' | 'webhook';
876
- fromDate?: string;
877
- toDate?: string;
878
- updateState: boolean;
879
- }
880
-
881
- export async function executeProductExtraction(ctx: any, options: ProductExtractionParams) {
882
- const { jobId, triggeredBy, fromDate, toDate, updateState } = options;
883
- const log = ctx.log;
884
- const retailerId = ctx.activation?.getVariable('retailerId');
885
- const pageSize = parseInt(ctx.activation?.getVariable('pageSize') || '200', 10);
886
- const maxRecords = parseInt(ctx.activation?.getVariable('maxRecords') || '50000', 10);
887
- const fallbackStartDate =
888
- ctx.activation?.getVariable('fallbackStartDate') || '2024-01-01T00:00:00Z';
889
- const productStatus = ctx.activation?.getVariable('productStatus') || 'ACTIVE';
890
-
891
- // Get SFTP credentials from Versori connection (Basic Auth)
892
- // RECOMMENDED: Use activation.connections (already decoded)
893
- const allConnections = ctx.activation.connections || [];
894
- const sftpConn = allConnections.find(c => c.name === 'versori_ftp_server');
895
-
896
- if (!sftpConn) {
897
- throw new Error('SFTP connection "versori_ftp_server" not found');
898
- }
899
-
900
- const credential = sftpConn.credentials[0]?.credential;
901
- if (!credential?.data?.basicAuth) {
902
- throw new Error('SFTP connection not configured with Basic Authentication');
903
- }
904
-
905
- const { username, password } = credential.data.basicAuth;
906
- // ✅ Already decoded - no Buffer.from() needed!
907
-
908
- const sftpSettings = {
909
- host: ctx.activation?.getVariable('sftpHost'),
910
- port: parseInt(ctx.activation?.getVariable('sftpPort') || '22', 10),
911
- username, // From connection (secure)
912
- password, // From connection (secure)
913
- privateKey: ctx.activation?.getVariable('sftpPrivateKey'),
914
- remotePath: ctx.activation?.getVariable('sftpRemotePath') || '/incoming/products/',
915
- };
916
-
917
- const missing: string[] = [];
918
- if (!retailerId) missing.push('retailerId');
919
- if (!sftpSettings.host) missing.push('sftpHost');
920
- if (missing.length)
921
- return { success: false, error: `Missing required variables: ${missing.join(', ')}` };
922
-
923
- // SFTP connection - MUST use try/finally with dispose()
924
- const sftp = new SftpDataSource(
925
- {
926
- type: 'SFTP_XML',
927
- connectionId: 'sftp-products-xml-export',
928
- name: 'SFTP Products XML Export',
929
- settings: {
930
- host: sftpSettings.host,
931
- port: sftpSettings.port,
932
- username: sftpSettings.username,
933
- password: sftpSettings.password,
934
- privateKey: sftpSettings.privateKey,
935
- remotePath: sftpSettings.remotePath,
936
- filePattern: '*.xml',
937
- },
938
- },
939
- log
940
- );
941
-
942
- try {
943
- // 
944
- // STEP 1/8: Initialize Job Tracking
945
- // 
946
- const kv = new VersoriKVAdapter(ctx.openKv(':project:'));
947
- const tracker = new JobTracker(kv, log);
948
-
949
- await tracker.createJob(jobId, {
950
- triggeredBy,
951
- hasDateOverride: !!fromDate,
952
- fromDate,
953
- toDate,
954
- updateStateAfterRun: updateState,
955
- });
956
-
957
- log.info('Job created', { jobId, triggeredBy });
958
-
959
- // 
960
- // STEP 2/8: Load State & Calculate Time Window
961
- // 
962
- await tracker.updateJob(jobId, {
963
- status: 'processing',
964
- stage: 'state_load',
965
- message: 'Loading last run state',
966
- });
967
-
968
- const stateKey = ['extraction', 'products-xml', 'lastProductSync'];
969
- const lastRunState = await kv.get(stateKey);
970
- const rawLastRunTime = fromDate || lastRunState?.value?.timestamp || fallbackStartDate;
971
-
972
- // Overlap buffer configuration (default: 60 seconds)
973
- const overlapBufferSeconds = parseInt(
974
- ctx.activation?.getVariable('overlapBufferSeconds') || '60',
975
- 10
976
- );
977
- const OVERLAP_BUFFER_MS = overlapBufferSeconds * 1000;
978
-
979
- // Apply overlap buffer for query (safety window)
980
- const bufferedLastRunTime = new Date(
981
- new Date(rawLastRunTime).getTime() - OVERLAP_BUFFER_MS
982
- ).toISOString();
983
-
984
- const effectiveEndTime = toDate || new Date().toISOString();
985
-
986
- log.info('🔍 Time window calculated', {
987
- rawLastRunTime,
988
- bufferedLastRunTime,
989
- effectiveEndTime,
990
- overlapBufferSeconds,
991
- retailerId,
992
- productStatus,
993
- });
994
-
995
- // 
996
- // STEP 3/8: Initialize Fluent Client & ExtractionOrchestrator
997
- // 
998
- await tracker.updateJob(jobId, {
999
- stage: 'client_init',
1000
- message: 'Initializing Fluent client',
1001
- });
1002
-
1003
- const client = await createClient(ctx);
1004
- const orchestrator = new ExtractionOrchestrator(client, log);
1005
-
1006
- // 
1007
- // STEP 4/8: Extract Data (ExtractionOrchestrator)
1008
- // 
1009
- await tracker.updateJob(jobId, {
1010
- stage: 'extraction',
1011
- message: 'Extracting data with auto-pagination',
1012
- });
1013
-
1014
- // ? Enhanced: Extract context for progress logging
1015
- const dateRangeInfo = {
1016
- start: bufferedLastRunTime || 'N/A',
1017
- end: effectiveEndTime || 'N/A',
1018
- catalogues: 'all'
1019
- };
1020
-
1021
- // ? Enhanced: Start logging with context
1022
- log.info(`📊 [ExtractionOrchestrator] Starting extraction`, {
1023
- query: 'products',
1024
- pageSize,
1025
- maxRecords,
1026
- dateRange: `${dateRangeInfo.start} to ${dateRangeInfo.end}`,
1027
- catalogues: dateRangeInfo.catalogues,
1028
- jobId
1029
- });
1030
-
1031
- const extractionResult = await orchestrator.extract({
1032
- query: PRODUCTS_EXTRACTION_QUERY,
1033
- resultPath: 'products.edges.node',
1034
- variables: {
1035
- catalogues: null, // All catalogues
1036
- dateRangeFilter: {
1037
- after: bufferedLastRunTime,
1038
- before: effectiveEndTime, // End of extraction window
1039
- },
1040
- first: pageSize,
1041
- },
1042
- pageSize,
1043
- maxRecords,
1044
- validateItem: item => !!(item.ref && item.name),
1045
- });
1046
-
1047
- const rawRecords = extractionResult.data;
1048
-
1049
- log.info('Extraction complete', {
1050
- totalRecords: extractionResult.stats.totalRecords,
1051
- totalPages: extractionResult.stats.totalPages,
1052
- validRecords: extractionResult.stats.validRecords ?? rawRecords.length,
1053
- errors: extractionResult.errors ? extractionResult.errors.length : 0,
1054
- });
1055
-
1056
- // ? Enhanced: Completion logging with summary
1057
- log.info(`✅ [ExtractionOrchestrator] Extraction completed`, {
1058
- totalRecords: extractionResult.stats.totalRecords,
1059
- totalPages: extractionResult.stats.totalPages,
1060
- validRecords: extractionResult.stats.validRecords ?? rawRecords.length,
1061
- failedValidations: extractionResult.stats.failedValidations,
1062
- truncated: extractionResult.stats.truncated,
1063
- truncationReason: extractionResult.stats.truncationReason,
1064
- dateRange: `${dateRangeInfo.start} to ${dateRangeInfo.end}`,
1065
- jobId
1066
- });
1067
-
1068
- if (extractionResult.errors && extractionResult.errors.length > 0) {
1069
- log.warn('Non-fatal extraction errors encountered', {
1070
- errorCount: extractionResult.errors.length,
1071
- sampleErrors: extractionResult.errors.slice(0, 3),
1072
- });
1073
- }
1074
-
1075
- if (rawRecords.length === 0) {
1076
- await tracker.markCompleted(jobId, {
1077
- recordCount: 0,
1078
- message: 'No new products to extract',
1079
- });
1080
-
1081
- if (updateState) {
1082
- await kv.set(stateKey, {
1083
- timestamp: new Date().toISOString(),
1084
- productCount: 0,
1085
- extractedAt: new Date().toISOString(),
1086
- });
1087
- }
1088
-
1089
- return { success: true, message: 'No new products to extract', lastRunTime: rawLastRunTime };
1090
- }
1091
-
1092
- // 
1093
- // STEP 5/8: Validate Extraction Limits
1094
- // 
1095
- await tracker.updateJob(jobId, {
1096
- stage: 'validation',
1097
- message: 'Validating extraction limits',
1098
- });
1099
-
1100
- const MAX_PRODUCTS_PER_RUN = 50000;
1101
- const ESTIMATED_BYTES_PER_PRODUCT_XML = 3000;
1102
- const estimatedSizeMB = (rawRecords.length * ESTIMATED_BYTES_PER_PRODUCT_XML) / (1024 * 1024);
1103
- const MAX_XML_SIZE_MB = 300;
1104
-
1105
- if (rawRecords.length > MAX_PRODUCTS_PER_RUN) {
1106
- log.error('Extraction limit exceeded', {
1107
- productCount: rawRecords.length,
1108
- maxAllowed: MAX_PRODUCTS_PER_RUN,
1109
- });
1110
-
1111
- await tracker.markFailed(jobId, {
1112
- error: `Extraction limit exceeded: ${rawRecords.length} products (max: ${MAX_PRODUCTS_PER_RUN})`,
1113
- recommendation: 'Increase extraction frequency or add filters',
1114
- });
1115
-
1116
- return {
1117
- success: false,
1118
- error: `Extraction limit exceeded: ${rawRecords.length} products (max: ${MAX_PRODUCTS_PER_RUN})`,
1119
- recommendation: `Too many products for single extraction. Consider:
1120
- 1. Increase extraction frequency (daily → hourly)
1121
- 2. Add product status filters (ACTIVE only)
1122
- 3. Split by catalogue
1123
- 4. Contact support if consistently exceeding limits`,
1124
- productCount: rawRecords.length,
1125
- maxAllowed: MAX_PRODUCTS_PER_RUN,
1126
- };
1127
- }
1128
-
1129
- if (estimatedSizeMB > MAX_XML_SIZE_MB) {
1130
- log.warn('XML size approaching limit', {
1131
- estimatedSizeMB: estimatedSizeMB.toFixed(2),
1132
- maxAllowed: MAX_XML_SIZE_MB,
1133
- recommendation: 'Consider file splitting or increase extraction frequency',
1134
- });
1135
- }
1136
-
1137
- log.info('Extraction limits validated', {
1138
- productCount: rawRecords.length,
1139
- estimatedSizeMB: estimatedSizeMB.toFixed(2),
1140
- withinLimits: true,
1141
- });
1142
-
1143
- // 
1144
- // STEP 6/8: Transform Data (UniversalMapper)
1145
- // 
1146
- await tracker.updateJob(jobId, {
1147
- stage: 'transformation',
1148
- message: 'Transforming data with UniversalMapper',
1149
- });
1150
-
1151
- const mapper = new UniversalMapper(productsExportMapping);
1152
- const mappingResult = await mapper.map(rawRecords);
1153
-
1154
- if (!mappingResult.success) {
1155
- const mappingErrors = mappingResult.errors || ['Unknown mapping failure'];
1156
- await tracker.markFailed(jobId, {
1157
- error: mappingErrors[0] || 'UniversalMapper returned unsuccessful result',
1158
- failedCount: mappingErrors.length,
1159
- });
1160
- return {
1161
- success: false,
1162
- error: `Transformation failed: ${mappingErrors[0] || 'Unknown error'}`,
1163
- errors: mappingErrors,
1164
- };
1165
- }
1166
-
1167
- const transformedProducts = Array.isArray(mappingResult.data) ? mappingResult.data : [];
1168
- const mappingErrors = mappingResult.errors || [];
1169
-
1170
- if (mappingErrors.length > 0) {
1171
- log.warn('Some products failed transformation', {
1172
- jobId,
1173
- errorCount: mappingErrors.length,
1174
- sampleErrors: mappingErrors.slice(0, 3),
1175
- });
1176
- }
1177
-
1178
- if (mappingResult.skippedFields && mappingResult.skippedFields.length > 0) {
1179
- log.info('ℹ️ [MAPPING] Optional fields skipped (undefined values)', {
1180
- jobId,
1181
- skippedFields: mappingResult.skippedFields,
1182
- note: 'These fields were not present in source data. Add defaultValue to mapping config if they should always appear.',
1183
- });
1184
- }
1185
-
1186
- if (transformedProducts.length === 0) {
1187
- await tracker.markFailed(jobId, {
1188
- error: 'All records failed mapping',
1189
- failedCount: mappingErrors.length,
1190
- });
1191
- return {
1192
- success: false,
1193
- error: 'All records failed mapping',
1194
- errors: mappingErrors,
1195
- };
1196
- }
1197
-
1198
- log.info('Products transformed', {
1199
- jobId,
1200
- transformedCount: transformedProducts.length,
1201
- skippedRecords: rawRecords.length - transformedProducts.length,
1202
- });
1203
-
1204
- // 
1205
- // STEP 7/8: Generate XML & Upload to SFTP
1206
- // 
1207
- await tracker.updateJob(jobId, {
1208
- stage: 'upload',
1209
- message: 'Generating XML and uploading to SFTP',
1210
- });
1211
-
1212
- const xmlContent = buildProductsXML(transformedProducts);
1213
-
1214
- // Generate timestamped filename
1215
- const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
1216
- const fileName = `products-${timestamp}.xml`;
1217
- const remotePath = `${sftpSettings.remotePath}${fileName}`;
1218
-
1219
- log.info('Generated XML file', {
1220
- fileName,
1221
- size: xmlContent.length,
1222
- productCount: transformedProducts.length,
1223
- });
1224
-
1225
- // Upload to SFTP
1226
- await sftp.uploadFile(remotePath, Buffer.from(xmlContent, 'utf8'));
1227
-
1228
- log.info('XML file uploaded to SFTP', { remotePath });
1229
-
1230
- // 
1231
- // STEP 8/8: Update State & Complete Job
1232
- // 
1233
- await tracker.updateJob(jobId, {
1234
- stage: 'state_update',
1235
- message: 'Updating state and completing job',
1236
- });
1237
-
1238
- // Calculate max updatedOn from extracted products
1239
- const maxUpdatedOn = transformedProducts.reduce((max, product) => {
1240
- const productTime = new Date(product.updated_on).getTime();
1241
- return productTime > max ? productTime : max;
1242
- }, new Date(rawLastRunTime).getTime());
1243
-
1244
- const newTimestamp = new Date(maxUpdatedOn).toISOString();
1245
-
1246
- // Update state with new timestamp (WITHOUT buffer)
1247
- if (updateState) {
1248
- await kv.set(stateKey, {
1249
- timestamp: newTimestamp, // ← NO buffer applied
1250
- productCount: transformedProducts.length,
1251
- extractedAt: new Date().toISOString(),
1252
- overlapBufferSeconds,
1253
- fileName,
1254
- remotePath,
1255
- errors: mappingErrors.length > 0 ? mappingErrors : undefined,
1256
- });
1257
-
1258
- log.info('State updated with new timestamp (without buffer)', {
1259
- newTimestamp,
1260
- overlapBufferSeconds,
1261
- });
1262
- }
1263
-
1264
- await tracker.markCompleted(jobId, {
1265
- recordCount: transformedProducts.length,
1266
- fileName,
1267
- sftpPath: remotePath,
1268
- errorCount: mappingErrors.length,
1269
- errors: mappingErrors,
1270
- });
1271
-
1272
- return {
1273
- success: true,
1274
- productsExtracted: transformedProducts.length,
1275
- fileName,
1276
- remotePath,
1277
- lastRunTime: rawLastRunTime,
1278
- newTimestamp,
1279
- jobId,
1280
- errors: mappingErrors.length > 0 ? mappingErrors : undefined,
1281
- };
1282
- } catch (error: any) {
1283
- log.error('Extraction failed', error, {
1284
- message: error?.message,
1285
- });
1286
-
1287
- const kv = new VersoriKVAdapter(ctx.openKv(':project:'));
1288
- const tracker = new JobTracker(kv, log);
1289
-
1290
- await tracker.markFailed(jobId, {
1291
- message: error instanceof Error ? error.message : String(error),
1292
-
1293
- stack: error instanceof Error ? error.stack : undefined,
1294
-
1295
- errorType: error instanceof Error ? error.constructor.name : 'UnknownError',
1296
- });
1297
-
1298
- return {
1299
- success: false,
1300
- message: error instanceof Error ? error.message : String(error),
1301
-
1302
- stack: error instanceof Error ? error.stack : undefined,
1303
-
1304
- errorType: error instanceof Error ? error.constructor.name : 'UnknownError',
1305
- jobId,
1306
- };
1307
- } finally {
1308
- // CRITICAL: Always clean up SFTP connections
1309
- await sftp.dispose();
1310
- log.info('SFTP connection disposed');
1311
- }
1312
- }
1313
-
1314
- export async function getJobStatus(kv: any, jobId: string, log: any) {
1315
- const tracker = new JobTracker(new VersoriKVAdapter(kv), log);
1316
- return await tracker.getJob(jobId);
1317
- }
1318
- ```
1319
-
1320
- ### 4. Job ID Generator (src/utils/job-id-generator.ts)
1321
-
1322
- ```typescript
1323
- /**
1324
- * Generate unique job ID
1325
- * Format: {PREFIX}-{ENTITY}-{TIMESTAMP}
1326
- */
1327
- export function generateJobId(prefix: 'SCHED' | 'ADHOC', entity: string): string {
1328
- const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
1329
- return `${prefix}-${entity}-${timestamp}`;
1330
- }
1331
- ```
1332
-
1333
- ### 5. Package Configuration (package.json)
1334
-
1335
- ```json
1336
- {
1337
- "name": "products-extraction-to-sftp-xml",
1338
- "version": "1.0.0",
1339
- "description": "Versori connector for products extraction to SFTP XML",
1340
- "main": "dist/index.js",
1341
- "type": "module",
1342
- "scripts": {
1343
- "build": "tsc",
1344
- "dev": "tsc --watch",
1345
- "lint": "eslint src/**/*.ts",
1346
- "test": "jest"
1347
- },
1348
- "dependencies": {
1349
- "@fluentcommerce/fc-connect-sdk": "^0.1.39",
1350
- "@versori/run": "latest"
1351
- },
1352
- "devDependencies": {
1353
- "@types/node": "^20.0.0",
1354
- "typescript": "^5.0.0"
1355
- }
1356
- }
1357
- ```
1358
-
1359
- ### 6. Deployment Instructions
1360
-
1361
- ```bash
1362
- # 1. Install dependencies
1363
- npm install
1364
-
1365
- # 2. Build the connector
1366
- npm run build
1367
-
1368
- # 3. Test locally (optional)
1369
- npm test
1370
-
1371
- # 4. Deploy to Versori
1372
- # - Upload to Versori workspace
1373
- # - Configure activation variables
1374
- # - Enable workflows
1375
-
1376
- # 5. Test workflows
1377
- # Scheduled: Wait for next cron trigger or manually trigger
1378
- # Ad-hoc: POST to webhook URL with API key header
1379
- # Status: Query job status by ID
1380
- ```
1381
-
1382
- ### 7. Testing
1383
-
1384
- #### Test Scheduled Extraction
1385
-
1386
- ```bash
1387
- # Trigger manually in Versori UI or wait for cron schedule
1388
- # Expected: XML file uploaded to SFTP
1389
- ```
1390
-
1391
- #### Test Ad-hoc Extraction
1392
-
1393
- ```bash
1394
- curl -X POST https://your-workspace.versori.run/products-adhoc \
1395
- -H "Content-Type: application/json" \
1396
- -d '{
1397
- "fromDate": "2025-01-01T00:00:00Z",
1398
- "toDate": "2025-01-22T23:59:59Z",
1399
- "updateState": false
1400
- }'
1401
- ```
1402
-
1403
- #### Test Job Status Query
1404
-
1405
- ```bash
1406
- curl -X POST https://your-workspace.versori.run/products-job-status \
1407
- -H "Content-Type: application/json" \
1408
- -d '{
1409
- "jobId": "SCHED-PRODUCTS-2025-01-22T02-00-00Z"
1410
- }'
1411
- ```
1412
-
1413
- ## Key Patterns Explained
1414
-
1415
- ### Pattern 1: ExtractionOrchestrator for Auto-Pagination
1416
-
1417
- ```typescript
1418
- // ✅ CORRECT - Use ExtractionOrchestrator (handles pagination automatically)
1419
- const orchestrator = new ExtractionOrchestrator(client, log);
1420
-
1421
- const extractionResult = await orchestrator.extract({
1422
- query: PRODUCTS_EXTRACTION_QUERY,
1423
- resultPath: 'products.edges.node',
1424
- variables: { dateRangeFilter: { after: bufferedLastRunTime } },
1425
- pageSize,
1426
- maxRecords,
1427
- validateItem: item => !!(item.ref && item.name),
1428
- });
1429
-
1430
- const records = extractionResult.data;
1431
-
1432
- // WRONG - Manual pagination (avoid this pattern)
1433
- // const result = await client.graphql({
1434
- // query: PRODUCTS_QUERY,
1435
- // variables: { first: pageSize },
1436
- // pagination: { maxRecords }
1437
- // });
1438
- ```
1439
-
1440
- ### Pattern 2: JobTracker for Lifecycle Management
1441
-
1442
- ```typescript
1443
- // ✅ CORRECT - Use JobTracker throughout workflow
1444
- const tracker = new JobTracker(kv, log);
1445
-
1446
- // Create job
1447
- await tracker.createJob(jobId, { triggeredBy, fromDate, toDate });
1448
-
1449
- // Update progress
1450
- await tracker.updateJob(jobId, { stage: 'extraction', message: 'Extracting data' });
1451
-
1452
- // Mark completed
1453
- await tracker.markCompleted(jobId, { recordCount, fileName });
1454
-
1455
- // Query status
1456
- const status = await tracker.getJob(jobId);
1457
- ```
1458
-
1459
- ### Pattern 3: 3-Workflow Pattern
1460
-
1461
- ```typescript
1462
- // ✅ CORRECT - 3 workflows for different use cases
1463
- // 1. Scheduled: Automated daily/hourly runs
1464
- export const scheduledProductsExtraction = schedule('products-extract-xml-daily', '0 2 * * *').then(...)
1465
-
1466
- // 2. Ad-hoc: Manual webhook triggers with date overrides
1467
- export const adhocProductsExtraction = webhook('products-adhoc', {
1468
- connection: 'products-adhoc',
1469
- response: { mode: 'sync' },
1470
- }).then(...)
1471
-
1472
- // 3. Status: Query job status by ID
1473
- export const productsJobStatus = webhook('products-job-status', {
1474
- connection: 'products-job-status',
1475
- response: { mode: 'sync' },
1476
- }).then(...)
1477
- ```
1478
-
1479
- ### Pattern 4: XMLBuilder for Safe XML Generation (CRITICAL)
1480
-
1481
- Use the SDK's `XMLBuilder` - it handles all XML escaping automatically:
1482
-
1483
- ```typescript
1484
- import { Buffer } from 'node:buffer';
1485
- import { XMLBuilder } from '@fluentcommerce/fc-connect-sdk';
1486
-
1487
- // Initialize XMLBuilder (handles all escaping automatically)
1488
- const xmlBuilder = new XMLBuilder({
1489
- rootElement: 'Products',
1490
- prettyPrint: true,
1491
- encoding: 'UTF-8',
1492
- });
1493
-
1494
- // ✅ CORRECT: XMLBuilder escapes automatically
1495
- const products = [
1496
- {
1497
- title: 'Smith & Jones <Corp>', // Contains & and <>
1498
- description: 'Special chars: ¢, ©, ®, "quotes"',
1499
- },
1500
- ];
1501
-
1502
- const xml = xmlBuilder.build({ Product: products });
1503
- // Result: All special characters properly escaped
1504
- // <title>Smith &amp; Jones &lt;Corp&gt;</title>
1505
- // <description>Special chars: ¢, ©, ®, &quot;quotes&quot;</description>
1506
-
1507
- // WRONG: Manual string concatenation (dangerous)
1508
- // const xml = `<title>${product.title}</title>`;
1509
- // This would produce INVALID XML: <title>Smith & Jones <Corp></title>
1510
- ```
1511
-
1512
- **Why XMLBuilder?**
1513
-
1514
- - ✅ Automatic escaping of &, <, >, ", '
1515
- - ✅ Handles special characters (¢, ©, ®)
1516
- - ✅ Prevents XML injection attacks
1517
- - ✅ Validates structure
1518
- - ✅ Consistent, maintainable code
1519
-
1520
- ### Pattern 5: SFTP Cleanup (CRITICAL)
1521
-
1522
- ```typescript
1523
- const sftp = new SftpDataSource(config, log);
1524
-
1525
- try {
1526
- await sftp.uploadFile(remotePath, buffer);
1527
- return { success: true };
1528
- } finally {
1529
- // ALWAYS dispose SFTP connection
1530
- await sftp.dispose();
1531
- }
1532
- ```
1533
-
1534
- **Why?** SFTP maintains open connections. Not calling `dispose()` leads to connection exhaustion.
1535
-
1536
- ### Pattern 6: Consistent Field Names Across Formats
1537
-
1538
- **Same data in CSV, JSON, and XML:**
1539
-
1540
- - `sku` (not productId, not sku_ref, not SKU)
1541
- - `title` (consistent with CSV version)
1542
- - `catalogue_ref` (matches CSV exactly)
1543
-
1544
- This allows users to switch formats without changing downstream systems.
1545
-
1546
- ---
1547
-
1548
- ### Pattern 7: State Management & Date Overrides
1549
-
1550
- **Use Case**: Understand how state management works with scheduled and ad-hoc extractions.
1551
-
1552
- **How it works**:
1553
-
1554
- VersoriKV stores the last successful extraction timestamp to enable incremental sync:
1555
-
1556
- ```typescript
1557
- interface ExtractionState {
1558
- timestamp: string; // Last run timestamp (WITHOUT overlap buffer)
1559
- recordCount: number; // Number of records extracted
1560
- extractedAt: string; // When extraction completed
1561
- fileName?: string; // Generated filename
1562
- remotePath?: string; // SFTP upload path
1563
- overlapBufferSeconds?: number; // Buffer configuration
1564
- }
1565
- ```
1566
-
1567
- **State Priority Chain** (highest to lowest):
1568
-
1569
- 1. **`fromDate` override** (manual date in webhook payload) - Highest priority
1570
- 2. **Stored state** (`await kv.get(stateKey)`) - Normal incremental mode
1571
- 3. **`fallbackStartDate`** (activation variable) - First run fallback
1572
-
1573
- **Three Scenarios**:
1574
-
1575
- #### Scenario 1: Normal Scheduled Runs (Incremental)
1576
-
1577
- ```typescript
1578
- // Payload: {} (empty - no overrides)
1579
-
1580
- // Behavior:
1581
- // 1. Load last timestamp from KV: "2025-01-22T10:00:00Z"
1582
- // 2. Apply overlap buffer: "2025-01-22T09:59:00Z" (query WITH buffer)
1583
- // 3. Extract records updated since buffered time
1584
- // 4. Calculate MAX(updatedOn) from results: "2025-01-22T14:30:00Z"
1585
- // 5. Save new timestamp WITHOUT buffer: "2025-01-22T14:30:00Z"
1586
- // 6. Next run starts from "2025-01-22T14:29:00Z" (with buffer)
1587
- ```
1588
-
1589
- **Test**:
1590
-
1591
- ```bash
1592
- # Trigger scheduled run (no payload needed)
1593
- # State advances automatically
1594
- curl -X POST https://workspace.versori.run/products-extract-daily
1595
- ```
1596
-
1597
- #### Scenario 2: Ad-hoc Extraction WITH State Update
1598
-
1599
- ```typescript
1600
- // Payload: { "fromDate": "2025-01-01T00:00:00Z", "updateState": true }
1601
-
1602
- // Behavior:
1603
- // 1. Ignore stored state
1604
- // 2. Use fromDate: "2025-01-01T00:00:00Z" (no buffer applied to manual dates)
1605
- // 3. Extract all records since 2025-01-01
1606
- // 4. Calculate MAX(updatedOn): "2025-01-22T14:30:00Z"
1607
- // 5. Save new timestamp: "2025-01-22T14:30:00Z" (updates state!)
1608
- // 6. Next scheduled run starts from this new timestamp
1609
- ```
1610
-
1611
- **Use Case**: One-time catch-up extraction that advances the state pointer.
1612
-
1613
- **Test**:
1614
-
1615
- ```bash
1616
- curl -X POST https://workspace.versori.run/products-extract-webhook \
1617
- -H "Content-Type: application/json" \
1618
- -d '{
1619
- "fromDate": "2025-01-01T00:00:00Z",
1620
- "updateState": true
1621
- }'
1622
- ```
1623
-
1624
- #### Scenario 3: Ad-hoc Extraction WITHOUT State Update
1625
-
1626
- ```typescript
1627
- // Payload: { "fromDate": "2025-01-01T00:00:00Z", "updateState": false }
1628
-
1629
- // Behavior:
1630
- // 1. Ignore stored state
1631
- // 2. Use fromDate: "2025-01-01T00:00:00Z"
1632
- // 3. Extract all records since 2025-01-01
1633
- // 4. DO NOT update state
1634
- // 5. Next scheduled run uses previous timestamp (unaffected)
1635
- ```
1636
-
1637
- **Use Case**: Historical backfill or testing without affecting incremental sync.
1638
-
1639
- **Test**:
1640
-
1641
- ```bash
1642
- curl -X POST https://workspace.versori.run/products-extract-webhook \
1643
- -H "Content-Type: application/json" \
1644
- -d '{
1645
- "fromDate": "2025-01-01T00:00:00Z",
1646
- "toDate": "2025-01-31T23:59:59Z",
1647
- "updateState": false
1648
- }'
1649
- ```
1650
-
1651
- **Why this matters**:
1652
-
1653
- - **Incremental sync** relies on state continuity
1654
- - **Manual overrides** allow catch-up without breaking incremental flow
1655
- - **Overlap buffer** prevents missed records at time boundaries
1656
- - **State isolation** lets you test/backfill without affecting production sync
1657
-
1658
- ---
1659
-
1660
- ### Pattern 8: Optional GraphQL Query Logging
1661
-
1662
- **Use Case**: Debug extraction issues by logging the exact GraphQL query sent to Fluent Commerce API.
1663
-
1664
- **When to use**:
1665
-
1666
- - ✅ Debugging pagination issues
1667
- - ✅ Verifying query variables (dates, filters, limits)
1668
- - ✅ Development and testing
1669
- - Production (verbose logs, potential secrets in variables)
1670
-
1671
- **How to enable**:
1672
-
1673
- Set `DEBUG_GRAPHQL=true` environment variable in Versori activation settings.
1674
-
1675
- **Implementation**:
1676
-
1677
- ```typescript
1678
- // In your extraction workflow
1679
- const DEBUG_GRAPHQL = activation?.getVariable('DEBUG_GRAPHQL') === 'true';
1680
-
1681
- if (DEBUG_GRAPHQL) {
1682
- log.info('GraphQL Query Debug', {
1683
- query: PRODUCTS_QUERY,
1684
- variables: {
1685
- catalogues,
1686
- dateRangeFilter: bufferedLastRunTime,
1687
- first: pageSize,
1688
- after: null, // First page
1689
- },
1690
- pagination: {
1691
- pageSize,
1692
- maxRecords,
1693
- currentPage: 1,
1694
- },
1695
- });
1696
- }
1697
-
1698
- const extractionResult = await orchestrator.extract({
1699
- query: PRODUCTS_QUERY,
1700
- resultPath: 'products.edges.node',
1701
- variables: {
1702
- catalogues,
1703
- dateRangeFilter: bufferedLastRunTime,
1704
- },
1705
- pageSize,
1706
- maxRecords,
1707
- });
1708
-
1709
- if (DEBUG_GRAPHQL) {
1710
- log.info('GraphQL Response Debug', {
1711
- totalRecords: extractionResult.stats.totalRecords,
1712
- totalPages: extractionResult.stats.totalPages,
1713
- validRecords: extractionResult.stats.validRecords ?? extractionResult.data.length,
1714
- firstRecordId: extractionResult.data[0]?.id,
1715
- lastRecordId: extractionResult.data[extractionResult.data.length - 1]?.id,
1716
- });
1717
- }
1718
- ```
1719
-
1720
- **What gets logged**:
1721
-
1722
- ```json
1723
- {
1724
- "level": "info",
1725
- "message": "GraphQL Query Debug",
1726
- "query": "query GetProducts($catalogues: [ProductCatalogueKey], $dateRangeFilter: DateRange, ...)",
1727
- "variables": {
1728
- "catalogues": [{ "ref": "DEFAULT_CATALOGUE" }],
1729
- "dateRangeFilter": "2025-01-22T09:59:00Z",
1730
- "first": 200,
1731
- "after": null
1732
- },
1733
- "pagination": {
1734
- "pageSize": 200,
1735
- "maxRecords": 50000,
1736
- "currentPage": 1
1737
- }
1738
- }
1739
- ```
1740
-
1741
- **Versori Environment Variables**:
1742
-
1743
- Add to activation settings:
1744
-
1745
- ```json
1746
- {
1747
- "DEBUG_GRAPHQL": "true"
1748
- }
1749
- ```
1750
-
1751
- **Testing**:
1752
-
1753
- ```bash
1754
- # Enable debug logging
1755
- curl -X POST https://workspace.versori.run/products-extract-daily
1756
-
1757
- # Check Versori logs for "GraphQL Query Debug" entries
1758
- # Verify query structure and variables are correct
1759
- ```
1760
-
1761
- **Sample Debug Output**:
1762
-
1763
- ```
1764
- [INFO] GraphQL Query Debug
1765
- query: "query GetProducts($catalogues: [ProductCatalogueKey], $dateRangeFilter: DateRange, ...)"
1766
- variables: { catalogues: [{ ref: "DEFAULT_CATALOGUE" }], dateRangeFilter: "2025-01-22T09:59:00Z", first: 200, after: null }
1767
- pagination: { pageSize: 200, maxRecords: 50000, currentPage: 1 }
1768
-
1769
- [INFO] Extraction complete
1770
- totalRecords: 1250
1771
- totalPages: 7
1772
- validRecords: 1250
1773
- failedValidations: 0
1774
-
1775
- [INFO] GraphQL Response Debug
1776
- totalRecords: 1250
1777
- totalPages: 7
1778
- validRecords: 1250
1779
- firstRecordId: "product_abc"
1780
- lastRecordId: "product_xyz"
1781
- ```
1782
-
1783
- **Key Benefits**:
1784
-
1785
- - Quickly identify pagination configuration issues
1786
- - Verify date filters are applied correctly
1787
- - Debug "no records found" scenarios
1788
- - Validate ExtractionOrchestrator variable injection
1789
-
1790
- **Production Best Practice**: Disable `DEBUG_GRAPHQL` in production to reduce log volume and avoid logging sensitive data.
1791
-
1792
- ---
1793
-
1794
- ## Common Issues
1795
-
1796
- **Issue 1: Malformed XML from unescaped characters**
1797
-
1798
- - Customer name contains `&` or `<`
1799
- - Solution: Always use XMLBuilder (automatic escaping)
1800
-
1801
- **Issue 2: Partner system rejects XML**
1802
-
1803
- - Missing required fields
1804
- - Solution: Verify mapping matches partner schema requirements
1805
-
1806
- **Issue 3: File too large for SFTP partner**
1807
-
1808
- - Partner has 50MB limit, file is 100MB
1809
- - Solution: Use file splitting (10k products per file)
1810
-
1811
- **Issue 4: SFTP connection timeouts**
1812
-
1813
- - Not calling `dispose()` in finally block
1814
- - Solution: Always use try/finally pattern
1815
-
1816
- **Issue 5: Job status not updating**
1817
-
1818
- - JobTracker not integrated
1819
- - Solution: Use JobTracker throughout workflow
1820
-
1821
- ## Testing
1822
-
1823
- ### 1. Test XML Structure
1824
-
1825
- ```typescript
1826
- export const testXmlGeneration = http('test-xml').then(
1827
- fn('test-xml-gen', async () => {
1828
- const testProducts = [
1829
- {
1830
- sku: 'TEST-001',
1831
- title: 'Test & Validate <Product>',
1832
- status: 'ACTIVE',
1833
- price: 29.99,
1834
- created_on: '2025-01-22T10:00:00Z',
1835
- updated_on: '2025-01-22T10:00:00Z',
1836
- },
1837
- ];
1838
-
1839
- const xml = buildProductsXML(testProducts);
1840
-
1841
- // Validate XML structure
1842
- if (!xml.includes('<?xml version="1.0"')) {
1843
- return { success: false, error: 'Missing XML declaration' };
1844
- }
1845
-
1846
- if (!xml.includes('&amp;') || !xml.includes('&lt;')) {
1847
- return { success: false, error: 'Special characters not escaped' };
1848
- }
1849
-
1850
- return { success: true, xml };
1851
- })
1852
- );
1853
- ```
1854
-
1855
- ### 2. Test SFTP Upload
1856
-
1857
- ```bash
1858
- curl https://your-workspace.versori.run/test-sftp-products-xml
1859
- ```
1860
-
1861
- ### 3. Validate Against Partner Schema
1862
-
1863
- - Download partner's XSD schema
1864
- - Validate generated XML against schema
1865
- - Fix any missing/incorrect elements
1866
-
1867
- ## Production Checklist
1868
-
1869
- - [ ] Test SFTP credentials and connection
1870
- - [ ] Verify SFTP server has write permissions to remotePath
1871
- - [ ] Set appropriate extraction frequency (daily for product feeds)
1872
- - [ ] Configure correct product status filters
1873
- - [ ] Test XML escaping with special characters (&, <, >, ", ')
1874
- - [ ] Validate XML against partner's schema (if provided)
1875
- - [ ] Test `dispose()` is always called (check logs)
1876
- - [ ] Document XML schema for partner integration team
1877
- - [ ] Set up monitoring for SFTP connection failures
1878
- - [ ] Test with real product data (names with special chars)
1879
- - [ ] Verify file size limits with SFTP partner
1880
- - [ ] Configure SFTP server IP whitelisting for Versori
1881
- - [ ] Test file splitting with large batches (>10k products)
1882
- - [ ] Test all 3 workflows (scheduled, ad-hoc, status)
1883
- - [ ] Verify JobTracker integration and status updates
1884
- - [ ] Test ExtractionOrchestrator pagination with large datasets
1885
-
1886
- ## Troubleshooting Guide
1887
-
1888
- **Issue**: "Extraction timeout after 10 minutes"
1889
-
1890
- - **Cause**: Too many records
1891
- - **Fix**: Reduce maxRecords, increase frequency
1892
-
1893
- **Issue**: "Mapping errors for 50% of records"
1894
-
1895
- - **Cause**: Schema mismatch
1896
- - **Fix**: Run schema validation, check field names
1897
-
1898
- **Issue**: "State not updating"
1899
-
1900
- - **Cause**: KV write failure or intentional retry
1901
- - **Fix**: Check KV logs, verify state update code
1902
-
1903
- **Issue**: "First run exceeds limits"
1904
-
1905
- - **Cause**: No previous timestamp, fetches all
1906
- - **Fix**: Set fallbackStartDate close to current, apply filters
1907
-
1908
- **Issue**: "Excessive duplicates"
1909
-
1910
- - **Cause**: Overlap buffer (expected) or timestamp not saved
1911
- - **Fix**: Verify newTimestamp saved WITHOUT buffer
1912
-
1913
- **Issue**: "Job status returns null"
1914
-
1915
- - **Cause**: Invalid job ID or job expired
1916
- - **Fix**: Verify job ID format, check KV TTL settings
1917
-
1918
- ## Security Best Practices
1919
-
1920
- ### Credential Management
1921
-
1922
- **✅ DO**:
1923
-
1924
- - Store credentials in Versori activation variables
1925
- - Rotate credentials quarterly
1926
- - Use least-privilege accounts
1927
-
1928
- ** DON'T**:
1929
-
1930
- - Never log credentials
1931
- - Never commit to git
1932
- - Never share across environments
1933
-
1934
- ### Data Security
1935
-
1936
- - Enable encryption in transit and at rest
1937
- - Apply data retention policies
1938
- - Monitor access logs
1939
- - Use VPC/private networks for sensitive data
1940
-
1941
- ### Webhook Security
1942
-
1943
- - Validate API keys for ad-hoc and status workflows
1944
- - Use HTTPS for all webhook endpoints
1945
- - Implement rate limiting
1946
- - Monitor for suspicious activity
1947
-
1948
- ---
1949
-
1950
- **Pattern**: Enterprise incremental extraction with ExtractionOrchestrator + JobTracker for products via SFTP (XML format)
1951
- **❌š ï¸ Versori Sample**: Reference implementation - adapt for your production use case
1952
- **Key Learning**: Use ExtractionOrchestrator for auto-pagination, JobTracker for lifecycle management, always escape XML and dispose SFTP
1953
- **Critical**: Apply 60-second overlap buffer to prevent missed records
1954
- **Buffer Pattern**: Query WITH buffer (`updatedOn >= lastRunTime - 60s`), save WITHOUT buffer (`MAX(updatedOn)`)
1955
- **Field Consistency**: Same field names as CSV version for easy format switching
1956
- **SFTP**: Use proper connection cleanup in finally block to prevent connection leaks
1957
- **XML**: Preserve hierarchical structure (no flattening needed like CSV)
1958
- **3 Workflows**: Scheduled, ad-hoc webhook, job status query
1959
-
1960
- ---
1961
-
1962
- ### Pattern 8: Backward Pagination (Optional - Advanced)
1963
-
1964
- **Use Case**: Extract data in reverse chronological order (newest to oldest) instead of oldest to newest.
1965
-
1966
- **When to Use**:
1967
-
1968
- - ✅ Need most recent records first (e.g., latest orders, recent inventory updates)
1969
- - ✅ Time-bounded reverse traversal for auditing
1970
- - ✅ Display newest-first in UI/reports
1971
- - **Don't use for standard incremental sync** - use forward pagination (default)
1972
-
1973
- **GraphQL Query Requirements**:
1974
-
1975
- Your query must support backward pagination by including `$last` and `$before`:
1976
-
1977
- ```graphql
1978
- query GetData(
1979
- $retailerId: ID!
1980
- $first: Int # For forward pagination
1981
- $after: String # For forward pagination
1982
- $last: Int # For backward pagination
1983
- $before: String # For backward pagination
1984
- ) {
1985
- data(retailerId: $retailerId, first: $first, after: $after, last: $last, before: $before) {
1986
- edges {
1987
- cursor # ✅ REQUIRED
1988
- node {
1989
- id
1990
- createdAt
1991
- # ... other fields
1992
- }
1993
- }
1994
- pageInfo {
1995
- hasNextPage # For forward
1996
- hasPreviousPage # ✅ REQUIRED for backward
1997
- }
1998
- }
1999
- }
2000
- ```
2001
-
2002
- **Implementation**:
2003
-
2004
- ```typescript
2005
- // Backward pagination - newest records first
2006
- const result = await orchestrator.extract({
2007
- query: YOUR_QUERY,
2008
- resultPath: 'data.edges.node',
2009
- variables: {
2010
- retailerId,
2011
- dateRangeFilter: { from: bufferedLastRunTime, to: effectiveEndTime },
2012
- // Don't include last/before - orchestrator injects them
2013
- },
2014
- pageSize: 200,
2015
- direction: 'backward', // ✅ Enable reverse pagination
2016
- maxRecords: 10000,
2017
- });
2018
-
2019
- // Records are returned in reverse chronological order
2020
- console.log(result.data[0].createdAt); // Newest
2021
- console.log(result.data[result.data.length - 1].createdAt); // Oldest (within range)
2022
- ```
2023
-
2024
- **Key Differences from Forward Pagination**:
2025
-
2026
- | Aspect | Forward (Default) | Backward |
2027
- | ---------------------- | -------------------------------- | ----------------------- |
2028
- | **Direction** | `direction: 'forward'` (default) | `direction: 'backward'` |
2029
- | **Variables Injected** | `first`, `after` | `last`, `before` |
2030
- | **PageInfo Field** | `hasNextPage` | `hasPreviousPage` |
2031
- | **Cursor Source** | Last edge of page | First edge of page |
2032
- | **Record Order** | Oldest → Newest | Newest → Oldest |
2033
-
2034
- **Important Notes**:
2035
-
2036
- 1. **Orchestrator injects variables**: Don't pass `last` or `before` in your variables object - the orchestrator injects them based on `pageSize` and cursor tracking.
2037
-
2038
- 2. **Query signature**: Your GraphQL query must declare `$last` and `$before` parameters even if you don't pass them explicitly.
2039
-
2040
- 3. **PageInfo requirement**: Response must include `pageInfo.hasPreviousPage` or the orchestrator will throw an error.
2041
-
2042
- 4. **Cursor requirement**: Each edge must include `cursor` field for pagination to work.
2043
-
2044
- **Example: Extract Latest 1000 Orders**
2045
-
2046
- ```typescript
2047
- const latestOrders = await orchestrator.extract({
2048
- query: ORDERS_QUERY,
2049
- resultPath: 'orders.edges.node',
2050
- variables: {
2051
- retailerId,
2052
- statuses: ['BOOKED', 'ALLOCATED'],
2053
- },
2054
- direction: 'backward', // Start from newest
2055
- maxRecords: 1000, // Stop after 1000 records
2056
- pageSize: 100, // 100 per page = 10 pages
2057
- });
2058
-
2059
- // latestOrders.data[0] is the newest order
2060
- // latestOrders.data[999] is the 1000th newest order
2061
- ```
2062
-
2063
- **When to Use Forward vs Backward**:
2064
-
2065
- ```typescript
2066
- // ✅ Forward (default) - For incremental sync
2067
- const incrementalData = await orchestrator.extract({
2068
- query: YOUR_QUERY,
2069
- resultPath: 'data.edges.node',
2070
- variables: {
2071
- dateRangeFilter: { from: lastSyncTime, to: now },
2072
- },
2073
- // direction defaults to 'forward'
2074
- // Processes oldest → newest for proper sequencing
2075
- });
2076
-
2077
- // ✅ Backward - For "latest N records" use cases
2078
- const latestData = await orchestrator.extract({
2079
- query: YOUR_QUERY,
2080
- resultPath: 'data.edges.node',
2081
- direction: 'backward',
2082
- maxRecords: 100, // Just get latest 100
2083
- // Gets newest → oldest
2084
- });
2085
- ```
2086
-
2087
- **Pagination Variables Reference**:
2088
-
2089
- | Variable | Forward | Backward | Injected By | Notes |
2090
- | -------- | ------------ | ------------ | ------------ | ------------------------ |
2091
- | `first` | ✅ Used | Not used | Orchestrator | From `pageSize` |
2092
- | `after` | ✅ Used | Not used | Orchestrator | From cursor (last edge) |
2093
- | `last` | Not used | ✅ Used | Orchestrator | From `pageSize` |
2094
- | `before` | Not used | ✅ Used | Orchestrator | From cursor (first edge) |
2095
-
2096
- **Common Mistakes to Avoid**:
2097
-
2098
- ```typescript
2099
- // WRONG - Don't pass pagination variables
2100
- const result = await orchestrator.extract({
2101
- variables: {
2102
- last: 200, // Orchestrator will override this
2103
- before: cursor, // Orchestrator manages cursor
2104
- },
2105
- direction: 'backward',
2106
- });
2107
-
2108
- // ✅ CORRECT - Let orchestrator inject pagination
2109
- const result = await orchestrator.extract({
2110
- variables: {
2111
- retailerId, // ✅ Your business variables only
2112
- },
2113
- pageSize: 200, // ✅ Orchestrator uses this for last/before
2114
- direction: 'backward',
2115
- });
2116
- ```
2117
-
2118
- #### Optional: Reverse Pagination
2119
-
2120
- - For reverse ordering, add $last/$before and pageInfo.hasPreviousPage to your query and set direction='backward'.
2121
-
2122
- GraphQL:
2123
-
2124
- ```graphql
2125
- query GetProductsBackward($last: Int!, $before: String) {
2126
- products(last: $last, before: $before) {
2127
- edges {
2128
- cursor
2129
- node {
2130
- id
2131
- ref
2132
- updatedOn
2133
- }
2134
- }
2135
- pageInfo {
2136
- hasPreviousPage
2137
- }
2138
- }
2139
- }
2140
- ```
2141
-
2142
- SDK:
2143
-
2144
- ```typescript
2145
- await orchestrator.extract({
2146
- query: PRODUCTS_BACKWARD_QUERY,
2147
- resultPath: 'products.edges.node',
2148
- variables: {},
2149
- pageSize,
2150
- direction: 'backward',
2151
- });
2152
- ```
2153
-
2154
- ---
2155
-
2156
- ## Testing Checklist
2157
-
2158
- **Before production deployment:**
2159
-
2160
- ### 1. Schema Validation
2161
-
2162
- - [ ] Run `npx fc-connect introspect-schema --url <your-graphql-url>`
2163
- - [ ] Run `npx fc-connect validate-schema --mapping ./config/products.export.xml.json --schema ./fluent-schema.json`
2164
- - [ ] Run `npx fc-connect analyze-coverage --mapping ./config/products.export.xml.json --schema ./fluent-schema.json`
2165
- - [ ] Verify all `source` paths in mapping exist in GraphQL schema
2166
- - [ ] Verify query structure matches schema (fields, types, filters)
2167
-
2168
- ### 2. Extraction Testing
2169
-
2170
- - [ ] Test with small dataset first (maxRecords=10)
2171
- - [ ] Verify ExtractionOrchestrator pagination works correctly
2172
- - [ ] Test with multiple pages of data (verify cursor handling)
2173
- - [ ] Verify date range filtering (updatedOn filter)
2174
- - [ ] Test empty result handling (no records in date range)
2175
- - [ ] Verify extraction stops at maxRecords limit
2176
-
2177
- ### 3. Mapping Testing
2178
-
2179
- - [ ] Verify required fields are populated
2180
- - [ ] Verify SDK resolvers work correctly (sdk.trim, sdk.parseInt, sdk.formatDate, etc.)
2181
- - [ ] Test custom resolvers with edge cases (if any)
2182
- - [ ] Verify nested field extraction
2183
- - [ ] Test with null/missing fields
2184
- - [ ] Verify mapping error collection works
2185
-
2186
- ### 4. XML Generation Testing
2187
-
2188
- - [ ] Verify XML structure matches expected format
2189
- - [ ] Test XML validation against XSD schema (if applicable)
2190
- - [ ] Verify special character escaping in XML
2191
- - [ ] Test with large datasets (>1000 records)
2192
- - [ ] Verify UTF-8 encoding
2193
- - [ ] Test XML namespace handling (if applicable)
2194
-
2195
- ### 5. SFTP Upload Testing
2196
-
2197
- - [ ] Test SFTP connection and authentication
2198
- - [ ] Verify file upload to correct path
2199
- - [ ] Test file naming convention (timestamp format)
2200
- - [ ] Verify file permissions on SFTP server
2201
- - [ ] Test upload retry logic (simulate network failure)
2202
- - [ ] Verify SFTP connection disposal (no connection leaks)
2203
-
2204
- ### 6. State Management Testing
2205
-
2206
- - [ ] Verify overlap buffer prevents missed records (60-second default)
2207
- - [ ] Test state recovery after extraction failure
2208
- - [ ] Verify timestamp saved WITHOUT buffer (MAX(updatedOn))
2209
- - [ ] Test first run with no previous state (uses fallbackStartDate)
2210
- - [ ] Verify state update only happens on successful upload
2211
- - [ ] Test manual date override (doesn't update state)
2212
-
2213
- ### 7. Job Tracking Testing
2214
-
2215
- - [ ] Test job creation with JobTracker
2216
- - [ ] Verify job status updates at each stage
2217
- - [ ] Test job completion with metadata
2218
- - [ ] Test job failure handling
2219
- - [ ] Query job status via webhook endpoint
2220
- - [ ] Verify job status persists in KV store
2221
-
2222
- ### 8. Error Handling Testing
2223
-
2224
- - [ ] Test with invalid GraphQL query
2225
- - [ ] Test with mapping errors (invalid field paths)
2226
- - [ ] Test with SFTP connection failures
2227
- - [ ] Test with authentication failures
2228
- - [ ] Test with network timeouts
2229
- - [ ] Verify error logging includes context (jobId, stage, error details)
2230
- - [ ] Test error threshold logic (if applicable)
2231
-
2232
- ### 9. Staging Environment Testing
2233
-
2234
- - [ ] Run full extraction in staging environment
2235
- - [ ] Verify XML file format with downstream system
2236
- - [ ] Monitor extraction duration and resource usage
2237
- - [ ] Test with production-like data volumes
2238
- - [ ] Verify no performance degradation over time
2239
-
2240
- ### 10. Integration Testing
2241
-
2242
- - [ ] Test scheduled workflow (cron trigger)
2243
- - [ ] Test ad hoc webhook trigger
2244
- - [ ] Test job status query webhook
2245
- - [ ] Verify activation variables are read correctly
2246
- - [ ] Test with different extraction modes (incremental, date range)
2247
- - [ ] End-to-end test: trigger → extract → transform → upload → verify file
2248
-
2249
- ---
2250
- ## Monitoring & Alerting
2251
-
2252
- ### Success Response Example
2253
-
2254
- ```json
2255
- {
2256
- "success": true,
2257
- "jobId": "SCHEDULED_PRD_20251102_140000_abc123",
2258
- "recordsExtracted": 1523,
2259
- "fileName": "products-2025-11-02T14-00-00-000Z.xml",
2260
- "sftpPath": "/outbound/products/products-2025-11-02T14-00-00-000Z.xml",
2261
- "metrics": {
2262
- "extractionDurationMs": 12543,
2263
- "totalPages": 8,
2264
- "pageSize": 200,
2265
- "mappingErrors": 0,
2266
- "fileSizeBytes": 524288,
2267
- "uploadDurationMs": 1234
2268
- },
2269
- "timestamps": {
2270
- "extractionStart": "2025-11-02T14:00:00.000Z",
2271
- "extractionEnd": "2025-11-02T14:00:12.543Z",
2272
- "uploadComplete": "2025-11-02T14:00:13.777Z"
2273
- },
2274
- "state": {
2275
- "previousTimestamp": "2025-11-02T13:00:00.000Z",
2276
- "newTimestamp": "2025-11-02T13:59:58.123Z",
2277
- "stateUpdated": true,
2278
- "overlapBufferSeconds": 60
2279
- }
2280
- }
2281
- ```
2282
-
2283
- ### Error Response Example
2284
-
2285
- ```json
2286
- {
2287
- "success": false,
2288
- "jobId": "ADHOC_PRD_20251102_140500_xyz789",
2289
- "error": "SFTP upload failed: Connection timeout",
2290
- "errorCategory": "NETWORK",
2291
- "recordsExtracted": 0,
2292
- "stage": "sftp_upload",
2293
- "details": {
2294
- "message": "Failed to upload file after 3 retry attempts",
2295
- "retryAttempts": 3,
2296
- "lastError": "ETIMEDOUT: Connection timed out after 30000ms"
2297
- },
2298
- "state": {
2299
- "stateUpdated": false,
2300
- "willRetryNextRun": true,
2301
- "note": "State not advanced - next extraction will retry same time window"
2302
- }
2303
- }
2304
- ```
2305
-
2306
- ### Key Metrics to Track
2307
-
2308
- ```typescript
2309
- const METRICS = {
2310
- // Extraction Performance
2311
- extractionDurationMs: Date.now() - extractionStart,
2312
- recordCount: records.length,
2313
- pageCount: extractionResult.stats.totalPages,
2314
- avgRecordsPerPage: records.length / extractionResult.stats.totalPages,
2315
-
2316
- // Transformation Performance
2317
- transformedCount: transformedRecords.length,
2318
- failedCount: mappingErrors.length,
2319
- errorRate: ((mappingErrors.length / records.length) * 100).toFixed(2) + '%',
2320
-
2321
- // File Generation
2322
- fileSizeMB: (xmlContent.length / (1024 * 1024)).toFixed(2),
2323
-
2324
- // Upload Performance
2325
- uploadDurationMs: uploadEnd - uploadStart,
2326
- uploadSpeedMBps: (fileSizeMB / (uploadDurationMs / 1000)).toFixed(2),
2327
-
2328
- // State Management
2329
- timeSinceLastRun: Date.now() - new Date(lastTimestamp).getTime(),
2330
- recordsPerMinute: (records.length / (extractionDurationMs / 60000)).toFixed(0),
2331
- };
2332
-
2333
- log.info('Extraction metrics', metrics);
2334
- ```
2335
-
2336
- ### Alert Thresholds
2337
-
2338
- ```typescript
2339
- const ALERT_THRESHOLDS = {
2340
- // Duration Alerts
2341
- EXTRACTION_DURATION_MS: 5 * 60 * 1000, // 5 minutes
2342
- UPLOAD_DURATION_MS: 2 * 60 * 1000, // 2 minutes
2343
- TOTAL_DURATION_MS: 10 * 60 * 1000, // 10 minutes
2344
-
2345
- // Error Rate Alerts
2346
- MAX_ERROR_RATE: 0.05, // 5% mapping errors
2347
- MAX_VALIDATION_FAILURES: 0.02, // 2% validation failures
2348
-
2349
- // Volume Alerts
2350
- MAX_RECORDS_PER_RUN: 100000,
2351
- MIN_RECORDS_WARNING: 0, // Alert if no records found
2352
- MAX_FILE_SIZE_MB: 150, // 150MB
2353
-
2354
- // State Alerts
2355
- MAX_TIME_SINCE_LAST_RUN_HOURS: 25, // Alert if >25 hours (should run hourly)
2356
- MAX_OVERLAP_BUFFER_SECONDS: 300, // Alert if buffer >5 minutes
2357
- };
2358
-
2359
- // Check thresholds
2360
- if (metrics.extractionDurationMs > ALERT_THRESHOLDS.EXTRACTION_DURATION_MS) {
2361
- log.warn('Extraction duration exceeded threshold', {
2362
- duration: metrics.extractionDurationMs,
2363
- threshold: ALERT_THRESHOLDS.EXTRACTION_DURATION_MS,
2364
- recommendation: 'Consider reducing maxRecords or increasing extraction frequency'
2365
- });
2366
- }
2367
- ```
2368
-
2369
- ### Monitoring Dashboard Queries
2370
-
2371
- **Versori Platform Logs Query:**
2372
-
2373
- ```
2374
- # Successful extractions
2375
- log_level:info AND message:"Extraction complete" AND jobId:*
2376
-
2377
- # Failed extractions
2378
- log_level:error AND message:"Extraction workflow failed" AND jobId:*
2379
-
2380
- # Performance issues
2381
- extractionDurationMs:>300000 OR uploadDurationMs:>120000
2382
-
2383
- # High error rates
2384
- errorRate:>5
2385
-
2386
- # State management issues
2387
- stateUpdated:false AND success:true
2388
- ```
2389
-
2390
- ### Common Issues and Solutions
2391
-
2392
- **Issue**: "Extraction timeout after 10 minutes"
2393
-
2394
- - **Cause**: Too many records in single extraction
2395
- - **Fix**: Reduce maxRecords, increase extraction frequency, or optimize query filters
2396
- - **Prevention**: Monitor recordCount trends, set appropriate maxRecords
2397
-
2398
- **Issue**: "Mapping errors for 50% of records"
2399
-
2400
- - **Cause**: Schema mismatch between GraphQL response and mapping config
2401
- - **Fix**: Run schema validation, update mapping config paths
2402
- - **Prevention**: Use `npx fc-connect validate-schema` before deployment
2403
-
2404
- **Issue**: "SFTP connection timeout"
2405
-
2406
- - **Cause**: Network issues, firewall, or connection pool exhaustion
2407
- - **Fix**: Check SFTP credentials, verify network connectivity
2408
- - **Prevention**: Implement connection health checks, monitor connection status
2409
-
2410
- **Issue**: "State not updating after successful extraction"
2411
-
2412
- - **Cause**: KV write failure or intentional retry logic
2413
- - **Fix**: Check KV logs, verify state update code executed
2414
- - **Prevention**: Add KV write verification, log state updates explicitly
2415
-
2416
- **Issue**: "First run exceeds record limits"
2417
-
2418
- - **Cause**: No previous timestamp, fetches all historical records
2419
- - **Fix**: Set fallbackStartDate close to current date, apply additional filters
2420
- - **Prevention**: Use appropriate fallbackStartDate for initial runs
2421
-
2422
- **Issue**: "Excessive duplicate records in output"
2423
-
2424
- - **Cause**: Overlap buffer (expected) or timestamp not saved correctly
2425
- - **Fix**: Verify newTimestamp saved WITHOUT buffer, check state persistence
2426
- - **Prevention**: Monitor duplicate rates, verify state update logic
2427
-
2428
- ---
2429
-
2430
- ## Troubleshooting Quick Reference
2431
-
2432
- | Error Message | Likely Cause | Solution |
2433
- |--------------|--------------|----------|
2434
- | "Failed to create Fluent Commerce client" | Authentication failure | Check OAuth2 credentials, verify connection config |
2435
- | "GraphQL query validation error" | Invalid query syntax | Validate query against schema with introspection tool |
2436
- | "Pagination cursor invalid" | Stale cursor or query change | Reset extraction, verify cursor handling in query |
2437
- | "Mapping failed: field not found" | Schema mismatch | Run schema validation, update mapping paths |
2438
- | "SFTP authentication failed" | Invalid credentials | Verify SFTP credentials in activation variables |
2439
- | "Connection pool exhausted" | Too many concurrent requests | Reduce concurrency, increase pool size |
2440
- | "KV operation failed" | Versori KV issue | Check Versori platform status, retry operation |
2441
- | "Job status not found" | Invalid jobId or expired | Verify jobId format, check KV retention policy |
2442
- | "Memory limit exceeded" | Dataset too large | Reduce maxRecords, enable streaming mode |
2443
- | "XML generation failed" | Format-specific error | Check XML generation logic, validate output |
2444
-
2445
- ---
1
+ ---
2
+ template_id: tpl-extract-products-to-sftp-xml
3
+ canonical_filename: template-extraction-products-to-sftp-xml.md
4
+ version: 2.0.0
5
+ sdk_version: ^0.1.39
6
+ runtime: versori
7
+ direction: extraction
8
+ source: fluent-graphql
9
+ destination: sftp-xml
10
+ entity: products
11
+ format: xml
12
+ logging: versori
13
+ status: stable
14
+ features:
15
+ - memory-management
16
+ - enhanced-logging
17
+ - pagination-progress
18
+ - dispose-finally
19
+ ---
20
+
21
+ # Template: Extraction - Products to SFTP XML
22
+
23
+ **Template Version:** 2.0.0
24
+ **SDK Version:** @fluentcommerce/fc-connect-sdk@^0.1.39
25
+ **Last Updated:** 2025-01-24
26
+ **Deployment Target:** Versori Platform
27
+
28
+ **🆕 Version 2.0.0 Enhancements:**
29
+ - ✅ **Memory Management** - Clear large result sets after processing batches
30
+ - ✅ **Enhanced Logging** - Pagination progress tracking with emoji indicators (📊, 📥, ✅)
31
+ - ✅ **Pagination Progress** - Real-time page-by-page progress logging with metrics
32
+ - ✅ **Resource Cleanup** - SFTP dispose in finally blocks prevents connection leaks
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ npm install @fluentcommerce/fc-connect-sdk@latest
38
+ ```
39
+
40
+ Use the latest SDK version to ensure compatibility with this template.
41
+
42
+ ---
43
+
44
+ ## 📚 STEP 1: Load These Docs (Human Checklist)
45
+
46
+ 1. REQUIRED (load all)
47
+ - [ ] fc-connect-sdk/docs/02-CORE-GUIDES/api-reference/
48
+ - [ ] fc-connect-sdk/docs/02-CORE-GUIDES/mapping/
49
+ - [ ] fc-connect-sdk/docs/03-PATTERN-GUIDES/data-sources/
50
+ - [ ] fc-connect-sdk/docs/03-PATTERN-GUIDES/parsers/
51
+ - [ ] fc-connect-sdk/docs/03-PATTERN-GUIDES/extraction/
52
+ - [ ] fc-connect-sdk/docs/04-REFERENCE/platforms/versori/
53
+
54
+ Copy-paste list (open these):
55
+ fc-connect-sdk/docs/02-CORE-GUIDES/api-reference/
56
+ fc-connect-sdk/docs/02-CORE-GUIDES/mapping/
57
+ fc-connect-sdk/docs/03-PATTERN-GUIDES/data-sources/
58
+ fc-connect-sdk/docs/03-PATTERN-GUIDES/parsers/
59
+ fc-connect-sdk/docs/03-PATTERN-GUIDES/extraction/
60
+ fc-connect-sdk/docs/04-REFERENCE/platforms/versori/
61
+
62
+ ---
63
+
64
+ ## 📋 Implementation Prompt
65
+
66
+ ```
67
+ Create a Versori scheduled extractor for products that uses ExtractionOrchestrator + JobTracker, incremental updatedOn with a 60s overlap buffer, transforms via UniversalMapper, generates XML with XMLBuilder, uploads to SFTP using SftpDataSource with dispose(). Include 3 workflows: scheduled, ad-hoc webhook, and job-status query with native Versori logging.
68
+ ```
69
+
70
+ ---
71
+
72
+ ## 📦 SDK Imports (Verified - Versori Optimized)
73
+
74
+ ```typescript
75
+ import { Buffer } from 'node:buffer';
76
+ import {
77
+ createClient,
78
+ ExtractionOrchestrator,
79
+ JobTracker,
80
+ UniversalMapper,
81
+ XMLBuilder,
82
+ SftpDataSource,
83
+ VersoriKVAdapter,
84
+ } from '@fluentcommerce/fc-connect-sdk';
85
+
86
+ import { schedule, webhook, http, fn } from '@versori/run';
87
+ ```
88
+
89
+ ---
90
+
91
+ # Versori Scheduled: Products Extraction to SFTP XML (Incremental)
92
+
93
+ **FC Connect SDK Use Case Guide**
94
+
95
+ > SDK: [@fluentcommerce/fc-connect-sdk](https://www.npmjs.com/package/@fluentcommerce/fc-connect-sdk)
96
+ > Version: Use ^0.1.39 - `npm install @fluentcommerce/fc-connect-sdk@^0.1.39`
97
+
98
+ Context: Scheduled Versori workflow that extracts product catalog from Fluent Commerce via GraphQL query with **ExtractionOrchestrator**, **JobTracker**, and **incremental timestamp tracking**, transforms with `UniversalMapper`, and writes **XML files** to partner SFTP server for marketplace/partner integrations (Amazon, eBay, distributors).
99
+
100
+ **Pattern**: EXTRACTION (Fluent → SFTP XML)
101
+ **Complexity**: High | Runtime: Versori Platform (Scheduled)
102
+
103
+ ---
104
+
105
+ ## ⚠️ IMPORTANT: Production-Ready Base Template
106
+
107
+ > **📋 BASE TEMPLATE - Ready for Production (Customize for Your Needs)**
108
+ >
109
+ > This is a **production-ready base template** demonstrating FC Connect SDK best practices for product extraction workflows with XML output.
110
+ >
111
+ > **✅ INCLUDED FEATURES:**
112
+ >
113
+ > - ✅ Comprehensive error handling with retry logic
114
+ > - ✅ SFTP upload with exponential backoff (3 attempts)
115
+ > - ✅ State management with overlap buffer (prevents missed records)
116
+ > - ✅ Job tracking with lifecycle management
117
+ > - ✅ Security (credential masking in logs)
118
+ > - ✅ UTC time enforcement (prevents timezone bugs)
119
+ > - ✅ Incremental extraction (safe, efficient, production-ready)
120
+ > - ✅ Natural rate limiting via timestamps
121
+ >
122
+ > **📝 BEFORE DEPLOYING:**
123
+ >
124
+ > 1. Review and customize activation variables for your environment
125
+ > 2. Test with sample data in your Versori workspace
126
+ > 3. Adjust safety limits (pageSize, maxRecords) if needed
127
+ > 4. Configure monitoring alerts for extraction failures
128
+ > 5. Verify SFTP credentials and paths
129
+ >
130
+ > **This base template follows SDK best practices - tweak specific to your needs.**
131
+
132
+ ---
133
+
134
+ ## What You'll Build
135
+
136
+ - **Incremental extraction** using `updatedOn >= (lastRunTime - buffer)` with **overlap buffer**
137
+ - **ExtractionOrchestrator** for auto-pagination and path-based extraction
138
+ - **JobTracker** for lifecycle management and status tracking
139
+ - **State management** with VersoriKV to track last successful run
140
+ - **Safety buffer** (60 seconds) to handle clock skew and race conditions
141
+ - GraphQL query for product catalog (SKU, title, description, pricing)
142
+ - `UniversalMapper` transformation for partner schema
143
+ - XML file generation with product catalog data
144
+ - **SFTP upload** to partner server (with `dispose()` cleanup)
145
+ - **3 workflow patterns**: scheduled, ad-hoc webhook, job status query
146
+ - **Failure recovery** with timestamp tracking
147
+
148
+ ## Business Use Case
149
+
150
+ **Daily product catalog feed to marketplace/partner:**
151
+
152
+ - Extract new and updated products since last run
153
+ - Generate XML file with product data for partner consumption
154
+ - Upload to partner SFTP server for marketplace integration
155
+ - Run daily to keep product catalog synchronized
156
+ - Support product updates (price changes, inventory status)
157
+ - Standard XML format for EDI/ERP integration
158
+
159
+ ## SDK Methods Used
160
+
161
+ ```typescript
162
+ import { Buffer } from 'node:buffer';
163
+ import {
164
+ createClient,
165
+ ExtractionOrchestrator,
166
+ JobTracker,
167
+ UniversalMapper,
168
+ XMLBuilder,
169
+ SftpDataSource,
170
+ VersoriKVAdapter,
171
+ } from '@fluentcommerce/fc-connect-sdk';
172
+
173
+ await createClient(ctx); // Versori-aware client
174
+ const orchestrator = new ExtractionOrchestrator(client, log); // Auto-pagination
175
+ const tracker = new JobTracker(kv, log); // Job lifecycle tracking
176
+ await orchestrator.extract({ query, resultPath, variables, pageSize, maxRecords }); // Extract
177
+ new VersoriKVAdapter(ctx.openKv(':project:')); // State management
178
+ new UniversalMapper(exportMapping); // Field transformation
179
+ new XMLBuilder(options); // XML generation with auto-escaping
180
+ await sftp.uploadFile(remotePath, buffer); // SFTP upload
181
+ await sftp.dispose(); // CRITICAL: Connection cleanup
182
+ ```
183
+
184
+ ## SFTP Connection Setup & Credential Access
185
+
186
+ ### Method 1: Versori Connections (Recommended)
187
+
188
+ **✅ BEST PRACTICE:** Store SFTP credentials in a Versori connection object with Basic Auth:
189
+
190
+ **Connection Configuration:**
191
+
192
+ 1. In Versori platform, create a connection named `versori_ftp_server`
193
+ 2. Set **Authentication Type**: `Basic Auth`
194
+ 3. Enter **Username**: Your SFTP username
195
+ 4. Enter **Password**: Your SFTP password
196
+
197
+ **Access Method: `activation.connections` (Recommended)**
198
+
199
+ ```typescript
200
+ import { Buffer } from 'node:buffer'; // ⚠️ CRITICAL: Required for Deno/Versori runtime
201
+
202
+ // Get SFTP credentials from Versori connection (Basic Auth)
203
+ // RECOMMENDED: Use activation.connections (already decoded)
204
+ const allConnections = ctx.activation.connections || [];
205
+ const sftpConn = allConnections.find(c => c.name === 'versori_ftp_server');
206
+
207
+ if (!sftpConn) {
208
+ throw new Error('SFTP connection "versori_ftp_server" not found');
209
+ }
210
+
211
+ const credential = sftpConn.credentials[0]?.credential;
212
+ if (!credential?.data?.basicAuth) {
213
+ throw new Error('SFTP connection not configured with Basic Authentication');
214
+ }
215
+
216
+ const { username, password } = credential.data.basicAuth;
217
+ // ✅ Already decoded - no Buffer.from() needed!
218
+ ```
219
+
220
+ **Alternative: `credentials().getAccessToken()` (Explicit)**
221
+
222
+ ```typescript
223
+ import { Buffer } from 'node:buffer'; // ⚠️ CRITICAL: Required for Deno/Versori runtime
224
+
225
+ const sftpCred = await ctx.credentials().getAccessToken('versori_ftp_server');
226
+ const rawAccessToken = sftpCred.accessToken;
227
+ const rawBasicAuth = Buffer.from(rawAccessToken, 'base64').toString('utf-8');
228
+ const [username, password] = rawBasicAuth.split(':');
229
+ ```
230
+
231
+ **Why use connections instead of activation variables?**
232
+
233
+ - ✅ Credentials stored securely in Versori vault
234
+ - ✅ Connection can be reused across workflows
235
+ - ✅ No need to manage sensitive data in activation variables
236
+ - ✅ Easier credential rotation
237
+ - ✅ Centralized credential management across projects
238
+
239
+ **🔍– Complete Guide:** See `docs/02-CORE-GUIDES/data-sources/sftp-credential-access-security.md` for comprehensive security patterns and credential management best practices.
240
+
241
+ ### Method 2: Activation Variables (Alternative)
242
+
243
+ Store credentials directly in activation variables (less secure):
244
+
245
+ ```typescript
246
+ const sftpUsername = ctx.activation?.getVariable('sftpUsername');
247
+ const sftpPassword = ctx.activation?.getVariable('sftpPassword');
248
+ ```
249
+
250
+ **When to use activation variables:**
251
+
252
+ - Quick prototyping or testing
253
+ - Non-production environments
254
+ - Single-use credentials
255
+
256
+ **⚠️ Security Warning:** Activation variables are less secure than Versori connections. Always prefer connection-based credential storage for production.
257
+
258
+ ### Buffer Import for Deno/Versori (CRITICAL)
259
+
260
+ **⚠️ ALWAYS import Buffer** when using SFTP operations in Versori/Deno runtime:
261
+
262
+ ```typescript
263
+ import { Buffer } from 'node:buffer';
264
+ ```
265
+
266
+ **Why?** Unlike Node.js where `Buffer` is global, Deno requires explicit imports from Node.js built-ins using the `node:` prefix.
267
+
268
+ **Common use cases:**
269
+
270
+ - SFTP uploads: `Buffer.from(content, 'utf8')`
271
+ - Base64 decoding: `Buffer.from(str, 'base64').toString('utf-8')`
272
+ - Binary data: `Buffer.from(data)`
273
+
274
+ **Error:** `Buffer is not defined` →' Add `import { Buffer } from 'node:buffer';`
275
+
276
+ ## Activation Variables
277
+
278
+ **Configuration is driven by activation variables - modify these instead of code:**
279
+
280
+ ```json
281
+ {
282
+ "retailerId": "your-retailer-id",
283
+ "sftpHost": "sftp.partner.com",
284
+ "sftpPort": 22,
285
+ "sftpPrivateKey": "-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----",
286
+ "sftpRemotePath": "/incoming/products/",
287
+ "pageSize": 200,
288
+ "maxRecords": 50000,
289
+ "fallbackStartDate": "2024-01-01T00:00:00Z",
290
+ "overlapBufferSeconds": "60",
291
+ "productStatus": "ACTIVE"
292
+ }
293
+ ```
294
+
295
+ > **Note:** `sftpUsername` and `sftpPassword` are fetched from the `versori_ftp_server` Basic Auth connection (see SFTP Connection Setup above).
296
+
297
+ ## Export Mapping Configuration
298
+
299
+ **IMPORTANT**: Fields match CSV version exactly for consistency.
300
+
301
+ Create file: `./config/products.export.xml.json`
302
+
303
+ ```json
304
+ {
305
+ "name": "products.export.xml",
306
+ "version": "1.0.0",
307
+ "description": "Fluent Products → Partner SFTP XML Export",
308
+ "fields": {
309
+ "sku": { "source": "ref", "required": true, "resolver": "sdk.trim" },
310
+ "title": { "source": "name", "required": true, "resolver": "sdk.trim" },
311
+ "description": { "source": "summary", "required": false, "resolver": "sdk.trim" },
312
+ "gtin": { "source": "gtin", "required": false, "resolver": "sdk.trim" },
313
+ "type": { "source": "type", "required": false, "resolver": "sdk.uppercase" },
314
+ "status": { "source": "status", "required": true, "resolver": "sdk.uppercase" },
315
+ "price": { "source": "price", "required": false, "resolver": "sdk.parseFloat" },
316
+ "catalogue_ref": { "source": "catalogue.ref", "required": false, "resolver": "sdk.trim" },
317
+ "catalogue_name": { "source": "catalogue.name", "required": false, "resolver": "sdk.trim" },
318
+ "created_on": { "source": "createdOn", "required": true, "resolver": "sdk.toString" },
319
+ "updated_on": { "source": "updatedOn", "required": true, "resolver": "sdk.toString" }
320
+ }
321
+ }
322
+ ```
323
+
324
+ ## Mapping & Resolvers Explained
325
+
326
+ ### SDK Resolvers Used
327
+
328
+ The export mapping uses **SDK resolvers** to transform GraphQL data into the target XML format:
329
+
330
+ | Field | Resolver | Why? | Example Transformation |
331
+ | --------------- | ---------------- | -------------------------------------------- | --------------------------------------- |
332
+ | `sku` | `sdk.trim` | Remove leading/trailing whitespace from SKUs | `" ABC-123 "` → `"ABC-123"` |
333
+ | `title` | `sdk.trim` | Clean product names | `"Widget "` → `"Widget"` |
334
+ | `description` | `sdk.trim` | Clean descriptions | `" Description"` → `"Description"` |
335
+ | `gtin` | `sdk.trim` | Clean barcode numbers | `" 012345678901"` → `"012345678901"` |
336
+ | `type` | `sdk.uppercase` | Normalize product type codes | `"standard"` → `"STANDARD"` |
337
+ | `status` | `sdk.uppercase` | Normalize status values | `"active"` → `"ACTIVE"` |
338
+ | `price` | `sdk.parseFloat` | Parse price as decimal | `"29.99"` → `29.99` |
339
+ | `catalogue_ref` | `sdk.trim` | Clean catalogue references | `" CAT-001 "` → `"CAT-001"` |
340
+ | `created_on` | `sdk.toString` | Ensure timestamp is string | `Date` → `"2025-01-21T10:30:00Z"` |
341
+ | `updated_on` | `sdk.toString` | Ensure timestamp is string | `Date` → `"2025-01-21T10:30:00Z"` |
342
+
343
+ ### Transformation Flow
344
+
345
+ ```typescript
346
+ // 1. GraphQL Response (from Fluent API)
347
+ {
348
+ ref: " SKU-001 ", // → Has whitespace
349
+ name: "Premium Widget ", // → Has trailing space
350
+ type: "standard", // → Lowercase
351
+ status: "active", // → Lowercase
352
+ price: "29.99", // → String
353
+ catalogue: {
354
+ ref: " CAT-001 ",
355
+ name: " Default Catalogue "
356
+ },
357
+ updatedOn: "2025-01-21T10:30:00Z"
358
+ }
359
+
360
+ // 2. UniversalMapper applies resolvers
361
+ const mapper = new UniversalMapper(productsExportMapping);
362
+ const result = await mapper.map(node);
363
+
364
+ // 3. Transformed Output (clean, normalized)
365
+ result.data = {
366
+ sku: "SKU-001", // ✅ Trimmed
367
+ title: "Premium Widget", // ✅ Trimmed
368
+ type: "STANDARD", // ✅ Uppercased
369
+ status: "ACTIVE", // ✅ Uppercased
370
+ price: 29.99, // ✅ Float
371
+ catalogue_ref: "CAT-001", // ✅ Trimmed from nested object
372
+ catalogue_name: "Default Catalogue", // ✅ Trimmed from nested object
373
+ updated_on: "2025-01-21T10:30:00Z"
374
+ }
375
+ ```
376
+
377
+ ### Custom Resolvers for Product-Specific Logic
378
+
379
+ You can add **custom resolvers** for business-specific transformations:
380
+
381
+ ```typescript
382
+ const productsExportMapping = {
383
+ name: 'products.export.xml',
384
+ version: '1.0.0',
385
+ fields: {
386
+ sku: { source: 'ref', required: true, resolver: 'sdk.trim' },
387
+ title: { source: 'name', required: true, resolver: 'sdk.trim' },
388
+
389
+ // Custom resolver: Calculate display price with tax
390
+ display_price: {
391
+ source: 'price',
392
+ resolver: 'custom.calculateDisplayPrice',
393
+ },
394
+
395
+ // Custom resolver: Map internal categories to partner categories
396
+ partner_category: {
397
+ source: 'category',
398
+ resolver: 'custom.mapCategory',
399
+ },
400
+
401
+ // Custom resolver: Generate SEO-friendly URL slug
402
+ url_slug: {
403
+ source: 'name',
404
+ resolver: 'custom.generateSlug',
405
+ },
406
+ },
407
+ };
408
+
409
+ // Custom resolver implementations
410
+ const customResolvers = {
411
+ 'custom.calculateDisplayPrice': (price: number) => {
412
+ const TAX_RATE = 0.1;
413
+ return (price * (1 + TAX_RATE)).toFixed(2);
414
+ },
415
+
416
+ 'custom.mapCategory': (category: string) => {
417
+ const categoryMap: Record<string, string> = {
418
+ ELECTRONICS: 'Electronics & Gadgets',
419
+ APPAREL: 'Clothing & Fashion',
420
+ HOME: 'Home & Living',
421
+ };
422
+ return categoryMap[category] || 'General';
423
+ },
424
+
425
+ 'custom.generateSlug': (name: string) => {
426
+ return name
427
+ .toLowerCase()
428
+ .replace(/[^\w\s-]/g, '')
429
+ .replace(/\s+/g, '-');
430
+ },
431
+ };
432
+
433
+ // Use with UniversalMapper
434
+ const mapper = new UniversalMapper(productsExportMapping, { customResolvers });
435
+ ```
436
+
437
+ ### Available SDK Resolvers
438
+
439
+ **String Transformations:**
440
+
441
+ - `sdk.trim` - Remove whitespace
442
+ - `sdk.uppercase` - Convert to uppercase
443
+ - `sdk.lowercase` - Convert to lowercase
444
+ - `sdk.toString` - Convert to string
445
+
446
+ **Number Transformations:**
447
+
448
+ - `sdk.parseInt` - Parse integer
449
+ - `sdk.parseFloat` - Parse decimal
450
+ - `sdk.number` - Generic number conversion
451
+
452
+ **Date Transformations:**
453
+
454
+ - `sdk.formatDate` - ISO 8601 format (`2025-01-22T14:30:00Z`)
455
+ - `sdk.formatDateShort` - Short date format (`2025-01-22`)
456
+ - `sdk.parseDate` - Parse date string
457
+
458
+ **Type Conversions:**
459
+
460
+ - `sdk.boolean` - Convert to boolean
461
+ - `sdk.parseJson` - Parse JSON string
462
+ - `sdk.toJson` - Convert to JSON string
463
+
464
+ **Utility:**
465
+
466
+ - `sdk.identity` - Pass through unchanged
467
+ - `sdk.coalesce` - Return first non-null value
468
+
469
+ See [Universal Mapping Guide](../../../../../02-CORE-GUIDES/advanced-services/advanced-services-readme.md) for complete resolver documentation.
470
+
471
+ ## GraphQL Query
472
+
473
+ ```graphql
474
+ query GetProducts(
475
+ $catalogues: [ProductCatalogueKey]
476
+ $dateRangeFilter: DateRange
477
+ $first: Int!
478
+ $after: String
479
+ ) {
480
+ products(catalogueRef: $catalogues, updatedOn: $dateRangeFilter, first: $first, after: $after) {
481
+ edges {
482
+ node {
483
+ id
484
+ ref
485
+ name
486
+ type
487
+ status
488
+ gtin
489
+ price
490
+ attributes
491
+ catalogue {
492
+ ref
493
+ name
494
+ }
495
+ createdOn
496
+ updatedOn
497
+ }
498
+ cursor
499
+ }
500
+ pageInfo {
501
+ hasNextPage
502
+ }
503
+ }
504
+ }
505
+ ```
506
+
507
+ ## Expected XML Output
508
+
509
+ **IMPORTANT**: XML structure with same fields as CSV version for consistency.
510
+
511
+ ```xml
512
+ <?xml version="1.0" encoding="UTF-8"?>
513
+ <Products>
514
+ <Product>
515
+ <sku>SKU-001</sku>
516
+ <title>Premium Widget</title>
517
+ <description>High-quality widget for all purposes</description>
518
+ <gtin>012345678901</gtin>
519
+ <type>STANDARD</type>
520
+ <status>ACTIVE</status>
521
+ <price>29.99</price>
522
+ <catalogue_ref>CAT-001</catalogue_ref>
523
+ <catalogue_name>Default Catalogue</catalogue_name>
524
+ <created_on>2025-01-21T10:30:00Z</created_on>
525
+ <updated_on>2025-01-21T10:30:00Z</updated_on>
526
+ </Product>
527
+ <Product>
528
+ <sku>SKU-002</sku>
529
+ <title>Deluxe Gadget</title>
530
+ <description>Advanced gadget with premium features</description>
531
+ <gtin>012345678902</gtin>
532
+ <type>STANDARD</type>
533
+ <status>ACTIVE</status>
534
+ <price>49.99</price>
535
+ <catalogue_ref>CAT-001</catalogue_ref>
536
+ <catalogue_name>Default Catalogue</catalogue_name>
537
+ <created_on>2025-01-21T14:15:00Z</created_on>
538
+ <updated_on>2025-01-21T14:15:00Z</updated_on>
539
+ </Product>
540
+ </Products>
541
+ ```
542
+
543
+ **Note**: XML preserves hierarchical structure unlike CSV which flattens to rows.
544
+
545
+ ## Production Safety & Guardrails
546
+
547
+ ### Overview
548
+
549
+ Product catalogs require strict guardrails even with incremental extraction:
550
+
551
+ - **Large initial extractions**: First run can include entire catalog (100k+ products)
552
+ - **Bulk updates**: Marketing campaigns can update thousands of products at once
553
+ - **XML overhead**: 2-3x larger than JSON/CSV for same data
554
+ - **SFTP limits**: Partner servers may reject large files
555
+ - **Memory pressure**: Product records are larger (descriptions, attributes)
556
+
557
+ ### Hard Limits
558
+
559
+ ```typescript
560
+ const SAFETY_LIMITS = {
561
+ MAX_RECORDS_PER_RUN: 50000, // 50k products per run (XML overhead)
562
+ MAX_RECORDS_PER_FILE: 10000, // 10k per XML file (SFTP-friendly)
563
+ MAX_FILE_SIZE_MB: 150, // 150MB per file
564
+ MAX_XML_SIZE_MB: 300, // Total extraction size
565
+ CHUNK_SIZE: 5000, // Process in chunks
566
+ ESTIMATED_BYTES_PER_PRODUCT_XML: 3000, // 3KB per product in XML (conservative)
567
+ };
568
+ ```
569
+
570
+ **Why different from JSON?**
571
+
572
+ - XML has 2-3x size overhead (tags, attributes, whitespace)
573
+ - Products have rich text content (descriptions, attributes)
574
+ - Partner SFTP servers often have smaller file size limits than S3
575
+ - 10k products per file = ~30MB (manageable for most SFTP systems)
576
+
577
+ ### Runtime Validation Function
578
+
579
+ ```typescript
580
+ /**
581
+ * Validate extraction safety limits before processing
582
+ * CRITICAL: Account for XML size overhead vs CSV
583
+ */
584
+ function validateExtractionLimits(productCount: number) {
585
+ const MAX_PRODUCTS_PER_RUN = 50000;
586
+ const ESTIMATED_BYTES_PER_PRODUCT_XML = 3000; // Full XML product element
587
+ const estimatedSizeMB = (productCount * ESTIMATED_BYTES_PER_PRODUCT_XML) / (1024 * 1024);
588
+ const MAX_XML_SIZE_MB = 300;
589
+
590
+ if (productCount > MAX_PRODUCTS_PER_RUN) {
591
+ return {
592
+ valid: false,
593
+ error: `Extraction limit exceeded: ${productCount} products (max: ${MAX_PRODUCTS_PER_RUN})`,
594
+ recommendation: `Too many products for single extraction. Consider:
595
+ 1. Increase extraction frequency (daily → hourly)
596
+ 2. Add product status filters (ACTIVE only)
597
+ 3. Split by catalogue
598
+ 4. Contact support if consistently exceeding limits`,
599
+ productCount,
600
+ maxAllowed: MAX_PRODUCTS_PER_RUN,
601
+ };
602
+ }
603
+
604
+ if (estimatedSizeMB > MAX_XML_SIZE_MB) {
605
+ return {
606
+ valid: false,
607
+ error: `XML size limit exceeded: ${estimatedSizeMB}MB (max: ${MAX_XML_SIZE_MB}MB)`,
608
+ recommendation:
609
+ 'File splitting required. Increase extraction frequency to reduce batch size.',
610
+ estimatedSizeMB,
611
+ maxAllowed: MAX_XML_SIZE_MB,
612
+ };
613
+ }
614
+
615
+ return { valid: true };
616
+ }
617
+ ```
618
+
619
+ ---
620
+
621
+ ## Versori Workflows Structure
622
+
623
+ **Key Concept**: Versori workflows are organized by **trigger type** at the first level, then by **specific workflow** with descriptive file names.
624
+
625
+ **Trigger Types:**
626
+ - **`schedule()`** → Time-based triggers (cron expressions) - NOT exposed as HTTP endpoints
627
+ - **`webhook()`** → HTTP-based triggers (event-driven) - Creates HTTP endpoints
628
+ - **`workflow()`** → Durable workflows (advanced, rarely used)
629
+
630
+ **Execution Steps (chained to triggers):**
631
+ - **`http()`** → External API calls (chained from schedule/webhook)
632
+ - **`fn()`** → Internal processing (chained from schedule/webhook)
633
+
634
+ ### Recommended Project Structure
635
+
636
+ ```
637
+ products-extraction/
638
+ ├── index.ts # Entry point - exports all workflows
639
+ └── src/
640
+ ├── workflows/
641
+ │ ├── scheduled/
642
+ │ │ └── daily-products-extraction.ts # Scheduled: Daily products extraction
643
+ │ │
644
+ │ └── webhook/
645
+ │ ├── adhoc-products-extraction.ts # Webhook: Manual trigger
646
+ │ └── job-status-check.ts # Webhook: Status query
647
+
648
+ ├── services/
649
+ │ └── products-extraction.service.ts # Shared orchestration logic (reusable)
650
+
651
+ └── config/
652
+ └── products.export.xml.json # Mapping configuration
653
+ ```
654
+
655
+ ---
656
+
657
+ ## Complete Workflow Code
658
+
659
+ The code below demonstrates the implementation of each component in the modular structure.
660
+
661
+ ### 1. Entry Point (`index.ts`)
662
+
663
+ ```typescript
664
+ /**
665
+ * Entry point - Export all workflows for Versori platform
666
+ *
667
+ * This file exports all workflows to be registered with Versori.
668
+ * Each workflow is defined in its own file for better organization.
669
+ */
670
+
671
+ // Scheduled workflows
672
+ export { dailyProductsExtraction } from './src/workflows/scheduled/daily-products-extraction';
673
+
674
+ // Webhook workflows
675
+ export { adhocProductsExtraction } from './src/workflows/webhook/adhoc-products-extraction';
676
+ export { productsExtractionJobStatus } from './src/workflows/webhook/job-status-check';
677
+ ```
678
+
679
+ ### 2. Workflows (src/workflows/products-extraction.ts)
680
+
681
+ ```typescript
682
+ // ⚠️ IMPORTANT: Do NOT import openKv - access it from context!
683
+ import { schedule, webhook, http, fn } from '@versori/run';
684
+ import {
685
+ executeProductExtraction,
686
+ getJobStatus,
687
+ generateJobId,
688
+ } from '../services/products-extraction.service';
689
+
690
+ // 
691
+ // WORKFLOW 1: Scheduled Extraction (Daily at 2 AM)
692
+ // 
693
+
694
+ export const scheduledProductsExtraction = schedule('products-extract-xml-daily', '0 2 * * *').then(
695
+ http('execute-scheduled-extraction', { connection: 'fluent_commerce' }, async ctx => {
696
+ const jobId = generateJobId('SCHED', 'PRODUCTS');
697
+
698
+ const result = await executeProductExtraction(ctx, {
699
+ jobId,
700
+ triggeredBy: 'schedule',
701
+ updateState: true, // Always update state for scheduled runs
702
+ });
703
+
704
+ return result;
705
+ })
706
+ );
707
+
708
+ // 
709
+ // WORKFLOW 2: Ad-hoc Webhook Extraction
710
+ // 
711
+
712
+ export const adhocProductsExtraction = webhook('products-adhoc', {
713
+ connection: 'products-adhoc',
714
+ response: { mode: 'sync' }, // ✅ Sync mode: response sent when handler returns
715
+ }).then(
716
+ http('execute-adhoc-extraction', { connection: 'fluent_commerce' }, async ctx => {
717
+ // Security is enforced by the 'products-adhoc' connection
718
+ const { log } = ctx;
719
+ const jobId = generateJobId('ADHOC', 'PRODUCTS');
720
+
721
+ log.info('🚀 [WEBHOOK] Adhoc products extraction triggered', {
722
+ jobId,
723
+ fromDate: ctx.data.fromDate,
724
+ toDate: ctx.data.toDate,
725
+ updateState: ctx.data.updateState,
726
+ });
727
+
728
+ // ✅ Fire-and-forget: Start background processing WITHOUT await
729
+ // The promise continues execution after we return the response
730
+ executeProductExtraction(ctx, {
731
+ jobId,
732
+ triggeredBy: 'webhook',
733
+ fromDate: ctx.data.fromDate,
734
+ toDate: ctx.data.toDate,
735
+ updateState: ctx.data.updateState === true,
736
+ })
737
+ .then((result) => {
738
+ log.info('✅ [BACKGROUND] Products extraction completed successfully', {
739
+ jobId,
740
+ recordCount: result.recordCount,
741
+ fileName: result.fileName,
742
+ });
743
+ })
744
+ .catch((error: unknown) => {
745
+ const errorMessage = error instanceof Error ? error.message : String(error);
746
+ log.error('❌ [BACKGROUND] Products extraction failed', {
747
+ jobId,
748
+ error: errorMessage,
749
+ stack: error instanceof Error ? error.stack : undefined,
750
+ });
751
+ });
752
+
753
+ // Return immediately with jobId (response sent with this return value)
754
+ return {
755
+ success: true,
756
+ jobId,
757
+ message: 'Products extraction started in background',
758
+ statusEndpoint: `https://{workspace}.versori.run/products-job-status`,
759
+ note: 'Poll the status endpoint with jobId to check progress',
760
+ };
761
+ })
762
+ );
763
+
764
+ // 
765
+ // WORKFLOW 3: Job Status Query
766
+ // 
767
+
768
+ export const productsJobStatus = webhook('products-job-status', {
769
+ connection: 'products-job-status',
770
+ response: { mode: 'sync' },
771
+ }).then(
772
+ fn('query-job-status', async ctx => {
773
+ const { data, log, openKv } = ctx;
774
+ // Security is enforced by the 'products-job-status' connection
775
+
776
+ const jobId = data.jobId;
777
+ if (!jobId) {
778
+ return { success: false, error: 'Job ID required' };
779
+ }
780
+
781
+ const status = await getJobStatus(openKv(':project:'), jobId, log);
782
+ return status
783
+ ? { success: true, jobId, ...status }
784
+ : { success: false, error: 'Job not found', jobId };
785
+ })
786
+ );
787
+ ```
788
+
789
+ ### 3. Main Orchestration Service (`src/services/products-extraction.service.ts`)
790
+
791
+ **Note:** This service file should be renamed from `extraction-orchestration.ts` to `products-extraction.service.ts` to match the new workflow structure.
792
+
793
+ ```typescript
794
+ import { Buffer } from 'node:buffer';
795
+ import {
796
+ createClient,
797
+ ExtractionOrchestrator,
798
+ JobTracker,
799
+ UniversalMapper,
800
+ XMLBuilder,
801
+ SftpDataSource,
802
+ VersoriKVAdapter,
803
+ } from '@fluentcommerce/fc-connect-sdk';
804
+ import productsExportMapping from '../../config/products.export.xml.json' with { type: 'json' };
805
+
806
+ const PRODUCTS_EXTRACTION_QUERY = `
807
+ query GetProducts(
808
+ $catalogues: [ProductCatalogueKey]
809
+ $dateRangeFilter: DateRange
810
+ $first: Int!
811
+ $after: String
812
+ ) {
813
+ products(
814
+ catalogueRef: $catalogues
815
+ updatedOn: $dateRangeFilter
816
+ first: $first
817
+ after: $after
818
+ ) {
819
+ edges {
820
+ node {
821
+ id
822
+ ref
823
+ name
824
+ type
825
+ status
826
+ gtin
827
+ price
828
+ attributes
829
+ catalogue {
830
+ ref
831
+ name
832
+ }
833
+ createdOn
834
+ updatedOn
835
+ }
836
+ cursor
837
+ }
838
+ pageInfo {
839
+ hasNextPage
840
+ }
841
+ }
842
+ }
843
+ `;
844
+
845
+ // Initialize XMLBuilder for products
846
+ const xmlBuilder = new XMLBuilder({
847
+ rootElement: 'Products',
848
+ prettyPrint: true,
849
+ indent: ' ',
850
+ xmlDeclaration: true,
851
+ encoding: 'UTF-8',
852
+ });
853
+
854
+ function buildProductsXML(products: any[]): string {
855
+ // Transform to XMLBuilder format
856
+ const productsForXml = products.map(p => ({
857
+ sku: p.sku,
858
+ title: p.title,
859
+ description: p.description || '',
860
+ gtin: p.gtin || '',
861
+ type: p.type || '',
862
+ status: p.status,
863
+ price: String(p.price || ''),
864
+ catalogue_ref: p.catalogue_ref || '',
865
+ catalogue_name: p.catalogue_name || '',
866
+ created_on: p.created_on,
867
+ updated_on: p.updated_on,
868
+ }));
869
+
870
+ return xmlBuilder.build({ Product: productsForXml });
871
+ }
872
+
873
+ interface ProductExtractionParams {
874
+ jobId: string;
875
+ triggeredBy: 'schedule' | 'webhook';
876
+ fromDate?: string;
877
+ toDate?: string;
878
+ updateState: boolean;
879
+ }
880
+
881
+ export async function executeProductExtraction(ctx: any, options: ProductExtractionParams) {
882
+ const { jobId, triggeredBy, fromDate, toDate, updateState } = options;
883
+ const log = ctx.log;
884
+ const retailerId = ctx.activation?.getVariable('retailerId');
885
+ const pageSize = parseInt(ctx.activation?.getVariable('pageSize') || '200', 10);
886
+ const maxRecords = parseInt(ctx.activation?.getVariable('maxRecords') || '50000', 10);
887
+ const fallbackStartDate =
888
+ ctx.activation?.getVariable('fallbackStartDate') || '2024-01-01T00:00:00Z';
889
+ const productStatus = ctx.activation?.getVariable('productStatus') || 'ACTIVE';
890
+
891
+ // Get SFTP credentials from Versori connection (Basic Auth)
892
+ // RECOMMENDED: Use activation.connections (already decoded)
893
+ const allConnections = ctx.activation.connections || [];
894
+ const sftpConn = allConnections.find(c => c.name === 'versori_ftp_server');
895
+
896
+ if (!sftpConn) {
897
+ throw new Error('SFTP connection "versori_ftp_server" not found');
898
+ }
899
+
900
+ const credential = sftpConn.credentials[0]?.credential;
901
+ if (!credential?.data?.basicAuth) {
902
+ throw new Error('SFTP connection not configured with Basic Authentication');
903
+ }
904
+
905
+ const { username, password } = credential.data.basicAuth;
906
+ // ✅ Already decoded - no Buffer.from() needed!
907
+
908
+ const sftpSettings = {
909
+ host: ctx.activation?.getVariable('sftpHost'),
910
+ port: parseInt(ctx.activation?.getVariable('sftpPort') || '22', 10),
911
+ username, // From connection (secure)
912
+ password, // From connection (secure)
913
+ privateKey: ctx.activation?.getVariable('sftpPrivateKey'),
914
+ remotePath: ctx.activation?.getVariable('sftpRemotePath') || '/incoming/products/',
915
+ };
916
+
917
+ const missing: string[] = [];
918
+ if (!retailerId) missing.push('retailerId');
919
+ if (!sftpSettings.host) missing.push('sftpHost');
920
+ if (missing.length)
921
+ return { success: false, error: `Missing required variables: ${missing.join(', ')}` };
922
+
923
+ // SFTP connection - MUST use try/finally with dispose()
924
+ const sftp = new SftpDataSource(
925
+ {
926
+ type: 'SFTP_XML',
927
+ connectionId: 'sftp-products-xml-export',
928
+ name: 'SFTP Products XML Export',
929
+ settings: {
930
+ host: sftpSettings.host,
931
+ port: sftpSettings.port,
932
+ username: sftpSettings.username,
933
+ password: sftpSettings.password,
934
+ privateKey: sftpSettings.privateKey,
935
+ remotePath: sftpSettings.remotePath,
936
+ filePattern: '*.xml',
937
+ },
938
+ },
939
+ log
940
+ );
941
+
942
+ try {
943
+ // 
944
+ // STEP 1/8: Initialize Job Tracking
945
+ // 
946
+ const kv = new VersoriKVAdapter(ctx.openKv(':project:'));
947
+ const tracker = new JobTracker(kv, log);
948
+
949
+ await tracker.createJob(jobId, {
950
+ triggeredBy,
951
+ hasDateOverride: !!fromDate,
952
+ fromDate,
953
+ toDate,
954
+ updateStateAfterRun: updateState,
955
+ });
956
+
957
+ log.info('Job created', { jobId, triggeredBy });
958
+
959
+ // 
960
+ // STEP 2/8: Load State & Calculate Time Window
961
+ // 
962
+ await tracker.updateJob(jobId, {
963
+ status: 'processing',
964
+ stage: 'state_load',
965
+ message: 'Loading last run state',
966
+ });
967
+
968
+ const stateKey = ['extraction', 'products-xml', 'lastProductSync'];
969
+ const lastRunState = await kv.get(stateKey);
970
+ const rawLastRunTime = fromDate || lastRunState?.value?.timestamp || fallbackStartDate;
971
+
972
+ // Overlap buffer configuration (default: 60 seconds)
973
+ const overlapBufferSeconds = parseInt(
974
+ ctx.activation?.getVariable('overlapBufferSeconds') || '60',
975
+ 10
976
+ );
977
+ const OVERLAP_BUFFER_MS = overlapBufferSeconds * 1000;
978
+
979
+ // Apply overlap buffer for query (safety window)
980
+ const bufferedLastRunTime = new Date(
981
+ new Date(rawLastRunTime).getTime() - OVERLAP_BUFFER_MS
982
+ ).toISOString();
983
+
984
+ const effectiveEndTime = toDate || new Date().toISOString();
985
+
986
+ log.info('🔍 Time window calculated', {
987
+ rawLastRunTime,
988
+ bufferedLastRunTime,
989
+ effectiveEndTime,
990
+ overlapBufferSeconds,
991
+ retailerId,
992
+ productStatus,
993
+ });
994
+
995
+ // 
996
+ // STEP 3/8: Initialize Fluent Client & ExtractionOrchestrator
997
+ // 
998
+ await tracker.updateJob(jobId, {
999
+ stage: 'client_init',
1000
+ message: 'Initializing Fluent client',
1001
+ });
1002
+
1003
+ const client = await createClient(ctx);
1004
+ const orchestrator = new ExtractionOrchestrator(client, log);
1005
+
1006
+ // 
1007
+ // STEP 4/8: Extract Data (ExtractionOrchestrator)
1008
+ // 
1009
+ await tracker.updateJob(jobId, {
1010
+ stage: 'extraction',
1011
+ message: 'Extracting data with auto-pagination',
1012
+ });
1013
+
1014
+ // ? Enhanced: Extract context for progress logging
1015
+ const dateRangeInfo = {
1016
+ start: bufferedLastRunTime || 'N/A',
1017
+ end: effectiveEndTime || 'N/A',
1018
+ catalogues: 'all'
1019
+ };
1020
+
1021
+ // ? Enhanced: Start logging with context
1022
+ log.info(`📊 [ExtractionOrchestrator] Starting extraction`, {
1023
+ query: 'products',
1024
+ pageSize,
1025
+ maxRecords,
1026
+ dateRange: `${dateRangeInfo.start} to ${dateRangeInfo.end}`,
1027
+ catalogues: dateRangeInfo.catalogues,
1028
+ jobId
1029
+ });
1030
+
1031
+ const extractionResult = await orchestrator.extract({
1032
+ query: PRODUCTS_EXTRACTION_QUERY,
1033
+ resultPath: 'products.edges.node',
1034
+ variables: {
1035
+ catalogues: null, // All catalogues
1036
+ dateRangeFilter: {
1037
+ after: bufferedLastRunTime,
1038
+ before: effectiveEndTime, // End of extraction window
1039
+ },
1040
+ first: pageSize,
1041
+ },
1042
+ pageSize,
1043
+ maxRecords,
1044
+ validateItem: item => !!(item.ref && item.name),
1045
+ });
1046
+
1047
+ const rawRecords = extractionResult.data;
1048
+
1049
+ log.info('Extraction complete', {
1050
+ totalRecords: extractionResult.stats.totalRecords,
1051
+ totalPages: extractionResult.stats.totalPages,
1052
+ validRecords: extractionResult.stats.validRecords ?? rawRecords.length,
1053
+ errors: extractionResult.errors ? extractionResult.errors.length : 0,
1054
+ });
1055
+
1056
+ // ? Enhanced: Completion logging with summary
1057
+ log.info(`✅ [ExtractionOrchestrator] Extraction completed`, {
1058
+ totalRecords: extractionResult.stats.totalRecords,
1059
+ totalPages: extractionResult.stats.totalPages,
1060
+ validRecords: extractionResult.stats.validRecords ?? rawRecords.length,
1061
+ failedValidations: extractionResult.stats.failedValidations,
1062
+ truncated: extractionResult.stats.truncated,
1063
+ truncationReason: extractionResult.stats.truncationReason,
1064
+ dateRange: `${dateRangeInfo.start} to ${dateRangeInfo.end}`,
1065
+ jobId
1066
+ });
1067
+
1068
+ if (extractionResult.errors && extractionResult.errors.length > 0) {
1069
+ log.warn('Non-fatal extraction errors encountered', {
1070
+ errorCount: extractionResult.errors.length,
1071
+ sampleErrors: extractionResult.errors.slice(0, 3),
1072
+ });
1073
+ }
1074
+
1075
+ if (rawRecords.length === 0) {
1076
+ await tracker.markCompleted(jobId, {
1077
+ recordCount: 0,
1078
+ message: 'No new products to extract',
1079
+ });
1080
+
1081
+ if (updateState) {
1082
+ await kv.set(stateKey, {
1083
+ timestamp: new Date().toISOString(),
1084
+ productCount: 0,
1085
+ extractedAt: new Date().toISOString(),
1086
+ });
1087
+ }
1088
+
1089
+ return { success: true, message: 'No new products to extract', lastRunTime: rawLastRunTime };
1090
+ }
1091
+
1092
+ // 
1093
+ // STEP 5/8: Validate Extraction Limits
1094
+ // 
1095
+ await tracker.updateJob(jobId, {
1096
+ stage: 'validation',
1097
+ message: 'Validating extraction limits',
1098
+ });
1099
+
1100
+ const MAX_PRODUCTS_PER_RUN = 50000;
1101
+ const ESTIMATED_BYTES_PER_PRODUCT_XML = 3000;
1102
+ const estimatedSizeMB = (rawRecords.length * ESTIMATED_BYTES_PER_PRODUCT_XML) / (1024 * 1024);
1103
+ const MAX_XML_SIZE_MB = 300;
1104
+
1105
+ if (rawRecords.length > MAX_PRODUCTS_PER_RUN) {
1106
+ log.error('Extraction limit exceeded', {
1107
+ productCount: rawRecords.length,
1108
+ maxAllowed: MAX_PRODUCTS_PER_RUN,
1109
+ });
1110
+
1111
+ await tracker.markFailed(jobId, {
1112
+ error: `Extraction limit exceeded: ${rawRecords.length} products (max: ${MAX_PRODUCTS_PER_RUN})`,
1113
+ recommendation: 'Increase extraction frequency or add filters',
1114
+ });
1115
+
1116
+ return {
1117
+ success: false,
1118
+ error: `Extraction limit exceeded: ${rawRecords.length} products (max: ${MAX_PRODUCTS_PER_RUN})`,
1119
+ recommendation: `Too many products for single extraction. Consider:
1120
+ 1. Increase extraction frequency (daily → hourly)
1121
+ 2. Add product status filters (ACTIVE only)
1122
+ 3. Split by catalogue
1123
+ 4. Contact support if consistently exceeding limits`,
1124
+ productCount: rawRecords.length,
1125
+ maxAllowed: MAX_PRODUCTS_PER_RUN,
1126
+ };
1127
+ }
1128
+
1129
+ if (estimatedSizeMB > MAX_XML_SIZE_MB) {
1130
+ log.warn('XML size approaching limit', {
1131
+ estimatedSizeMB: estimatedSizeMB.toFixed(2),
1132
+ maxAllowed: MAX_XML_SIZE_MB,
1133
+ recommendation: 'Consider file splitting or increase extraction frequency',
1134
+ });
1135
+ }
1136
+
1137
+ log.info('Extraction limits validated', {
1138
+ productCount: rawRecords.length,
1139
+ estimatedSizeMB: estimatedSizeMB.toFixed(2),
1140
+ withinLimits: true,
1141
+ });
1142
+
1143
+ // 
1144
+ // STEP 6/8: Transform Data (UniversalMapper)
1145
+ // 
1146
+ await tracker.updateJob(jobId, {
1147
+ stage: 'transformation',
1148
+ message: 'Transforming data with UniversalMapper',
1149
+ });
1150
+
1151
+ const mapper = new UniversalMapper(productsExportMapping);
1152
+ const mappingResult = await mapper.map(rawRecords);
1153
+
1154
+ if (!mappingResult.success) {
1155
+ const mappingErrors = mappingResult.errors || ['Unknown mapping failure'];
1156
+ await tracker.markFailed(jobId, {
1157
+ error: mappingErrors[0] || 'UniversalMapper returned unsuccessful result',
1158
+ failedCount: mappingErrors.length,
1159
+ });
1160
+ return {
1161
+ success: false,
1162
+ error: `Transformation failed: ${mappingErrors[0] || 'Unknown error'}`,
1163
+ errors: mappingErrors,
1164
+ };
1165
+ }
1166
+
1167
+ const transformedProducts = Array.isArray(mappingResult.data) ? mappingResult.data : [];
1168
+ const mappingErrors = mappingResult.errors || [];
1169
+
1170
+ if (mappingErrors.length > 0) {
1171
+ log.warn('Some products failed transformation', {
1172
+ jobId,
1173
+ errorCount: mappingErrors.length,
1174
+ sampleErrors: mappingErrors.slice(0, 3),
1175
+ });
1176
+ }
1177
+
1178
+ if (mappingResult.skippedFields && mappingResult.skippedFields.length > 0) {
1179
+ log.info('ℹ️ [MAPPING] Optional fields skipped (undefined values)', {
1180
+ jobId,
1181
+ skippedFields: mappingResult.skippedFields,
1182
+ note: 'These fields were not present in source data. Add defaultValue to mapping config if they should always appear.',
1183
+ });
1184
+ }
1185
+
1186
+ if (transformedProducts.length === 0) {
1187
+ await tracker.markFailed(jobId, {
1188
+ error: 'All records failed mapping',
1189
+ failedCount: mappingErrors.length,
1190
+ });
1191
+ return {
1192
+ success: false,
1193
+ error: 'All records failed mapping',
1194
+ errors: mappingErrors,
1195
+ };
1196
+ }
1197
+
1198
+ log.info('Products transformed', {
1199
+ jobId,
1200
+ transformedCount: transformedProducts.length,
1201
+ skippedRecords: rawRecords.length - transformedProducts.length,
1202
+ });
1203
+
1204
+ // 
1205
+ // STEP 7/8: Generate XML & Upload to SFTP
1206
+ // 
1207
+ await tracker.updateJob(jobId, {
1208
+ stage: 'upload',
1209
+ message: 'Generating XML and uploading to SFTP',
1210
+ });
1211
+
1212
+ const xmlContent = buildProductsXML(transformedProducts);
1213
+
1214
+ // Generate timestamped filename
1215
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
1216
+ const fileName = `products-${timestamp}.xml`;
1217
+ const remotePath = `${sftpSettings.remotePath}${fileName}`;
1218
+
1219
+ log.info('Generated XML file', {
1220
+ fileName,
1221
+ size: xmlContent.length,
1222
+ productCount: transformedProducts.length,
1223
+ });
1224
+
1225
+ // Upload to SFTP
1226
+ await sftp.uploadFile(remotePath, Buffer.from(xmlContent, 'utf8'));
1227
+
1228
+ log.info('XML file uploaded to SFTP', { remotePath });
1229
+
1230
+ // 
1231
+ // STEP 8/8: Update State & Complete Job
1232
+ // 
1233
+ await tracker.updateJob(jobId, {
1234
+ stage: 'state_update',
1235
+ message: 'Updating state and completing job',
1236
+ });
1237
+
1238
+ // Calculate max updatedOn from extracted products
1239
+ const maxUpdatedOn = transformedProducts.reduce((max, product) => {
1240
+ const productTime = new Date(product.updated_on).getTime();
1241
+ return productTime > max ? productTime : max;
1242
+ }, new Date(rawLastRunTime).getTime());
1243
+
1244
+ const newTimestamp = new Date(maxUpdatedOn).toISOString();
1245
+
1246
+ // Update state with new timestamp (WITHOUT buffer)
1247
+ if (updateState) {
1248
+ await kv.set(stateKey, {
1249
+ timestamp: newTimestamp, // ← NO buffer applied
1250
+ productCount: transformedProducts.length,
1251
+ extractedAt: new Date().toISOString(),
1252
+ overlapBufferSeconds,
1253
+ fileName,
1254
+ remotePath,
1255
+ errors: mappingErrors.length > 0 ? mappingErrors : undefined,
1256
+ });
1257
+
1258
+ log.info('State updated with new timestamp (without buffer)', {
1259
+ newTimestamp,
1260
+ overlapBufferSeconds,
1261
+ });
1262
+ }
1263
+
1264
+ await tracker.markCompleted(jobId, {
1265
+ recordCount: transformedProducts.length,
1266
+ fileName,
1267
+ sftpPath: remotePath,
1268
+ errorCount: mappingErrors.length,
1269
+ errors: mappingErrors,
1270
+ });
1271
+
1272
+ return {
1273
+ success: true,
1274
+ productsExtracted: transformedProducts.length,
1275
+ fileName,
1276
+ remotePath,
1277
+ lastRunTime: rawLastRunTime,
1278
+ newTimestamp,
1279
+ jobId,
1280
+ errors: mappingErrors.length > 0 ? mappingErrors : undefined,
1281
+ };
1282
+ } catch (error: any) {
1283
+ log.error('Extraction failed', error, {
1284
+ message: error?.message,
1285
+ });
1286
+
1287
+ const kv = new VersoriKVAdapter(ctx.openKv(':project:'));
1288
+ const tracker = new JobTracker(kv, log);
1289
+
1290
+ await tracker.markFailed(jobId, {
1291
+ message: error instanceof Error ? error.message : String(error),
1292
+
1293
+ stack: error instanceof Error ? error.stack : undefined,
1294
+
1295
+ errorType: error instanceof Error ? error.constructor.name : 'UnknownError',
1296
+ });
1297
+
1298
+ return {
1299
+ success: false,
1300
+ message: error instanceof Error ? error.message : String(error),
1301
+
1302
+ stack: error instanceof Error ? error.stack : undefined,
1303
+
1304
+ errorType: error instanceof Error ? error.constructor.name : 'UnknownError',
1305
+ jobId,
1306
+ };
1307
+ } finally {
1308
+ // CRITICAL: Always clean up SFTP connections
1309
+ await sftp.dispose();
1310
+ log.info('SFTP connection disposed');
1311
+ }
1312
+ }
1313
+
1314
+ export async function getJobStatus(kv: any, jobId: string, log: any) {
1315
+ const tracker = new JobTracker(new VersoriKVAdapter(kv), log);
1316
+ return await tracker.getJob(jobId);
1317
+ }
1318
+ ```
1319
+
1320
+ ### 4. Job ID Generator (src/utils/job-id-generator.ts)
1321
+
1322
+ ```typescript
1323
+ /**
1324
+ * Generate unique job ID
1325
+ * Format: {PREFIX}-{ENTITY}-{TIMESTAMP}
1326
+ */
1327
+ export function generateJobId(prefix: 'SCHED' | 'ADHOC', entity: string): string {
1328
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
1329
+ return `${prefix}-${entity}-${timestamp}`;
1330
+ }
1331
+ ```
1332
+
1333
+ ### 5. Package Configuration (package.json)
1334
+
1335
+ ```json
1336
+ {
1337
+ "name": "products-extraction-to-sftp-xml",
1338
+ "version": "1.0.0",
1339
+ "description": "Versori connector for products extraction to SFTP XML",
1340
+ "main": "dist/index.js",
1341
+ "type": "module",
1342
+ "scripts": {
1343
+ "build": "tsc",
1344
+ "dev": "tsc --watch",
1345
+ "lint": "eslint src/**/*.ts",
1346
+ "test": "jest"
1347
+ },
1348
+ "dependencies": {
1349
+ "@fluentcommerce/fc-connect-sdk": "^0.1.39",
1350
+ "@versori/run": "latest"
1351
+ },
1352
+ "devDependencies": {
1353
+ "@types/node": "^20.0.0",
1354
+ "typescript": "^5.0.0"
1355
+ }
1356
+ }
1357
+ ```
1358
+
1359
+ ### 6. Deployment Instructions
1360
+
1361
+ ```bash
1362
+ # 1. Install dependencies
1363
+ npm install
1364
+
1365
+ # 2. Build the connector
1366
+ npm run build
1367
+
1368
+ # 3. Test locally (optional)
1369
+ npm test
1370
+
1371
+ # 4. Deploy to Versori
1372
+ # - Upload to Versori workspace
1373
+ # - Configure activation variables
1374
+ # - Enable workflows
1375
+
1376
+ # 5. Test workflows
1377
+ # Scheduled: Wait for next cron trigger or manually trigger
1378
+ # Ad-hoc: POST to webhook URL with API key header
1379
+ # Status: Query job status by ID
1380
+ ```
1381
+
1382
+ ### 7. Testing
1383
+
1384
+ #### Test Scheduled Extraction
1385
+
1386
+ ```bash
1387
+ # Trigger manually in Versori UI or wait for cron schedule
1388
+ # Expected: XML file uploaded to SFTP
1389
+ ```
1390
+
1391
+ #### Test Ad-hoc Extraction
1392
+
1393
+ ```bash
1394
+ curl -X POST https://your-workspace.versori.run/products-adhoc \
1395
+ -H "Content-Type: application/json" \
1396
+ -d '{
1397
+ "fromDate": "2025-01-01T00:00:00Z",
1398
+ "toDate": "2025-01-22T23:59:59Z",
1399
+ "updateState": false
1400
+ }'
1401
+ ```
1402
+
1403
+ #### Test Job Status Query
1404
+
1405
+ ```bash
1406
+ curl -X POST https://your-workspace.versori.run/products-job-status \
1407
+ -H "Content-Type: application/json" \
1408
+ -d '{
1409
+ "jobId": "SCHED-PRODUCTS-2025-01-22T02-00-00Z"
1410
+ }'
1411
+ ```
1412
+
1413
+ ## Key Patterns Explained
1414
+
1415
+ ### Pattern 1: ExtractionOrchestrator for Auto-Pagination
1416
+
1417
+ ```typescript
1418
+ // ✅ CORRECT - Use ExtractionOrchestrator (handles pagination automatically)
1419
+ const orchestrator = new ExtractionOrchestrator(client, log);
1420
+
1421
+ const extractionResult = await orchestrator.extract({
1422
+ query: PRODUCTS_EXTRACTION_QUERY,
1423
+ resultPath: 'products.edges.node',
1424
+ variables: { dateRangeFilter: { after: bufferedLastRunTime } },
1425
+ pageSize,
1426
+ maxRecords,
1427
+ validateItem: item => !!(item.ref && item.name),
1428
+ });
1429
+
1430
+ const records = extractionResult.data;
1431
+
1432
+ // WRONG - Manual pagination (avoid this pattern)
1433
+ // const result = await client.graphql({
1434
+ // query: PRODUCTS_QUERY,
1435
+ // variables: { first: pageSize },
1436
+ // pagination: { maxRecords }
1437
+ // });
1438
+ ```
1439
+
1440
+ ### Pattern 2: JobTracker for Lifecycle Management
1441
+
1442
+ ```typescript
1443
+ // ✅ CORRECT - Use JobTracker throughout workflow
1444
+ const tracker = new JobTracker(kv, log);
1445
+
1446
+ // Create job
1447
+ await tracker.createJob(jobId, { triggeredBy, fromDate, toDate });
1448
+
1449
+ // Update progress
1450
+ await tracker.updateJob(jobId, { stage: 'extraction', message: 'Extracting data' });
1451
+
1452
+ // Mark completed
1453
+ await tracker.markCompleted(jobId, { recordCount, fileName });
1454
+
1455
+ // Query status
1456
+ const status = await tracker.getJob(jobId);
1457
+ ```
1458
+
1459
+ ### Pattern 3: 3-Workflow Pattern
1460
+
1461
+ ```typescript
1462
+ // ✅ CORRECT - 3 workflows for different use cases
1463
+ // 1. Scheduled: Automated daily/hourly runs
1464
+ export const scheduledProductsExtraction = schedule('products-extract-xml-daily', '0 2 * * *').then(...)
1465
+
1466
+ // 2. Ad-hoc: Manual webhook triggers with date overrides
1467
+ export const adhocProductsExtraction = webhook('products-adhoc', {
1468
+ connection: 'products-adhoc',
1469
+ response: { mode: 'sync' },
1470
+ }).then(...)
1471
+
1472
+ // 3. Status: Query job status by ID
1473
+ export const productsJobStatus = webhook('products-job-status', {
1474
+ connection: 'products-job-status',
1475
+ response: { mode: 'sync' },
1476
+ }).then(...)
1477
+ ```
1478
+
1479
+ ### Pattern 4: XMLBuilder for Safe XML Generation (CRITICAL)
1480
+
1481
+ Use the SDK's `XMLBuilder` - it handles all XML escaping automatically:
1482
+
1483
+ ```typescript
1484
+ import { Buffer } from 'node:buffer';
1485
+ import { XMLBuilder } from '@fluentcommerce/fc-connect-sdk';
1486
+
1487
+ // Initialize XMLBuilder (handles all escaping automatically)
1488
+ const xmlBuilder = new XMLBuilder({
1489
+ rootElement: 'Products',
1490
+ prettyPrint: true,
1491
+ encoding: 'UTF-8',
1492
+ });
1493
+
1494
+ // ✅ CORRECT: XMLBuilder escapes automatically
1495
+ const products = [
1496
+ {
1497
+ title: 'Smith & Jones <Corp>', // Contains & and <>
1498
+ description: 'Special chars: ¢, ©, ®, "quotes"',
1499
+ },
1500
+ ];
1501
+
1502
+ const xml = xmlBuilder.build({ Product: products });
1503
+ // Result: All special characters properly escaped
1504
+ // <title>Smith &amp; Jones &lt;Corp&gt;</title>
1505
+ // <description>Special chars: ¢, ©, ®, &quot;quotes&quot;</description>
1506
+
1507
+ // WRONG: Manual string concatenation (dangerous)
1508
+ // const xml = `<title>${product.title}</title>`;
1509
+ // This would produce INVALID XML: <title>Smith & Jones <Corp></title>
1510
+ ```
1511
+
1512
+ **Why XMLBuilder?**
1513
+
1514
+ - ✅ Automatic escaping of &, <, >, ", '
1515
+ - ✅ Handles special characters (¢, ©, ®)
1516
+ - ✅ Prevents XML injection attacks
1517
+ - ✅ Validates structure
1518
+ - ✅ Consistent, maintainable code
1519
+
1520
+ ### Pattern 5: SFTP Cleanup (CRITICAL)
1521
+
1522
+ ```typescript
1523
+ const sftp = new SftpDataSource(config, log);
1524
+
1525
+ try {
1526
+ await sftp.uploadFile(remotePath, buffer);
1527
+ return { success: true };
1528
+ } finally {
1529
+ // ALWAYS dispose SFTP connection
1530
+ await sftp.dispose();
1531
+ }
1532
+ ```
1533
+
1534
+ **Why?** SFTP maintains open connections. Not calling `dispose()` leads to connection exhaustion.
1535
+
1536
+ ### Pattern 6: Consistent Field Names Across Formats
1537
+
1538
+ **Same data in CSV, JSON, and XML:**
1539
+
1540
+ - `sku` (not productId, not sku_ref, not SKU)
1541
+ - `title` (consistent with CSV version)
1542
+ - `catalogue_ref` (matches CSV exactly)
1543
+
1544
+ This allows users to switch formats without changing downstream systems.
1545
+
1546
+ ---
1547
+
1548
+ ### Pattern 7: State Management & Date Overrides
1549
+
1550
+ **Use Case**: Understand how state management works with scheduled and ad-hoc extractions.
1551
+
1552
+ **How it works**:
1553
+
1554
+ VersoriKV stores the last successful extraction timestamp to enable incremental sync:
1555
+
1556
+ ```typescript
1557
+ interface ExtractionState {
1558
+ timestamp: string; // Last run timestamp (WITHOUT overlap buffer)
1559
+ recordCount: number; // Number of records extracted
1560
+ extractedAt: string; // When extraction completed
1561
+ fileName?: string; // Generated filename
1562
+ remotePath?: string; // SFTP upload path
1563
+ overlapBufferSeconds?: number; // Buffer configuration
1564
+ }
1565
+ ```
1566
+
1567
+ **State Priority Chain** (highest to lowest):
1568
+
1569
+ 1. **`fromDate` override** (manual date in webhook payload) - Highest priority
1570
+ 2. **Stored state** (`await kv.get(stateKey)`) - Normal incremental mode
1571
+ 3. **`fallbackStartDate`** (activation variable) - First run fallback
1572
+
1573
+ **Three Scenarios**:
1574
+
1575
+ #### Scenario 1: Normal Scheduled Runs (Incremental)
1576
+
1577
+ ```typescript
1578
+ // Payload: {} (empty - no overrides)
1579
+
1580
+ // Behavior:
1581
+ // 1. Load last timestamp from KV: "2025-01-22T10:00:00Z"
1582
+ // 2. Apply overlap buffer: "2025-01-22T09:59:00Z" (query WITH buffer)
1583
+ // 3. Extract records updated since buffered time
1584
+ // 4. Calculate MAX(updatedOn) from results: "2025-01-22T14:30:00Z"
1585
+ // 5. Save new timestamp WITHOUT buffer: "2025-01-22T14:30:00Z"
1586
+ // 6. Next run starts from "2025-01-22T14:29:00Z" (with buffer)
1587
+ ```
1588
+
1589
+ **Test**:
1590
+
1591
+ ```bash
1592
+ # Trigger scheduled run (no payload needed)
1593
+ # State advances automatically
1594
+ curl -X POST https://workspace.versori.run/products-extract-daily
1595
+ ```
1596
+
1597
+ #### Scenario 2: Ad-hoc Extraction WITH State Update
1598
+
1599
+ ```typescript
1600
+ // Payload: { "fromDate": "2025-01-01T00:00:00Z", "updateState": true }
1601
+
1602
+ // Behavior:
1603
+ // 1. Ignore stored state
1604
+ // 2. Use fromDate: "2025-01-01T00:00:00Z" (no buffer applied to manual dates)
1605
+ // 3. Extract all records since 2025-01-01
1606
+ // 4. Calculate MAX(updatedOn): "2025-01-22T14:30:00Z"
1607
+ // 5. Save new timestamp: "2025-01-22T14:30:00Z" (updates state!)
1608
+ // 6. Next scheduled run starts from this new timestamp
1609
+ ```
1610
+
1611
+ **Use Case**: One-time catch-up extraction that advances the state pointer.
1612
+
1613
+ **Test**:
1614
+
1615
+ ```bash
1616
+ curl -X POST https://workspace.versori.run/products-extract-webhook \
1617
+ -H "Content-Type: application/json" \
1618
+ -d '{
1619
+ "fromDate": "2025-01-01T00:00:00Z",
1620
+ "updateState": true
1621
+ }'
1622
+ ```
1623
+
1624
+ #### Scenario 3: Ad-hoc Extraction WITHOUT State Update
1625
+
1626
+ ```typescript
1627
+ // Payload: { "fromDate": "2025-01-01T00:00:00Z", "updateState": false }
1628
+
1629
+ // Behavior:
1630
+ // 1. Ignore stored state
1631
+ // 2. Use fromDate: "2025-01-01T00:00:00Z"
1632
+ // 3. Extract all records since 2025-01-01
1633
+ // 4. DO NOT update state
1634
+ // 5. Next scheduled run uses previous timestamp (unaffected)
1635
+ ```
1636
+
1637
+ **Use Case**: Historical backfill or testing without affecting incremental sync.
1638
+
1639
+ **Test**:
1640
+
1641
+ ```bash
1642
+ curl -X POST https://workspace.versori.run/products-extract-webhook \
1643
+ -H "Content-Type: application/json" \
1644
+ -d '{
1645
+ "fromDate": "2025-01-01T00:00:00Z",
1646
+ "toDate": "2025-01-31T23:59:59Z",
1647
+ "updateState": false
1648
+ }'
1649
+ ```
1650
+
1651
+ **Why this matters**:
1652
+
1653
+ - **Incremental sync** relies on state continuity
1654
+ - **Manual overrides** allow catch-up without breaking incremental flow
1655
+ - **Overlap buffer** prevents missed records at time boundaries
1656
+ - **State isolation** lets you test/backfill without affecting production sync
1657
+
1658
+ ---
1659
+
1660
+ ### Pattern 8: Optional GraphQL Query Logging
1661
+
1662
+ **Use Case**: Debug extraction issues by logging the exact GraphQL query sent to Fluent Commerce API.
1663
+
1664
+ **When to use**:
1665
+
1666
+ - ✅ Debugging pagination issues
1667
+ - ✅ Verifying query variables (dates, filters, limits)
1668
+ - ✅ Development and testing
1669
+ - Production (verbose logs, potential secrets in variables)
1670
+
1671
+ **How to enable**:
1672
+
1673
+ Set `DEBUG_GRAPHQL=true` environment variable in Versori activation settings.
1674
+
1675
+ **Implementation**:
1676
+
1677
+ ```typescript
1678
+ // In your extraction workflow
1679
+ const DEBUG_GRAPHQL = activation?.getVariable('DEBUG_GRAPHQL') === 'true';
1680
+
1681
+ if (DEBUG_GRAPHQL) {
1682
+ log.info('GraphQL Query Debug', {
1683
+ query: PRODUCTS_QUERY,
1684
+ variables: {
1685
+ catalogues,
1686
+ dateRangeFilter: bufferedLastRunTime,
1687
+ first: pageSize,
1688
+ after: null, // First page
1689
+ },
1690
+ pagination: {
1691
+ pageSize,
1692
+ maxRecords,
1693
+ currentPage: 1,
1694
+ },
1695
+ });
1696
+ }
1697
+
1698
+ const extractionResult = await orchestrator.extract({
1699
+ query: PRODUCTS_QUERY,
1700
+ resultPath: 'products.edges.node',
1701
+ variables: {
1702
+ catalogues,
1703
+ dateRangeFilter: bufferedLastRunTime,
1704
+ },
1705
+ pageSize,
1706
+ maxRecords,
1707
+ });
1708
+
1709
+ if (DEBUG_GRAPHQL) {
1710
+ log.info('GraphQL Response Debug', {
1711
+ totalRecords: extractionResult.stats.totalRecords,
1712
+ totalPages: extractionResult.stats.totalPages,
1713
+ validRecords: extractionResult.stats.validRecords ?? extractionResult.data.length,
1714
+ firstRecordId: extractionResult.data[0]?.id,
1715
+ lastRecordId: extractionResult.data[extractionResult.data.length - 1]?.id,
1716
+ });
1717
+ }
1718
+ ```
1719
+
1720
+ **What gets logged**:
1721
+
1722
+ ```json
1723
+ {
1724
+ "level": "info",
1725
+ "message": "GraphQL Query Debug",
1726
+ "query": "query GetProducts($catalogues: [ProductCatalogueKey], $dateRangeFilter: DateRange, ...)",
1727
+ "variables": {
1728
+ "catalogues": [{ "ref": "DEFAULT_CATALOGUE" }],
1729
+ "dateRangeFilter": "2025-01-22T09:59:00Z",
1730
+ "first": 200,
1731
+ "after": null
1732
+ },
1733
+ "pagination": {
1734
+ "pageSize": 200,
1735
+ "maxRecords": 50000,
1736
+ "currentPage": 1
1737
+ }
1738
+ }
1739
+ ```
1740
+
1741
+ **Versori Environment Variables**:
1742
+
1743
+ Add to activation settings:
1744
+
1745
+ ```json
1746
+ {
1747
+ "DEBUG_GRAPHQL": "true"
1748
+ }
1749
+ ```
1750
+
1751
+ **Testing**:
1752
+
1753
+ ```bash
1754
+ # Enable debug logging
1755
+ curl -X POST https://workspace.versori.run/products-extract-daily
1756
+
1757
+ # Check Versori logs for "GraphQL Query Debug" entries
1758
+ # Verify query structure and variables are correct
1759
+ ```
1760
+
1761
+ **Sample Debug Output**:
1762
+
1763
+ ```
1764
+ [INFO] GraphQL Query Debug
1765
+ query: "query GetProducts($catalogues: [ProductCatalogueKey], $dateRangeFilter: DateRange, ...)"
1766
+ variables: { catalogues: [{ ref: "DEFAULT_CATALOGUE" }], dateRangeFilter: "2025-01-22T09:59:00Z", first: 200, after: null }
1767
+ pagination: { pageSize: 200, maxRecords: 50000, currentPage: 1 }
1768
+
1769
+ [INFO] Extraction complete
1770
+ totalRecords: 1250
1771
+ totalPages: 7
1772
+ validRecords: 1250
1773
+ failedValidations: 0
1774
+
1775
+ [INFO] GraphQL Response Debug
1776
+ totalRecords: 1250
1777
+ totalPages: 7
1778
+ validRecords: 1250
1779
+ firstRecordId: "product_abc"
1780
+ lastRecordId: "product_xyz"
1781
+ ```
1782
+
1783
+ **Key Benefits**:
1784
+
1785
+ - Quickly identify pagination configuration issues
1786
+ - Verify date filters are applied correctly
1787
+ - Debug "no records found" scenarios
1788
+ - Validate ExtractionOrchestrator variable injection
1789
+
1790
+ **Production Best Practice**: Disable `DEBUG_GRAPHQL` in production to reduce log volume and avoid logging sensitive data.
1791
+
1792
+ ---
1793
+
1794
+ ## Common Issues
1795
+
1796
+ **Issue 1: Malformed XML from unescaped characters**
1797
+
1798
+ - Customer name contains `&` or `<`
1799
+ - Solution: Always use XMLBuilder (automatic escaping)
1800
+
1801
+ **Issue 2: Partner system rejects XML**
1802
+
1803
+ - Missing required fields
1804
+ - Solution: Verify mapping matches partner schema requirements
1805
+
1806
+ **Issue 3: File too large for SFTP partner**
1807
+
1808
+ - Partner has 50MB limit, file is 100MB
1809
+ - Solution: Use file splitting (10k products per file)
1810
+
1811
+ **Issue 4: SFTP connection timeouts**
1812
+
1813
+ - Not calling `dispose()` in finally block
1814
+ - Solution: Always use try/finally pattern
1815
+
1816
+ **Issue 5: Job status not updating**
1817
+
1818
+ - JobTracker not integrated
1819
+ - Solution: Use JobTracker throughout workflow
1820
+
1821
+ ## Testing
1822
+
1823
+ ### 1. Test XML Structure
1824
+
1825
+ ```typescript
1826
+ export const testXmlGeneration = http('test-xml').then(
1827
+ fn('test-xml-gen', async () => {
1828
+ const testProducts = [
1829
+ {
1830
+ sku: 'TEST-001',
1831
+ title: 'Test & Validate <Product>',
1832
+ status: 'ACTIVE',
1833
+ price: 29.99,
1834
+ created_on: '2025-01-22T10:00:00Z',
1835
+ updated_on: '2025-01-22T10:00:00Z',
1836
+ },
1837
+ ];
1838
+
1839
+ const xml = buildProductsXML(testProducts);
1840
+
1841
+ // Validate XML structure
1842
+ if (!xml.includes('<?xml version="1.0"')) {
1843
+ return { success: false, error: 'Missing XML declaration' };
1844
+ }
1845
+
1846
+ if (!xml.includes('&amp;') || !xml.includes('&lt;')) {
1847
+ return { success: false, error: 'Special characters not escaped' };
1848
+ }
1849
+
1850
+ return { success: true, xml };
1851
+ })
1852
+ );
1853
+ ```
1854
+
1855
+ ### 2. Test SFTP Upload
1856
+
1857
+ ```bash
1858
+ curl https://your-workspace.versori.run/test-sftp-products-xml
1859
+ ```
1860
+
1861
+ ### 3. Validate Against Partner Schema
1862
+
1863
+ - Download partner's XSD schema
1864
+ - Validate generated XML against schema
1865
+ - Fix any missing/incorrect elements
1866
+
1867
+ ## Production Checklist
1868
+
1869
+ - [ ] Test SFTP credentials and connection
1870
+ - [ ] Verify SFTP server has write permissions to remotePath
1871
+ - [ ] Set appropriate extraction frequency (daily for product feeds)
1872
+ - [ ] Configure correct product status filters
1873
+ - [ ] Test XML escaping with special characters (&, <, >, ", ')
1874
+ - [ ] Validate XML against partner's schema (if provided)
1875
+ - [ ] Test `dispose()` is always called (check logs)
1876
+ - [ ] Document XML schema for partner integration team
1877
+ - [ ] Set up monitoring for SFTP connection failures
1878
+ - [ ] Test with real product data (names with special chars)
1879
+ - [ ] Verify file size limits with SFTP partner
1880
+ - [ ] Configure SFTP server IP whitelisting for Versori
1881
+ - [ ] Test file splitting with large batches (>10k products)
1882
+ - [ ] Test all 3 workflows (scheduled, ad-hoc, status)
1883
+ - [ ] Verify JobTracker integration and status updates
1884
+ - [ ] Test ExtractionOrchestrator pagination with large datasets
1885
+
1886
+ ## Troubleshooting Guide
1887
+
1888
+ **Issue**: "Extraction timeout after 10 minutes"
1889
+
1890
+ - **Cause**: Too many records
1891
+ - **Fix**: Reduce maxRecords, increase frequency
1892
+
1893
+ **Issue**: "Mapping errors for 50% of records"
1894
+
1895
+ - **Cause**: Schema mismatch
1896
+ - **Fix**: Run schema validation, check field names
1897
+
1898
+ **Issue**: "State not updating"
1899
+
1900
+ - **Cause**: KV write failure or intentional retry
1901
+ - **Fix**: Check KV logs, verify state update code
1902
+
1903
+ **Issue**: "First run exceeds limits"
1904
+
1905
+ - **Cause**: No previous timestamp, fetches all
1906
+ - **Fix**: Set fallbackStartDate close to current, apply filters
1907
+
1908
+ **Issue**: "Excessive duplicates"
1909
+
1910
+ - **Cause**: Overlap buffer (expected) or timestamp not saved
1911
+ - **Fix**: Verify newTimestamp saved WITHOUT buffer
1912
+
1913
+ **Issue**: "Job status returns null"
1914
+
1915
+ - **Cause**: Invalid job ID or job expired
1916
+ - **Fix**: Verify job ID format, check KV TTL settings
1917
+
1918
+ ## Security Best Practices
1919
+
1920
+ ### Credential Management
1921
+
1922
+ **✅ DO**:
1923
+
1924
+ - Store credentials in Versori activation variables
1925
+ - Rotate credentials quarterly
1926
+ - Use least-privilege accounts
1927
+
1928
+ ** DON'T**:
1929
+
1930
+ - Never log credentials
1931
+ - Never commit to git
1932
+ - Never share across environments
1933
+
1934
+ ### Data Security
1935
+
1936
+ - Enable encryption in transit and at rest
1937
+ - Apply data retention policies
1938
+ - Monitor access logs
1939
+ - Use VPC/private networks for sensitive data
1940
+
1941
+ ### Webhook Security
1942
+
1943
+ - Validate API keys for ad-hoc and status workflows
1944
+ - Use HTTPS for all webhook endpoints
1945
+ - Implement rate limiting
1946
+ - Monitor for suspicious activity
1947
+
1948
+ ---
1949
+
1950
+ **Pattern**: Enterprise incremental extraction with ExtractionOrchestrator + JobTracker for products via SFTP (XML format)
1951
+ **❌š ï¸ Versori Sample**: Reference implementation - adapt for your production use case
1952
+ **Key Learning**: Use ExtractionOrchestrator for auto-pagination, JobTracker for lifecycle management, always escape XML and dispose SFTP
1953
+ **Critical**: Apply 60-second overlap buffer to prevent missed records
1954
+ **Buffer Pattern**: Query WITH buffer (`updatedOn >= lastRunTime - 60s`), save WITHOUT buffer (`MAX(updatedOn)`)
1955
+ **Field Consistency**: Same field names as CSV version for easy format switching
1956
+ **SFTP**: Use proper connection cleanup in finally block to prevent connection leaks
1957
+ **XML**: Preserve hierarchical structure (no flattening needed like CSV)
1958
+ **3 Workflows**: Scheduled, ad-hoc webhook, job status query
1959
+
1960
+ ---
1961
+
1962
+ ### Pattern 8: Backward Pagination (Optional - Advanced)
1963
+
1964
+ **Use Case**: Extract data in reverse chronological order (newest to oldest) instead of oldest to newest.
1965
+
1966
+ **When to Use**:
1967
+
1968
+ - ✅ Need most recent records first (e.g., latest orders, recent inventory updates)
1969
+ - ✅ Time-bounded reverse traversal for auditing
1970
+ - ✅ Display newest-first in UI/reports
1971
+ - **Don't use for standard incremental sync** - use forward pagination (default)
1972
+
1973
+ **GraphQL Query Requirements**:
1974
+
1975
+ Your query must support backward pagination by including `$last` and `$before`:
1976
+
1977
+ ```graphql
1978
+ query GetData(
1979
+ $retailerId: ID!
1980
+ $first: Int # For forward pagination
1981
+ $after: String # For forward pagination
1982
+ $last: Int # For backward pagination
1983
+ $before: String # For backward pagination
1984
+ ) {
1985
+ data(retailerId: $retailerId, first: $first, after: $after, last: $last, before: $before) {
1986
+ edges {
1987
+ cursor # ✅ REQUIRED
1988
+ node {
1989
+ id
1990
+ createdAt
1991
+ # ... other fields
1992
+ }
1993
+ }
1994
+ pageInfo {
1995
+ hasNextPage # For forward
1996
+ hasPreviousPage # ✅ REQUIRED for backward
1997
+ }
1998
+ }
1999
+ }
2000
+ ```
2001
+
2002
+ **Implementation**:
2003
+
2004
+ ```typescript
2005
+ // Backward pagination - newest records first
2006
+ const result = await orchestrator.extract({
2007
+ query: YOUR_QUERY,
2008
+ resultPath: 'data.edges.node',
2009
+ variables: {
2010
+ retailerId,
2011
+ dateRangeFilter: { from: bufferedLastRunTime, to: effectiveEndTime },
2012
+ // Don't include last/before - orchestrator injects them
2013
+ },
2014
+ pageSize: 200,
2015
+ direction: 'backward', // ✅ Enable reverse pagination
2016
+ maxRecords: 10000,
2017
+ });
2018
+
2019
+ // Records are returned in reverse chronological order
2020
+ console.log(result.data[0].createdAt); // Newest
2021
+ console.log(result.data[result.data.length - 1].createdAt); // Oldest (within range)
2022
+ ```
2023
+
2024
+ **Key Differences from Forward Pagination**:
2025
+
2026
+ | Aspect | Forward (Default) | Backward |
2027
+ | ---------------------- | -------------------------------- | ----------------------- |
2028
+ | **Direction** | `direction: 'forward'` (default) | `direction: 'backward'` |
2029
+ | **Variables Injected** | `first`, `after` | `last`, `before` |
2030
+ | **PageInfo Field** | `hasNextPage` | `hasPreviousPage` |
2031
+ | **Cursor Source** | Last edge of page | First edge of page |
2032
+ | **Record Order** | Oldest → Newest | Newest → Oldest |
2033
+
2034
+ **Important Notes**:
2035
+
2036
+ 1. **Orchestrator injects variables**: Don't pass `last` or `before` in your variables object - the orchestrator injects them based on `pageSize` and cursor tracking.
2037
+
2038
+ 2. **Query signature**: Your GraphQL query must declare `$last` and `$before` parameters even if you don't pass them explicitly.
2039
+
2040
+ 3. **PageInfo requirement**: Response must include `pageInfo.hasPreviousPage` or the orchestrator will throw an error.
2041
+
2042
+ 4. **Cursor requirement**: Each edge must include `cursor` field for pagination to work.
2043
+
2044
+ **Example: Extract Latest 1000 Orders**
2045
+
2046
+ ```typescript
2047
+ const latestOrders = await orchestrator.extract({
2048
+ query: ORDERS_QUERY,
2049
+ resultPath: 'orders.edges.node',
2050
+ variables: {
2051
+ retailerId,
2052
+ statuses: ['BOOKED', 'ALLOCATED'],
2053
+ },
2054
+ direction: 'backward', // Start from newest
2055
+ maxRecords: 1000, // Stop after 1000 records
2056
+ pageSize: 100, // 100 per page = 10 pages
2057
+ });
2058
+
2059
+ // latestOrders.data[0] is the newest order
2060
+ // latestOrders.data[999] is the 1000th newest order
2061
+ ```
2062
+
2063
+ **When to Use Forward vs Backward**:
2064
+
2065
+ ```typescript
2066
+ // ✅ Forward (default) - For incremental sync
2067
+ const incrementalData = await orchestrator.extract({
2068
+ query: YOUR_QUERY,
2069
+ resultPath: 'data.edges.node',
2070
+ variables: {
2071
+ dateRangeFilter: { from: lastSyncTime, to: now },
2072
+ },
2073
+ // direction defaults to 'forward'
2074
+ // Processes oldest → newest for proper sequencing
2075
+ });
2076
+
2077
+ // ✅ Backward - For "latest N records" use cases
2078
+ const latestData = await orchestrator.extract({
2079
+ query: YOUR_QUERY,
2080
+ resultPath: 'data.edges.node',
2081
+ direction: 'backward',
2082
+ maxRecords: 100, // Just get latest 100
2083
+ // Gets newest → oldest
2084
+ });
2085
+ ```
2086
+
2087
+ **Pagination Variables Reference**:
2088
+
2089
+ | Variable | Forward | Backward | Injected By | Notes |
2090
+ | -------- | ------------ | ------------ | ------------ | ------------------------ |
2091
+ | `first` | ✅ Used | Not used | Orchestrator | From `pageSize` |
2092
+ | `after` | ✅ Used | Not used | Orchestrator | From cursor (last edge) |
2093
+ | `last` | Not used | ✅ Used | Orchestrator | From `pageSize` |
2094
+ | `before` | Not used | ✅ Used | Orchestrator | From cursor (first edge) |
2095
+
2096
+ **Common Mistakes to Avoid**:
2097
+
2098
+ ```typescript
2099
+ // WRONG - Don't pass pagination variables
2100
+ const result = await orchestrator.extract({
2101
+ variables: {
2102
+ last: 200, // Orchestrator will override this
2103
+ before: cursor, // Orchestrator manages cursor
2104
+ },
2105
+ direction: 'backward',
2106
+ });
2107
+
2108
+ // ✅ CORRECT - Let orchestrator inject pagination
2109
+ const result = await orchestrator.extract({
2110
+ variables: {
2111
+ retailerId, // ✅ Your business variables only
2112
+ },
2113
+ pageSize: 200, // ✅ Orchestrator uses this for last/before
2114
+ direction: 'backward',
2115
+ });
2116
+ ```
2117
+
2118
+ #### Optional: Reverse Pagination
2119
+
2120
+ - For reverse ordering, add $last/$before and pageInfo.hasPreviousPage to your query and set direction='backward'.
2121
+
2122
+ GraphQL:
2123
+
2124
+ ```graphql
2125
+ query GetProductsBackward($last: Int!, $before: String) {
2126
+ products(last: $last, before: $before) {
2127
+ edges {
2128
+ cursor
2129
+ node {
2130
+ id
2131
+ ref
2132
+ updatedOn
2133
+ }
2134
+ }
2135
+ pageInfo {
2136
+ hasPreviousPage
2137
+ }
2138
+ }
2139
+ }
2140
+ ```
2141
+
2142
+ SDK:
2143
+
2144
+ ```typescript
2145
+ await orchestrator.extract({
2146
+ query: PRODUCTS_BACKWARD_QUERY,
2147
+ resultPath: 'products.edges.node',
2148
+ variables: {},
2149
+ pageSize,
2150
+ direction: 'backward',
2151
+ });
2152
+ ```
2153
+
2154
+ ---
2155
+
2156
+ ## Testing Checklist
2157
+
2158
+ **Before production deployment:**
2159
+
2160
+ ### 1. Schema Validation
2161
+
2162
+ - [ ] Run `npx fc-connect introspect-schema --url <your-graphql-url>`
2163
+ - [ ] Run `npx fc-connect validate-schema --mapping ./config/products.export.xml.json --schema ./fluent-schema.json`
2164
+ - [ ] Run `npx fc-connect analyze-coverage --mapping ./config/products.export.xml.json --schema ./fluent-schema.json`
2165
+ - [ ] Verify all `source` paths in mapping exist in GraphQL schema
2166
+ - [ ] Verify query structure matches schema (fields, types, filters)
2167
+
2168
+ ### 2. Extraction Testing
2169
+
2170
+ - [ ] Test with small dataset first (maxRecords=10)
2171
+ - [ ] Verify ExtractionOrchestrator pagination works correctly
2172
+ - [ ] Test with multiple pages of data (verify cursor handling)
2173
+ - [ ] Verify date range filtering (updatedOn filter)
2174
+ - [ ] Test empty result handling (no records in date range)
2175
+ - [ ] Verify extraction stops at maxRecords limit
2176
+
2177
+ ### 3. Mapping Testing
2178
+
2179
+ - [ ] Verify required fields are populated
2180
+ - [ ] Verify SDK resolvers work correctly (sdk.trim, sdk.parseInt, sdk.formatDate, etc.)
2181
+ - [ ] Test custom resolvers with edge cases (if any)
2182
+ - [ ] Verify nested field extraction
2183
+ - [ ] Test with null/missing fields
2184
+ - [ ] Verify mapping error collection works
2185
+
2186
+ ### 4. XML Generation Testing
2187
+
2188
+ - [ ] Verify XML structure matches expected format
2189
+ - [ ] Test XML validation against XSD schema (if applicable)
2190
+ - [ ] Verify special character escaping in XML
2191
+ - [ ] Test with large datasets (>1000 records)
2192
+ - [ ] Verify UTF-8 encoding
2193
+ - [ ] Test XML namespace handling (if applicable)
2194
+
2195
+ ### 5. SFTP Upload Testing
2196
+
2197
+ - [ ] Test SFTP connection and authentication
2198
+ - [ ] Verify file upload to correct path
2199
+ - [ ] Test file naming convention (timestamp format)
2200
+ - [ ] Verify file permissions on SFTP server
2201
+ - [ ] Test upload retry logic (simulate network failure)
2202
+ - [ ] Verify SFTP connection disposal (no connection leaks)
2203
+
2204
+ ### 6. State Management Testing
2205
+
2206
+ - [ ] Verify overlap buffer prevents missed records (60-second default)
2207
+ - [ ] Test state recovery after extraction failure
2208
+ - [ ] Verify timestamp saved WITHOUT buffer (MAX(updatedOn))
2209
+ - [ ] Test first run with no previous state (uses fallbackStartDate)
2210
+ - [ ] Verify state update only happens on successful upload
2211
+ - [ ] Test manual date override (doesn't update state)
2212
+
2213
+ ### 7. Job Tracking Testing
2214
+
2215
+ - [ ] Test job creation with JobTracker
2216
+ - [ ] Verify job status updates at each stage
2217
+ - [ ] Test job completion with metadata
2218
+ - [ ] Test job failure handling
2219
+ - [ ] Query job status via webhook endpoint
2220
+ - [ ] Verify job status persists in KV store
2221
+
2222
+ ### 8. Error Handling Testing
2223
+
2224
+ - [ ] Test with invalid GraphQL query
2225
+ - [ ] Test with mapping errors (invalid field paths)
2226
+ - [ ] Test with SFTP connection failures
2227
+ - [ ] Test with authentication failures
2228
+ - [ ] Test with network timeouts
2229
+ - [ ] Verify error logging includes context (jobId, stage, error details)
2230
+ - [ ] Test error threshold logic (if applicable)
2231
+
2232
+ ### 9. Staging Environment Testing
2233
+
2234
+ - [ ] Run full extraction in staging environment
2235
+ - [ ] Verify XML file format with downstream system
2236
+ - [ ] Monitor extraction duration and resource usage
2237
+ - [ ] Test with production-like data volumes
2238
+ - [ ] Verify no performance degradation over time
2239
+
2240
+ ### 10. Integration Testing
2241
+
2242
+ - [ ] Test scheduled workflow (cron trigger)
2243
+ - [ ] Test ad hoc webhook trigger
2244
+ - [ ] Test job status query webhook
2245
+ - [ ] Verify activation variables are read correctly
2246
+ - [ ] Test with different extraction modes (incremental, date range)
2247
+ - [ ] End-to-end test: trigger → extract → transform → upload → verify file
2248
+
2249
+ ---
2250
+ ## Monitoring & Alerting
2251
+
2252
+ ### Success Response Example
2253
+
2254
+ ```json
2255
+ {
2256
+ "success": true,
2257
+ "jobId": "SCHEDULED_PRD_20251102_140000_abc123",
2258
+ "recordsExtracted": 1523,
2259
+ "fileName": "products-2025-11-02T14-00-00-000Z.xml",
2260
+ "sftpPath": "/outbound/products/products-2025-11-02T14-00-00-000Z.xml",
2261
+ "metrics": {
2262
+ "extractionDurationMs": 12543,
2263
+ "totalPages": 8,
2264
+ "pageSize": 200,
2265
+ "mappingErrors": 0,
2266
+ "fileSizeBytes": 524288,
2267
+ "uploadDurationMs": 1234
2268
+ },
2269
+ "timestamps": {
2270
+ "extractionStart": "2025-11-02T14:00:00.000Z",
2271
+ "extractionEnd": "2025-11-02T14:00:12.543Z",
2272
+ "uploadComplete": "2025-11-02T14:00:13.777Z"
2273
+ },
2274
+ "state": {
2275
+ "previousTimestamp": "2025-11-02T13:00:00.000Z",
2276
+ "newTimestamp": "2025-11-02T13:59:58.123Z",
2277
+ "stateUpdated": true,
2278
+ "overlapBufferSeconds": 60
2279
+ }
2280
+ }
2281
+ ```
2282
+
2283
+ ### Error Response Example
2284
+
2285
+ ```json
2286
+ {
2287
+ "success": false,
2288
+ "jobId": "ADHOC_PRD_20251102_140500_xyz789",
2289
+ "error": "SFTP upload failed: Connection timeout",
2290
+ "errorCategory": "NETWORK",
2291
+ "recordsExtracted": 0,
2292
+ "stage": "sftp_upload",
2293
+ "details": {
2294
+ "message": "Failed to upload file after 3 retry attempts",
2295
+ "retryAttempts": 3,
2296
+ "lastError": "ETIMEDOUT: Connection timed out after 30000ms"
2297
+ },
2298
+ "state": {
2299
+ "stateUpdated": false,
2300
+ "willRetryNextRun": true,
2301
+ "note": "State not advanced - next extraction will retry same time window"
2302
+ }
2303
+ }
2304
+ ```
2305
+
2306
+ ### Key Metrics to Track
2307
+
2308
+ ```typescript
2309
+ const METRICS = {
2310
+ // Extraction Performance
2311
+ extractionDurationMs: Date.now() - extractionStart,
2312
+ recordCount: records.length,
2313
+ pageCount: extractionResult.stats.totalPages,
2314
+ avgRecordsPerPage: records.length / extractionResult.stats.totalPages,
2315
+
2316
+ // Transformation Performance
2317
+ transformedCount: transformedRecords.length,
2318
+ failedCount: mappingErrors.length,
2319
+ errorRate: ((mappingErrors.length / records.length) * 100).toFixed(2) + '%',
2320
+
2321
+ // File Generation
2322
+ fileSizeMB: (xmlContent.length / (1024 * 1024)).toFixed(2),
2323
+
2324
+ // Upload Performance
2325
+ uploadDurationMs: uploadEnd - uploadStart,
2326
+ uploadSpeedMBps: (fileSizeMB / (uploadDurationMs / 1000)).toFixed(2),
2327
+
2328
+ // State Management
2329
+ timeSinceLastRun: Date.now() - new Date(lastTimestamp).getTime(),
2330
+ recordsPerMinute: (records.length / (extractionDurationMs / 60000)).toFixed(0),
2331
+ };
2332
+
2333
+ log.info('Extraction metrics', metrics);
2334
+ ```
2335
+
2336
+ ### Alert Thresholds
2337
+
2338
+ ```typescript
2339
+ const ALERT_THRESHOLDS = {
2340
+ // Duration Alerts
2341
+ EXTRACTION_DURATION_MS: 5 * 60 * 1000, // 5 minutes
2342
+ UPLOAD_DURATION_MS: 2 * 60 * 1000, // 2 minutes
2343
+ TOTAL_DURATION_MS: 10 * 60 * 1000, // 10 minutes
2344
+
2345
+ // Error Rate Alerts
2346
+ MAX_ERROR_RATE: 0.05, // 5% mapping errors
2347
+ MAX_VALIDATION_FAILURES: 0.02, // 2% validation failures
2348
+
2349
+ // Volume Alerts
2350
+ MAX_RECORDS_PER_RUN: 100000,
2351
+ MIN_RECORDS_WARNING: 0, // Alert if no records found
2352
+ MAX_FILE_SIZE_MB: 150, // 150MB
2353
+
2354
+ // State Alerts
2355
+ MAX_TIME_SINCE_LAST_RUN_HOURS: 25, // Alert if >25 hours (should run hourly)
2356
+ MAX_OVERLAP_BUFFER_SECONDS: 300, // Alert if buffer >5 minutes
2357
+ };
2358
+
2359
+ // Check thresholds
2360
+ if (metrics.extractionDurationMs > ALERT_THRESHOLDS.EXTRACTION_DURATION_MS) {
2361
+ log.warn('Extraction duration exceeded threshold', {
2362
+ duration: metrics.extractionDurationMs,
2363
+ threshold: ALERT_THRESHOLDS.EXTRACTION_DURATION_MS,
2364
+ recommendation: 'Consider reducing maxRecords or increasing extraction frequency'
2365
+ });
2366
+ }
2367
+ ```
2368
+
2369
+ ### Monitoring Dashboard Queries
2370
+
2371
+ **Versori Platform Logs Query:**
2372
+
2373
+ ```
2374
+ # Successful extractions
2375
+ log_level:info AND message:"Extraction complete" AND jobId:*
2376
+
2377
+ # Failed extractions
2378
+ log_level:error AND message:"Extraction workflow failed" AND jobId:*
2379
+
2380
+ # Performance issues
2381
+ extractionDurationMs:>300000 OR uploadDurationMs:>120000
2382
+
2383
+ # High error rates
2384
+ errorRate:>5
2385
+
2386
+ # State management issues
2387
+ stateUpdated:false AND success:true
2388
+ ```
2389
+
2390
+ ### Common Issues and Solutions
2391
+
2392
+ **Issue**: "Extraction timeout after 10 minutes"
2393
+
2394
+ - **Cause**: Too many records in single extraction
2395
+ - **Fix**: Reduce maxRecords, increase extraction frequency, or optimize query filters
2396
+ - **Prevention**: Monitor recordCount trends, set appropriate maxRecords
2397
+
2398
+ **Issue**: "Mapping errors for 50% of records"
2399
+
2400
+ - **Cause**: Schema mismatch between GraphQL response and mapping config
2401
+ - **Fix**: Run schema validation, update mapping config paths
2402
+ - **Prevention**: Use `npx fc-connect validate-schema` before deployment
2403
+
2404
+ **Issue**: "SFTP connection timeout"
2405
+
2406
+ - **Cause**: Network issues, firewall, or connection pool exhaustion
2407
+ - **Fix**: Check SFTP credentials, verify network connectivity
2408
+ - **Prevention**: Implement connection health checks, monitor connection status
2409
+
2410
+ **Issue**: "State not updating after successful extraction"
2411
+
2412
+ - **Cause**: KV write failure or intentional retry logic
2413
+ - **Fix**: Check KV logs, verify state update code executed
2414
+ - **Prevention**: Add KV write verification, log state updates explicitly
2415
+
2416
+ **Issue**: "First run exceeds record limits"
2417
+
2418
+ - **Cause**: No previous timestamp, fetches all historical records
2419
+ - **Fix**: Set fallbackStartDate close to current date, apply additional filters
2420
+ - **Prevention**: Use appropriate fallbackStartDate for initial runs
2421
+
2422
+ **Issue**: "Excessive duplicate records in output"
2423
+
2424
+ - **Cause**: Overlap buffer (expected) or timestamp not saved correctly
2425
+ - **Fix**: Verify newTimestamp saved WITHOUT buffer, check state persistence
2426
+ - **Prevention**: Monitor duplicate rates, verify state update logic
2427
+
2428
+ ---
2429
+
2430
+ ## Troubleshooting Quick Reference
2431
+
2432
+ | Error Message | Likely Cause | Solution |
2433
+ |--------------|--------------|----------|
2434
+ | "Failed to create Fluent Commerce client" | Authentication failure | Check OAuth2 credentials, verify connection config |
2435
+ | "GraphQL query validation error" | Invalid query syntax | Validate query against schema with introspection tool |
2436
+ | "Pagination cursor invalid" | Stale cursor or query change | Reset extraction, verify cursor handling in query |
2437
+ | "Mapping failed: field not found" | Schema mismatch | Run schema validation, update mapping paths |
2438
+ | "SFTP authentication failed" | Invalid credentials | Verify SFTP credentials in activation variables |
2439
+ | "Connection pool exhausted" | Too many concurrent requests | Reduce concurrency, increase pool size |
2440
+ | "KV operation failed" | Versori KV issue | Check Versori platform status, retry operation |
2441
+ | "Job status not found" | Invalid jobId or expired | Verify jobId format, check KV retention policy |
2442
+ | "Memory limit exceeded" | Dataset too large | Reduce maxRecords, enable streaming mode |
2443
+ | "XML generation failed" | Format-specific error | Check XML generation logic, validate output |
2444
+
2445
+ ---