@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,377 +0,0 @@
1
- /**
2
- * Platform publisher bridge — connects social microservice to real platform APIs.
3
- *
4
- * Supports X (Twitter) and Meta (Facebook Pages) via direct HTTP calls to their
5
- * public APIs. No dependency on open-connectors at runtime — we replicate only
6
- * the minimal fetch logic needed so the microservice stays self-contained.
7
- */
8
-
9
- import { getPost, getAccount, updatePost, type Post, type Engagement } from "../db/social.js";
10
-
11
- // ---- Interfaces ----
12
-
13
- export interface PublishResult {
14
- platformPostId: string;
15
- url?: string;
16
- }
17
-
18
- export interface PostMetrics {
19
- likes: number;
20
- shares: number;
21
- comments: number;
22
- impressions: number;
23
- clicks: number;
24
- }
25
-
26
- export interface PlatformPublisher {
27
- /** Publish content to the platform. Returns the platform-native post ID. */
28
- publishPost(content: string, mediaIds?: string[]): Promise<PublishResult>;
29
- /** Delete a post by its platform-native ID. */
30
- deletePost(platformPostId: string): Promise<boolean>;
31
- /** Fetch engagement metrics for a published post. */
32
- getPostMetrics(platformPostId: string): Promise<PostMetrics>;
33
- }
34
-
35
- // ---- X (Twitter) Publisher ----
36
-
37
- interface XAuthConfig {
38
- bearerToken?: string;
39
- apiKey?: string;
40
- apiSecret?: string;
41
- accessToken?: string;
42
- accessSecret?: string;
43
- }
44
-
45
- function getXAuth(): XAuthConfig {
46
- return {
47
- bearerToken: process.env.X_BEARER_TOKEN,
48
- apiKey: process.env.X_API_KEY,
49
- apiSecret: process.env.X_API_SECRET,
50
- accessToken: process.env.X_ACCESS_TOKEN,
51
- accessSecret: process.env.X_ACCESS_TOKEN_SECRET,
52
- };
53
- }
54
-
55
- export class XPublisher implements PlatformPublisher {
56
- private readonly baseUrl = "https://api.twitter.com";
57
- private auth: XAuthConfig;
58
-
59
- constructor(auth?: XAuthConfig) {
60
- this.auth = auth || getXAuth();
61
- }
62
-
63
- private getAuthHeader(): string {
64
- if (this.auth.bearerToken) {
65
- return `Bearer ${this.auth.bearerToken}`;
66
- }
67
- // OAuth 2.0 user access token
68
- if (this.auth.accessToken) {
69
- return `Bearer ${this.auth.accessToken}`;
70
- }
71
- throw new Error("X publisher: no valid auth token. Set X_BEARER_TOKEN or X_ACCESS_TOKEN.");
72
- }
73
-
74
- async publishPost(content: string, mediaIds?: string[]): Promise<PublishResult> {
75
- const body: Record<string, unknown> = { text: content };
76
- if (mediaIds?.length) {
77
- body.media = { media_ids: mediaIds };
78
- }
79
-
80
- const res = await fetch(`${this.baseUrl}/2/tweets`, {
81
- method: "POST",
82
- headers: {
83
- Authorization: this.getAuthHeader(),
84
- "Content-Type": "application/json",
85
- },
86
- body: JSON.stringify(body),
87
- });
88
-
89
- if (!res.ok) {
90
- const text = await res.text();
91
- throw new Error(`X API error ${res.status}: ${text}`);
92
- }
93
-
94
- const data = (await res.json()) as { data: { id: string; text: string } };
95
- return {
96
- platformPostId: data.data.id,
97
- url: `https://x.com/i/status/${data.data.id}`,
98
- };
99
- }
100
-
101
- async deletePost(platformPostId: string): Promise<boolean> {
102
- const res = await fetch(`${this.baseUrl}/2/tweets/${platformPostId}`, {
103
- method: "DELETE",
104
- headers: { Authorization: this.getAuthHeader() },
105
- });
106
-
107
- if (!res.ok) {
108
- const text = await res.text();
109
- throw new Error(`X API error ${res.status}: ${text}`);
110
- }
111
-
112
- const data = (await res.json()) as { data: { deleted: boolean } };
113
- return data.data.deleted;
114
- }
115
-
116
- async getPostMetrics(platformPostId: string): Promise<PostMetrics> {
117
- const res = await fetch(
118
- `${this.baseUrl}/2/tweets/${platformPostId}?tweet.fields=public_metrics`,
119
- {
120
- method: "GET",
121
- headers: { Authorization: this.getAuthHeader() },
122
- }
123
- );
124
-
125
- if (!res.ok) {
126
- const text = await res.text();
127
- throw new Error(`X API error ${res.status}: ${text}`);
128
- }
129
-
130
- const data = (await res.json()) as {
131
- data: {
132
- public_metrics: {
133
- like_count: number;
134
- retweet_count: number;
135
- reply_count: number;
136
- impression_count: number;
137
- bookmark_count: number;
138
- };
139
- };
140
- };
141
-
142
- const m = data.data.public_metrics;
143
- return {
144
- likes: m.like_count,
145
- shares: m.retweet_count,
146
- comments: m.reply_count,
147
- impressions: m.impression_count,
148
- clicks: m.bookmark_count, // X doesn't expose link clicks via v2; bookmark is closest proxy
149
- };
150
- }
151
- }
152
-
153
- // ---- Meta (Facebook Pages) Publisher ----
154
-
155
- interface MetaAuthConfig {
156
- accessToken?: string;
157
- pageId?: string;
158
- }
159
-
160
- function getMetaAuth(): MetaAuthConfig {
161
- return {
162
- accessToken: process.env.META_ACCESS_TOKEN,
163
- pageId: process.env.META_PAGE_ID,
164
- };
165
- }
166
-
167
- export class MetaPublisher implements PlatformPublisher {
168
- private readonly baseUrl = "https://graph.facebook.com/v22.0";
169
- private auth: MetaAuthConfig;
170
-
171
- constructor(auth?: MetaAuthConfig) {
172
- this.auth = auth || getMetaAuth();
173
- }
174
-
175
- private requireAuth(): { accessToken: string; pageId: string } {
176
- if (!this.auth.accessToken) {
177
- throw new Error("Meta publisher: META_ACCESS_TOKEN is required.");
178
- }
179
- if (!this.auth.pageId) {
180
- throw new Error("Meta publisher: META_PAGE_ID is required.");
181
- }
182
- return { accessToken: this.auth.accessToken, pageId: this.auth.pageId };
183
- }
184
-
185
- async publishPost(content: string, _mediaIds?: string[]): Promise<PublishResult> {
186
- const { accessToken, pageId } = this.requireAuth();
187
-
188
- const res = await fetch(
189
- `${this.baseUrl}/${pageId}/feed?access_token=${encodeURIComponent(accessToken)}`,
190
- {
191
- method: "POST",
192
- headers: { "Content-Type": "application/json" },
193
- body: JSON.stringify({ message: content }),
194
- }
195
- );
196
-
197
- if (!res.ok) {
198
- const text = await res.text();
199
- throw new Error(`Meta API error ${res.status}: ${text}`);
200
- }
201
-
202
- const data = (await res.json()) as { id: string };
203
- return {
204
- platformPostId: data.id,
205
- url: `https://facebook.com/${data.id}`,
206
- };
207
- }
208
-
209
- async deletePost(platformPostId: string): Promise<boolean> {
210
- const { accessToken } = this.requireAuth();
211
-
212
- const res = await fetch(
213
- `${this.baseUrl}/${platformPostId}?access_token=${encodeURIComponent(accessToken)}`,
214
- { method: "DELETE" }
215
- );
216
-
217
- if (!res.ok) {
218
- const text = await res.text();
219
- throw new Error(`Meta API error ${res.status}: ${text}`);
220
- }
221
-
222
- const data = (await res.json()) as { success: boolean };
223
- return data.success;
224
- }
225
-
226
- async getPostMetrics(platformPostId: string): Promise<PostMetrics> {
227
- const { accessToken } = this.requireAuth();
228
-
229
- const fields = "shares,reactions.summary(true),comments.summary(true),insights.metric(post_impressions,post_clicks)";
230
- const res = await fetch(
231
- `${this.baseUrl}/${platformPostId}?fields=${encodeURIComponent(fields)}&access_token=${encodeURIComponent(accessToken)}`,
232
- { method: "GET" }
233
- );
234
-
235
- if (!res.ok) {
236
- const text = await res.text();
237
- throw new Error(`Meta API error ${res.status}: ${text}`);
238
- }
239
-
240
- const data = (await res.json()) as {
241
- shares?: { count: number };
242
- reactions?: { summary: { total_count: number } };
243
- comments?: { summary: { total_count: number } };
244
- insights?: { data: { name: string; values: { value: number }[] }[] };
245
- };
246
-
247
- let impressions = 0;
248
- let clicks = 0;
249
- if (data.insights?.data) {
250
- for (const metric of data.insights.data) {
251
- if (metric.name === "post_impressions" && metric.values?.[0]) {
252
- impressions = metric.values[0].value;
253
- }
254
- if (metric.name === "post_clicks" && metric.values?.[0]) {
255
- clicks = metric.values[0].value;
256
- }
257
- }
258
- }
259
-
260
- return {
261
- likes: data.reactions?.summary?.total_count ?? 0,
262
- shares: data.shares?.count ?? 0,
263
- comments: data.comments?.summary?.total_count ?? 0,
264
- impressions,
265
- clicks,
266
- };
267
- }
268
- }
269
-
270
- // ---- Factory ----
271
-
272
- const publishers: Record<string, () => PlatformPublisher> = {
273
- x: () => new XPublisher(),
274
- meta: () => new MetaPublisher(),
275
- facebook: () => new MetaPublisher(),
276
- };
277
-
278
- /**
279
- * Get a publisher instance for the given platform string.
280
- * Throws if the platform is not supported.
281
- */
282
- export function getPublisher(platform: string): PlatformPublisher {
283
- const factory = publishers[platform.toLowerCase()];
284
- if (!factory) {
285
- throw new Error(`Unsupported publishing platform: '${platform}'. Supported: ${Object.keys(publishers).join(", ")}`);
286
- }
287
- return factory();
288
- }
289
-
290
- /**
291
- * Check which platform providers have env vars configured.
292
- */
293
- export function checkProviders(): { x: boolean; meta: boolean } {
294
- const xAuth = getXAuth();
295
- const metaAuth = getMetaAuth();
296
-
297
- return {
298
- x: !!(xAuth.bearerToken || (xAuth.accessToken)),
299
- meta: !!(metaAuth.accessToken && metaAuth.pageId),
300
- };
301
- }
302
-
303
- // ---- High-level publish function ----
304
-
305
- /**
306
- * Publish a post to its platform API.
307
- *
308
- * 1. Loads the post and its account from DB
309
- * 2. Gets the appropriate publisher for the account's platform
310
- * 3. Calls the platform API
311
- * 4. Updates the post in DB with platform_post_id and status='published'
312
- *
313
- * Returns the updated post.
314
- */
315
- export async function publishToApi(postId: string): Promise<Post> {
316
- const post = getPost(postId);
317
- if (!post) {
318
- throw new Error(`Post '${postId}' not found.`);
319
- }
320
-
321
- if (post.status === "published") {
322
- throw new Error(`Post '${postId}' is already published.`);
323
- }
324
-
325
- const account = getAccount(post.account_id);
326
- if (!account) {
327
- throw new Error(`Account '${post.account_id}' not found for post '${postId}'.`);
328
- }
329
-
330
- // Map the social account platform to a publisher key
331
- // instagram/threads/bluesky/linkedin don't have publishers yet
332
- const platformKey = account.platform === "x" ? "x" : account.platform;
333
- const publisher = getPublisher(platformKey);
334
-
335
- try {
336
- const result = await publisher.publishPost(post.content, post.media_urls.length ? post.media_urls : undefined);
337
-
338
- const updated = updatePost(postId, {
339
- status: "published",
340
- published_at: new Date().toISOString().replace("T", " ").replace(/\.\d+Z$/, ""),
341
- platform_post_id: result.platformPostId,
342
- });
343
-
344
- return updated!;
345
- } catch (err) {
346
- // Mark as failed with the error info
347
- updatePost(postId, { status: "failed" });
348
- throw err;
349
- }
350
- }
351
-
352
- /**
353
- * Fetch metrics from the platform API and sync them into the DB engagement column.
354
- */
355
- export async function syncPostMetrics(postId: string): Promise<Post> {
356
- const post = getPost(postId);
357
- if (!post) throw new Error(`Post '${postId}' not found.`);
358
- if (!post.platform_post_id) throw new Error(`Post '${postId}' has no platform_post_id.`);
359
-
360
- const account = getAccount(post.account_id);
361
- if (!account) throw new Error(`Account '${post.account_id}' not found.`);
362
-
363
- const platformKey = account.platform === "x" ? "x" : account.platform;
364
- const publisher = getPublisher(platformKey);
365
- const metrics = await publisher.getPostMetrics(post.platform_post_id);
366
-
367
- const engagement: Engagement = {
368
- likes: metrics.likes,
369
- shares: metrics.shares,
370
- comments: metrics.comments,
371
- impressions: metrics.impressions,
372
- clicks: metrics.clicks,
373
- };
374
-
375
- const updated = updatePost(postId, { engagement });
376
- return updated!;
377
- }
@@ -1,229 +0,0 @@
1
- /**
2
- * Scheduled post publishing worker
3
- *
4
- * Checks for posts where scheduled_at <= now AND status = 'scheduled',
5
- * marks them as published, and handles recurrence by creating the next
6
- * scheduled post.
7
- */
8
-
9
- import {
10
- getPost,
11
- listPosts,
12
- updatePost,
13
- createPost,
14
- publishPost,
15
- type Post,
16
- type Recurrence,
17
- } from "../db/social.js";
18
-
19
- // ---- Types ----
20
-
21
- export interface SchedulerStatus {
22
- running: boolean;
23
- lastCheck: string | null;
24
- postsProcessed: number;
25
- errors: number;
26
- }
27
-
28
- export interface ProcessResult {
29
- postId: string;
30
- published: boolean;
31
- error?: string;
32
- nextPostId?: string;
33
- }
34
-
35
- // ---- State ----
36
-
37
- let _interval: ReturnType<typeof setInterval> | null = null;
38
- let _status: SchedulerStatus = {
39
- running: false,
40
- lastCheck: null,
41
- postsProcessed: 0,
42
- errors: 0,
43
- };
44
-
45
- // ---- Core Functions ----
46
-
47
- /**
48
- * Find all posts where scheduled_at <= now AND status = 'scheduled'
49
- */
50
- export function getDuePosts(now?: Date): Post[] {
51
- const currentTime = now || new Date();
52
- const posts = listPosts({ status: "scheduled" });
53
-
54
- return posts.filter((post) => {
55
- if (!post.scheduled_at) return false;
56
- const scheduledTime = new Date(post.scheduled_at.replace(" ", "T") + (post.scheduled_at.includes("Z") ? "" : "Z"));
57
- return scheduledTime <= currentTime;
58
- });
59
- }
60
-
61
- /**
62
- * Attempt to publish a scheduled post.
63
- * Marks the post as published locally. When a publisher module is available,
64
- * it would call the platform API first.
65
- */
66
- export function processScheduledPost(postId: string): ProcessResult {
67
- const post = getPost(postId);
68
- if (!post) {
69
- return { postId, published: false, error: `Post '${postId}' not found` };
70
- }
71
-
72
- if (post.status !== "scheduled") {
73
- return { postId, published: false, error: `Post status is '${post.status}', expected 'scheduled'` };
74
- }
75
-
76
- try {
77
- // Mark as published locally (future: call platform API via publisher.ts)
78
- publishPost(postId);
79
-
80
- const result: ProcessResult = { postId, published: true };
81
-
82
- // Handle recurrence
83
- if (post.recurrence) {
84
- const nextPost = handleRecurrence(postId);
85
- if (nextPost) {
86
- result.nextPostId = nextPost.id;
87
- }
88
- }
89
-
90
- return result;
91
- } catch (err) {
92
- // Mark as failed
93
- updatePost(postId, { status: "failed" });
94
- return {
95
- postId,
96
- published: false,
97
- error: err instanceof Error ? err.message : String(err),
98
- };
99
- }
100
- }
101
-
102
- /**
103
- * If post has recurrence (daily/weekly/biweekly/monthly), create the next
104
- * scheduled post with the same content, account, tags, and media.
105
- */
106
- export function handleRecurrence(postId: string): Post | null {
107
- const post = getPost(postId);
108
- if (!post || !post.recurrence || !post.scheduled_at) return null;
109
-
110
- const nextDate = computeNextDate(post.scheduled_at, post.recurrence);
111
-
112
- const nextPost = createPost({
113
- account_id: post.account_id,
114
- content: post.content,
115
- media_urls: post.media_urls.length > 0 ? post.media_urls : undefined,
116
- status: "scheduled",
117
- scheduled_at: nextDate,
118
- tags: post.tags.length > 0 ? post.tags : undefined,
119
- recurrence: post.recurrence,
120
- });
121
-
122
- return nextPost;
123
- }
124
-
125
- /**
126
- * Compute the next scheduled date based on recurrence type
127
- */
128
- export function computeNextDate(scheduledAt: string, recurrence: Recurrence): string {
129
- const date = new Date(scheduledAt.replace(" ", "T") + (scheduledAt.includes("Z") ? "" : "Z"));
130
-
131
- switch (recurrence) {
132
- case "daily":
133
- date.setUTCDate(date.getUTCDate() + 1);
134
- break;
135
- case "weekly":
136
- date.setUTCDate(date.getUTCDate() + 7);
137
- break;
138
- case "biweekly":
139
- date.setUTCDate(date.getUTCDate() + 14);
140
- break;
141
- case "monthly":
142
- date.setUTCMonth(date.getUTCMonth() + 1);
143
- break;
144
- }
145
-
146
- // Format as YYYY-MM-DD HH:MM:SS to match the existing convention
147
- const y = date.getUTCFullYear();
148
- const m = String(date.getUTCMonth() + 1).padStart(2, "0");
149
- const d = String(date.getUTCDate()).padStart(2, "0");
150
- const hh = String(date.getUTCHours()).padStart(2, "0");
151
- const mm = String(date.getUTCMinutes()).padStart(2, "0");
152
- const ss = String(date.getUTCSeconds()).padStart(2, "0");
153
-
154
- return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
155
- }
156
-
157
- // ---- Scheduler Loop ----
158
-
159
- /**
160
- * Run one check cycle: find due posts and process them.
161
- * Returns an array of results.
162
- */
163
- export function runOnce(now?: Date): ProcessResult[] {
164
- const duePosts = getDuePosts(now);
165
- const results: ProcessResult[] = [];
166
-
167
- for (const post of duePosts) {
168
- const result = processScheduledPost(post.id);
169
- results.push(result);
170
-
171
- if (result.published) {
172
- _status.postsProcessed++;
173
- } else {
174
- _status.errors++;
175
- }
176
- }
177
-
178
- _status.lastCheck = new Date().toISOString();
179
- return results;
180
- }
181
-
182
- /**
183
- * Start the scheduler loop that checks for due posts at the given interval.
184
- */
185
- export function startScheduler(intervalMs: number = 60000): void {
186
- if (_interval) {
187
- throw new Error("Scheduler is already running. Stop it first.");
188
- }
189
-
190
- _status.running = true;
191
- _status.lastCheck = new Date().toISOString();
192
-
193
- // Run immediately on start
194
- runOnce();
195
-
196
- _interval = setInterval(() => {
197
- runOnce();
198
- }, intervalMs);
199
- }
200
-
201
- /**
202
- * Stop the scheduler loop.
203
- */
204
- export function stopScheduler(): void {
205
- if (_interval) {
206
- clearInterval(_interval);
207
- _interval = null;
208
- }
209
- _status.running = false;
210
- }
211
-
212
- /**
213
- * Get the current scheduler status.
214
- */
215
- export function getSchedulerStatus(): SchedulerStatus {
216
- return { ..._status };
217
- }
218
-
219
- /**
220
- * Reset scheduler status counters (useful for testing).
221
- */
222
- export function resetSchedulerStatus(): void {
223
- _status = {
224
- running: _interval !== null,
225
- lastCheck: null,
226
- postsProcessed: 0,
227
- errors: 0,
228
- };
229
- }