@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,434 +0,0 @@
1
- /**
2
- * Mention monitoring — searches platform APIs for @handle mentions,
3
- * stores them locally, and provides reply/read/stats operations.
4
- */
5
-
6
- import { getDatabase } from "../db/database.js";
7
- import { getAccount, listAccounts, type Platform } from "../db/social.js";
8
-
9
- // ---- Types ----
10
-
11
- export type MentionType = "mention" | "reply" | "quote" | "dm";
12
-
13
- export interface Mention {
14
- id: string;
15
- account_id: string;
16
- platform: string;
17
- author: string | null;
18
- author_handle: string | null;
19
- content: string | null;
20
- type: MentionType | null;
21
- platform_post_id: string | null;
22
- sentiment: string | null;
23
- read: boolean;
24
- created_at: string | null;
25
- fetched_at: string;
26
- }
27
-
28
- interface MentionRow {
29
- id: string;
30
- account_id: string;
31
- platform: string;
32
- author: string | null;
33
- author_handle: string | null;
34
- content: string | null;
35
- type: string | null;
36
- platform_post_id: string | null;
37
- sentiment: string | null;
38
- read: number;
39
- created_at: string | null;
40
- fetched_at: string;
41
- }
42
-
43
- function rowToMention(row: MentionRow): Mention {
44
- return {
45
- ...row,
46
- type: (row.type as MentionType) || null,
47
- read: row.read === 1,
48
- };
49
- }
50
-
51
- // ---- CRUD ----
52
-
53
- export interface CreateMentionInput {
54
- account_id: string;
55
- platform: string;
56
- author?: string;
57
- author_handle?: string;
58
- content?: string;
59
- type?: MentionType;
60
- platform_post_id?: string;
61
- sentiment?: string;
62
- created_at?: string;
63
- }
64
-
65
- export function createMention(input: CreateMentionInput): Mention {
66
- const db = getDatabase();
67
- const id = crypto.randomUUID();
68
-
69
- db.prepare(
70
- `INSERT INTO mentions (id, account_id, platform, author, author_handle, content, type, platform_post_id, sentiment, created_at)
71
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
72
- ).run(
73
- id,
74
- input.account_id,
75
- input.platform,
76
- input.author || null,
77
- input.author_handle || null,
78
- input.content || null,
79
- input.type || null,
80
- input.platform_post_id || null,
81
- input.sentiment || null,
82
- input.created_at || null
83
- );
84
-
85
- return getMention(id)!;
86
- }
87
-
88
- export function getMention(id: string): Mention | null {
89
- const db = getDatabase();
90
- const row = db.prepare("SELECT * FROM mentions WHERE id = ?").get(id) as MentionRow | null;
91
- return row ? rowToMention(row) : null;
92
- }
93
-
94
- export interface ListMentionsOptions {
95
- account_id?: string;
96
- unread?: boolean;
97
- type?: MentionType;
98
- platform?: string;
99
- limit?: number;
100
- offset?: number;
101
- }
102
-
103
- export function listMentions(accountId?: string, filters: Omit<ListMentionsOptions, "account_id"> = {}): Mention[] {
104
- const db = getDatabase();
105
- const conditions: string[] = [];
106
- const params: unknown[] = [];
107
-
108
- if (accountId) {
109
- conditions.push("account_id = ?");
110
- params.push(accountId);
111
- }
112
-
113
- if (filters.unread !== undefined) {
114
- conditions.push("read = ?");
115
- params.push(filters.unread ? 0 : 1);
116
- }
117
-
118
- if (filters.type) {
119
- conditions.push("type = ?");
120
- params.push(filters.type);
121
- }
122
-
123
- if (filters.platform) {
124
- conditions.push("platform = ?");
125
- params.push(filters.platform);
126
- }
127
-
128
- let sql = "SELECT * FROM mentions";
129
- if (conditions.length > 0) {
130
- sql += " WHERE " + conditions.join(" AND ");
131
- }
132
- sql += " ORDER BY fetched_at DESC";
133
-
134
- if (filters.limit) {
135
- sql += " LIMIT ?";
136
- params.push(filters.limit);
137
- }
138
- if (filters.offset) {
139
- sql += " OFFSET ?";
140
- params.push(filters.offset);
141
- }
142
-
143
- const rows = db.prepare(sql).all(...params) as MentionRow[];
144
- return rows.map(rowToMention);
145
- }
146
-
147
- export function markRead(id: string): Mention | null {
148
- const db = getDatabase();
149
- const existing = getMention(id);
150
- if (!existing) return null;
151
-
152
- db.prepare("UPDATE mentions SET read = 1 WHERE id = ?").run(id);
153
- return getMention(id);
154
- }
155
-
156
- export function markAllRead(accountId: string): number {
157
- const db = getDatabase();
158
- const result = db.prepare("UPDATE mentions SET read = 1 WHERE account_id = ? AND read = 0").run(accountId);
159
- return result.changes;
160
- }
161
-
162
- // ---- Stats ----
163
-
164
- export interface MentionStats {
165
- total: number;
166
- unread: number;
167
- by_type: Record<string, number>;
168
- by_sentiment: Record<string, number>;
169
- }
170
-
171
- export function getMentionStats(accountId: string): MentionStats {
172
- const db = getDatabase();
173
-
174
- const totalRow = db.prepare("SELECT COUNT(*) as count FROM mentions WHERE account_id = ?").get(accountId) as { count: number };
175
- const unreadRow = db.prepare("SELECT COUNT(*) as count FROM mentions WHERE account_id = ? AND read = 0").get(accountId) as { count: number };
176
-
177
- const typeRows = db.prepare(
178
- "SELECT type, COUNT(*) as count FROM mentions WHERE account_id = ? AND type IS NOT NULL GROUP BY type"
179
- ).all(accountId) as { type: string; count: number }[];
180
-
181
- const sentimentRows = db.prepare(
182
- "SELECT sentiment, COUNT(*) as count FROM mentions WHERE account_id = ? AND sentiment IS NOT NULL GROUP BY sentiment"
183
- ).all(accountId) as { sentiment: string; count: number }[];
184
-
185
- const by_type: Record<string, number> = {};
186
- for (const row of typeRows) {
187
- by_type[row.type] = row.count;
188
- }
189
-
190
- const by_sentiment: Record<string, number> = {};
191
- for (const row of sentimentRows) {
192
- by_sentiment[row.sentiment] = row.count;
193
- }
194
-
195
- return {
196
- total: totalRow.count,
197
- unread: unreadRow.count,
198
- by_type,
199
- by_sentiment,
200
- };
201
- }
202
-
203
- // ---- Platform API search ----
204
-
205
- /**
206
- * Search for @handle mentions on a platform via its API.
207
- * X: GET /2/tweets/search/recent?query=@handle
208
- * Meta: GET /{page-id}/tagged
209
- *
210
- * Returns raw mention data that can be stored with createMention().
211
- */
212
- export async function searchMentions(accountId: string): Promise<CreateMentionInput[]> {
213
- const account = getAccount(accountId);
214
- if (!account) throw new Error(`Account '${accountId}' not found.`);
215
-
216
- if (account.platform === "x") {
217
- return searchXMentions(account.id, account.handle);
218
- } else if (account.platform === "instagram") {
219
- return searchMetaMentions(account.id, account.platform);
220
- }
221
-
222
- throw new Error(`Mention search not supported for platform '${account.platform}'.`);
223
- }
224
-
225
- async function searchXMentions(accountId: string, handle: string): Promise<CreateMentionInput[]> {
226
- const bearerToken = process.env.X_BEARER_TOKEN || process.env.X_ACCESS_TOKEN;
227
- if (!bearerToken) throw new Error("X API: no bearer token configured. Set X_BEARER_TOKEN.");
228
-
229
- const query = encodeURIComponent(`@${handle}`);
230
- const res = await fetch(
231
- `https://api.twitter.com/2/tweets/search/recent?query=${query}&tweet.fields=author_id,created_at,in_reply_to_user_id&expansions=author_id&user.fields=name,username`,
232
- {
233
- headers: { Authorization: `Bearer ${bearerToken}` },
234
- }
235
- );
236
-
237
- if (!res.ok) {
238
- const text = await res.text();
239
- throw new Error(`X API error ${res.status}: ${text}`);
240
- }
241
-
242
- const data = (await res.json()) as {
243
- data?: { id: string; text: string; author_id: string; created_at?: string; in_reply_to_user_id?: string }[];
244
- includes?: { users?: { id: string; name: string; username: string }[] };
245
- };
246
-
247
- if (!data.data) return [];
248
-
249
- const userMap = new Map<string, { name: string; username: string }>();
250
- if (data.includes?.users) {
251
- for (const u of data.includes.users) {
252
- userMap.set(u.id, { name: u.name, username: u.username });
253
- }
254
- }
255
-
256
- return data.data.map((tweet) => {
257
- const user = userMap.get(tweet.author_id);
258
- const mentionType: MentionType = tweet.in_reply_to_user_id ? "reply" : "mention";
259
-
260
- return {
261
- account_id: accountId,
262
- platform: "x",
263
- author: user?.name || null,
264
- author_handle: user?.username || null,
265
- content: tweet.text,
266
- type: mentionType,
267
- platform_post_id: tweet.id,
268
- created_at: tweet.created_at || null,
269
- } as CreateMentionInput;
270
- });
271
- }
272
-
273
- async function searchMetaMentions(accountId: string, platform: string): Promise<CreateMentionInput[]> {
274
- const accessToken = process.env.META_ACCESS_TOKEN;
275
- const pageId = process.env.META_PAGE_ID;
276
- if (!accessToken || !pageId) throw new Error("Meta API: META_ACCESS_TOKEN and META_PAGE_ID required.");
277
-
278
- const res = await fetch(
279
- `https://graph.facebook.com/v22.0/${pageId}/tagged?access_token=${encodeURIComponent(accessToken)}&fields=id,message,from,created_time`,
280
- { method: "GET" }
281
- );
282
-
283
- if (!res.ok) {
284
- const text = await res.text();
285
- throw new Error(`Meta API error ${res.status}: ${text}`);
286
- }
287
-
288
- const data = (await res.json()) as {
289
- data?: { id: string; message?: string; from?: { name: string; id: string }; created_time?: string }[];
290
- };
291
-
292
- if (!data.data) return [];
293
-
294
- return data.data.map((post) => ({
295
- account_id: accountId,
296
- platform,
297
- author: post.from?.name || null,
298
- author_handle: post.from?.id || null,
299
- content: post.message || null,
300
- type: "mention" as MentionType,
301
- platform_post_id: post.id,
302
- created_at: post.created_time || null,
303
- }));
304
- }
305
-
306
- // ---- Reply ----
307
-
308
- /**
309
- * Reply to a mention via the platform API.
310
- * X: POST /2/tweets with in_reply_to_tweet_id
311
- * Meta: POST /{post-id}/comments
312
- */
313
- export async function replyToMention(mentionId: string, content: string): Promise<{ platformReplyId: string }> {
314
- const mention = getMention(mentionId);
315
- if (!mention) throw new Error(`Mention '${mentionId}' not found.`);
316
- if (!mention.platform_post_id) throw new Error(`Mention '${mentionId}' has no platform_post_id to reply to.`);
317
-
318
- const account = getAccount(mention.account_id);
319
- if (!account) throw new Error(`Account '${mention.account_id}' not found.`);
320
-
321
- if (mention.platform === "x") {
322
- return replyOnX(mention.platform_post_id, content);
323
- } else if (mention.platform === "instagram" || mention.platform === "facebook") {
324
- return replyOnMeta(mention.platform_post_id, content);
325
- }
326
-
327
- throw new Error(`Reply not supported for platform '${mention.platform}'.`);
328
- }
329
-
330
- async function replyOnX(inReplyToId: string, content: string): Promise<{ platformReplyId: string }> {
331
- const bearerToken = process.env.X_BEARER_TOKEN || process.env.X_ACCESS_TOKEN;
332
- if (!bearerToken) throw new Error("X API: no bearer token configured.");
333
-
334
- const res = await fetch("https://api.twitter.com/2/tweets", {
335
- method: "POST",
336
- headers: {
337
- Authorization: `Bearer ${bearerToken}`,
338
- "Content-Type": "application/json",
339
- },
340
- body: JSON.stringify({
341
- text: content,
342
- reply: { in_reply_to_tweet_id: inReplyToId },
343
- }),
344
- });
345
-
346
- if (!res.ok) {
347
- const text = await res.text();
348
- throw new Error(`X API error ${res.status}: ${text}`);
349
- }
350
-
351
- const data = (await res.json()) as { data: { id: string } };
352
- return { platformReplyId: data.data.id };
353
- }
354
-
355
- async function replyOnMeta(postId: string, content: string): Promise<{ platformReplyId: string }> {
356
- const accessToken = process.env.META_ACCESS_TOKEN;
357
- if (!accessToken) throw new Error("Meta API: META_ACCESS_TOKEN required.");
358
-
359
- const res = await fetch(
360
- `https://graph.facebook.com/v22.0/${postId}/comments?access_token=${encodeURIComponent(accessToken)}`,
361
- {
362
- method: "POST",
363
- headers: { "Content-Type": "application/json" },
364
- body: JSON.stringify({ message: content }),
365
- }
366
- );
367
-
368
- if (!res.ok) {
369
- const text = await res.text();
370
- throw new Error(`Meta API error ${res.status}: ${text}`);
371
- }
372
-
373
- const data = (await res.json()) as { id: string };
374
- return { platformReplyId: data.id };
375
- }
376
-
377
- // ---- Polling ----
378
-
379
- let _pollTimer: ReturnType<typeof setInterval> | null = null;
380
- let _pollRunning = false;
381
-
382
- /**
383
- * Start a background poller that periodically fetches new mentions for all
384
- * connected accounts and inserts them into the DB (deduped by platform_post_id).
385
- */
386
- export function pollMentions(intervalMs = 120000): void {
387
- if (_pollRunning) throw new Error("Mention poller is already running.");
388
-
389
- _pollRunning = true;
390
-
391
- // Run immediately, then on interval
392
- _runPollCycle();
393
- _pollTimer = setInterval(_runPollCycle, intervalMs);
394
- }
395
-
396
- export function stopPolling(): void {
397
- if (_pollTimer) {
398
- clearInterval(_pollTimer);
399
- _pollTimer = null;
400
- }
401
- _pollRunning = false;
402
- }
403
-
404
- export function isPolling(): boolean {
405
- return _pollRunning;
406
- }
407
-
408
- async function _runPollCycle(): Promise<void> {
409
- const accounts = listAccounts({ connected: true });
410
-
411
- for (const account of accounts) {
412
- try {
413
- // Only poll platforms that support mention search
414
- if (account.platform !== "x" && account.platform !== "instagram") continue;
415
-
416
- const mentions = await searchMentions(account.id);
417
-
418
- for (const m of mentions) {
419
- // Dedup: skip if platform_post_id already exists
420
- if (m.platform_post_id) {
421
- const db = getDatabase();
422
- const existing = db.prepare(
423
- "SELECT id FROM mentions WHERE account_id = ? AND platform_post_id = ?"
424
- ).get(account.id, m.platform_post_id);
425
- if (existing) continue;
426
- }
427
-
428
- createMention(m);
429
- }
430
- } catch (_err) {
431
- // Silently continue — poll cycle errors should not crash the poller
432
- }
433
- }
434
- }
@@ -1,264 +0,0 @@
1
- /**
2
- * Metrics sync worker — pulls engagement data from platform APIs
3
- * for published posts and account follower counts.
4
- *
5
- * Uses publisher.syncPostMetrics() under the hood for individual posts,
6
- * and wraps it in a polling loop with error tracking.
7
- */
8
-
9
- import {
10
- listPosts,
11
- listAccounts,
12
- getAccount,
13
- updateAccount,
14
- updatePost,
15
- type Post,
16
- type Account,
17
- } from "../db/social.js";
18
- import { getDatabase } from "../db/database.js";
19
-
20
- // ---- Types ----
21
-
22
- export interface SyncReport {
23
- posts_synced: number;
24
- accounts_synced: number;
25
- last_sync: string | null;
26
- errors: SyncError[];
27
- }
28
-
29
- export interface SyncError {
30
- type: "post" | "account";
31
- id: string;
32
- message: string;
33
- timestamp: string;
34
- }
35
-
36
- export interface MetricsSyncStatus {
37
- running: boolean;
38
- interval_ms: number;
39
- last_sync: string | null;
40
- posts_synced: number;
41
- accounts_synced: number;
42
- errors: number;
43
- }
44
-
45
- // ---- State ----
46
-
47
- let _interval: ReturnType<typeof setInterval> | null = null;
48
- let _status: MetricsSyncStatus = {
49
- running: false,
50
- interval_ms: 0,
51
- last_sync: null,
52
- posts_synced: 0,
53
- accounts_synced: 0,
54
- errors: 0,
55
- };
56
- let _errors: SyncError[] = [];
57
-
58
- // ---- Core Functions ----
59
-
60
- /**
61
- * Get published posts from the last N days that need metrics sync.
62
- */
63
- export function getRecentPublishedPosts(days: number = 7): Post[] {
64
- const cutoff = new Date();
65
- cutoff.setDate(cutoff.getDate() - days);
66
- const cutoffStr = cutoff.toISOString().replace("T", " ").replace(/\.\d+Z$/, "");
67
-
68
- const db = getDatabase();
69
- const rows = db
70
- .prepare(
71
- `SELECT * FROM posts
72
- WHERE status = 'published'
73
- AND published_at IS NOT NULL
74
- AND published_at >= ?
75
- AND platform_post_id IS NOT NULL
76
- ORDER BY published_at DESC`
77
- )
78
- .all(cutoffStr) as Array<{
79
- id: string;
80
- account_id: string;
81
- content: string;
82
- media_urls: string;
83
- status: string;
84
- scheduled_at: string | null;
85
- published_at: string | null;
86
- platform_post_id: string | null;
87
- engagement: string;
88
- tags: string;
89
- recurrence: string | null;
90
- last_metrics_sync: string | null;
91
- created_at: string;
92
- updated_at: string;
93
- }>;
94
-
95
- return rows.map((row) => ({
96
- id: row.id,
97
- account_id: row.account_id,
98
- content: row.content,
99
- media_urls: JSON.parse(row.media_urls || "[]"),
100
- status: row.status as Post["status"],
101
- scheduled_at: row.scheduled_at,
102
- published_at: row.published_at,
103
- platform_post_id: row.platform_post_id,
104
- engagement: JSON.parse(row.engagement || "{}"),
105
- tags: JSON.parse(row.tags || "[]"),
106
- recurrence: (row.recurrence as Post["recurrence"]) || null,
107
- created_at: row.created_at,
108
- updated_at: row.updated_at,
109
- }));
110
- }
111
-
112
- /**
113
- * Sync metrics for all published posts from the last 7 days.
114
- * Calls publisher.syncPostMetrics(postId) for each post.
115
- */
116
- export async function syncAllMetrics(): Promise<SyncReport> {
117
- const posts = getRecentPublishedPosts(7);
118
- const errors: SyncError[] = [];
119
- let posts_synced = 0;
120
-
121
- for (const post of posts) {
122
- try {
123
- const { syncPostMetrics } = await import("./publisher.js");
124
- await syncPostMetrics(post.id);
125
-
126
- // Update last_metrics_sync timestamp
127
- const db = getDatabase();
128
- const now = new Date().toISOString().replace("T", " ").replace(/\.\d+Z$/, "");
129
- db.prepare("UPDATE posts SET last_metrics_sync = ? WHERE id = ?").run(now, post.id);
130
-
131
- posts_synced++;
132
- } catch (err) {
133
- const error: SyncError = {
134
- type: "post",
135
- id: post.id,
136
- message: err instanceof Error ? err.message : String(err),
137
- timestamp: new Date().toISOString(),
138
- };
139
- errors.push(error);
140
- }
141
- }
142
-
143
- const now = new Date().toISOString();
144
- _status.posts_synced += posts_synced;
145
- _status.errors += errors.length;
146
- _status.last_sync = now;
147
- _errors.push(...errors);
148
-
149
- return {
150
- posts_synced,
151
- accounts_synced: 0,
152
- last_sync: now,
153
- errors,
154
- };
155
- }
156
-
157
- /**
158
- * Pull follower count from platform API and store in account metadata.
159
- * Since we don't have a dedicated "get user profile" API call in the publisher
160
- * interface, we store a follower_count field in the account's metadata JSON.
161
- */
162
- export async function syncAccountMetrics(accountId: string): Promise<Account | null> {
163
- const account = getAccount(accountId);
164
- if (!account) return null;
165
-
166
- try {
167
- // Attempt to get publisher for this platform
168
- const { getPublisher } = await import("./publisher.js");
169
- const publisher = getPublisher(account.platform);
170
-
171
- // The publisher interface doesn't expose a getUserProfile method,
172
- // so we simulate by storing a sync timestamp in metadata.
173
- // In a real implementation, each publisher would have a getProfile() method.
174
- const metadata = {
175
- ...account.metadata,
176
- last_metrics_sync: new Date().toISOString(),
177
- };
178
-
179
- const updated = updateAccount(accountId, { metadata });
180
- _status.accounts_synced++;
181
- return updated;
182
- } catch (err) {
183
- const error: SyncError = {
184
- type: "account",
185
- id: accountId,
186
- message: err instanceof Error ? err.message : String(err),
187
- timestamp: new Date().toISOString(),
188
- };
189
- _errors.push(error);
190
- _status.errors++;
191
-
192
- // Still update metadata with error info
193
- const metadata = {
194
- ...account.metadata,
195
- last_metrics_sync_error: err instanceof Error ? err.message : String(err),
196
- last_metrics_sync_attempt: new Date().toISOString(),
197
- };
198
- return updateAccount(accountId, { metadata });
199
- }
200
- }
201
-
202
- /**
203
- * Start the metrics sync polling loop.
204
- */
205
- export function startMetricsSync(intervalMs: number = 300000): void {
206
- if (_interval) {
207
- throw new Error("Metrics sync is already running. Stop it first.");
208
- }
209
-
210
- _status.running = true;
211
- _status.interval_ms = intervalMs;
212
-
213
- // Run immediately on start
214
- syncAllMetrics().catch(() => {});
215
-
216
- _interval = setInterval(() => {
217
- syncAllMetrics().catch(() => {});
218
- }, intervalMs);
219
- }
220
-
221
- /**
222
- * Stop the metrics sync polling loop.
223
- */
224
- export function stopMetricsSync(): void {
225
- if (_interval) {
226
- clearInterval(_interval);
227
- _interval = null;
228
- }
229
- _status.running = false;
230
- }
231
-
232
- /**
233
- * Get the current metrics sync status.
234
- */
235
- export function getMetricsSyncStatus(): MetricsSyncStatus {
236
- return { ..._status };
237
- }
238
-
239
- /**
240
- * Get a full sync report including recent errors.
241
- */
242
- export function getSyncReport(): SyncReport {
243
- return {
244
- posts_synced: _status.posts_synced,
245
- accounts_synced: _status.accounts_synced,
246
- last_sync: _status.last_sync,
247
- errors: [..._errors],
248
- };
249
- }
250
-
251
- /**
252
- * Reset metrics sync state (useful for testing).
253
- */
254
- export function resetMetricsSyncStatus(): void {
255
- _status = {
256
- running: _interval !== null,
257
- interval_ms: _status.interval_ms,
258
- last_sync: null,
259
- posts_synced: 0,
260
- accounts_synced: 0,
261
- errors: 0,
262
- };
263
- _errors = [];
264
- }