@hasna/microservices 0.0.15 → 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 (296) hide show
  1. package/README.md +120 -138
  2. package/bin/index.js +703 -12728
  3. package/bin/mcp.js +250 -9726
  4. package/dist/index.js +170 -9407
  5. package/package.json +25 -25
  6. package/microservices/microservice-ads/package.json +0 -27
  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 -93
  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 -27
  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 -93
  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 -27
  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 -93
  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 -27
  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 -93
  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 -27
  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 -82
  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 -27
  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 -93
  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 -27
  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 -93
  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 -27
  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 -93
  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 -27
  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 -93
  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 -27
  77. package/microservices/microservice-crm/src/cli/index.ts +0 -396
  78. package/microservices/microservice-crm/src/db/database.ts +0 -82
  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 -27
  84. package/microservices/microservice-documents/src/cli/index.ts +0 -246
  85. package/microservices/microservice-documents/src/db/database.ts +0 -82
  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 -27
  91. package/microservices/microservice-domains/src/cli/index.ts +0 -1111
  92. package/microservices/microservice-domains/src/db/database.ts +0 -93
  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 -27
  102. package/microservices/microservice-expenses/src/cli/index.ts +0 -267
  103. package/microservices/microservice-expenses/src/db/database.ts +0 -82
  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 -27
  109. package/microservices/microservice-habits/src/cli/index.ts +0 -315
  110. package/microservices/microservice-habits/src/db/database.ts +0 -93
  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 -27
  116. package/microservices/microservice-health/src/cli/index.ts +0 -484
  117. package/microservices/microservice-health/src/db/database.ts +0 -93
  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 -27
  123. package/microservices/microservice-hiring/src/cli/index.ts +0 -741
  124. package/microservices/microservice-hiring/src/db/database.ts +0 -93
  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 -27
  131. package/microservices/microservice-inventory/src/cli/index.ts +0 -365
  132. package/microservices/microservice-inventory/src/db/database.ts +0 -82
  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/dist/assets/index-Bngq7FNM.css +0 -1
  138. package/microservices/microservice-invoices/dashboard/dist/assets/index-aHW4ARZR.js +0 -124
  139. package/microservices/microservice-invoices/dashboard/dist/index.html +0 -13
  140. package/microservices/microservice-invoices/dashboard/index.html +0 -12
  141. package/microservices/microservice-invoices/dashboard/package.json +0 -29
  142. package/microservices/microservice-invoices/dashboard/tsconfig.json +0 -14
  143. package/microservices/microservice-invoices/dashboard/vite.config.ts +0 -15
  144. package/microservices/microservice-invoices/package.json +0 -30
  145. package/microservices/microservice-invoices/src/cli/index.ts +0 -308
  146. package/microservices/microservice-invoices/src/db/business.ts +0 -241
  147. package/microservices/microservice-invoices/src/db/clients.ts +0 -127
  148. package/microservices/microservice-invoices/src/db/database.ts +0 -82
  149. package/microservices/microservice-invoices/src/db/invoices.ts +0 -345
  150. package/microservices/microservice-invoices/src/db/migrations.ts +0 -184
  151. package/microservices/microservice-invoices/src/index.ts +0 -56
  152. package/microservices/microservice-invoices/src/mcp/index.ts +0 -242
  153. package/microservices/microservice-invoices/src/server/index.ts +0 -162
  154. package/microservices/microservice-leads/package.json +0 -27
  155. package/microservices/microservice-leads/src/cli/index.ts +0 -596
  156. package/microservices/microservice-leads/src/db/database.ts +0 -93
  157. package/microservices/microservice-leads/src/db/leads.ts +0 -520
  158. package/microservices/microservice-leads/src/db/lists.ts +0 -151
  159. package/microservices/microservice-leads/src/db/migrations.ts +0 -93
  160. package/microservices/microservice-leads/src/index.ts +0 -65
  161. package/microservices/microservice-leads/src/lib/enrichment.ts +0 -202
  162. package/microservices/microservice-leads/src/lib/scoring.ts +0 -134
  163. package/microservices/microservice-leads/src/mcp/index.ts +0 -533
  164. package/microservices/microservice-notes/package.json +0 -27
  165. package/microservices/microservice-notes/src/cli/index.ts +0 -63
  166. package/microservices/microservice-notes/src/db/database.ts +0 -93
  167. package/microservices/microservice-notes/src/db/migrations.ts +0 -40
  168. package/microservices/microservice-notes/src/db/notes.ts +0 -114
  169. package/microservices/microservice-notes/src/index.ts +0 -2
  170. package/microservices/microservice-notes/src/mcp/index.ts +0 -37
  171. package/microservices/microservice-notifications/package.json +0 -27
  172. package/microservices/microservice-notifications/src/cli/index.ts +0 -349
  173. package/microservices/microservice-notifications/src/db/database.ts +0 -93
  174. package/microservices/microservice-notifications/src/db/migrations.ts +0 -62
  175. package/microservices/microservice-notifications/src/db/notifications.ts +0 -509
  176. package/microservices/microservice-notifications/src/index.ts +0 -41
  177. package/microservices/microservice-notifications/src/mcp/index.ts +0 -422
  178. package/microservices/microservice-payments/package.json +0 -27
  179. package/microservices/microservice-payments/src/cli/index.ts +0 -609
  180. package/microservices/microservice-payments/src/db/database.ts +0 -93
  181. package/microservices/microservice-payments/src/db/migrations.ts +0 -81
  182. package/microservices/microservice-payments/src/db/payments.ts +0 -1204
  183. package/microservices/microservice-payments/src/index.ts +0 -51
  184. package/microservices/microservice-payments/src/mcp/index.ts +0 -683
  185. package/microservices/microservice-payroll/package.json +0 -27
  186. package/microservices/microservice-payroll/src/cli/index.ts +0 -643
  187. package/microservices/microservice-payroll/src/db/database.ts +0 -93
  188. package/microservices/microservice-payroll/src/db/migrations.ts +0 -95
  189. package/microservices/microservice-payroll/src/db/payroll.ts +0 -1377
  190. package/microservices/microservice-payroll/src/index.ts +0 -48
  191. package/microservices/microservice-payroll/src/mcp/index.ts +0 -666
  192. package/microservices/microservice-products/package.json +0 -27
  193. package/microservices/microservice-products/src/cli/index.ts +0 -416
  194. package/microservices/microservice-products/src/db/categories.ts +0 -154
  195. package/microservices/microservice-products/src/db/database.ts +0 -93
  196. package/microservices/microservice-products/src/db/migrations.ts +0 -58
  197. package/microservices/microservice-products/src/db/pricing-tiers.ts +0 -66
  198. package/microservices/microservice-products/src/db/products.ts +0 -452
  199. package/microservices/microservice-products/src/index.ts +0 -53
  200. package/microservices/microservice-products/src/mcp/index.ts +0 -453
  201. package/microservices/microservice-projects/package.json +0 -27
  202. package/microservices/microservice-projects/src/cli/index.ts +0 -480
  203. package/microservices/microservice-projects/src/db/database.ts +0 -93
  204. package/microservices/microservice-projects/src/db/migrations.ts +0 -65
  205. package/microservices/microservice-projects/src/db/projects.ts +0 -715
  206. package/microservices/microservice-projects/src/index.ts +0 -57
  207. package/microservices/microservice-projects/src/mcp/index.ts +0 -501
  208. package/microservices/microservice-proposals/package.json +0 -27
  209. package/microservices/microservice-proposals/src/cli/index.ts +0 -400
  210. package/microservices/microservice-proposals/src/db/database.ts +0 -93
  211. package/microservices/microservice-proposals/src/db/migrations.ts +0 -52
  212. package/microservices/microservice-proposals/src/db/proposals.ts +0 -532
  213. package/microservices/microservice-proposals/src/index.ts +0 -37
  214. package/microservices/microservice-proposals/src/mcp/index.ts +0 -375
  215. package/microservices/microservice-reading/package.json +0 -27
  216. package/microservices/microservice-reading/src/cli/index.ts +0 -464
  217. package/microservices/microservice-reading/src/db/database.ts +0 -93
  218. package/microservices/microservice-reading/src/db/migrations.ts +0 -59
  219. package/microservices/microservice-reading/src/db/reading.ts +0 -524
  220. package/microservices/microservice-reading/src/index.ts +0 -51
  221. package/microservices/microservice-reading/src/mcp/index.ts +0 -368
  222. package/microservices/microservice-shipping/package.json +0 -27
  223. package/microservices/microservice-shipping/src/cli/index.ts +0 -606
  224. package/microservices/microservice-shipping/src/db/database.ts +0 -93
  225. package/microservices/microservice-shipping/src/db/migrations.ts +0 -69
  226. package/microservices/microservice-shipping/src/db/shipping.ts +0 -1093
  227. package/microservices/microservice-shipping/src/index.ts +0 -53
  228. package/microservices/microservice-shipping/src/mcp/index.ts +0 -533
  229. package/microservices/microservice-social/package.json +0 -28
  230. package/microservices/microservice-social/src/cli/index.ts +0 -1583
  231. package/microservices/microservice-social/src/db/database.ts +0 -93
  232. package/microservices/microservice-social/src/db/migrations.ts +0 -160
  233. package/microservices/microservice-social/src/db/social.ts +0 -1076
  234. package/microservices/microservice-social/src/index.ts +0 -46
  235. package/microservices/microservice-social/src/lib/audience.ts +0 -353
  236. package/microservices/microservice-social/src/lib/content-ai.ts +0 -278
  237. package/microservices/microservice-social/src/lib/media.ts +0 -311
  238. package/microservices/microservice-social/src/lib/mentions.ts +0 -434
  239. package/microservices/microservice-social/src/lib/metrics-sync.ts +0 -264
  240. package/microservices/microservice-social/src/lib/publisher.ts +0 -377
  241. package/microservices/microservice-social/src/lib/scheduler.ts +0 -229
  242. package/microservices/microservice-social/src/lib/sentiment.ts +0 -256
  243. package/microservices/microservice-social/src/lib/threads.ts +0 -291
  244. package/microservices/microservice-social/src/mcp/index.ts +0 -1425
  245. package/microservices/microservice-social/src/server/index.ts +0 -441
  246. package/microservices/microservice-subscriptions/package.json +0 -27
  247. package/microservices/microservice-subscriptions/src/cli/index.ts +0 -715
  248. package/microservices/microservice-subscriptions/src/db/database.ts +0 -93
  249. package/microservices/microservice-subscriptions/src/db/migrations.ts +0 -125
  250. package/microservices/microservice-subscriptions/src/db/subscriptions.ts +0 -1256
  251. package/microservices/microservice-subscriptions/src/index.ts +0 -41
  252. package/microservices/microservice-subscriptions/src/mcp/index.ts +0 -631
  253. package/microservices/microservice-timesheets/package.json +0 -27
  254. package/microservices/microservice-timesheets/src/cli/index.ts +0 -373
  255. package/microservices/microservice-timesheets/src/db/database.ts +0 -82
  256. package/microservices/microservice-timesheets/src/db/locale.ts +0 -217
  257. package/microservices/microservice-timesheets/src/db/migrations.ts +0 -74
  258. package/microservices/microservice-timesheets/src/db/timesheets.ts +0 -447
  259. package/microservices/microservice-timesheets/src/index.ts +0 -44
  260. package/microservices/microservice-timesheets/src/mcp/index.ts +0 -269
  261. package/microservices/microservice-transcriber/package.json +0 -28
  262. package/microservices/microservice-transcriber/src/cli/index.ts +0 -1593
  263. package/microservices/microservice-transcriber/src/db/annotations.ts +0 -37
  264. package/microservices/microservice-transcriber/src/db/comments.ts +0 -166
  265. package/microservices/microservice-transcriber/src/db/database.ts +0 -82
  266. package/microservices/microservice-transcriber/src/db/migrations.ts +0 -118
  267. package/microservices/microservice-transcriber/src/db/proofread.ts +0 -119
  268. package/microservices/microservice-transcriber/src/db/transcripts.ts +0 -395
  269. package/microservices/microservice-transcriber/src/index.ts +0 -43
  270. package/microservices/microservice-transcriber/src/lib/config.ts +0 -77
  271. package/microservices/microservice-transcriber/src/lib/diff.ts +0 -91
  272. package/microservices/microservice-transcriber/src/lib/downloader.ts +0 -638
  273. package/microservices/microservice-transcriber/src/lib/feeds.ts +0 -62
  274. package/microservices/microservice-transcriber/src/lib/live.ts +0 -94
  275. package/microservices/microservice-transcriber/src/lib/notion.ts +0 -129
  276. package/microservices/microservice-transcriber/src/lib/proofread.ts +0 -296
  277. package/microservices/microservice-transcriber/src/lib/providers.ts +0 -713
  278. package/microservices/microservice-transcriber/src/lib/summarizer.ts +0 -147
  279. package/microservices/microservice-transcriber/src/lib/translator.ts +0 -75
  280. package/microservices/microservice-transcriber/src/lib/webhook.ts +0 -37
  281. package/microservices/microservice-transcriber/src/mcp/index.ts +0 -1330
  282. package/microservices/microservice-transcriber/src/server/index.ts +0 -199
  283. package/microservices/microservice-travel/package.json +0 -27
  284. package/microservices/microservice-travel/src/cli/index.ts +0 -505
  285. package/microservices/microservice-travel/src/db/database.ts +0 -93
  286. package/microservices/microservice-travel/src/db/migrations.ts +0 -77
  287. package/microservices/microservice-travel/src/db/travel.ts +0 -802
  288. package/microservices/microservice-travel/src/index.ts +0 -60
  289. package/microservices/microservice-travel/src/mcp/index.ts +0 -495
  290. package/microservices/microservice-wiki/package.json +0 -27
  291. package/microservices/microservice-wiki/src/cli/index.ts +0 -345
  292. package/microservices/microservice-wiki/src/db/database.ts +0 -93
  293. package/microservices/microservice-wiki/src/db/migrations.ts +0 -55
  294. package/microservices/microservice-wiki/src/db/wiki.ts +0 -395
  295. package/microservices/microservice-wiki/src/index.ts +0 -32
  296. package/microservices/microservice-wiki/src/mcp/index.ts +0 -344
@@ -1,93 +0,0 @@
1
- /**
2
- * Database connection for microservice-notifications
3
- */
4
-
5
- import { Database } from "bun:sqlite";
6
- import { existsSync, mkdirSync } from "node:fs";
7
- import { dirname, join, resolve } from "node:path";
8
- import { MIGRATIONS } from "./migrations.js";
9
-
10
- let _db: Database | null = null;
11
-
12
- function getDbPath(): string {
13
- // Environment variable override
14
- if (process.env["MICROSERVICES_DIR"]) {
15
- return join(process.env["MICROSERVICES_DIR"], "microservice-notifications", "data.db");
16
- }
17
-
18
- // Check for .microservices in current or parent directories
19
- let dir = resolve(process.cwd());
20
- while (true) {
21
- const candidate = join(dir, ".microservices", "microservice-notifications", "data.db");
22
- const msDir = join(dir, ".microservices");
23
- if (existsSync(msDir)) return candidate;
24
- const parent = dirname(dir);
25
- if (parent === dir) break;
26
- dir = parent;
27
- }
28
-
29
- // Global fallback
30
- const home = process.env["HOME"] || process.env["USERPROFILE"] || "~";
31
- return join(home, ".microservices", "microservice-notifications", "data.db");
32
- }
33
-
34
- function ensureDir(filePath: string): void {
35
- const dir = dirname(resolve(filePath));
36
- if (!existsSync(dir)) {
37
- mkdirSync(dir, { recursive: true });
38
- }
39
- }
40
-
41
- export function getDatabase(): Database {
42
- if (_db) return _db;
43
-
44
- const dbPath = getDbPath();
45
- ensureDir(dbPath);
46
-
47
- _db = new Database(dbPath);
48
- _db.exec("PRAGMA journal_mode = WAL");
49
- _db.exec("PRAGMA foreign_keys = ON");
50
-
51
- // Create migrations table
52
- _db.exec(`
53
- CREATE TABLE IF NOT EXISTS _migrations (
54
- id INTEGER PRIMARY KEY,
55
- name TEXT NOT NULL,
56
- applied_at TEXT NOT NULL DEFAULT (datetime('now'))
57
- )
58
- `);
59
-
60
- // Apply pending migrations
61
- const applied = _db
62
- .query("SELECT id FROM _migrations ORDER BY id")
63
- .all() as { id: number }[];
64
- const appliedIds = new Set(applied.map((r) => r.id));
65
-
66
- for (const migration of MIGRATIONS) {
67
- if (appliedIds.has(migration.id)) continue;
68
-
69
- _db.exec("BEGIN");
70
- try {
71
- _db.exec(migration.sql);
72
- _db.prepare("INSERT INTO _migrations (id, name) VALUES (?, ?)").run(
73
- migration.id,
74
- migration.name
75
- );
76
- _db.exec("COMMIT");
77
- } catch (error) {
78
- _db.exec("ROLLBACK");
79
- throw new Error(
80
- `Migration ${migration.id} (${migration.name}) failed: ${error instanceof Error ? error.message : String(error)}`
81
- );
82
- }
83
- }
84
-
85
- return _db;
86
- }
87
-
88
- export function closeDatabase(): void {
89
- if (_db) {
90
- _db.close();
91
- _db = null;
92
- }
93
- }
@@ -1,62 +0,0 @@
1
- export interface MigrationEntry {
2
- id: number;
3
- name: string;
4
- sql: string;
5
- }
6
-
7
- export const MIGRATIONS: MigrationEntry[] = [
8
- {
9
- id: 1,
10
- name: "initial_schema",
11
- sql: `
12
- CREATE TABLE IF NOT EXISTS notifications (
13
- id TEXT PRIMARY KEY,
14
- channel TEXT NOT NULL CHECK(channel IN ('email','slack','sms','webhook','in_app')),
15
- recipient TEXT NOT NULL,
16
- subject TEXT,
17
- body TEXT,
18
- status TEXT NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','sent','failed','read')),
19
- source_service TEXT,
20
- source_event TEXT,
21
- priority TEXT NOT NULL DEFAULT 'normal' CHECK(priority IN ('low','normal','high','urgent')),
22
- metadata TEXT NOT NULL DEFAULT '{}',
23
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
24
- sent_at TEXT
25
- );
26
-
27
- CREATE INDEX IF NOT EXISTS idx_notifications_status ON notifications(status);
28
- CREATE INDEX IF NOT EXISTS idx_notifications_channel ON notifications(channel);
29
- CREATE INDEX IF NOT EXISTS idx_notifications_recipient ON notifications(recipient);
30
- CREATE INDEX IF NOT EXISTS idx_notifications_priority ON notifications(priority);
31
- CREATE INDEX IF NOT EXISTS idx_notifications_created_at ON notifications(created_at);
32
-
33
- CREATE TABLE IF NOT EXISTS notification_rules (
34
- id TEXT PRIMARY KEY,
35
- name TEXT,
36
- trigger_event TEXT NOT NULL,
37
- channel TEXT NOT NULL,
38
- recipient TEXT NOT NULL,
39
- template_id TEXT,
40
- enabled INTEGER NOT NULL DEFAULT 1,
41
- metadata TEXT NOT NULL DEFAULT '{}',
42
- created_at TEXT NOT NULL DEFAULT (datetime('now'))
43
- );
44
-
45
- CREATE INDEX IF NOT EXISTS idx_rules_trigger ON notification_rules(trigger_event);
46
- CREATE INDEX IF NOT EXISTS idx_rules_enabled ON notification_rules(enabled);
47
-
48
- CREATE TABLE IF NOT EXISTS notification_templates (
49
- id TEXT PRIMARY KEY,
50
- name TEXT NOT NULL,
51
- channel TEXT,
52
- subject_template TEXT,
53
- body_template TEXT,
54
- variables TEXT NOT NULL DEFAULT '[]',
55
- created_at TEXT NOT NULL DEFAULT (datetime('now'))
56
- );
57
-
58
- CREATE INDEX IF NOT EXISTS idx_templates_name ON notification_templates(name);
59
- CREATE INDEX IF NOT EXISTS idx_templates_channel ON notification_templates(channel);
60
- `,
61
- },
62
- ];
@@ -1,509 +0,0 @@
1
- /**
2
- * Notification CRUD, rules, templates, and event processing
3
- */
4
-
5
- import { getDatabase } from "./database.js";
6
-
7
- // ─── Types ───────────────────────────────────────────────────────────────────
8
-
9
- export type Channel = "email" | "slack" | "sms" | "webhook" | "in_app";
10
- export type NotificationStatus = "pending" | "sent" | "failed" | "read";
11
- export type Priority = "low" | "normal" | "high" | "urgent";
12
-
13
- export interface Notification {
14
- id: string;
15
- channel: Channel;
16
- recipient: string;
17
- subject: string | null;
18
- body: string | null;
19
- status: NotificationStatus;
20
- source_service: string | null;
21
- source_event: string | null;
22
- priority: Priority;
23
- metadata: Record<string, unknown>;
24
- created_at: string;
25
- sent_at: string | null;
26
- }
27
-
28
- interface NotificationRow {
29
- id: string;
30
- channel: string;
31
- recipient: string;
32
- subject: string | null;
33
- body: string | null;
34
- status: string;
35
- source_service: string | null;
36
- source_event: string | null;
37
- priority: string;
38
- metadata: string;
39
- created_at: string;
40
- sent_at: string | null;
41
- }
42
-
43
- function rowToNotification(row: NotificationRow): Notification {
44
- return {
45
- ...row,
46
- channel: row.channel as Channel,
47
- status: row.status as NotificationStatus,
48
- priority: row.priority as Priority,
49
- metadata: JSON.parse(row.metadata || "{}"),
50
- };
51
- }
52
-
53
- export interface NotificationRule {
54
- id: string;
55
- name: string | null;
56
- trigger_event: string;
57
- channel: string;
58
- recipient: string;
59
- template_id: string | null;
60
- enabled: boolean;
61
- metadata: Record<string, unknown>;
62
- created_at: string;
63
- }
64
-
65
- interface RuleRow {
66
- id: string;
67
- name: string | null;
68
- trigger_event: string;
69
- channel: string;
70
- recipient: string;
71
- template_id: string | null;
72
- enabled: number;
73
- metadata: string;
74
- created_at: string;
75
- }
76
-
77
- function rowToRule(row: RuleRow): NotificationRule {
78
- return {
79
- ...row,
80
- enabled: row.enabled === 1,
81
- metadata: JSON.parse(row.metadata || "{}"),
82
- };
83
- }
84
-
85
- export interface NotificationTemplate {
86
- id: string;
87
- name: string;
88
- channel: string | null;
89
- subject_template: string | null;
90
- body_template: string | null;
91
- variables: string[];
92
- created_at: string;
93
- }
94
-
95
- interface TemplateRow {
96
- id: string;
97
- name: string;
98
- channel: string | null;
99
- subject_template: string | null;
100
- body_template: string | null;
101
- variables: string;
102
- created_at: string;
103
- }
104
-
105
- function rowToTemplate(row: TemplateRow): NotificationTemplate {
106
- return {
107
- ...row,
108
- variables: JSON.parse(row.variables || "[]"),
109
- };
110
- }
111
-
112
- // ─── Notifications ───────────────────────────────────────────────────────────
113
-
114
- export interface SendNotificationInput {
115
- channel: Channel;
116
- recipient: string;
117
- subject?: string;
118
- body?: string;
119
- source_service?: string;
120
- source_event?: string;
121
- priority?: Priority;
122
- metadata?: Record<string, unknown>;
123
- }
124
-
125
- export function sendNotification(input: SendNotificationInput): Notification {
126
- const db = getDatabase();
127
- const id = crypto.randomUUID();
128
- const metadata = JSON.stringify(input.metadata || {});
129
-
130
- db.prepare(
131
- `INSERT INTO notifications (id, channel, recipient, subject, body, source_service, source_event, priority, metadata)
132
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
133
- ).run(
134
- id,
135
- input.channel,
136
- input.recipient,
137
- input.subject || null,
138
- input.body || null,
139
- input.source_service || null,
140
- input.source_event || null,
141
- input.priority || "normal",
142
- metadata
143
- );
144
-
145
- return getNotification(id)!;
146
- }
147
-
148
- export function getNotification(id: string): Notification | null {
149
- const db = getDatabase();
150
- const row = db.prepare("SELECT * FROM notifications WHERE id = ?").get(id) as NotificationRow | null;
151
- return row ? rowToNotification(row) : null;
152
- }
153
-
154
- export interface ListNotificationsOptions {
155
- status?: NotificationStatus;
156
- channel?: Channel;
157
- priority?: Priority;
158
- recipient?: string;
159
- limit?: number;
160
- offset?: number;
161
- }
162
-
163
- export function listNotifications(options: ListNotificationsOptions = {}): Notification[] {
164
- const db = getDatabase();
165
- const conditions: string[] = [];
166
- const params: unknown[] = [];
167
-
168
- if (options.status) {
169
- conditions.push("status = ?");
170
- params.push(options.status);
171
- }
172
- if (options.channel) {
173
- conditions.push("channel = ?");
174
- params.push(options.channel);
175
- }
176
- if (options.priority) {
177
- conditions.push("priority = ?");
178
- params.push(options.priority);
179
- }
180
- if (options.recipient) {
181
- conditions.push("recipient = ?");
182
- params.push(options.recipient);
183
- }
184
-
185
- let sql = "SELECT * FROM notifications";
186
- if (conditions.length > 0) {
187
- sql += " WHERE " + conditions.join(" AND ");
188
- }
189
- sql += " ORDER BY created_at DESC";
190
-
191
- if (options.limit) {
192
- sql += " LIMIT ?";
193
- params.push(options.limit);
194
- }
195
- if (options.offset) {
196
- sql += " OFFSET ?";
197
- params.push(options.offset);
198
- }
199
-
200
- const rows = db.prepare(sql).all(...params) as NotificationRow[];
201
- return rows.map(rowToNotification);
202
- }
203
-
204
- export function markRead(id: string): Notification | null {
205
- const db = getDatabase();
206
- const existing = getNotification(id);
207
- if (!existing) return null;
208
-
209
- db.prepare("UPDATE notifications SET status = 'read' WHERE id = ?").run(id);
210
- return getNotification(id);
211
- }
212
-
213
- export function markAllRead(recipient: string): number {
214
- const db = getDatabase();
215
- const result = db.prepare(
216
- "UPDATE notifications SET status = 'read' WHERE recipient = ? AND status != 'read'"
217
- ).run(recipient);
218
- return result.changes;
219
- }
220
-
221
- export function markSent(id: string): Notification | null {
222
- const db = getDatabase();
223
- const existing = getNotification(id);
224
- if (!existing) return null;
225
-
226
- db.prepare(
227
- "UPDATE notifications SET status = 'sent', sent_at = datetime('now') WHERE id = ?"
228
- ).run(id);
229
- return getNotification(id);
230
- }
231
-
232
- export function markFailed(id: string): Notification | null {
233
- const db = getDatabase();
234
- const existing = getNotification(id);
235
- if (!existing) return null;
236
-
237
- db.prepare("UPDATE notifications SET status = 'failed' WHERE id = ?").run(id);
238
- return getNotification(id);
239
- }
240
-
241
- // ─── Rules ───────────────────────────────────────────────────────────────────
242
-
243
- export interface CreateRuleInput {
244
- name?: string;
245
- trigger_event: string;
246
- channel: string;
247
- recipient: string;
248
- template_id?: string;
249
- metadata?: Record<string, unknown>;
250
- }
251
-
252
- export function createRule(input: CreateRuleInput): NotificationRule {
253
- const db = getDatabase();
254
- const id = crypto.randomUUID();
255
- const metadata = JSON.stringify(input.metadata || {});
256
-
257
- db.prepare(
258
- `INSERT INTO notification_rules (id, name, trigger_event, channel, recipient, template_id, metadata)
259
- VALUES (?, ?, ?, ?, ?, ?, ?)`
260
- ).run(
261
- id,
262
- input.name || null,
263
- input.trigger_event,
264
- input.channel,
265
- input.recipient,
266
- input.template_id || null,
267
- metadata
268
- );
269
-
270
- return getRule(id)!;
271
- }
272
-
273
- export function getRule(id: string): NotificationRule | null {
274
- const db = getDatabase();
275
- const row = db.prepare("SELECT * FROM notification_rules WHERE id = ?").get(id) as RuleRow | null;
276
- return row ? rowToRule(row) : null;
277
- }
278
-
279
- export function listRules(): NotificationRule[] {
280
- const db = getDatabase();
281
- const rows = db.prepare("SELECT * FROM notification_rules ORDER BY created_at DESC").all() as RuleRow[];
282
- return rows.map(rowToRule);
283
- }
284
-
285
- export interface UpdateRuleInput {
286
- name?: string;
287
- trigger_event?: string;
288
- channel?: string;
289
- recipient?: string;
290
- template_id?: string | null;
291
- metadata?: Record<string, unknown>;
292
- }
293
-
294
- export function updateRule(id: string, input: UpdateRuleInput): NotificationRule | null {
295
- const db = getDatabase();
296
- const existing = getRule(id);
297
- if (!existing) return null;
298
-
299
- const sets: string[] = [];
300
- const params: unknown[] = [];
301
-
302
- if (input.name !== undefined) {
303
- sets.push("name = ?");
304
- params.push(input.name);
305
- }
306
- if (input.trigger_event !== undefined) {
307
- sets.push("trigger_event = ?");
308
- params.push(input.trigger_event);
309
- }
310
- if (input.channel !== undefined) {
311
- sets.push("channel = ?");
312
- params.push(input.channel);
313
- }
314
- if (input.recipient !== undefined) {
315
- sets.push("recipient = ?");
316
- params.push(input.recipient);
317
- }
318
- if (input.template_id !== undefined) {
319
- sets.push("template_id = ?");
320
- params.push(input.template_id);
321
- }
322
- if (input.metadata !== undefined) {
323
- sets.push("metadata = ?");
324
- params.push(JSON.stringify(input.metadata));
325
- }
326
-
327
- if (sets.length === 0) return existing;
328
-
329
- params.push(id);
330
- db.prepare(`UPDATE notification_rules SET ${sets.join(", ")} WHERE id = ?`).run(...params);
331
-
332
- return getRule(id);
333
- }
334
-
335
- export function deleteRule(id: string): boolean {
336
- const db = getDatabase();
337
- const result = db.prepare("DELETE FROM notification_rules WHERE id = ?").run(id);
338
- return result.changes > 0;
339
- }
340
-
341
- export function enableRule(id: string): NotificationRule | null {
342
- const db = getDatabase();
343
- const existing = getRule(id);
344
- if (!existing) return null;
345
-
346
- db.prepare("UPDATE notification_rules SET enabled = 1 WHERE id = ?").run(id);
347
- return getRule(id);
348
- }
349
-
350
- export function disableRule(id: string): NotificationRule | null {
351
- const db = getDatabase();
352
- const existing = getRule(id);
353
- if (!existing) return null;
354
-
355
- db.prepare("UPDATE notification_rules SET enabled = 0 WHERE id = ?").run(id);
356
- return getRule(id);
357
- }
358
-
359
- // ─── Templates ───────────────────────────────────────────────────────────────
360
-
361
- export interface CreateTemplateInput {
362
- name: string;
363
- channel?: string;
364
- subject_template?: string;
365
- body_template?: string;
366
- variables?: string[];
367
- }
368
-
369
- export function createTemplate(input: CreateTemplateInput): NotificationTemplate {
370
- const db = getDatabase();
371
- const id = crypto.randomUUID();
372
- const variables = JSON.stringify(input.variables || []);
373
-
374
- db.prepare(
375
- `INSERT INTO notification_templates (id, name, channel, subject_template, body_template, variables)
376
- VALUES (?, ?, ?, ?, ?, ?)`
377
- ).run(
378
- id,
379
- input.name,
380
- input.channel || null,
381
- input.subject_template || null,
382
- input.body_template || null,
383
- variables
384
- );
385
-
386
- return getTemplate(id)!;
387
- }
388
-
389
- export function getTemplate(id: string): NotificationTemplate | null {
390
- const db = getDatabase();
391
- const row = db.prepare("SELECT * FROM notification_templates WHERE id = ?").get(id) as TemplateRow | null;
392
- return row ? rowToTemplate(row) : null;
393
- }
394
-
395
- export function listTemplates(): NotificationTemplate[] {
396
- const db = getDatabase();
397
- const rows = db.prepare("SELECT * FROM notification_templates ORDER BY created_at DESC").all() as TemplateRow[];
398
- return rows.map(rowToTemplate);
399
- }
400
-
401
- export function deleteTemplate(id: string): boolean {
402
- const db = getDatabase();
403
- const result = db.prepare("DELETE FROM notification_templates WHERE id = ?").run(id);
404
- return result.changes > 0;
405
- }
406
-
407
- export function renderTemplate(
408
- templateId: string,
409
- variables: Record<string, string>
410
- ): { subject: string | null; body: string | null } | null {
411
- const template = getTemplate(templateId);
412
- if (!template) return null;
413
-
414
- function substitute(text: string | null): string | null {
415
- if (!text) return null;
416
- return text.replace(/\{\{(\w+)\}\}/g, (_, key) => variables[key] ?? `{{${key}}}`);
417
- }
418
-
419
- return {
420
- subject: substitute(template.subject_template),
421
- body: substitute(template.body_template),
422
- };
423
- }
424
-
425
- // ─── Event Processing ────────────────────────────────────────────────────────
426
-
427
- export function processEvent(
428
- event: string,
429
- data: Record<string, string> = {}
430
- ): Notification[] {
431
- const db = getDatabase();
432
- const rows = db.prepare(
433
- "SELECT * FROM notification_rules WHERE trigger_event = ? AND enabled = 1"
434
- ).all(event) as RuleRow[];
435
-
436
- const rules = rows.map(rowToRule);
437
- const notifications: Notification[] = [];
438
-
439
- for (const rule of rules) {
440
- let subject: string | null = null;
441
- let body: string | null = null;
442
-
443
- if (rule.template_id) {
444
- const rendered = renderTemplate(rule.template_id, data);
445
- if (rendered) {
446
- subject = rendered.subject;
447
- body = rendered.body;
448
- }
449
- }
450
-
451
- const notification = sendNotification({
452
- channel: rule.channel as Channel,
453
- recipient: rule.recipient,
454
- subject,
455
- body,
456
- source_service: undefined,
457
- source_event: event,
458
- priority: "normal",
459
- metadata: data,
460
- });
461
-
462
- notifications.push(notification);
463
- }
464
-
465
- return notifications;
466
- }
467
-
468
- // ─── Stats ───────────────────────────────────────────────────────────────────
469
-
470
- export interface NotificationStats {
471
- total: number;
472
- by_channel: Record<string, number>;
473
- by_status: Record<string, number>;
474
- by_priority: Record<string, number>;
475
- }
476
-
477
- export function getNotificationStats(): NotificationStats {
478
- const db = getDatabase();
479
-
480
- const totalRow = db.prepare("SELECT COUNT(*) as count FROM notifications").get() as { count: number };
481
-
482
- const channelRows = db.prepare(
483
- "SELECT channel, COUNT(*) as count FROM notifications GROUP BY channel"
484
- ).all() as { channel: string; count: number }[];
485
-
486
- const statusRows = db.prepare(
487
- "SELECT status, COUNT(*) as count FROM notifications GROUP BY status"
488
- ).all() as { status: string; count: number }[];
489
-
490
- const priorityRows = db.prepare(
491
- "SELECT priority, COUNT(*) as count FROM notifications GROUP BY priority"
492
- ).all() as { priority: string; count: number }[];
493
-
494
- const by_channel: Record<string, number> = {};
495
- for (const row of channelRows) by_channel[row.channel] = row.count;
496
-
497
- const by_status: Record<string, number> = {};
498
- for (const row of statusRows) by_status[row.status] = row.count;
499
-
500
- const by_priority: Record<string, number> = {};
501
- for (const row of priorityRows) by_priority[row.priority] = row.count;
502
-
503
- return {
504
- total: totalRow.count,
505
- by_channel,
506
- by_status,
507
- by_priority,
508
- };
509
- }
@@ -1,41 +0,0 @@
1
- /**
2
- * microservice-notifications — Notification management microservice
3
- */
4
-
5
- export {
6
- sendNotification,
7
- getNotification,
8
- listNotifications,
9
- markRead,
10
- markAllRead,
11
- markSent,
12
- markFailed,
13
- createRule,
14
- getRule,
15
- listRules,
16
- updateRule,
17
- deleteRule,
18
- enableRule,
19
- disableRule,
20
- createTemplate,
21
- getTemplate,
22
- listTemplates,
23
- deleteTemplate,
24
- renderTemplate,
25
- processEvent,
26
- getNotificationStats,
27
- type Notification,
28
- type NotificationRule,
29
- type NotificationTemplate,
30
- type NotificationStats,
31
- type SendNotificationInput,
32
- type ListNotificationsOptions,
33
- type CreateRuleInput,
34
- type UpdateRuleInput,
35
- type CreateTemplateInput,
36
- type Channel,
37
- type NotificationStatus,
38
- type Priority,
39
- } from "./db/notifications.js";
40
-
41
- export { getDatabase, closeDatabase } from "./db/database.js";