@dizzlkheinz/ynab-mcpb 0.18.3 → 0.19.0

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 (346) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/CLAUDE.md +87 -8
  3. package/bin/ynab-mcp-server.cjs +2 -2
  4. package/bin/ynab-mcp-server.js +3 -3
  5. package/biome.json +39 -0
  6. package/dist/bundle/index.cjs +67 -67
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.js +27 -27
  9. package/dist/server/YNABMCPServer.d.ts +3 -4
  10. package/dist/server/YNABMCPServer.js +111 -116
  11. package/dist/server/budgetResolver.d.ts +6 -5
  12. package/dist/server/budgetResolver.js +46 -36
  13. package/dist/server/cacheKeys.js +6 -6
  14. package/dist/server/cacheManager.js +14 -11
  15. package/dist/server/completions.d.ts +2 -2
  16. package/dist/server/completions.js +20 -15
  17. package/dist/server/config.d.ts +10 -5
  18. package/dist/server/config.js +24 -7
  19. package/dist/server/deltaCache.d.ts +2 -2
  20. package/dist/server/deltaCache.js +22 -16
  21. package/dist/server/deltaCache.merge.d.ts +2 -2
  22. package/dist/server/diagnostics.d.ts +4 -4
  23. package/dist/server/diagnostics.js +38 -32
  24. package/dist/server/errorHandler.d.ts +5 -12
  25. package/dist/server/errorHandler.js +219 -217
  26. package/dist/server/prompts.d.ts +2 -2
  27. package/dist/server/prompts.js +45 -45
  28. package/dist/server/rateLimiter.js +4 -4
  29. package/dist/server/requestLogger.d.ts +1 -1
  30. package/dist/server/requestLogger.js +40 -35
  31. package/dist/server/resources.d.ts +3 -3
  32. package/dist/server/resources.js +55 -52
  33. package/dist/server/responseFormatter.js +6 -6
  34. package/dist/server/securityMiddleware.d.ts +2 -2
  35. package/dist/server/securityMiddleware.js +22 -20
  36. package/dist/server/serverKnowledgeStore.js +1 -1
  37. package/dist/server/toolRegistry.d.ts +3 -3
  38. package/dist/server/toolRegistry.js +47 -40
  39. package/dist/tools/__tests__/deltaTestUtils.d.ts +3 -3
  40. package/dist/tools/__tests__/deltaTestUtils.js +2 -2
  41. package/dist/tools/accountTools.d.ts +9 -8
  42. package/dist/tools/accountTools.js +47 -47
  43. package/dist/tools/adapters.d.ts +13 -8
  44. package/dist/tools/adapters.js +21 -11
  45. package/dist/tools/budgetTools.d.ts +8 -7
  46. package/dist/tools/budgetTools.js +22 -22
  47. package/dist/tools/categoryTools.d.ts +9 -8
  48. package/dist/tools/categoryTools.js +68 -59
  49. package/dist/tools/compareTransactions/formatter.d.ts +3 -3
  50. package/dist/tools/compareTransactions/formatter.js +9 -9
  51. package/dist/tools/compareTransactions/index.d.ts +6 -6
  52. package/dist/tools/compareTransactions/index.js +58 -43
  53. package/dist/tools/compareTransactions/matcher.d.ts +1 -1
  54. package/dist/tools/compareTransactions/matcher.js +28 -15
  55. package/dist/tools/compareTransactions/parser.d.ts +2 -2
  56. package/dist/tools/compareTransactions/parser.js +144 -138
  57. package/dist/tools/compareTransactions/types.d.ts +4 -4
  58. package/dist/tools/compareTransactions.d.ts +1 -1
  59. package/dist/tools/compareTransactions.js +1 -1
  60. package/dist/tools/deltaFetcher.d.ts +2 -2
  61. package/dist/tools/deltaFetcher.js +16 -15
  62. package/dist/tools/deltaSupport.d.ts +4 -4
  63. package/dist/tools/deltaSupport.js +35 -41
  64. package/dist/tools/exportTransactions.d.ts +5 -4
  65. package/dist/tools/exportTransactions.js +61 -59
  66. package/dist/tools/monthTools.d.ts +7 -6
  67. package/dist/tools/monthTools.js +31 -29
  68. package/dist/tools/payeeTools.d.ts +7 -6
  69. package/dist/tools/payeeTools.js +28 -28
  70. package/dist/tools/reconcileAdapter.d.ts +2 -2
  71. package/dist/tools/reconcileAdapter.js +21 -11
  72. package/dist/tools/reconciliation/analyzer.d.ts +4 -4
  73. package/dist/tools/reconciliation/analyzer.js +136 -57
  74. package/dist/tools/reconciliation/csvParser.d.ts +3 -3
  75. package/dist/tools/reconciliation/csvParser.js +128 -104
  76. package/dist/tools/reconciliation/executor.d.ts +4 -4
  77. package/dist/tools/reconciliation/executor.js +148 -109
  78. package/dist/tools/reconciliation/index.d.ts +10 -10
  79. package/dist/tools/reconciliation/index.js +96 -83
  80. package/dist/tools/reconciliation/matcher.d.ts +3 -3
  81. package/dist/tools/reconciliation/matcher.js +17 -16
  82. package/dist/tools/reconciliation/payeeNormalizer.js +19 -8
  83. package/dist/tools/reconciliation/recommendationEngine.d.ts +1 -1
  84. package/dist/tools/reconciliation/recommendationEngine.js +40 -40
  85. package/dist/tools/reconciliation/reportFormatter.d.ts +2 -2
  86. package/dist/tools/reconciliation/reportFormatter.js +79 -54
  87. package/dist/tools/reconciliation/signDetector.d.ts +1 -1
  88. package/dist/tools/reconciliation/types.d.ts +19 -16
  89. package/dist/tools/reconciliation/ynabAdapter.d.ts +2 -2
  90. package/dist/tools/schemas/common.d.ts +1 -1
  91. package/dist/tools/schemas/common.js +1 -1
  92. package/dist/tools/schemas/outputs/accountOutputs.d.ts +1 -1
  93. package/dist/tools/schemas/outputs/accountOutputs.js +24 -18
  94. package/dist/tools/schemas/outputs/budgetOutputs.d.ts +1 -1
  95. package/dist/tools/schemas/outputs/budgetOutputs.js +14 -11
  96. package/dist/tools/schemas/outputs/categoryOutputs.d.ts +1 -1
  97. package/dist/tools/schemas/outputs/categoryOutputs.js +49 -29
  98. package/dist/tools/schemas/outputs/comparisonOutputs.d.ts +1 -1
  99. package/dist/tools/schemas/outputs/comparisonOutputs.js +12 -12
  100. package/dist/tools/schemas/outputs/index.d.ts +14 -14
  101. package/dist/tools/schemas/outputs/index.js +14 -14
  102. package/dist/tools/schemas/outputs/monthOutputs.d.ts +1 -1
  103. package/dist/tools/schemas/outputs/monthOutputs.js +56 -41
  104. package/dist/tools/schemas/outputs/payeeOutputs.d.ts +1 -1
  105. package/dist/tools/schemas/outputs/payeeOutputs.js +10 -10
  106. package/dist/tools/schemas/outputs/reconciliationOutputs.d.ts +2 -2
  107. package/dist/tools/schemas/outputs/reconciliationOutputs.js +45 -45
  108. package/dist/tools/schemas/outputs/transactionMutationOutputs.d.ts +1 -1
  109. package/dist/tools/schemas/outputs/transactionMutationOutputs.js +28 -22
  110. package/dist/tools/schemas/outputs/transactionOutputs.d.ts +1 -1
  111. package/dist/tools/schemas/outputs/transactionOutputs.js +43 -35
  112. package/dist/tools/schemas/outputs/utilityOutputs.d.ts +1 -1
  113. package/dist/tools/schemas/outputs/utilityOutputs.js +5 -3
  114. package/dist/tools/schemas/shared/commonOutputs.d.ts +1 -1
  115. package/dist/tools/schemas/shared/commonOutputs.js +15 -9
  116. package/dist/tools/transactionReadTools.d.ts +11 -0
  117. package/dist/tools/transactionReadTools.js +202 -0
  118. package/dist/tools/transactionSchemas.d.ts +309 -0
  119. package/dist/tools/transactionSchemas.js +235 -0
  120. package/dist/tools/transactionTools.d.ts +6 -302
  121. package/dist/tools/transactionTools.js +7 -2054
  122. package/dist/tools/transactionUtils.d.ts +31 -0
  123. package/dist/tools/transactionUtils.js +364 -0
  124. package/dist/tools/transactionWriteTools.d.ts +20 -0
  125. package/dist/tools/transactionWriteTools.js +1342 -0
  126. package/dist/tools/utilityTools.d.ts +5 -4
  127. package/dist/tools/utilityTools.js +11 -11
  128. package/dist/types/index.d.ts +7 -7
  129. package/dist/types/index.js +6 -6
  130. package/dist/types/reconciliation.d.ts +1 -1
  131. package/dist/types/toolRegistration.d.ts +14 -12
  132. package/dist/utils/amountUtils.js +1 -1
  133. package/dist/utils/dateUtils.js +4 -4
  134. package/dist/utils/errors.d.ts +3 -3
  135. package/dist/utils/errors.js +4 -4
  136. package/dist/utils/money.d.ts +2 -2
  137. package/dist/utils/money.js +8 -8
  138. package/dist/utils/validationError.d.ts +1 -1
  139. package/dist/utils/validationError.js +1 -1
  140. package/docs/assets/examples/reconciliation-with-recommendations.json +66 -66
  141. package/docs/assets/schemas/reconciliation-v2.json +360 -336
  142. package/docs/plans/2025-12-25-transaction-tools-refactor-design.md +211 -0
  143. package/docs/plans/2025-12-25-transaction-tools-refactor.md +905 -0
  144. package/esbuild.config.mjs +53 -50
  145. package/meta.json +12548 -12548
  146. package/package.json +98 -109
  147. package/scripts/analyze-bundle.mjs +33 -30
  148. package/scripts/create-pr-description.js +169 -120
  149. package/scripts/run-all-tests.js +205 -0
  150. package/scripts/run-domain-integration-tests.js +28 -18
  151. package/scripts/run-generate-mcpb.js +19 -17
  152. package/scripts/run-throttled-integration-tests.js +92 -83
  153. package/scripts/test-delta-params.mjs +149 -120
  154. package/scripts/test-recommendations.ts +36 -32
  155. package/scripts/tmpTransaction.ts +80 -43
  156. package/scripts/validate-env.js +98 -91
  157. package/scripts/verify-build.js +78 -76
  158. package/src/__tests__/comprehensive.integration.test.ts +1281 -1154
  159. package/src/__tests__/performance.test.ts +723 -671
  160. package/src/__tests__/setup.ts +442 -395
  161. package/src/__tests__/smoke.e2e.test.ts +41 -39
  162. package/src/__tests__/testRunner.ts +314 -295
  163. package/src/__tests__/testUtils.ts +456 -364
  164. package/src/__tests__/tools/reconciliation/csvParser.integration.test.ts +109 -107
  165. package/src/__tests__/tools/reconciliation/real-world.integration.test.ts +41 -41
  166. package/src/index.ts +68 -59
  167. package/src/server/CLAUDE.md +480 -0
  168. package/src/server/YNABMCPServer.ts +821 -794
  169. package/src/server/__tests__/YNABMCPServer.integration.test.ts +929 -893
  170. package/src/server/__tests__/YNABMCPServer.test.ts +903 -899
  171. package/src/server/__tests__/budgetResolver.test.ts +466 -423
  172. package/src/server/__tests__/cacheManager.test.ts +891 -874
  173. package/src/server/__tests__/completions.integration.test.ts +115 -106
  174. package/src/server/__tests__/completions.test.ts +334 -313
  175. package/src/server/__tests__/config.test.ts +98 -86
  176. package/src/server/__tests__/deltaCache.merge.test.ts +774 -703
  177. package/src/server/__tests__/deltaCache.swr.test.ts +198 -153
  178. package/src/server/__tests__/deltaCache.test.ts +946 -759
  179. package/src/server/__tests__/diagnostics.test.ts +825 -792
  180. package/src/server/__tests__/errorHandler.integration.test.ts +512 -462
  181. package/src/server/__tests__/errorHandler.test.ts +402 -397
  182. package/src/server/__tests__/prompts.test.ts +424 -347
  183. package/src/server/__tests__/rateLimiter.test.ts +313 -309
  184. package/src/server/__tests__/requestLogger.test.ts +443 -403
  185. package/src/server/__tests__/resources.template.test.ts +196 -185
  186. package/src/server/__tests__/resources.test.ts +294 -288
  187. package/src/server/__tests__/security.integration.test.ts +487 -421
  188. package/src/server/__tests__/securityMiddleware.test.ts +519 -444
  189. package/src/server/__tests__/server-startup.integration.test.ts +509 -490
  190. package/src/server/__tests__/serverKnowledgeStore.test.ts +174 -173
  191. package/src/server/__tests__/toolRegistration.test.ts +239 -210
  192. package/src/server/__tests__/toolRegistry.test.ts +907 -845
  193. package/src/server/budgetResolver.ts +221 -181
  194. package/src/server/cacheKeys.ts +6 -6
  195. package/src/server/cacheManager.ts +498 -484
  196. package/src/server/completions.ts +267 -243
  197. package/src/server/config.ts +35 -14
  198. package/src/server/deltaCache.merge.ts +146 -128
  199. package/src/server/deltaCache.ts +352 -309
  200. package/src/server/diagnostics.ts +257 -242
  201. package/src/server/errorHandler.ts +747 -744
  202. package/src/server/prompts.ts +181 -176
  203. package/src/server/rateLimiter.ts +131 -129
  204. package/src/server/requestLogger.ts +350 -322
  205. package/src/server/resources.ts +442 -374
  206. package/src/server/responseFormatter.ts +41 -37
  207. package/src/server/securityMiddleware.ts +223 -205
  208. package/src/server/serverKnowledgeStore.ts +67 -67
  209. package/src/server/toolRegistry.ts +508 -474
  210. package/src/tools/CLAUDE.md +604 -0
  211. package/src/tools/__tests__/accountTools.delta.integration.test.ts +128 -111
  212. package/src/tools/__tests__/accountTools.integration.test.ts +129 -111
  213. package/src/tools/__tests__/accountTools.test.ts +685 -638
  214. package/src/tools/__tests__/adapters.test.ts +142 -108
  215. package/src/tools/__tests__/budgetTools.delta.integration.test.ts +73 -73
  216. package/src/tools/__tests__/budgetTools.integration.test.ts +132 -124
  217. package/src/tools/__tests__/budgetTools.test.ts +442 -413
  218. package/src/tools/__tests__/categoryTools.delta.integration.test.ts +76 -68
  219. package/src/tools/__tests__/categoryTools.integration.test.ts +314 -288
  220. package/src/tools/__tests__/categoryTools.test.ts +656 -625
  221. package/src/tools/__tests__/compareTransactions/formatter.test.ts +535 -462
  222. package/src/tools/__tests__/compareTransactions/index.test.ts +378 -358
  223. package/src/tools/__tests__/compareTransactions/matcher.test.ts +497 -398
  224. package/src/tools/__tests__/compareTransactions/parser.test.ts +765 -747
  225. package/src/tools/__tests__/compareTransactions.test.ts +352 -332
  226. package/src/tools/__tests__/compareTransactions.window.test.ts +150 -146
  227. package/src/tools/__tests__/deltaFetcher.scheduled.integration.test.ts +69 -65
  228. package/src/tools/__tests__/deltaFetcher.test.ts +325 -265
  229. package/src/tools/__tests__/deltaSupport.test.ts +211 -184
  230. package/src/tools/__tests__/deltaTestUtils.ts +37 -33
  231. package/src/tools/__tests__/exportTransactions.test.ts +205 -200
  232. package/src/tools/__tests__/monthTools.delta.integration.test.ts +68 -68
  233. package/src/tools/__tests__/monthTools.integration.test.ts +178 -166
  234. package/src/tools/__tests__/monthTools.test.ts +561 -512
  235. package/src/tools/__tests__/payeeTools.delta.integration.test.ts +68 -68
  236. package/src/tools/__tests__/payeeTools.integration.test.ts +158 -142
  237. package/src/tools/__tests__/payeeTools.test.ts +486 -434
  238. package/src/tools/__tests__/transactionSchemas.test.ts +1204 -0
  239. package/src/tools/__tests__/transactionTools.integration.test.ts +875 -825
  240. package/src/tools/__tests__/transactionTools.test.ts +4923 -4366
  241. package/src/tools/__tests__/transactionUtils.test.ts +1016 -0
  242. package/src/tools/__tests__/utilityTools.integration.test.ts +32 -32
  243. package/src/tools/__tests__/utilityTools.test.ts +68 -58
  244. package/src/tools/accountTools.ts +293 -271
  245. package/src/tools/adapters.ts +120 -63
  246. package/src/tools/budgetTools.ts +121 -116
  247. package/src/tools/categoryTools.ts +379 -339
  248. package/src/tools/compareTransactions/formatter.ts +131 -119
  249. package/src/tools/compareTransactions/index.ts +249 -214
  250. package/src/tools/compareTransactions/matcher.ts +259 -209
  251. package/src/tools/compareTransactions/parser.ts +517 -487
  252. package/src/tools/compareTransactions/types.ts +38 -38
  253. package/src/tools/compareTransactions.ts +1 -1
  254. package/src/tools/deltaFetcher.ts +281 -260
  255. package/src/tools/deltaSupport.ts +264 -259
  256. package/src/tools/exportTransactions.ts +230 -218
  257. package/src/tools/monthTools.ts +180 -165
  258. package/src/tools/payeeTools.ts +152 -140
  259. package/src/tools/reconcileAdapter.ts +297 -246
  260. package/src/tools/reconciliation/CLAUDE.md +506 -0
  261. package/src/tools/reconciliation/__tests__/adapter.causes.test.ts +135 -112
  262. package/src/tools/reconciliation/__tests__/adapter.test.ts +249 -227
  263. package/src/tools/reconciliation/__tests__/analyzer.test.ts +408 -335
  264. package/src/tools/reconciliation/__tests__/csvParser.test.ts +71 -69
  265. package/src/tools/reconciliation/__tests__/executor.integration.test.ts +348 -323
  266. package/src/tools/reconciliation/__tests__/executor.progress.test.ts +503 -457
  267. package/src/tools/reconciliation/__tests__/executor.test.ts +898 -831
  268. package/src/tools/reconciliation/__tests__/matcher.test.ts +667 -663
  269. package/src/tools/reconciliation/__tests__/payeeNormalizer.test.ts +296 -276
  270. package/src/tools/reconciliation/__tests__/recommendationEngine.integration.test.ts +692 -624
  271. package/src/tools/reconciliation/__tests__/recommendationEngine.test.ts +1008 -986
  272. package/src/tools/reconciliation/__tests__/reconciliation.delta.integration.test.ts +187 -146
  273. package/src/tools/reconciliation/__tests__/reportFormatter.test.ts +583 -530
  274. package/src/tools/reconciliation/__tests__/scenarios/adapterCurrency.scenario.test.ts +75 -71
  275. package/src/tools/reconciliation/__tests__/scenarios/extremes.scenario.test.ts +70 -58
  276. package/src/tools/reconciliation/__tests__/scenarios/repeatAmount.scenario.test.ts +102 -88
  277. package/src/tools/reconciliation/__tests__/schemaUrl.test.ts +58 -43
  278. package/src/tools/reconciliation/__tests__/signDetector.test.ts +209 -206
  279. package/src/tools/reconciliation/__tests__/ynabAdapter.test.ts +66 -60
  280. package/src/tools/reconciliation/analyzer.ts +582 -406
  281. package/src/tools/reconciliation/csvParser.ts +656 -609
  282. package/src/tools/reconciliation/executor.ts +1290 -1128
  283. package/src/tools/reconciliation/index.ts +580 -528
  284. package/src/tools/reconciliation/matcher.ts +256 -240
  285. package/src/tools/reconciliation/payeeNormalizer.ts +92 -78
  286. package/src/tools/reconciliation/recommendationEngine.ts +357 -345
  287. package/src/tools/reconciliation/reportFormatter.ts +349 -276
  288. package/src/tools/reconciliation/signDetector.ts +89 -83
  289. package/src/tools/reconciliation/types.ts +164 -153
  290. package/src/tools/reconciliation/ynabAdapter.ts +17 -15
  291. package/src/tools/schemas/CLAUDE.md +546 -0
  292. package/src/tools/schemas/common.ts +1 -1
  293. package/src/tools/schemas/outputs/__tests__/accountOutputs.test.ts +410 -409
  294. package/src/tools/schemas/outputs/__tests__/budgetOutputs.test.ts +305 -299
  295. package/src/tools/schemas/outputs/__tests__/categoryOutputs.test.ts +431 -430
  296. package/src/tools/schemas/outputs/__tests__/comparisonOutputs.test.ts +510 -495
  297. package/src/tools/schemas/outputs/__tests__/dateValidation.test.ts +179 -153
  298. package/src/tools/schemas/outputs/__tests__/discrepancyDirection.test.ts +293 -254
  299. package/src/tools/schemas/outputs/__tests__/monthOutputs.test.ts +457 -457
  300. package/src/tools/schemas/outputs/__tests__/payeeOutputs.test.ts +362 -356
  301. package/src/tools/schemas/outputs/__tests__/reconciliationOutputs.test.ts +402 -399
  302. package/src/tools/schemas/outputs/__tests__/transactionMutationSchemas.test.ts +225 -211
  303. package/src/tools/schemas/outputs/__tests__/transactionOutputs.test.ts +457 -454
  304. package/src/tools/schemas/outputs/__tests__/utilityOutputs.test.ts +316 -315
  305. package/src/tools/schemas/outputs/accountOutputs.ts +40 -34
  306. package/src/tools/schemas/outputs/budgetOutputs.ts +24 -19
  307. package/src/tools/schemas/outputs/categoryOutputs.ts +76 -56
  308. package/src/tools/schemas/outputs/comparisonOutputs.ts +192 -169
  309. package/src/tools/schemas/outputs/index.ts +163 -163
  310. package/src/tools/schemas/outputs/monthOutputs.ts +95 -80
  311. package/src/tools/schemas/outputs/payeeOutputs.ts +18 -18
  312. package/src/tools/schemas/outputs/reconciliationOutputs.ts +386 -373
  313. package/src/tools/schemas/outputs/transactionMutationOutputs.ts +259 -231
  314. package/src/tools/schemas/outputs/transactionOutputs.ts +81 -71
  315. package/src/tools/schemas/outputs/utilityOutputs.ts +90 -84
  316. package/src/tools/schemas/shared/commonOutputs.ts +27 -19
  317. package/src/tools/toolCategories.ts +114 -114
  318. package/src/tools/transactionReadTools.ts +327 -0
  319. package/src/tools/transactionSchemas.ts +484 -0
  320. package/src/tools/transactionTools.ts +107 -2990
  321. package/src/tools/transactionUtils.ts +621 -0
  322. package/src/tools/transactionWriteTools.ts +2110 -0
  323. package/src/tools/utilityTools.ts +46 -41
  324. package/src/types/CLAUDE.md +477 -0
  325. package/src/types/__tests__/index.test.ts +51 -51
  326. package/src/types/index.ts +43 -39
  327. package/src/types/integration-tests.d.ts +26 -26
  328. package/src/types/reconciliation.ts +29 -29
  329. package/src/types/toolAnnotations.ts +30 -30
  330. package/src/types/toolRegistration.ts +43 -32
  331. package/src/utils/CLAUDE.md +508 -0
  332. package/src/utils/__tests__/dateUtils.test.ts +174 -168
  333. package/src/utils/__tests__/money.test.ts +193 -187
  334. package/src/utils/amountUtils.ts +5 -5
  335. package/src/utils/baseError.ts +5 -5
  336. package/src/utils/dateUtils.ts +29 -26
  337. package/src/utils/errors.ts +14 -14
  338. package/src/utils/money.ts +66 -52
  339. package/src/utils/validationError.ts +1 -1
  340. package/tsconfig.json +29 -29
  341. package/tsconfig.prod.json +16 -16
  342. package/vitest-reporters/split-json-reporter.ts +247 -204
  343. package/vitest.config.ts +99 -95
  344. package/.prettierignore +0 -10
  345. package/.prettierrc.json +0 -10
  346. package/eslint.config.js +0 -49
@@ -1,524 +1,554 @@
1
- import { parse } from 'csv-parse/sync';
2
- import { parse as parseDateFns } from 'date-fns';
3
- import { toMilli } from '../../utils/money.js';
4
- import type { Milli } from '../../utils/money.js';
5
- import { BankTransaction, CSVFormat } from './types.js';
6
- import { readFileSync } from 'fs';
1
+ import { readFileSync } from "node:fs";
2
+ import { parse } from "csv-parse/sync";
3
+ import { parse as parseDateFns } from "date-fns";
4
+ import { toMilli } from "../../utils/money.js";
5
+ import type { Milli } from "../../utils/money.js";
6
+ import type { BankTransaction, CSVFormat } from "./types.js";
7
7
 
8
8
  /**
9
9
  * Parse date string using date-fns for better reliability
10
10
  */
11
11
  export function parseDate(dateStr: string, format: string): Date {
12
- const cleanDate = dateStr.trim();
13
-
14
- // Map our format strings to date-fns format patterns
15
- const formatMap: Record<string, string> = {
16
- 'MM/DD/YYYY': 'MM/dd/yyyy',
17
- 'M/D/YYYY': 'M/d/yyyy',
18
- 'DD/MM/YYYY': 'dd/MM/yyyy',
19
- 'D/M/YYYY': 'd/M/yyyy',
20
- 'YYYY-MM-DD': 'yyyy-MM-dd',
21
- 'MM-DD-YYYY': 'MM-dd-yyyy',
22
- 'MMM dd, yyyy': 'MMM dd, yyyy',
23
- 'MMM d, yyyy': 'MMM d, yyyy',
24
- };
25
-
26
- const dateFnsFormat = formatMap[format];
27
- if (dateFnsFormat) {
28
- try {
29
- const parsed = parseDateFns(cleanDate, dateFnsFormat, new Date());
30
- if (!isNaN(parsed.getTime())) {
31
- return parsed;
32
- }
33
- } catch {
34
- // Fall through to generic parsing
35
- }
36
- }
37
-
38
- // Fallback to native Date parsing for any unrecognized formats
39
- const parsed = new Date(cleanDate);
40
- if (isNaN(parsed.getTime())) {
41
- throw new Error(`Unable to parse date: ${dateStr} with format: ${format}`);
42
- }
43
- return parsed;
12
+ const cleanDate = dateStr.trim();
13
+
14
+ // Map our format strings to date-fns format patterns
15
+ const formatMap: Record<string, string> = {
16
+ "MM/DD/YYYY": "MM/dd/yyyy",
17
+ "M/D/YYYY": "M/d/yyyy",
18
+ "DD/MM/YYYY": "dd/MM/yyyy",
19
+ "D/M/YYYY": "d/M/yyyy",
20
+ "YYYY-MM-DD": "yyyy-MM-dd",
21
+ "MM-DD-YYYY": "MM-dd-yyyy",
22
+ "MMM dd, yyyy": "MMM dd, yyyy",
23
+ "MMM d, yyyy": "MMM d, yyyy",
24
+ };
25
+
26
+ const dateFnsFormat = formatMap[format];
27
+ if (dateFnsFormat) {
28
+ try {
29
+ const parsed = parseDateFns(cleanDate, dateFnsFormat, new Date());
30
+ if (!Number.isNaN(parsed.getTime())) {
31
+ return parsed;
32
+ }
33
+ } catch {
34
+ // Fall through to generic parsing
35
+ }
36
+ }
37
+
38
+ // Fallback to native Date parsing for any unrecognized formats
39
+ const parsed = new Date(cleanDate);
40
+ if (Number.isNaN(parsed.getTime())) {
41
+ throw new Error(`Unable to parse date: ${dateStr} with format: ${format}`);
42
+ }
43
+ return parsed;
44
44
  }
45
45
 
46
46
  /**
47
47
  * Convert dollar amount to milliunits
48
48
  */
49
49
  export function amountToMilliunits(amountStr: string): Milli {
50
- const cleaned = amountStr.replace(/[$,\s]/g, '').trim();
51
- let s = cleaned,
52
- neg = false;
53
- if (s.startsWith('(') && s.endsWith(')')) {
54
- neg = true;
55
- s = s.slice(1, -1);
56
- }
57
- if (s.startsWith('+')) s = s.slice(1);
58
-
59
- const n = Number(s);
60
- if (isNaN(n) || !isFinite(n)) {
61
- throw new Error(`Invalid amount value: "${amountStr}" (cleaned: "${s}")`);
62
- }
63
-
64
- return toMilli(neg ? -n : n);
50
+ const cleaned = amountStr.replace(/[$,\s]/g, "").trim();
51
+ let s = cleaned;
52
+ let neg = false;
53
+ if (s.startsWith("(") && s.endsWith(")")) {
54
+ neg = true;
55
+ s = s.slice(1, -1);
56
+ }
57
+ if (s.startsWith("+")) s = s.slice(1);
58
+
59
+ const n = Number(s);
60
+ if (Number.isNaN(n) || !Number.isFinite(n)) {
61
+ throw new Error(`Invalid amount value: "${amountStr}" (cleaned: "${s}")`);
62
+ }
63
+
64
+ return toMilli(neg ? -n : n);
65
65
  }
66
66
 
67
67
  /**
68
68
  * Check if a string looks like a date
69
69
  */
70
70
  function isDateLike(str: string): boolean {
71
- if (!str) return false;
72
- // Common date patterns
73
- const datePatterns = [
74
- /^\d{1,2}\/\d{1,2}\/\d{4}$/, // MM/DD/YYYY
75
- /^\d{4}-\d{1,2}-\d{1,2}$/, // YYYY-MM-DD
76
- /^\d{1,2}-\d{1,2}-\d{4}$/, // MM-DD-YYYY
77
- /^[A-Za-z]{3}\s+\d{1,2},\s+\d{4}$/, // MMM dd, yyyy (e.g., "Sep 18, 2025")
78
- ];
79
- return datePatterns.some((pattern) => pattern.test(str.trim()));
71
+ if (!str) return false;
72
+ // Common date patterns
73
+ const datePatterns = [
74
+ /^\d{1,2}\/\d{1,2}\/\d{4}$/, // MM/DD/YYYY
75
+ /^\d{4}-\d{1,2}-\d{1,2}$/, // YYYY-MM-DD
76
+ /^\d{1,2}-\d{1,2}-\d{4}$/, // MM-DD-YYYY
77
+ /^[A-Za-z]{3}\s+\d{1,2},\s+\d{4}$/, // MMM dd, yyyy (e.g., "Sep 18, 2025")
78
+ ];
79
+ return datePatterns.some((pattern) => pattern.test(str.trim()));
80
80
  }
81
81
 
82
82
  /**
83
83
  * Detect date format from a sample date string
84
84
  */
85
85
  export function detectDateFormat(dateStr: string | undefined): string {
86
- if (!dateStr) return 'MM/DD/YYYY';
87
- const cleaned = dateStr.trim();
88
-
89
- if (cleaned.includes('/')) {
90
- return 'MM/DD/YYYY';
91
- } else if (cleaned.includes('-')) {
92
- if (/^\d{4}-/.test(cleaned)) {
93
- return 'YYYY-MM-DD';
94
- } else {
95
- return 'MM-DD-YYYY';
96
- }
97
- } else if (/^[A-Za-z]{3}\s+\d{1,2},\s+\d{4}$/.test(cleaned)) {
98
- // Detect "Sep 18, 2025" format
99
- return 'MMM dd, yyyy';
100
- }
101
- return 'MM/DD/YYYY';
86
+ if (!dateStr) return "MM/DD/YYYY";
87
+ const cleaned = dateStr.trim();
88
+
89
+ if (cleaned.includes("/")) {
90
+ return "MM/DD/YYYY";
91
+ }
92
+ if (cleaned.includes("-")) {
93
+ if (/^\d{4}-/.test(cleaned)) {
94
+ return "YYYY-MM-DD";
95
+ }
96
+ return "MM-DD-YYYY";
97
+ }
98
+ if (/^[A-Za-z]{3}\s+\d{1,2},\s+\d{4}$/.test(cleaned)) {
99
+ // Detect "Sep 18, 2025" format
100
+ return "MMM dd, yyyy";
101
+ }
102
+ return "MM/DD/YYYY";
102
103
  }
103
104
 
104
105
  /**
105
106
  * Detect the most likely delimiter by evaluating candidates across sample lines
106
107
  */
107
108
  function detectDelimiter(lines: string[]): string {
108
- const candidates = [',', ';', '\t', '|'];
109
- const sampleLines = lines.slice(0, 3).filter((line) => line.trim()); // Use first 2-3 non-empty lines
110
-
111
- if (sampleLines.length === 0) {
112
- return ','; // Default fallback
113
- }
114
-
115
- let bestDelimiter = ',';
116
- let bestScore = -1;
117
-
118
- for (const delimiter of candidates) {
119
- let score = 0;
120
- const columnCounts: number[] = [];
121
- let parseFailed = false;
122
-
123
- for (const line of sampleLines) {
124
- try {
125
- const rows = parse(line, {
126
- delimiter,
127
- quote: '"',
128
- escape: '"',
129
- skip_empty_lines: true,
130
- trim: true,
131
- relax_column_count: true,
132
- });
133
-
134
- // rows should be an array with one row (since we're parsing one line)
135
- if (rows && rows.length > 0 && rows[0]) {
136
- const columns = Array.isArray(rows[0]) ? rows[0] : Object.values(rows[0]);
137
- columnCounts.push(columns.length);
138
- } else {
139
- // If parsing failed or returned empty, fall back to simple split
140
- const columns = line.split(delimiter);
141
- columnCounts.push(columns.length);
142
- }
143
- } catch {
144
- // If csv-parse fails, fall back to simple split method
145
- parseFailed = true;
146
- const columns = line.split(delimiter);
147
- columnCounts.push(columns.length);
148
- }
149
- }
150
-
151
- // Check consistency: all lines should have the same column count
152
- if (columnCounts.length > 1) {
153
- const firstCount = columnCounts[0];
154
- if (firstCount === undefined) continue;
155
- const isConsistent = columnCounts.every((count) => count === firstCount);
156
-
157
- if (isConsistent && firstCount > 1) {
158
- // Score based on column count (more columns = better, up to a reasonable limit)
159
- score = Math.min(firstCount, 10); // Cap at 10 to avoid excessive weight
160
-
161
- // Bonus points for common delimiters
162
- if (delimiter === ',') score += 0.5;
163
- if (delimiter === ';') score += 0.3;
164
-
165
- // Bonus points if csv-parse succeeded (indicates proper CSV format)
166
- if (!parseFailed) score += 0.2;
167
- }
168
- }
169
-
170
- if (score > bestScore) {
171
- bestScore = score;
172
- bestDelimiter = delimiter;
173
- }
174
- }
175
-
176
- return bestDelimiter;
109
+ const candidates = [",", ";", "\t", "|"];
110
+ const sampleLines = lines.slice(0, 3).filter((line) => line.trim()); // Use first 2-3 non-empty lines
111
+
112
+ if (sampleLines.length === 0) {
113
+ return ","; // Default fallback
114
+ }
115
+
116
+ let bestDelimiter = ",";
117
+ let bestScore = -1;
118
+
119
+ for (const delimiter of candidates) {
120
+ let score = 0;
121
+ const columnCounts: number[] = [];
122
+ let parseFailed = false;
123
+
124
+ for (const line of sampleLines) {
125
+ try {
126
+ const rows = parse(line, {
127
+ delimiter,
128
+ quote: '"',
129
+ escape: '"',
130
+ skip_empty_lines: true,
131
+ trim: true,
132
+ relax_column_count: true,
133
+ });
134
+
135
+ // rows should be an array with one row (since we're parsing one line)
136
+ if (rows && rows.length > 0 && rows[0]) {
137
+ const columns = Array.isArray(rows[0])
138
+ ? rows[0]
139
+ : Object.values(rows[0]);
140
+ columnCounts.push(columns.length);
141
+ } else {
142
+ // If parsing failed or returned empty, fall back to simple split
143
+ const columns = line.split(delimiter);
144
+ columnCounts.push(columns.length);
145
+ }
146
+ } catch {
147
+ // If csv-parse fails, fall back to simple split method
148
+ parseFailed = true;
149
+ const columns = line.split(delimiter);
150
+ columnCounts.push(columns.length);
151
+ }
152
+ }
153
+
154
+ // Check consistency: all lines should have the same column count
155
+ if (columnCounts.length > 1) {
156
+ const firstCount = columnCounts[0];
157
+ if (firstCount === undefined) continue;
158
+ const isConsistent = columnCounts.every((count) => count === firstCount);
159
+
160
+ if (isConsistent && firstCount > 1) {
161
+ // Score based on column count (more columns = better, up to a reasonable limit)
162
+ score = Math.min(firstCount, 10); // Cap at 10 to avoid excessive weight
163
+
164
+ // Bonus points for common delimiters
165
+ if (delimiter === ",") score += 0.5;
166
+ if (delimiter === ";") score += 0.3;
167
+
168
+ // Bonus points if csv-parse succeeded (indicates proper CSV format)
169
+ if (!parseFailed) score += 0.2;
170
+ }
171
+ }
172
+
173
+ if (score > bestScore) {
174
+ bestScore = score;
175
+ bestDelimiter = delimiter;
176
+ }
177
+ }
178
+
179
+ return bestDelimiter;
177
180
  }
178
181
 
179
182
  /**
180
183
  * Analyze header names to detect column purposes
181
184
  */
182
185
  function analyzeHeaders(headers: string[]): {
183
- dateColumn: string | null;
184
- amountColumn: string | null;
185
- descriptionColumn: string | null;
186
- debitColumn: string | null;
187
- creditColumn: string | null;
186
+ dateColumn: string | null;
187
+ amountColumn: string | null;
188
+ descriptionColumn: string | null;
189
+ debitColumn: string | null;
190
+ creditColumn: string | null;
188
191
  } {
189
- const datePattern = /^(date|trans.*date|transaction.*date|post.*date|dt)$/i;
190
- const amountPattern = /^(amount|amt|dollar.*amount|transaction.*amount)$/i;
191
- const descriptionPattern = /^(description|desc|memo|transaction.*description|payee|merchant)$/i;
192
- const debitPattern = /^(debit|debits|withdrawal|withdrawals|out|outgoing)$/i;
193
- const creditPattern = /^(credit|credits|deposit|deposits|in|incoming)$/i;
194
-
195
- let dateColumn: string | null = null;
196
- let amountColumn: string | null = null;
197
- let descriptionColumn: string | null = null;
198
- let debitColumn: string | null = null;
199
- let creditColumn: string | null = null;
200
-
201
- for (const header of headers) {
202
- const cleanHeader = header.trim();
203
-
204
- if (datePattern.test(cleanHeader)) {
205
- dateColumn = cleanHeader;
206
- } else if (amountPattern.test(cleanHeader)) {
207
- amountColumn = cleanHeader;
208
- } else if (descriptionPattern.test(cleanHeader)) {
209
- descriptionColumn = cleanHeader;
210
- } else if (debitPattern.test(cleanHeader)) {
211
- debitColumn = cleanHeader;
212
- } else if (creditPattern.test(cleanHeader)) {
213
- creditColumn = cleanHeader;
214
- }
215
- }
216
-
217
- return { dateColumn, amountColumn, descriptionColumn, debitColumn, creditColumn };
192
+ const datePattern = /^(date|trans.*date|transaction.*date|post.*date|dt)$/i;
193
+ const amountPattern = /^(amount|amt|dollar.*amount|transaction.*amount)$/i;
194
+ const descriptionPattern =
195
+ /^(description|desc|memo|transaction.*description|payee|merchant)$/i;
196
+ const debitPattern = /^(debit|debits|withdrawal|withdrawals|out|outgoing)$/i;
197
+ const creditPattern = /^(credit|credits|deposit|deposits|in|incoming)$/i;
198
+
199
+ let dateColumn: string | null = null;
200
+ let amountColumn: string | null = null;
201
+ let descriptionColumn: string | null = null;
202
+ let debitColumn: string | null = null;
203
+ let creditColumn: string | null = null;
204
+
205
+ for (const header of headers) {
206
+ const cleanHeader = header.trim();
207
+
208
+ if (datePattern.test(cleanHeader)) {
209
+ dateColumn = cleanHeader;
210
+ } else if (amountPattern.test(cleanHeader)) {
211
+ amountColumn = cleanHeader;
212
+ } else if (descriptionPattern.test(cleanHeader)) {
213
+ descriptionColumn = cleanHeader;
214
+ } else if (debitPattern.test(cleanHeader)) {
215
+ debitColumn = cleanHeader;
216
+ } else if (creditPattern.test(cleanHeader)) {
217
+ creditColumn = cleanHeader;
218
+ }
219
+ }
220
+
221
+ return {
222
+ dateColumn,
223
+ amountColumn,
224
+ descriptionColumn,
225
+ debitColumn,
226
+ creditColumn,
227
+ };
218
228
  }
219
229
 
220
230
  /**
221
231
  * Auto-detect CSV format by analyzing the first few rows
222
232
  */
223
233
  export function autoDetectCSVFormat(csvContent: string): CSVFormat {
224
- const linesRaw = csvContent.trim().split('\n').slice(0, 3);
225
- if (linesRaw.length === 0) {
226
- throw new Error('CSV file is empty');
227
- }
228
-
229
- // Safely handle the first line - check if it exists and is not empty after trimming
230
- const firstLineRaw = linesRaw[0];
231
- if (!firstLineRaw || !firstLineRaw.trim()) {
232
- throw new Error('CSV file contains empty first line');
233
- }
234
-
235
- // Detect delimiter across sample lines
236
- const delimiter = detectDelimiter(linesRaw);
237
-
238
- const firstLine = firstLineRaw.split(delimiter);
239
- const hasHeader = !isDateLike(firstLine[0] || '');
240
-
241
- // Check for separate debit/credit columns by looking for empty cells pattern
242
- let hasDebitCredit = false;
243
- if (linesRaw.length > 1) {
244
- const dataLines = hasHeader ? linesRaw.slice(1) : linesRaw;
245
- hasDebitCredit = dataLines.some((line) => {
246
- const cols = line.split(delimiter);
247
- // Look for pattern: amount in col2 OR col3, but not both
248
- return (
249
- cols.length >= 4 &&
250
- ((cols[2]?.trim() && !cols[3]?.trim()) || (!cols[2]?.trim() && cols[3]?.trim()))
251
- );
252
- });
253
- }
254
-
255
- if (hasHeader) {
256
- const { dateColumn, amountColumn, descriptionColumn, debitColumn, creditColumn } =
257
- analyzeHeaders(firstLine);
258
-
259
- const safe = (v?: string) => (v && v.trim() ? v : undefined);
260
-
261
- if (hasDebitCredit && debitColumn && creditColumn) {
262
- const dateCol = safe(dateColumn ?? undefined) ?? safe(firstLine[0]);
263
- if (!dateCol) throw new Error('Unable to detect date column name from header');
264
- const descCol = safe(descriptionColumn ?? undefined) ?? safe(firstLine[1]);
265
- if (!descCol) throw new Error('Unable to detect description column name from header');
266
-
267
- return {
268
- date_column: dateCol,
269
- description_column: descCol,
270
- debit_column: debitColumn,
271
- credit_column: creditColumn,
272
- date_format: detectDateFormat(linesRaw[1]?.split(delimiter)[0]),
273
- has_header: hasHeader,
274
- delimiter: delimiter,
275
- };
276
- } else {
277
- const dateCol = safe(dateColumn ?? undefined) ?? safe(firstLine[0]);
278
- if (!dateCol) throw new Error('Unable to detect date column name from header');
279
- const amountCol = safe(amountColumn ?? undefined) ?? safe(firstLine[1]);
280
- if (!amountCol) throw new Error('Unable to detect amount column name from header');
281
- const descCol =
282
- safe(descriptionColumn ?? undefined) ??
283
- safe(firstLine.length >= 3 ? firstLine[2] : firstLine[1]);
284
- if (!descCol) throw new Error('Unable to detect description column name from header');
285
-
286
- return {
287
- date_column: dateCol,
288
- amount_column: amountCol,
289
- description_column: descCol,
290
- date_format: detectDateFormat(linesRaw[1]?.split(delimiter)[0]),
291
- has_header: hasHeader,
292
- delimiter: delimiter,
293
- };
294
- }
295
- } else {
296
- if (hasDebitCredit && firstLine.length >= 4) {
297
- return {
298
- date_column: 0,
299
- description_column: 1,
300
- debit_column: 2,
301
- credit_column: 3,
302
- date_format: detectDateFormat(firstLine[0]),
303
- has_header: hasHeader,
304
- delimiter: delimiter,
305
- };
306
- } else {
307
- return {
308
- date_column: 0,
309
- amount_column: 1,
310
- description_column: firstLine.length >= 3 ? 2 : 1,
311
- date_format: detectDateFormat(firstLine[0]),
312
- has_header: hasHeader,
313
- delimiter: delimiter,
314
- };
315
- }
316
- }
234
+ const linesRaw = csvContent.trim().split("\n").slice(0, 3);
235
+ if (linesRaw.length === 0) {
236
+ throw new Error("CSV file is empty");
237
+ }
238
+
239
+ // Safely handle the first line - check if it exists and is not empty after trimming
240
+ const firstLineRaw = linesRaw[0];
241
+ if (!firstLineRaw || !firstLineRaw.trim()) {
242
+ throw new Error("CSV file contains empty first line");
243
+ }
244
+
245
+ // Detect delimiter across sample lines
246
+ const delimiter = detectDelimiter(linesRaw);
247
+
248
+ const firstLine = firstLineRaw.split(delimiter);
249
+ const hasHeader = !isDateLike(firstLine[0] || "");
250
+
251
+ // Check for separate debit/credit columns by looking for empty cells pattern
252
+ let hasDebitCredit = false;
253
+ if (linesRaw.length > 1) {
254
+ const dataLines = hasHeader ? linesRaw.slice(1) : linesRaw;
255
+ hasDebitCredit = dataLines.some((line) => {
256
+ const cols = line.split(delimiter);
257
+ // Look for pattern: amount in col2 OR col3, but not both
258
+ return (
259
+ cols.length >= 4 &&
260
+ ((cols[2]?.trim() && !cols[3]?.trim()) ||
261
+ (!cols[2]?.trim() && cols[3]?.trim()))
262
+ );
263
+ });
264
+ }
265
+
266
+ if (hasHeader) {
267
+ const {
268
+ dateColumn,
269
+ amountColumn,
270
+ descriptionColumn,
271
+ debitColumn,
272
+ creditColumn,
273
+ } = analyzeHeaders(firstLine);
274
+
275
+ const safe = (v?: string) => (v?.trim() ? v : undefined);
276
+
277
+ if (hasDebitCredit && debitColumn && creditColumn) {
278
+ const dateCol = safe(dateColumn ?? undefined) ?? safe(firstLine[0]);
279
+ if (!dateCol)
280
+ throw new Error("Unable to detect date column name from header");
281
+ const descCol =
282
+ safe(descriptionColumn ?? undefined) ?? safe(firstLine[1]);
283
+ if (!descCol)
284
+ throw new Error("Unable to detect description column name from header");
285
+
286
+ return {
287
+ date_column: dateCol,
288
+ description_column: descCol,
289
+ debit_column: debitColumn,
290
+ credit_column: creditColumn,
291
+ date_format: detectDateFormat(linesRaw[1]?.split(delimiter)[0]),
292
+ has_header: hasHeader,
293
+ delimiter: delimiter,
294
+ };
295
+ }
296
+ const dateCol = safe(dateColumn ?? undefined) ?? safe(firstLine[0]);
297
+ if (!dateCol)
298
+ throw new Error("Unable to detect date column name from header");
299
+ const amountCol = safe(amountColumn ?? undefined) ?? safe(firstLine[1]);
300
+ if (!amountCol)
301
+ throw new Error("Unable to detect amount column name from header");
302
+ const descCol =
303
+ safe(descriptionColumn ?? undefined) ??
304
+ safe(firstLine.length >= 3 ? firstLine[2] : firstLine[1]);
305
+ if (!descCol)
306
+ throw new Error("Unable to detect description column name from header");
307
+
308
+ return {
309
+ date_column: dateCol,
310
+ amount_column: amountCol,
311
+ description_column: descCol,
312
+ date_format: detectDateFormat(linesRaw[1]?.split(delimiter)[0]),
313
+ has_header: hasHeader,
314
+ delimiter: delimiter,
315
+ };
316
+ }
317
+ if (hasDebitCredit && firstLine.length >= 4) {
318
+ return {
319
+ date_column: 0,
320
+ description_column: 1,
321
+ debit_column: 2,
322
+ credit_column: 3,
323
+ date_format: detectDateFormat(firstLine[0]),
324
+ has_header: hasHeader,
325
+ delimiter: delimiter,
326
+ };
327
+ }
328
+ return {
329
+ date_column: 0,
330
+ amount_column: 1,
331
+ description_column: firstLine.length >= 3 ? 2 : 1,
332
+ date_format: detectDateFormat(firstLine[0]),
333
+ has_header: hasHeader,
334
+ delimiter: delimiter,
335
+ };
317
336
  }
318
337
 
319
338
  /**
320
339
  * Automatically fix common CSV issues like unquoted dates with commas
321
340
  */
322
341
  function preprocessCSV(csvContent: string, format: CSVFormat): string {
323
- // Check if we're dealing with MMM dd, yyyy format dates that might need quoting
324
- if (format.date_format?.includes('MMM') && format.date_format?.includes(',')) {
325
- const lines = csvContent.split('\n');
326
- const fixedLines = lines.map((line, index) => {
327
- // Skip header row
328
- if (format.has_header && index === 0) return line;
329
- if (!line.trim()) return line;
330
-
331
- // Check if this line has unquoted dates (more commas than expected)
332
- const parts = line.split(format.delimiter);
333
- const expectedColumns = format.has_header ? lines[0]?.split(format.delimiter).length || 3 : 3;
334
-
335
- if (parts.length > expectedColumns) {
336
- // Check if we have a date pattern split across first two parts (like "Sep 18, 2025")
337
- const potentialDate = parts.slice(0, 2).join(',');
338
- if (/^[A-Za-z]{3}\s+\d{1,2},\s+\d{4}/.test(potentialDate)) {
339
- // This looks like "Sep 18, 2025" - quote it
340
- const dateField = parts.slice(0, 2).join(','); // "Sep 18, 2025"
341
- const remainingFields = parts.slice(2);
342
- return `"${dateField}"${format.delimiter}${remainingFields.join(format.delimiter)}`;
343
- }
344
- }
345
-
346
- return line;
347
- });
348
-
349
- return fixedLines.join('\n');
350
- }
351
-
352
- return csvContent;
342
+ // Check if we're dealing with MMM dd, yyyy format dates that might need quoting
343
+ if (
344
+ format.date_format?.includes("MMM") &&
345
+ format.date_format?.includes(",")
346
+ ) {
347
+ const lines = csvContent.split("\n");
348
+ const fixedLines = lines.map((line, index) => {
349
+ // Skip header row
350
+ if (format.has_header && index === 0) return line;
351
+ if (!line.trim()) return line;
352
+
353
+ // Check if this line has unquoted dates (more commas than expected)
354
+ const parts = line.split(format.delimiter);
355
+ const expectedColumns = format.has_header
356
+ ? lines[0]?.split(format.delimiter).length || 3
357
+ : 3;
358
+
359
+ if (parts.length > expectedColumns) {
360
+ // Check if we have a date pattern split across first two parts (like "Sep 18, 2025")
361
+ const potentialDate = parts.slice(0, 2).join(",");
362
+ if (/^[A-Za-z]{3}\s+\d{1,2},\s+\d{4}/.test(potentialDate)) {
363
+ // This looks like "Sep 18, 2025" - quote it
364
+ const dateField = parts.slice(0, 2).join(","); // "Sep 18, 2025"
365
+ const remainingFields = parts.slice(2);
366
+ return `"${dateField}"${format.delimiter}${remainingFields.join(format.delimiter)}`;
367
+ }
368
+ }
369
+
370
+ return line;
371
+ });
372
+
373
+ return fixedLines.join("\n");
374
+ }
375
+
376
+ return csvContent;
353
377
  }
354
378
 
355
379
  /**
356
380
  * Parse CSV data into bank transactions
357
381
  */
358
382
  export function parseBankCSV(
359
- csvContent: string,
360
- format: CSVFormat,
361
- options: { debug?: boolean } = {},
383
+ csvContent: string,
384
+ format: CSVFormat,
385
+ options: { debug?: boolean } = {},
362
386
  ): BankTransaction[] {
363
- // Preprocess CSV to fix common issues like unquoted dates
364
- const processedCSV = preprocessCSV(csvContent, format);
365
-
366
- const records = parse(processedCSV, {
367
- delimiter: format.delimiter,
368
- columns: format.has_header,
369
- skip_empty_lines: true,
370
- trim: true,
371
- // Enhanced CSV parsing options for robust handling
372
- quote: '"', // Handle quoted fields (for dates with commas)
373
- escape: '"', // Handle escaped quotes within fields
374
- relax_column_count: true, // Handle varying column counts
375
- // Removed deprecated auto_parse and auto_parse_date options
376
- // Removed relax_quotes as it may not be supported in current csv-parse version
377
- });
378
-
379
- const transactions: BankTransaction[] = [];
380
-
381
- for (let i = 0; i < records.length; i++) {
382
- const record = records[i];
383
- const rowNumber = format.has_header ? i + 2 : i + 1; // Account for header row
384
-
385
- try {
386
- let rawDate: string;
387
- let rawAmount: string;
388
- let description: string;
389
-
390
- if (format.has_header) {
391
- // Record is an object when using headers
392
- const recordObj = record as unknown as Record<string, string>;
393
- rawDate = recordObj[format.date_column as string] || '';
394
-
395
- if (format.amount_column) {
396
- rawAmount = recordObj[format.amount_column as string] || '';
397
- } else if (format.debit_column !== undefined && format.credit_column !== undefined) {
398
- const debitVal = recordObj[format.debit_column as string] || '';
399
- const creditVal = recordObj[format.credit_column as string] || '';
400
- // Convert: debits negative, credits positive
401
- // Check if debit has a value and is non-zero
402
- const debitNum = parseFloat(debitVal.replace(/[^\d.-]/g, ''));
403
- const creditNum = parseFloat(creditVal.replace(/[^\d.-]/g, ''));
404
- if (!isNaN(debitNum) && debitNum !== 0) {
405
- rawAmount = `-${debitVal}`;
406
- } else if (!isNaN(creditNum) && creditNum !== 0) {
407
- rawAmount = creditVal;
408
- } else {
409
- rawAmount = '0';
410
- }
411
- } else {
412
- throw new Error('No amount column configuration found');
413
- }
414
-
415
- description = recordObj[format.description_column as string] || '';
416
- } else {
417
- // Record is an array when not using headers, so use column indices
418
- const recordArray = record as string[];
419
- const dateIndex =
420
- typeof format.date_column === 'number'
421
- ? format.date_column
422
- : parseInt(format.date_column, 10);
423
- const descIndex =
424
- typeof format.description_column === 'number'
425
- ? format.description_column
426
- : parseInt(format.description_column, 10);
427
-
428
- // Validate indices are valid numbers (fallback to defaults if invalid)
429
- const safeDateIndex = isNaN(dateIndex) ? 0 : dateIndex;
430
- const safeDescIndex = isNaN(descIndex) ? 2 : descIndex;
431
-
432
- rawDate = recordArray[safeDateIndex] || '';
433
-
434
- if (format.amount_column !== undefined) {
435
- const amountIndex =
436
- typeof format.amount_column === 'number'
437
- ? format.amount_column
438
- : parseInt(format.amount_column, 10);
439
- const safeAmountIndex = isNaN(amountIndex) ? 1 : amountIndex;
440
- rawAmount = recordArray[safeAmountIndex] || '';
441
- } else if (format.debit_column !== undefined && format.credit_column !== undefined) {
442
- const debitIndex =
443
- typeof format.debit_column === 'number'
444
- ? format.debit_column
445
- : parseInt(format.debit_column, 10);
446
- const creditIndex =
447
- typeof format.credit_column === 'number'
448
- ? format.credit_column
449
- : parseInt(format.credit_column, 10);
450
-
451
- const debitVal = recordArray[debitIndex] || '';
452
- const creditVal = recordArray[creditIndex] || '';
453
-
454
- // Convert: debits negative, credits positive
455
- // Check if debit has a value and is non-zero
456
- const debitNum = parseFloat(debitVal.replace(/[^\d.-]/g, ''));
457
- const creditNum = parseFloat(creditVal.replace(/[^\d.-]/g, ''));
458
- if (!isNaN(debitNum) && debitNum !== 0) {
459
- rawAmount = `-${debitVal}`;
460
- } else if (!isNaN(creditNum) && creditNum !== 0) {
461
- rawAmount = creditVal;
462
- } else {
463
- rawAmount = '0';
464
- }
465
- } else {
466
- throw new Error('No amount column configuration found');
467
- }
468
-
469
- description = recordArray[safeDescIndex] || '';
470
- }
471
-
472
- if (!rawDate || !rawAmount) {
473
- if (options.debug) {
474
- console.warn(`Skipping row ${rowNumber}: missing date or amount`);
475
- }
476
- continue;
477
- }
478
-
479
- const date = parseDate(rawDate, format.date_format);
480
- let amount: Milli;
481
- try {
482
- amount = amountToMilliunits(rawAmount);
483
- } catch (error) {
484
- if (options.debug) {
485
- console.warn(
486
- `Skipping row ${rowNumber}: ${error instanceof Error ? error.message : 'Invalid amount'}`,
487
- );
488
- }
489
- continue;
490
- }
491
-
492
- transactions.push({
493
- date,
494
- amount,
495
- description: description.trim(),
496
- raw_amount: rawAmount,
497
- raw_date: rawDate,
498
- row_number: rowNumber,
499
- });
500
- } catch (error) {
501
- if (options.debug) {
502
- console.warn(`Error parsing row ${rowNumber}:`, error);
503
- }
504
- continue;
505
- }
506
- }
507
-
508
- return transactions;
387
+ // Preprocess CSV to fix common issues like unquoted dates
388
+ const processedCSV = preprocessCSV(csvContent, format);
389
+
390
+ const records = parse(processedCSV, {
391
+ delimiter: format.delimiter,
392
+ columns: format.has_header,
393
+ skip_empty_lines: true,
394
+ trim: true,
395
+ quote: '"',
396
+ escape: '"',
397
+ relax_column_count: true,
398
+ });
399
+
400
+ const transactions: BankTransaction[] = [];
401
+
402
+ for (let i = 0; i < records.length; i++) {
403
+ const record = records[i];
404
+ const rowNumber = format.has_header ? i + 2 : i + 1; // Account for header row
405
+
406
+ try {
407
+ let rawDate: string;
408
+ let rawAmount: string;
409
+ let description: string;
410
+
411
+ if (format.has_header) {
412
+ // Record is an object when using headers
413
+ const recordObj = record as unknown as Record<string, string>;
414
+ rawDate = recordObj[format.date_column as string] || "";
415
+
416
+ if (format.amount_column) {
417
+ rawAmount = recordObj[format.amount_column as string] || "";
418
+ } else if (
419
+ format.debit_column !== undefined &&
420
+ format.credit_column !== undefined
421
+ ) {
422
+ const debitVal = recordObj[format.debit_column as string] || "";
423
+ const creditVal = recordObj[format.credit_column as string] || "";
424
+ // Convert: debits negative, credits positive
425
+ // Check if debit has a value and is non-zero
426
+ const debitNum = Number.parseFloat(debitVal.replace(/[^\d.-]/g, ""));
427
+ const creditNum = Number.parseFloat(
428
+ creditVal.replace(/[^\d.-]/g, ""),
429
+ );
430
+ if (!Number.isNaN(debitNum) && debitNum !== 0) {
431
+ rawAmount = `-${debitVal}`;
432
+ } else if (!Number.isNaN(creditNum) && creditNum !== 0) {
433
+ rawAmount = creditVal;
434
+ } else {
435
+ rawAmount = "0";
436
+ }
437
+ } else {
438
+ throw new Error("No amount column configuration found");
439
+ }
440
+
441
+ description = recordObj[format.description_column as string] || "";
442
+ } else {
443
+ // Record is an array when not using headers, so use column indices
444
+ const recordArray = record as string[];
445
+ const dateIndex =
446
+ typeof format.date_column === "number"
447
+ ? format.date_column
448
+ : Number.parseInt(format.date_column, 10);
449
+ const descIndex =
450
+ typeof format.description_column === "number"
451
+ ? format.description_column
452
+ : Number.parseInt(format.description_column, 10);
453
+
454
+ // Validate indices are valid numbers (fallback to defaults if invalid)
455
+ const safeDateIndex = Number.isNaN(dateIndex) ? 0 : dateIndex;
456
+ const safeDescIndex = Number.isNaN(descIndex) ? 2 : descIndex;
457
+
458
+ rawDate = recordArray[safeDateIndex] || "";
459
+
460
+ if (format.amount_column !== undefined) {
461
+ const amountIndex =
462
+ typeof format.amount_column === "number"
463
+ ? format.amount_column
464
+ : Number.parseInt(format.amount_column, 10);
465
+ const safeAmountIndex = Number.isNaN(amountIndex) ? 1 : amountIndex;
466
+ rawAmount = recordArray[safeAmountIndex] || "";
467
+ } else if (
468
+ format.debit_column !== undefined &&
469
+ format.credit_column !== undefined
470
+ ) {
471
+ const debitIndex =
472
+ typeof format.debit_column === "number"
473
+ ? format.debit_column
474
+ : Number.parseInt(format.debit_column, 10);
475
+ const creditIndex =
476
+ typeof format.credit_column === "number"
477
+ ? format.credit_column
478
+ : Number.parseInt(format.credit_column, 10);
479
+
480
+ const debitVal = recordArray[debitIndex] || "";
481
+ const creditVal = recordArray[creditIndex] || "";
482
+
483
+ // Convert: debits negative, credits positive
484
+ // Check if debit has a value and is non-zero
485
+ const debitNum = Number.parseFloat(debitVal.replace(/[^\d.-]/g, ""));
486
+ const creditNum = Number.parseFloat(
487
+ creditVal.replace(/[^\d.-]/g, ""),
488
+ );
489
+ if (!Number.isNaN(debitNum) && debitNum !== 0) {
490
+ rawAmount = `-${debitVal}`;
491
+ } else if (!Number.isNaN(creditNum) && creditNum !== 0) {
492
+ rawAmount = creditVal;
493
+ } else {
494
+ rawAmount = "0";
495
+ }
496
+ } else {
497
+ throw new Error("No amount column configuration found");
498
+ }
499
+
500
+ description = recordArray[safeDescIndex] || "";
501
+ }
502
+
503
+ if (!rawDate || !rawAmount) {
504
+ if (options.debug) {
505
+ console.warn(`Skipping row ${rowNumber}: missing date or amount`);
506
+ }
507
+ continue;
508
+ }
509
+
510
+ const date = parseDate(rawDate, format.date_format);
511
+ let amount: Milli;
512
+ try {
513
+ amount = amountToMilliunits(rawAmount);
514
+ } catch (error) {
515
+ if (options.debug) {
516
+ console.warn(
517
+ `Skipping row ${rowNumber}: ${error instanceof Error ? error.message : "Invalid amount"}`,
518
+ );
519
+ }
520
+ continue;
521
+ }
522
+
523
+ transactions.push({
524
+ date,
525
+ amount,
526
+ description: description.trim(),
527
+ raw_amount: rawAmount,
528
+ raw_date: rawDate,
529
+ row_number: rowNumber,
530
+ });
531
+ } catch (error) {
532
+ if (options.debug) {
533
+ console.warn(`Error parsing row ${rowNumber}:`, error);
534
+ }
535
+ }
536
+ }
537
+
538
+ return transactions;
509
539
  }
510
540
 
511
541
  /**
512
542
  * Read CSV file safely with error handling
513
543
  */
514
544
  export function readCSVFile(filePath: string): string {
515
- try {
516
- return readFileSync(filePath, 'utf-8');
517
- } catch (error) {
518
- throw new Error(
519
- `Unable to read CSV file: ${error instanceof Error ? error.message : 'Unknown error'}`,
520
- );
521
- }
545
+ try {
546
+ return readFileSync(filePath, "utf-8");
547
+ } catch (error) {
548
+ throw new Error(
549
+ `Unable to read CSV file: ${error instanceof Error ? error.message : "Unknown error"}`,
550
+ );
551
+ }
522
552
  }
523
553
 
524
554
  /**
@@ -526,32 +556,32 @@ export function readCSVFile(filePath: string): string {
526
556
  * Returns min and max dates in YYYY-MM-DD format
527
557
  */
528
558
  export function extractDateRangeFromCSV(
529
- csvContent: string,
530
- format: CSVFormat,
559
+ csvContent: string,
560
+ format: CSVFormat,
531
561
  ): { minDate: string; maxDate: string } {
532
- const transactions = parseBankCSV(csvContent, format);
533
-
534
- if (transactions.length === 0) {
535
- throw new Error('No transactions found in CSV');
536
- }
537
-
538
- // Extract all dates (already Date objects from parseBankCSV)
539
- const dates = transactions.map((txn) => txn.date.getTime());
540
-
541
- // Find min and max
542
- const minDateObj = new Date(Math.min(...dates));
543
- const maxDateObj = new Date(Math.max(...dates));
544
-
545
- // Convert to YYYY-MM-DD format
546
- const toYYYYMMDD = (date: Date): string => {
547
- const year = date.getFullYear();
548
- const month = String(date.getMonth() + 1).padStart(2, '0');
549
- const day = String(date.getDate()).padStart(2, '0');
550
- return `${year}-${month}-${day}`;
551
- };
552
-
553
- return {
554
- minDate: toYYYYMMDD(minDateObj),
555
- maxDate: toYYYYMMDD(maxDateObj),
556
- };
562
+ const transactions = parseBankCSV(csvContent, format);
563
+
564
+ if (transactions.length === 0) {
565
+ throw new Error("No transactions found in CSV");
566
+ }
567
+
568
+ // Extract all dates (already Date objects from parseBankCSV)
569
+ const dates = transactions.map((txn) => txn.date.getTime());
570
+
571
+ // Find min and max
572
+ const minDateObj = new Date(Math.min(...dates));
573
+ const maxDateObj = new Date(Math.max(...dates));
574
+
575
+ // Convert to YYYY-MM-DD format
576
+ const toYYYYMMDD = (date: Date): string => {
577
+ const year = date.getFullYear();
578
+ const month = String(date.getMonth() + 1).padStart(2, "0");
579
+ const day = String(date.getDate()).padStart(2, "0");
580
+ return `${year}-${month}-${day}`;
581
+ };
582
+
583
+ return {
584
+ minDate: toYYYYMMDD(minDateObj),
585
+ maxDate: toYYYYMMDD(maxDateObj),
586
+ };
557
587
  }