@hasna/microservices 0.0.16 → 0.0.17

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 (293) hide show
  1. package/README.md +143 -23
  2. package/bin/index.js +711 -13998
  3. package/bin/mcp.js +228 -10987
  4. package/dist/index.js +166 -10056
  5. package/package.json +25 -25
  6. package/microservices/microservice-ads/package.json +0 -28
  7. package/microservices/microservice-ads/src/cli/index.ts +0 -605
  8. package/microservices/microservice-ads/src/db/campaigns.ts +0 -797
  9. package/microservices/microservice-ads/src/db/database.ts +0 -91
  10. package/microservices/microservice-ads/src/db/migrations.ts +0 -60
  11. package/microservices/microservice-ads/src/index.ts +0 -39
  12. package/microservices/microservice-ads/src/mcp/index.ts +0 -480
  13. package/microservices/microservice-analytics/package.json +0 -28
  14. package/microservices/microservice-analytics/src/cli/index.ts +0 -373
  15. package/microservices/microservice-analytics/src/db/analytics.ts +0 -564
  16. package/microservices/microservice-analytics/src/db/database.ts +0 -91
  17. package/microservices/microservice-analytics/src/db/migrations.ts +0 -50
  18. package/microservices/microservice-analytics/src/index.ts +0 -37
  19. package/microservices/microservice-analytics/src/mcp/index.ts +0 -334
  20. package/microservices/microservice-assets/package.json +0 -28
  21. package/microservices/microservice-assets/src/cli/index.ts +0 -375
  22. package/microservices/microservice-assets/src/db/assets.ts +0 -370
  23. package/microservices/microservice-assets/src/db/database.ts +0 -91
  24. package/microservices/microservice-assets/src/db/migrations.ts +0 -51
  25. package/microservices/microservice-assets/src/index.ts +0 -32
  26. package/microservices/microservice-assets/src/mcp/index.ts +0 -346
  27. package/microservices/microservice-bookkeeping/package.json +0 -28
  28. package/microservices/microservice-bookkeeping/src/cli/index.ts +0 -386
  29. package/microservices/microservice-bookkeeping/src/db/bookkeeping.ts +0 -591
  30. package/microservices/microservice-bookkeeping/src/db/database.ts +0 -91
  31. package/microservices/microservice-bookkeeping/src/db/migrations.ts +0 -52
  32. package/microservices/microservice-bookkeeping/src/index.ts +0 -32
  33. package/microservices/microservice-bookkeeping/src/mcp/index.ts +0 -284
  34. package/microservices/microservice-calendar/package.json +0 -28
  35. package/microservices/microservice-calendar/src/cli/index.ts +0 -287
  36. package/microservices/microservice-calendar/src/db/calendar.ts +0 -328
  37. package/microservices/microservice-calendar/src/db/database.ts +0 -91
  38. package/microservices/microservice-calendar/src/db/migrations.ts +0 -47
  39. package/microservices/microservice-calendar/src/index.ts +0 -24
  40. package/microservices/microservice-calendar/src/mcp/index.ts +0 -226
  41. package/microservices/microservice-company/package.json +0 -28
  42. package/microservices/microservice-company/src/cli/index.ts +0 -1126
  43. package/microservices/microservice-company/src/db/company.ts +0 -854
  44. package/microservices/microservice-company/src/db/database.ts +0 -91
  45. package/microservices/microservice-company/src/db/migrations.ts +0 -214
  46. package/microservices/microservice-company/src/db/workflow-migrations.ts +0 -44
  47. package/microservices/microservice-company/src/index.ts +0 -60
  48. package/microservices/microservice-company/src/lib/audit.ts +0 -168
  49. package/microservices/microservice-company/src/lib/finance.ts +0 -299
  50. package/microservices/microservice-company/src/lib/settings.ts +0 -85
  51. package/microservices/microservice-company/src/lib/workflows.ts +0 -698
  52. package/microservices/microservice-company/src/mcp/index.ts +0 -991
  53. package/microservices/microservice-compliance/package.json +0 -28
  54. package/microservices/microservice-compliance/src/cli/index.ts +0 -467
  55. package/microservices/microservice-compliance/src/db/compliance.ts +0 -633
  56. package/microservices/microservice-compliance/src/db/database.ts +0 -91
  57. package/microservices/microservice-compliance/src/db/migrations.ts +0 -63
  58. package/microservices/microservice-compliance/src/index.ts +0 -46
  59. package/microservices/microservice-compliance/src/mcp/index.ts +0 -438
  60. package/microservices/microservice-contacts/package.json +0 -28
  61. package/microservices/microservice-contacts/src/cli/index.ts +0 -393
  62. package/microservices/microservice-contacts/src/db/companies.ts +0 -167
  63. package/microservices/microservice-contacts/src/db/contacts.ts +0 -249
  64. package/microservices/microservice-contacts/src/db/database.ts +0 -91
  65. package/microservices/microservice-contacts/src/db/migrations.ts +0 -71
  66. package/microservices/microservice-contacts/src/db/relationships.ts +0 -53
  67. package/microservices/microservice-contacts/src/index.ts +0 -42
  68. package/microservices/microservice-contacts/src/mcp/index.ts +0 -303
  69. package/microservices/microservice-contracts/package.json +0 -28
  70. package/microservices/microservice-contracts/src/cli/index.ts +0 -770
  71. package/microservices/microservice-contracts/src/db/contracts.ts +0 -925
  72. package/microservices/microservice-contracts/src/db/database.ts +0 -91
  73. package/microservices/microservice-contracts/src/db/migrations.ts +0 -141
  74. package/microservices/microservice-contracts/src/index.ts +0 -43
  75. package/microservices/microservice-contracts/src/mcp/index.ts +0 -617
  76. package/microservices/microservice-crm/package.json +0 -28
  77. package/microservices/microservice-crm/src/cli/index.ts +0 -396
  78. package/microservices/microservice-crm/src/db/database.ts +0 -91
  79. package/microservices/microservice-crm/src/db/migrations.ts +0 -66
  80. package/microservices/microservice-crm/src/db/pipeline.ts +0 -397
  81. package/microservices/microservice-crm/src/index.ts +0 -34
  82. package/microservices/microservice-crm/src/mcp/index.ts +0 -294
  83. package/microservices/microservice-documents/package.json +0 -28
  84. package/microservices/microservice-documents/src/cli/index.ts +0 -246
  85. package/microservices/microservice-documents/src/db/database.ts +0 -91
  86. package/microservices/microservice-documents/src/db/documents.ts +0 -316
  87. package/microservices/microservice-documents/src/db/migrations.ts +0 -49
  88. package/microservices/microservice-documents/src/index.ts +0 -24
  89. package/microservices/microservice-documents/src/mcp/index.ts +0 -202
  90. package/microservices/microservice-domains/package.json +0 -28
  91. package/microservices/microservice-domains/src/cli/index.ts +0 -1111
  92. package/microservices/microservice-domains/src/db/database.ts +0 -91
  93. package/microservices/microservice-domains/src/db/domains.ts +0 -1164
  94. package/microservices/microservice-domains/src/db/migrations.ts +0 -60
  95. package/microservices/microservice-domains/src/index.ts +0 -65
  96. package/microservices/microservice-domains/src/lib/brandsight.ts +0 -350
  97. package/microservices/microservice-domains/src/lib/godaddy.ts +0 -338
  98. package/microservices/microservice-domains/src/lib/namecheap.ts +0 -262
  99. package/microservices/microservice-domains/src/lib/registrar.ts +0 -355
  100. package/microservices/microservice-domains/src/mcp/index.ts +0 -781
  101. package/microservices/microservice-expenses/package.json +0 -28
  102. package/microservices/microservice-expenses/src/cli/index.ts +0 -267
  103. package/microservices/microservice-expenses/src/db/database.ts +0 -91
  104. package/microservices/microservice-expenses/src/db/expenses.ts +0 -345
  105. package/microservices/microservice-expenses/src/db/migrations.ts +0 -45
  106. package/microservices/microservice-expenses/src/index.ts +0 -25
  107. package/microservices/microservice-expenses/src/mcp/index.ts +0 -196
  108. package/microservices/microservice-habits/package.json +0 -28
  109. package/microservices/microservice-habits/src/cli/index.ts +0 -315
  110. package/microservices/microservice-habits/src/db/database.ts +0 -91
  111. package/microservices/microservice-habits/src/db/habits.ts +0 -451
  112. package/microservices/microservice-habits/src/db/migrations.ts +0 -46
  113. package/microservices/microservice-habits/src/index.ts +0 -31
  114. package/microservices/microservice-habits/src/mcp/index.ts +0 -313
  115. package/microservices/microservice-health/package.json +0 -28
  116. package/microservices/microservice-health/src/cli/index.ts +0 -484
  117. package/microservices/microservice-health/src/db/database.ts +0 -91
  118. package/microservices/microservice-health/src/db/health.ts +0 -708
  119. package/microservices/microservice-health/src/db/migrations.ts +0 -70
  120. package/microservices/microservice-health/src/index.ts +0 -63
  121. package/microservices/microservice-health/src/mcp/index.ts +0 -437
  122. package/microservices/microservice-hiring/package.json +0 -28
  123. package/microservices/microservice-hiring/src/cli/index.ts +0 -741
  124. package/microservices/microservice-hiring/src/db/database.ts +0 -91
  125. package/microservices/microservice-hiring/src/db/hiring.ts +0 -1085
  126. package/microservices/microservice-hiring/src/db/migrations.ts +0 -89
  127. package/microservices/microservice-hiring/src/index.ts +0 -80
  128. package/microservices/microservice-hiring/src/lib/scoring.ts +0 -206
  129. package/microservices/microservice-hiring/src/mcp/index.ts +0 -709
  130. package/microservices/microservice-inventory/package.json +0 -28
  131. package/microservices/microservice-inventory/src/cli/index.ts +0 -365
  132. package/microservices/microservice-inventory/src/db/database.ts +0 -91
  133. package/microservices/microservice-inventory/src/db/inventory.ts +0 -393
  134. package/microservices/microservice-inventory/src/db/migrations.ts +0 -54
  135. package/microservices/microservice-inventory/src/index.ts +0 -28
  136. package/microservices/microservice-inventory/src/mcp/index.ts +0 -250
  137. package/microservices/microservice-invoices/dashboard/index.html +0 -12
  138. package/microservices/microservice-invoices/dashboard/package.json +0 -29
  139. package/microservices/microservice-invoices/dashboard/tsconfig.json +0 -14
  140. package/microservices/microservice-invoices/dashboard/vite.config.ts +0 -15
  141. package/microservices/microservice-invoices/package.json +0 -31
  142. package/microservices/microservice-invoices/src/cli/index.ts +0 -308
  143. package/microservices/microservice-invoices/src/db/business.ts +0 -241
  144. package/microservices/microservice-invoices/src/db/clients.ts +0 -127
  145. package/microservices/microservice-invoices/src/db/database.ts +0 -91
  146. package/microservices/microservice-invoices/src/db/invoices.ts +0 -345
  147. package/microservices/microservice-invoices/src/db/migrations.ts +0 -184
  148. package/microservices/microservice-invoices/src/index.ts +0 -56
  149. package/microservices/microservice-invoices/src/mcp/index.ts +0 -242
  150. package/microservices/microservice-invoices/src/server/index.ts +0 -162
  151. package/microservices/microservice-leads/package.json +0 -28
  152. package/microservices/microservice-leads/src/cli/index.ts +0 -596
  153. package/microservices/microservice-leads/src/db/database.ts +0 -91
  154. package/microservices/microservice-leads/src/db/leads.ts +0 -520
  155. package/microservices/microservice-leads/src/db/lists.ts +0 -151
  156. package/microservices/microservice-leads/src/db/migrations.ts +0 -93
  157. package/microservices/microservice-leads/src/index.ts +0 -65
  158. package/microservices/microservice-leads/src/lib/enrichment.ts +0 -202
  159. package/microservices/microservice-leads/src/lib/scoring.ts +0 -134
  160. package/microservices/microservice-leads/src/mcp/index.ts +0 -533
  161. package/microservices/microservice-notes/package.json +0 -28
  162. package/microservices/microservice-notes/src/cli/index.ts +0 -63
  163. package/microservices/microservice-notes/src/db/database.ts +0 -91
  164. package/microservices/microservice-notes/src/db/migrations.ts +0 -40
  165. package/microservices/microservice-notes/src/db/notes.ts +0 -114
  166. package/microservices/microservice-notes/src/index.ts +0 -2
  167. package/microservices/microservice-notes/src/mcp/index.ts +0 -37
  168. package/microservices/microservice-notifications/package.json +0 -28
  169. package/microservices/microservice-notifications/src/cli/index.ts +0 -349
  170. package/microservices/microservice-notifications/src/db/database.ts +0 -91
  171. package/microservices/microservice-notifications/src/db/migrations.ts +0 -62
  172. package/microservices/microservice-notifications/src/db/notifications.ts +0 -509
  173. package/microservices/microservice-notifications/src/index.ts +0 -41
  174. package/microservices/microservice-notifications/src/mcp/index.ts +0 -422
  175. package/microservices/microservice-payments/package.json +0 -28
  176. package/microservices/microservice-payments/src/cli/index.ts +0 -609
  177. package/microservices/microservice-payments/src/db/database.ts +0 -91
  178. package/microservices/microservice-payments/src/db/migrations.ts +0 -81
  179. package/microservices/microservice-payments/src/db/payments.ts +0 -1204
  180. package/microservices/microservice-payments/src/index.ts +0 -51
  181. package/microservices/microservice-payments/src/mcp/index.ts +0 -683
  182. package/microservices/microservice-payroll/package.json +0 -28
  183. package/microservices/microservice-payroll/src/cli/index.ts +0 -643
  184. package/microservices/microservice-payroll/src/db/database.ts +0 -91
  185. package/microservices/microservice-payroll/src/db/migrations.ts +0 -95
  186. package/microservices/microservice-payroll/src/db/payroll.ts +0 -1377
  187. package/microservices/microservice-payroll/src/index.ts +0 -48
  188. package/microservices/microservice-payroll/src/mcp/index.ts +0 -666
  189. package/microservices/microservice-products/package.json +0 -28
  190. package/microservices/microservice-products/src/cli/index.ts +0 -416
  191. package/microservices/microservice-products/src/db/categories.ts +0 -154
  192. package/microservices/microservice-products/src/db/database.ts +0 -91
  193. package/microservices/microservice-products/src/db/migrations.ts +0 -58
  194. package/microservices/microservice-products/src/db/pricing-tiers.ts +0 -66
  195. package/microservices/microservice-products/src/db/products.ts +0 -452
  196. package/microservices/microservice-products/src/index.ts +0 -53
  197. package/microservices/microservice-products/src/mcp/index.ts +0 -453
  198. package/microservices/microservice-projects/package.json +0 -28
  199. package/microservices/microservice-projects/src/cli/index.ts +0 -480
  200. package/microservices/microservice-projects/src/db/database.ts +0 -91
  201. package/microservices/microservice-projects/src/db/migrations.ts +0 -65
  202. package/microservices/microservice-projects/src/db/projects.ts +0 -715
  203. package/microservices/microservice-projects/src/index.ts +0 -57
  204. package/microservices/microservice-projects/src/mcp/index.ts +0 -501
  205. package/microservices/microservice-proposals/package.json +0 -28
  206. package/microservices/microservice-proposals/src/cli/index.ts +0 -400
  207. package/microservices/microservice-proposals/src/db/database.ts +0 -91
  208. package/microservices/microservice-proposals/src/db/migrations.ts +0 -52
  209. package/microservices/microservice-proposals/src/db/proposals.ts +0 -532
  210. package/microservices/microservice-proposals/src/index.ts +0 -37
  211. package/microservices/microservice-proposals/src/mcp/index.ts +0 -375
  212. package/microservices/microservice-reading/package.json +0 -28
  213. package/microservices/microservice-reading/src/cli/index.ts +0 -464
  214. package/microservices/microservice-reading/src/db/database.ts +0 -91
  215. package/microservices/microservice-reading/src/db/migrations.ts +0 -59
  216. package/microservices/microservice-reading/src/db/reading.ts +0 -524
  217. package/microservices/microservice-reading/src/index.ts +0 -51
  218. package/microservices/microservice-reading/src/mcp/index.ts +0 -368
  219. package/microservices/microservice-shipping/package.json +0 -28
  220. package/microservices/microservice-shipping/src/cli/index.ts +0 -606
  221. package/microservices/microservice-shipping/src/db/database.ts +0 -91
  222. package/microservices/microservice-shipping/src/db/migrations.ts +0 -69
  223. package/microservices/microservice-shipping/src/db/shipping.ts +0 -1093
  224. package/microservices/microservice-shipping/src/index.ts +0 -53
  225. package/microservices/microservice-shipping/src/mcp/index.ts +0 -533
  226. package/microservices/microservice-social/package.json +0 -29
  227. package/microservices/microservice-social/src/cli/index.ts +0 -1583
  228. package/microservices/microservice-social/src/db/database.ts +0 -91
  229. package/microservices/microservice-social/src/db/migrations.ts +0 -160
  230. package/microservices/microservice-social/src/db/social.ts +0 -1076
  231. package/microservices/microservice-social/src/index.ts +0 -46
  232. package/microservices/microservice-social/src/lib/audience.ts +0 -353
  233. package/microservices/microservice-social/src/lib/content-ai.ts +0 -278
  234. package/microservices/microservice-social/src/lib/media.ts +0 -311
  235. package/microservices/microservice-social/src/lib/mentions.ts +0 -434
  236. package/microservices/microservice-social/src/lib/metrics-sync.ts +0 -264
  237. package/microservices/microservice-social/src/lib/publisher.ts +0 -377
  238. package/microservices/microservice-social/src/lib/scheduler.ts +0 -229
  239. package/microservices/microservice-social/src/lib/sentiment.ts +0 -256
  240. package/microservices/microservice-social/src/lib/threads.ts +0 -291
  241. package/microservices/microservice-social/src/mcp/index.ts +0 -1425
  242. package/microservices/microservice-social/src/server/index.ts +0 -441
  243. package/microservices/microservice-subscriptions/package.json +0 -28
  244. package/microservices/microservice-subscriptions/src/cli/index.ts +0 -715
  245. package/microservices/microservice-subscriptions/src/db/database.ts +0 -91
  246. package/microservices/microservice-subscriptions/src/db/migrations.ts +0 -125
  247. package/microservices/microservice-subscriptions/src/db/subscriptions.ts +0 -1256
  248. package/microservices/microservice-subscriptions/src/index.ts +0 -41
  249. package/microservices/microservice-subscriptions/src/mcp/index.ts +0 -631
  250. package/microservices/microservice-timesheets/package.json +0 -28
  251. package/microservices/microservice-timesheets/src/cli/index.ts +0 -373
  252. package/microservices/microservice-timesheets/src/db/database.ts +0 -91
  253. package/microservices/microservice-timesheets/src/db/locale.ts +0 -217
  254. package/microservices/microservice-timesheets/src/db/migrations.ts +0 -74
  255. package/microservices/microservice-timesheets/src/db/timesheets.ts +0 -447
  256. package/microservices/microservice-timesheets/src/index.ts +0 -44
  257. package/microservices/microservice-timesheets/src/mcp/index.ts +0 -269
  258. package/microservices/microservice-transcriber/package.json +0 -29
  259. package/microservices/microservice-transcriber/src/cli/index.ts +0 -1593
  260. package/microservices/microservice-transcriber/src/db/annotations.ts +0 -37
  261. package/microservices/microservice-transcriber/src/db/comments.ts +0 -166
  262. package/microservices/microservice-transcriber/src/db/database.ts +0 -91
  263. package/microservices/microservice-transcriber/src/db/migrations.ts +0 -118
  264. package/microservices/microservice-transcriber/src/db/proofread.ts +0 -119
  265. package/microservices/microservice-transcriber/src/db/transcripts.ts +0 -395
  266. package/microservices/microservice-transcriber/src/index.ts +0 -43
  267. package/microservices/microservice-transcriber/src/lib/config.ts +0 -77
  268. package/microservices/microservice-transcriber/src/lib/diff.ts +0 -91
  269. package/microservices/microservice-transcriber/src/lib/downloader.ts +0 -638
  270. package/microservices/microservice-transcriber/src/lib/feeds.ts +0 -62
  271. package/microservices/microservice-transcriber/src/lib/live.ts +0 -94
  272. package/microservices/microservice-transcriber/src/lib/notion.ts +0 -129
  273. package/microservices/microservice-transcriber/src/lib/proofread.ts +0 -296
  274. package/microservices/microservice-transcriber/src/lib/providers.ts +0 -713
  275. package/microservices/microservice-transcriber/src/lib/summarizer.ts +0 -147
  276. package/microservices/microservice-transcriber/src/lib/translator.ts +0 -75
  277. package/microservices/microservice-transcriber/src/lib/webhook.ts +0 -37
  278. package/microservices/microservice-transcriber/src/mcp/index.ts +0 -1330
  279. package/microservices/microservice-transcriber/src/server/index.ts +0 -199
  280. package/microservices/microservice-travel/package.json +0 -28
  281. package/microservices/microservice-travel/src/cli/index.ts +0 -505
  282. package/microservices/microservice-travel/src/db/database.ts +0 -91
  283. package/microservices/microservice-travel/src/db/migrations.ts +0 -77
  284. package/microservices/microservice-travel/src/db/travel.ts +0 -802
  285. package/microservices/microservice-travel/src/index.ts +0 -60
  286. package/microservices/microservice-travel/src/mcp/index.ts +0 -495
  287. package/microservices/microservice-wiki/package.json +0 -28
  288. package/microservices/microservice-wiki/src/cli/index.ts +0 -345
  289. package/microservices/microservice-wiki/src/db/database.ts +0 -91
  290. package/microservices/microservice-wiki/src/db/migrations.ts +0 -55
  291. package/microservices/microservice-wiki/src/db/wiki.ts +0 -395
  292. package/microservices/microservice-wiki/src/index.ts +0 -32
  293. package/microservices/microservice-wiki/src/mcp/index.ts +0 -344
@@ -1,925 +0,0 @@
1
- /**
2
- * Contract, Clause, and Reminder CRUD operations
3
- */
4
-
5
- import { getDatabase } from "./database.js";
6
-
7
- // --- Contract types ---
8
-
9
- export type ContractType = "nda" | "service" | "employment" | "license" | "other";
10
- export type ContractStatus = "draft" | "pending_review" | "pending_signature" | "active" | "expired" | "terminated";
11
-
12
- export interface Contract {
13
- id: string;
14
- title: string;
15
- type: ContractType;
16
- status: ContractStatus;
17
- counterparty: string | null;
18
- counterparty_email: string | null;
19
- start_date: string | null;
20
- end_date: string | null;
21
- auto_renew: boolean;
22
- renewal_period: string | null;
23
- value: number | null;
24
- currency: string;
25
- file_path: string | null;
26
- metadata: Record<string, unknown>;
27
- created_at: string;
28
- updated_at: string;
29
- }
30
-
31
- interface ContractRow {
32
- id: string;
33
- title: string;
34
- type: string;
35
- status: string;
36
- counterparty: string | null;
37
- counterparty_email: string | null;
38
- start_date: string | null;
39
- end_date: string | null;
40
- auto_renew: number;
41
- renewal_period: string | null;
42
- value: number | null;
43
- currency: string;
44
- file_path: string | null;
45
- metadata: string;
46
- created_at: string;
47
- updated_at: string;
48
- }
49
-
50
- function rowToContract(row: ContractRow): Contract {
51
- return {
52
- ...row,
53
- type: row.type as ContractType,
54
- status: row.status as ContractStatus,
55
- auto_renew: row.auto_renew === 1,
56
- metadata: JSON.parse(row.metadata || "{}"),
57
- };
58
- }
59
-
60
- export interface CreateContractInput {
61
- title: string;
62
- type?: ContractType;
63
- status?: ContractStatus;
64
- counterparty?: string;
65
- counterparty_email?: string;
66
- start_date?: string;
67
- end_date?: string;
68
- auto_renew?: boolean;
69
- renewal_period?: string;
70
- value?: number;
71
- currency?: string;
72
- file_path?: string;
73
- metadata?: Record<string, unknown>;
74
- }
75
-
76
- export function createContract(input: CreateContractInput): Contract {
77
- const db = getDatabase();
78
- const id = crypto.randomUUID();
79
- const metadata = JSON.stringify(input.metadata || {});
80
-
81
- db.prepare(
82
- `INSERT INTO contracts (id, title, type, status, counterparty, counterparty_email, start_date, end_date, auto_renew, renewal_period, value, currency, file_path, metadata)
83
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
84
- ).run(
85
- id,
86
- input.title,
87
- input.type || "other",
88
- input.status || "draft",
89
- input.counterparty || null,
90
- input.counterparty_email || null,
91
- input.start_date || null,
92
- input.end_date || null,
93
- input.auto_renew ? 1 : 0,
94
- input.renewal_period || null,
95
- input.value ?? null,
96
- input.currency || "USD",
97
- input.file_path || null,
98
- metadata
99
- );
100
-
101
- return getContract(id)!;
102
- }
103
-
104
- export function getContract(id: string): Contract | null {
105
- const db = getDatabase();
106
- const row = db.prepare("SELECT * FROM contracts WHERE id = ?").get(id) as ContractRow | null;
107
- return row ? rowToContract(row) : null;
108
- }
109
-
110
- export interface ListContractsOptions {
111
- search?: string;
112
- type?: ContractType;
113
- status?: ContractStatus;
114
- counterparty?: string;
115
- limit?: number;
116
- offset?: number;
117
- }
118
-
119
- export function listContracts(options: ListContractsOptions = {}): Contract[] {
120
- const db = getDatabase();
121
- const conditions: string[] = [];
122
- const params: unknown[] = [];
123
-
124
- if (options.search) {
125
- conditions.push(
126
- "(title LIKE ? OR counterparty LIKE ? OR counterparty_email LIKE ?)"
127
- );
128
- const q = `%${options.search}%`;
129
- params.push(q, q, q);
130
- }
131
-
132
- if (options.type) {
133
- conditions.push("type = ?");
134
- params.push(options.type);
135
- }
136
-
137
- if (options.status) {
138
- conditions.push("status = ?");
139
- params.push(options.status);
140
- }
141
-
142
- if (options.counterparty) {
143
- conditions.push("counterparty LIKE ?");
144
- params.push(`%${options.counterparty}%`);
145
- }
146
-
147
- let sql = "SELECT * FROM contracts";
148
- if (conditions.length > 0) {
149
- sql += " WHERE " + conditions.join(" AND ");
150
- }
151
- sql += " ORDER BY created_at DESC";
152
-
153
- if (options.limit) {
154
- sql += " LIMIT ?";
155
- params.push(options.limit);
156
- }
157
- if (options.offset) {
158
- sql += " OFFSET ?";
159
- params.push(options.offset);
160
- }
161
-
162
- const rows = db.prepare(sql).all(...params) as ContractRow[];
163
- return rows.map(rowToContract);
164
- }
165
-
166
- export interface UpdateContractInput {
167
- title?: string;
168
- type?: ContractType;
169
- status?: ContractStatus;
170
- counterparty?: string;
171
- counterparty_email?: string;
172
- start_date?: string;
173
- end_date?: string;
174
- auto_renew?: boolean;
175
- renewal_period?: string;
176
- value?: number;
177
- currency?: string;
178
- file_path?: string;
179
- metadata?: Record<string, unknown>;
180
- }
181
-
182
- export function updateContract(
183
- id: string,
184
- input: UpdateContractInput
185
- ): Contract | null {
186
- const db = getDatabase();
187
- const existing = getContract(id);
188
- if (!existing) return null;
189
-
190
- const sets: string[] = [];
191
- const params: unknown[] = [];
192
-
193
- if (input.title !== undefined) {
194
- sets.push("title = ?");
195
- params.push(input.title);
196
- }
197
- if (input.type !== undefined) {
198
- sets.push("type = ?");
199
- params.push(input.type);
200
- }
201
- if (input.status !== undefined) {
202
- sets.push("status = ?");
203
- params.push(input.status);
204
- }
205
- if (input.counterparty !== undefined) {
206
- sets.push("counterparty = ?");
207
- params.push(input.counterparty);
208
- }
209
- if (input.counterparty_email !== undefined) {
210
- sets.push("counterparty_email = ?");
211
- params.push(input.counterparty_email);
212
- }
213
- if (input.start_date !== undefined) {
214
- sets.push("start_date = ?");
215
- params.push(input.start_date);
216
- }
217
- if (input.end_date !== undefined) {
218
- sets.push("end_date = ?");
219
- params.push(input.end_date);
220
- }
221
- if (input.auto_renew !== undefined) {
222
- sets.push("auto_renew = ?");
223
- params.push(input.auto_renew ? 1 : 0);
224
- }
225
- if (input.renewal_period !== undefined) {
226
- sets.push("renewal_period = ?");
227
- params.push(input.renewal_period);
228
- }
229
- if (input.value !== undefined) {
230
- sets.push("value = ?");
231
- params.push(input.value);
232
- }
233
- if (input.currency !== undefined) {
234
- sets.push("currency = ?");
235
- params.push(input.currency);
236
- }
237
- if (input.file_path !== undefined) {
238
- sets.push("file_path = ?");
239
- params.push(input.file_path);
240
- }
241
- if (input.metadata !== undefined) {
242
- sets.push("metadata = ?");
243
- params.push(JSON.stringify(input.metadata));
244
- }
245
-
246
- if (sets.length === 0) return existing;
247
-
248
- // Save version history before updating
249
- saveContractVersion(existing);
250
-
251
- sets.push("updated_at = datetime('now')");
252
- params.push(id);
253
-
254
- db.prepare(
255
- `UPDATE contracts SET ${sets.join(", ")} WHERE id = ?`
256
- ).run(...params);
257
-
258
- return getContract(id);
259
- }
260
-
261
- export function deleteContract(id: string): boolean {
262
- const db = getDatabase();
263
- const result = db.prepare("DELETE FROM contracts WHERE id = ?").run(id);
264
- return result.changes > 0;
265
- }
266
-
267
- export function searchContracts(query: string): Contract[] {
268
- return listContracts({ search: query });
269
- }
270
-
271
- /**
272
- * List contracts expiring within the given number of days
273
- */
274
- export function listExpiring(days: number): Contract[] {
275
- const db = getDatabase();
276
- const rows = db
277
- .prepare(
278
- `SELECT * FROM contracts
279
- WHERE end_date IS NOT NULL
280
- AND status IN ('active', 'pending_signature')
281
- AND date(end_date) <= date('now', '+' || ? || ' days')
282
- AND date(end_date) >= date('now')
283
- ORDER BY end_date ASC`
284
- )
285
- .all(days) as ContractRow[];
286
- return rows.map(rowToContract);
287
- }
288
-
289
- /**
290
- * Renew a contract by extending its end_date based on renewal_period.
291
- * If renewal_period is not set, extends by 1 year.
292
- */
293
- export function renewContract(id: string): Contract | null {
294
- const contract = getContract(id);
295
- if (!contract) return null;
296
-
297
- const period = contract.renewal_period || "1 year";
298
- const baseDate = contract.end_date || new Date().toISOString().split("T")[0];
299
-
300
- const db = getDatabase();
301
- db.prepare(
302
- `UPDATE contracts
303
- SET end_date = date(?, '+' || ?),
304
- status = 'active',
305
- updated_at = datetime('now')
306
- WHERE id = ?`
307
- ).run(baseDate, period, id);
308
-
309
- return getContract(id);
310
- }
311
-
312
- /**
313
- * Get aggregate stats about contracts
314
- */
315
- export function getContractStats(): {
316
- total: number;
317
- by_status: Record<string, number>;
318
- by_type: Record<string, number>;
319
- total_value: number;
320
- expiring_30_days: number;
321
- } {
322
- const db = getDatabase();
323
-
324
- const total = (
325
- db.prepare("SELECT COUNT(*) as count FROM contracts").get() as { count: number }
326
- ).count;
327
-
328
- const statusRows = db
329
- .prepare("SELECT status, COUNT(*) as count FROM contracts GROUP BY status")
330
- .all() as { status: string; count: number }[];
331
- const by_status: Record<string, number> = {};
332
- for (const row of statusRows) {
333
- by_status[row.status] = row.count;
334
- }
335
-
336
- const typeRows = db
337
- .prepare("SELECT type, COUNT(*) as count FROM contracts GROUP BY type")
338
- .all() as { type: string; count: number }[];
339
- const by_type: Record<string, number> = {};
340
- for (const row of typeRows) {
341
- by_type[row.type] = row.count;
342
- }
343
-
344
- const valueRow = db
345
- .prepare("SELECT COALESCE(SUM(value), 0) as total FROM contracts WHERE status = 'active'")
346
- .get() as { total: number };
347
-
348
- const expiringRow = db
349
- .prepare(
350
- `SELECT COUNT(*) as count FROM contracts
351
- WHERE end_date IS NOT NULL
352
- AND status IN ('active', 'pending_signature')
353
- AND date(end_date) <= date('now', '+30 days')
354
- AND date(end_date) >= date('now')`
355
- )
356
- .get() as { count: number };
357
-
358
- return {
359
- total,
360
- by_status,
361
- by_type,
362
- total_value: valueRow.total,
363
- expiring_30_days: expiringRow.count,
364
- };
365
- }
366
-
367
- // --- Clause operations ---
368
-
369
- export interface Clause {
370
- id: string;
371
- contract_id: string;
372
- name: string;
373
- text: string;
374
- type: "standard" | "custom" | "negotiated";
375
- created_at: string;
376
- }
377
-
378
- export interface CreateClauseInput {
379
- contract_id: string;
380
- name: string;
381
- text: string;
382
- type?: "standard" | "custom" | "negotiated";
383
- }
384
-
385
- export function createClause(input: CreateClauseInput): Clause {
386
- const db = getDatabase();
387
- const id = crypto.randomUUID();
388
-
389
- db.prepare(
390
- `INSERT INTO clauses (id, contract_id, name, text, type)
391
- VALUES (?, ?, ?, ?, ?)`
392
- ).run(id, input.contract_id, input.name, input.text, input.type || "standard");
393
-
394
- return getClause(id)!;
395
- }
396
-
397
- export function getClause(id: string): Clause | null {
398
- const db = getDatabase();
399
- const row = db.prepare("SELECT * FROM clauses WHERE id = ?").get(id) as Clause | null;
400
- return row || null;
401
- }
402
-
403
- export function listClauses(contractId: string): Clause[] {
404
- const db = getDatabase();
405
- return db
406
- .prepare("SELECT * FROM clauses WHERE contract_id = ? ORDER BY created_at ASC")
407
- .all(contractId) as Clause[];
408
- }
409
-
410
- export function deleteClause(id: string): boolean {
411
- const db = getDatabase();
412
- const result = db.prepare("DELETE FROM clauses WHERE id = ?").run(id);
413
- return result.changes > 0;
414
- }
415
-
416
- // --- Reminder operations ---
417
-
418
- export interface Reminder {
419
- id: string;
420
- contract_id: string;
421
- remind_at: string;
422
- message: string;
423
- sent: boolean;
424
- created_at: string;
425
- }
426
-
427
- interface ReminderRow {
428
- id: string;
429
- contract_id: string;
430
- remind_at: string;
431
- message: string;
432
- sent: number;
433
- created_at: string;
434
- }
435
-
436
- function rowToReminder(row: ReminderRow): Reminder {
437
- return {
438
- ...row,
439
- sent: row.sent === 1,
440
- };
441
- }
442
-
443
- export interface CreateReminderInput {
444
- contract_id: string;
445
- remind_at: string;
446
- message: string;
447
- }
448
-
449
- export function createReminder(input: CreateReminderInput): Reminder {
450
- const db = getDatabase();
451
- const id = crypto.randomUUID();
452
-
453
- db.prepare(
454
- `INSERT INTO reminders (id, contract_id, remind_at, message)
455
- VALUES (?, ?, ?, ?)`
456
- ).run(id, input.contract_id, input.remind_at, input.message);
457
-
458
- return getReminder(id)!;
459
- }
460
-
461
- export function getReminder(id: string): Reminder | null {
462
- const db = getDatabase();
463
- const row = db.prepare("SELECT * FROM reminders WHERE id = ?").get(id) as ReminderRow | null;
464
- return row ? rowToReminder(row) : null;
465
- }
466
-
467
- export function listReminders(contractId: string): Reminder[] {
468
- const db = getDatabase();
469
- const rows = db
470
- .prepare("SELECT * FROM reminders WHERE contract_id = ? ORDER BY remind_at ASC")
471
- .all(contractId) as ReminderRow[];
472
- return rows.map(rowToReminder);
473
- }
474
-
475
- export function deleteReminder(id: string): boolean {
476
- const db = getDatabase();
477
- const result = db.prepare("DELETE FROM reminders WHERE id = ?").run(id);
478
- return result.changes > 0;
479
- }
480
-
481
- export function listPendingReminders(): Reminder[] {
482
- const db = getDatabase();
483
- const rows = db
484
- .prepare(
485
- `SELECT * FROM reminders
486
- WHERE sent = 0 AND datetime(remind_at) <= datetime('now')
487
- ORDER BY remind_at ASC`
488
- )
489
- .all() as ReminderRow[];
490
- return rows.map(rowToReminder);
491
- }
492
-
493
- export function markReminderSent(id: string): boolean {
494
- const db = getDatabase();
495
- const result = db
496
- .prepare("UPDATE reminders SET sent = 1 WHERE id = ?")
497
- .run(id);
498
- return result.changes > 0;
499
- }
500
-
501
- // --- Obligation operations ---
502
-
503
- export type ObligationStatus = "pending" | "completed" | "overdue";
504
-
505
- export interface Obligation {
506
- id: string;
507
- clause_id: string;
508
- description: string;
509
- due_date: string | null;
510
- status: ObligationStatus;
511
- assigned_to: string | null;
512
- created_at: string;
513
- }
514
-
515
- export interface CreateObligationInput {
516
- clause_id: string;
517
- description: string;
518
- due_date?: string;
519
- assigned_to?: string;
520
- }
521
-
522
- export function createObligation(input: CreateObligationInput): Obligation {
523
- const db = getDatabase();
524
- const id = crypto.randomUUID();
525
-
526
- db.prepare(
527
- `INSERT INTO obligations (id, clause_id, description, due_date, assigned_to)
528
- VALUES (?, ?, ?, ?, ?)`
529
- ).run(id, input.clause_id, input.description, input.due_date || null, input.assigned_to || null);
530
-
531
- return getObligation(id)!;
532
- }
533
-
534
- export function getObligation(id: string): Obligation | null {
535
- const db = getDatabase();
536
- const row = db.prepare("SELECT * FROM obligations WHERE id = ?").get(id) as Obligation | null;
537
- return row || null;
538
- }
539
-
540
- export function listObligations(clauseId: string): Obligation[] {
541
- const db = getDatabase();
542
- return db
543
- .prepare("SELECT * FROM obligations WHERE clause_id = ? ORDER BY created_at ASC")
544
- .all(clauseId) as Obligation[];
545
- }
546
-
547
- export function completeObligation(id: string): Obligation | null {
548
- const db = getDatabase();
549
- const result = db
550
- .prepare("UPDATE obligations SET status = 'completed' WHERE id = ?")
551
- .run(id);
552
- if (result.changes === 0) return null;
553
- return getObligation(id);
554
- }
555
-
556
- export function listOverdueObligations(): Obligation[] {
557
- const db = getDatabase();
558
- // Mark obligations past due_date as overdue, then return them
559
- db.prepare(
560
- `UPDATE obligations SET status = 'overdue'
561
- WHERE status = 'pending' AND due_date IS NOT NULL AND date(due_date) < date('now')`
562
- ).run();
563
-
564
- return db
565
- .prepare(
566
- `SELECT * FROM obligations
567
- WHERE status = 'overdue'
568
- ORDER BY due_date ASC`
569
- )
570
- .all() as Obligation[];
571
- }
572
-
573
- // --- Approval workflow ---
574
-
575
- const APPROVAL_FLOW: Record<string, string> = {
576
- draft: "pending_review",
577
- pending_review: "pending_signature",
578
- pending_signature: "active",
579
- };
580
-
581
- /**
582
- * Submit a draft contract for review: draft -> pending_review
583
- */
584
- export function submitForReview(id: string): Contract | null {
585
- const contract = getContract(id);
586
- if (!contract) return null;
587
- if (contract.status !== "draft") {
588
- throw new Error(`Cannot submit for review: contract status is '${contract.status}', expected 'draft'`);
589
- }
590
- return updateContract(id, { status: "pending_review" });
591
- }
592
-
593
- /**
594
- * Approve a contract: pending_review -> pending_signature -> active
595
- * Advances the contract one step in the approval flow.
596
- */
597
- export function approveContract(id: string): Contract | null {
598
- const contract = getContract(id);
599
- if (!contract) return null;
600
- const nextStatus = APPROVAL_FLOW[contract.status];
601
- if (!nextStatus) {
602
- throw new Error(
603
- `Cannot approve: contract status is '${contract.status}'. Approval flow: draft -> pending_review -> pending_signature -> active`
604
- );
605
- }
606
- return updateContract(id, { status: nextStatus as ContractStatus });
607
- }
608
-
609
- // --- Version history ---
610
-
611
- export interface ContractVersion {
612
- id: string;
613
- contract_id: string;
614
- title: string;
615
- status: string;
616
- value: number | null;
617
- metadata_snapshot: Record<string, unknown>;
618
- changed_at: string;
619
- }
620
-
621
- interface ContractVersionRow {
622
- id: string;
623
- contract_id: string;
624
- title: string;
625
- status: string;
626
- value: number | null;
627
- metadata_snapshot: string;
628
- changed_at: string;
629
- }
630
-
631
- function rowToVersion(row: ContractVersionRow): ContractVersion {
632
- return {
633
- ...row,
634
- metadata_snapshot: JSON.parse(row.metadata_snapshot || "{}"),
635
- };
636
- }
637
-
638
- function saveContractVersion(contract: Contract): void {
639
- const db = getDatabase();
640
- const id = crypto.randomUUID();
641
- db.prepare(
642
- `INSERT INTO contract_versions (id, contract_id, title, status, value, metadata_snapshot)
643
- VALUES (?, ?, ?, ?, ?, ?)`
644
- ).run(
645
- id,
646
- contract.id,
647
- contract.title,
648
- contract.status,
649
- contract.value,
650
- JSON.stringify(contract.metadata)
651
- );
652
- }
653
-
654
- export function getContractHistory(contractId: string): ContractVersion[] {
655
- const db = getDatabase();
656
- const rows = db
657
- .prepare("SELECT * FROM contract_versions WHERE contract_id = ? ORDER BY changed_at ASC")
658
- .all(contractId) as ContractVersionRow[];
659
- return rows.map(rowToVersion);
660
- }
661
-
662
- // --- Signature logging ---
663
-
664
- export type SignatureMethod = "digital" | "wet" | "docusign";
665
-
666
- export interface Signature {
667
- id: string;
668
- contract_id: string;
669
- signer_name: string;
670
- signer_email: string | null;
671
- signed_at: string;
672
- method: SignatureMethod;
673
- }
674
-
675
- export interface RecordSignatureInput {
676
- contract_id: string;
677
- signer_name: string;
678
- signer_email?: string;
679
- method?: SignatureMethod;
680
- }
681
-
682
- export function recordSignature(input: RecordSignatureInput): Signature {
683
- const db = getDatabase();
684
- const id = crypto.randomUUID();
685
-
686
- db.prepare(
687
- `INSERT INTO signatures (id, contract_id, signer_name, signer_email, method)
688
- VALUES (?, ?, ?, ?, ?)`
689
- ).run(id, input.contract_id, input.signer_name, input.signer_email || null, input.method || "digital");
690
-
691
- return getSignature(id)!;
692
- }
693
-
694
- export function getSignature(id: string): Signature | null {
695
- const db = getDatabase();
696
- const row = db.prepare("SELECT * FROM signatures WHERE id = ?").get(id) as Signature | null;
697
- return row || null;
698
- }
699
-
700
- export function listSignatures(contractId: string): Signature[] {
701
- const db = getDatabase();
702
- return db
703
- .prepare("SELECT * FROM signatures WHERE contract_id = ? ORDER BY signed_at ASC")
704
- .all(contractId) as Signature[];
705
- }
706
-
707
- // --- Clause templates (library) ---
708
-
709
- export interface ClauseTemplate {
710
- id: string;
711
- name: string;
712
- text: string;
713
- type: "standard" | "custom" | "negotiated";
714
- created_at: string;
715
- }
716
-
717
- export interface SaveClauseTemplateInput {
718
- name: string;
719
- text: string;
720
- type?: "standard" | "custom" | "negotiated";
721
- }
722
-
723
- export function saveClauseTemplate(input: SaveClauseTemplateInput): ClauseTemplate {
724
- const db = getDatabase();
725
- const id = crypto.randomUUID();
726
-
727
- db.prepare(
728
- `INSERT INTO clause_templates (id, name, text, type)
729
- VALUES (?, ?, ?, ?)`
730
- ).run(id, input.name, input.text, input.type || "standard");
731
-
732
- return getClauseTemplate(id)!;
733
- }
734
-
735
- export function getClauseTemplate(id: string): ClauseTemplate | null {
736
- const db = getDatabase();
737
- const row = db.prepare("SELECT * FROM clause_templates WHERE id = ?").get(id) as ClauseTemplate | null;
738
- return row || null;
739
- }
740
-
741
- export function getClauseTemplateByName(name: string): ClauseTemplate | null {
742
- const db = getDatabase();
743
- const row = db.prepare("SELECT * FROM clause_templates WHERE name = ?").get(name) as ClauseTemplate | null;
744
- return row || null;
745
- }
746
-
747
- export function listClauseTemplates(): ClauseTemplate[] {
748
- const db = getDatabase();
749
- return db
750
- .prepare("SELECT * FROM clause_templates ORDER BY name ASC")
751
- .all() as ClauseTemplate[];
752
- }
753
-
754
- export function addClauseFromTemplate(contractId: string, templateName: string): Clause {
755
- const template = getClauseTemplateByName(templateName);
756
- if (!template) {
757
- throw new Error(`Clause template '${templateName}' not found`);
758
- }
759
- return createClause({
760
- contract_id: contractId,
761
- name: template.name,
762
- text: template.text,
763
- type: template.type,
764
- });
765
- }
766
-
767
- // --- Multi-stage reminders ---
768
-
769
- export function setMultiReminders(
770
- contractId: string,
771
- daysBefore: number[]
772
- ): Reminder[] {
773
- const contract = getContract(contractId);
774
- if (!contract) {
775
- throw new Error(`Contract '${contractId}' not found`);
776
- }
777
- if (!contract.end_date) {
778
- throw new Error(`Contract '${contractId}' has no end_date set`);
779
- }
780
-
781
- const endDate = new Date(contract.end_date);
782
- const reminders: Reminder[] = [];
783
-
784
- for (const days of daysBefore) {
785
- const remindDate = new Date(endDate);
786
- remindDate.setDate(remindDate.getDate() - days);
787
- const remindAt = remindDate.toISOString().split("T")[0] + "T09:00:00";
788
-
789
- const reminder = createReminder({
790
- contract_id: contractId,
791
- remind_at: remindAt,
792
- message: `Contract "${contract.title}" expires in ${days} day(s)`,
793
- });
794
- reminders.push(reminder);
795
- }
796
-
797
- return reminders;
798
- }
799
-
800
- // --- Contract comparison ---
801
-
802
- export interface ContractComparison {
803
- contract1: { id: string; title: string };
804
- contract2: { id: string; title: string };
805
- field_differences: { field: string; contract1_value: unknown; contract2_value: unknown }[];
806
- clause_only_in_1: Clause[];
807
- clause_only_in_2: Clause[];
808
- clause_differences: { name: string; contract1_text: string; contract2_text: string }[];
809
- }
810
-
811
- export function compareContracts(id1: string, id2: string): ContractComparison {
812
- const c1 = getContract(id1);
813
- const c2 = getContract(id2);
814
- if (!c1) throw new Error(`Contract '${id1}' not found`);
815
- if (!c2) throw new Error(`Contract '${id2}' not found`);
816
-
817
- // Compare fields
818
- const fieldsToCompare: (keyof Contract)[] = [
819
- "title", "type", "status", "counterparty", "counterparty_email",
820
- "start_date", "end_date", "auto_renew", "renewal_period",
821
- "value", "currency",
822
- ];
823
- const field_differences: { field: string; contract1_value: unknown; contract2_value: unknown }[] = [];
824
- for (const field of fieldsToCompare) {
825
- const v1 = c1[field];
826
- const v2 = c2[field];
827
- if (JSON.stringify(v1) !== JSON.stringify(v2)) {
828
- field_differences.push({ field, contract1_value: v1, contract2_value: v2 });
829
- }
830
- }
831
-
832
- // Compare clauses by name
833
- const clauses1 = listClauses(id1);
834
- const clauses2 = listClauses(id2);
835
- const names1 = new Set(clauses1.map((c) => c.name));
836
- const names2 = new Set(clauses2.map((c) => c.name));
837
-
838
- const clause_only_in_1 = clauses1.filter((c) => !names2.has(c.name));
839
- const clause_only_in_2 = clauses2.filter((c) => !names1.has(c.name));
840
-
841
- const clause_differences: { name: string; contract1_text: string; contract2_text: string }[] = [];
842
- for (const cl1 of clauses1) {
843
- const cl2 = clauses2.find((c) => c.name === cl1.name);
844
- if (cl2 && cl1.text !== cl2.text) {
845
- clause_differences.push({ name: cl1.name, contract1_text: cl1.text, contract2_text: cl2.text });
846
- }
847
- }
848
-
849
- return {
850
- contract1: { id: c1.id, title: c1.title },
851
- contract2: { id: c2.id, title: c2.title },
852
- field_differences,
853
- clause_only_in_1,
854
- clause_only_in_2,
855
- clause_differences,
856
- };
857
- }
858
-
859
- // --- Markdown export ---
860
-
861
- export function exportContract(id: string, format: "md" | "json" = "md"): string {
862
- const contract = getContract(id);
863
- if (!contract) throw new Error(`Contract '${id}' not found`);
864
-
865
- if (format === "json") {
866
- const clauses = listClauses(id);
867
- const sigs = listSignatures(id);
868
- const reminders = listReminders(id);
869
- return JSON.stringify({ contract, clauses, signatures: sigs, reminders }, null, 2);
870
- }
871
-
872
- // Markdown export
873
- const lines: string[] = [];
874
- lines.push(`# ${contract.title}`);
875
- lines.push("");
876
- lines.push(`| Field | Value |`);
877
- lines.push(`|-------|-------|`);
878
- lines.push(`| Type | ${contract.type} |`);
879
- lines.push(`| Status | ${contract.status} |`);
880
- if (contract.counterparty) lines.push(`| Counterparty | ${contract.counterparty} |`);
881
- if (contract.counterparty_email) lines.push(`| Email | ${contract.counterparty_email} |`);
882
- if (contract.start_date) lines.push(`| Start Date | ${contract.start_date} |`);
883
- if (contract.end_date) lines.push(`| End Date | ${contract.end_date} |`);
884
- if (contract.value !== null) lines.push(`| Value | ${contract.value} ${contract.currency} |`);
885
- if (contract.auto_renew) lines.push(`| Auto-Renew | ${contract.renewal_period || "1 year"} |`);
886
- lines.push(`| Created | ${contract.created_at} |`);
887
- lines.push(`| Updated | ${contract.updated_at} |`);
888
-
889
- const clauses = listClauses(id);
890
- if (clauses.length > 0) {
891
- lines.push("");
892
- lines.push("## Clauses");
893
- lines.push("");
894
- for (const clause of clauses) {
895
- lines.push(`### ${clause.name} (${clause.type})`);
896
- lines.push("");
897
- lines.push(clause.text);
898
- lines.push("");
899
- }
900
- }
901
-
902
- const sigs = listSignatures(id);
903
- if (sigs.length > 0) {
904
- lines.push("## Signatures");
905
- lines.push("");
906
- for (const sig of sigs) {
907
- const email = sig.signer_email ? ` (${sig.signer_email})` : "";
908
- lines.push(`- **${sig.signer_name}**${email} — ${sig.method} — ${sig.signed_at}`);
909
- }
910
- lines.push("");
911
- }
912
-
913
- const reminders = listReminders(id);
914
- if (reminders.length > 0) {
915
- lines.push("## Reminders");
916
- lines.push("");
917
- for (const r of reminders) {
918
- const sent = r.sent ? " (sent)" : "";
919
- lines.push(`- ${r.remind_at} — ${r.message}${sent}`);
920
- }
921
- lines.push("");
922
- }
923
-
924
- return lines.join("\n");
925
- }