@goscribe/server 1.3.4 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (383) hide show
  1. package/.env.example +12 -0
  2. package/.vscode/settings.json +3 -0
  3. package/REFACTOR_NOTES.md +60 -0
  4. package/TESTING_PROMPT.md +225 -0
  5. package/dist/context.d.ts +14 -1
  6. package/dist/context.js +23 -2
  7. package/dist/controllers/admin.controller.d.ts +715 -0
  8. package/dist/controllers/admin.controller.js +9 -0
  9. package/dist/controllers/annotations.controller.d.ts +439 -0
  10. package/dist/controllers/annotations.controller.js +9 -0
  11. package/dist/controllers/app-router.controller.d.ts +3011 -0
  12. package/dist/controllers/app-router.controller.js +38 -0
  13. package/dist/controllers/app-router.controller.test.d.ts +1 -0
  14. package/dist/controllers/app-router.controller.test.js +36 -0
  15. package/dist/controllers/auth.controller.d.ts +323 -0
  16. package/dist/controllers/auth.controller.js +9 -0
  17. package/dist/controllers/base.controller.d.ts +4 -0
  18. package/dist/controllers/base.controller.js +5 -0
  19. package/dist/controllers/chat.controller.d.ts +341 -0
  20. package/dist/controllers/chat.controller.js +9 -0
  21. package/dist/controllers/copilot.controller.d.ts +397 -0
  22. package/dist/controllers/copilot.controller.js +9 -0
  23. package/dist/controllers/flashcards.controller.d.ts +651 -0
  24. package/dist/controllers/flashcards.controller.js +9 -0
  25. package/dist/controllers/members.controller.d.ts +339 -0
  26. package/dist/controllers/members.controller.js +9 -0
  27. package/dist/controllers/notifications.controller.d.ts +199 -0
  28. package/dist/controllers/notifications.controller.js +9 -0
  29. package/dist/controllers/payment.controller.d.ts +181 -0
  30. package/dist/controllers/payment.controller.js +9 -0
  31. package/dist/controllers/podcast.controller.d.ts +575 -0
  32. package/dist/controllers/podcast.controller.js +9 -0
  33. package/dist/controllers/router-module.controller.d.ts +5 -0
  34. package/dist/controllers/router-module.controller.js +6 -0
  35. package/dist/controllers/studyguide.controller.d.ts +73 -0
  36. package/dist/controllers/studyguide.controller.js +9 -0
  37. package/dist/controllers/worksheets.controller.d.ts +829 -0
  38. package/dist/controllers/worksheets.controller.js +9 -0
  39. package/dist/controllers/workspace.controller.d.ts +1207 -0
  40. package/dist/controllers/workspace.controller.js +9 -0
  41. package/dist/lib/activity_human_description.test.js +16 -15
  42. package/dist/lib/activity_log_service.test.js +28 -23
  43. package/dist/lib/ai/config.d.ts +20 -0
  44. package/dist/lib/ai/config.js +31 -0
  45. package/dist/lib/ai/embedding-client.d.ts +8 -0
  46. package/dist/lib/ai/embedding-client.js +30 -0
  47. package/dist/lib/ai/index.d.ts +48 -0
  48. package/dist/lib/ai/index.js +29 -0
  49. package/dist/lib/ai/inference-backend/client.d.ts +28 -0
  50. package/dist/lib/ai/inference-backend/client.js +301 -0
  51. package/dist/lib/ai/inference-backend/mocks.d.ts +12 -0
  52. package/dist/lib/ai/inference-backend/mocks.js +133 -0
  53. package/dist/lib/ai/inference-backend/types.d.ts +44 -0
  54. package/dist/lib/ai/inference-backend/types.js +1 -0
  55. package/dist/lib/ai/json-parse.d.ts +2 -0
  56. package/dist/lib/ai/json-parse.js +34 -0
  57. package/dist/lib/ai/llm-client.d.ts +7 -0
  58. package/dist/lib/ai/llm-client.js +36 -0
  59. package/dist/lib/ai/mock.d.ts +2 -0
  60. package/dist/lib/ai/mock.js +10 -0
  61. package/dist/lib/ai/types.d.ts +9 -0
  62. package/dist/lib/ai/types.js +1 -0
  63. package/dist/lib/chunking.d.ts +19 -0
  64. package/dist/lib/chunking.js +47 -0
  65. package/dist/lib/curated-kb-seed.d.ts +12 -0
  66. package/dist/lib/curated-kb-seed.js +155 -0
  67. package/dist/lib/email.js +67 -108
  68. package/dist/lib/embeddings.d.ts +2 -0
  69. package/dist/lib/embeddings.js +1 -0
  70. package/dist/lib/ensure-curated-kb-catalog.d.ts +6 -0
  71. package/dist/lib/ensure-curated-kb-catalog.js +53 -0
  72. package/dist/lib/env.d.ts +1 -5
  73. package/dist/lib/env.js +2 -7
  74. package/dist/lib/inference.d.ts +1 -8
  75. package/dist/lib/inference.js +1 -19
  76. package/dist/lib/kb-meta.d.ts +8 -0
  77. package/dist/lib/kb-meta.js +77 -0
  78. package/dist/lib/note-text.d.ts +1 -0
  79. package/dist/lib/note-text.js +47 -0
  80. package/dist/lib/notification-service.test.js +37 -36
  81. package/dist/lib/pdf.d.ts +11 -0
  82. package/dist/lib/pdf.js +11 -0
  83. package/dist/lib/usage_service.d.ts +2 -1
  84. package/dist/lib/usage_service.js +30 -12
  85. package/dist/lib/worksheet-generation.js +4 -4
  86. package/dist/lib/worksheet-generation.test.js +32 -17
  87. package/dist/lib/workspace-kb.d.ts +5 -0
  88. package/dist/lib/workspace-kb.js +7 -0
  89. package/dist/models/controller-context.model.d.ts +8 -0
  90. package/dist/models/controller-context.model.js +1 -0
  91. package/dist/repositories/artifact.repository.d.ts +60 -0
  92. package/dist/repositories/artifact.repository.js +40 -0
  93. package/dist/repositories/base.repository.d.ts +14 -0
  94. package/dist/repositories/base.repository.js +14 -0
  95. package/dist/repositories/invitation.repository.d.ts +94 -0
  96. package/dist/repositories/invitation.repository.js +44 -0
  97. package/dist/repositories/notification.repository.d.ts +72 -0
  98. package/dist/repositories/notification.repository.js +44 -0
  99. package/dist/repositories/router-module.repository.d.ts +10 -0
  100. package/dist/repositories/router-module.repository.js +14 -0
  101. package/dist/repositories/user.repository.d.ts +74 -0
  102. package/dist/repositories/user.repository.js +37 -0
  103. package/dist/repositories/workspace-member.repository.d.ts +31 -0
  104. package/dist/repositories/workspace-member.repository.js +31 -0
  105. package/dist/repositories/workspace.repository.d.ts +97 -0
  106. package/dist/repositories/workspace.repository.js +79 -0
  107. package/dist/routers/_app.d.ts +566 -111
  108. package/dist/routers/_app.js +4 -0
  109. package/dist/routers/admin.d.ts +0 -4
  110. package/dist/routers/admin.js +21 -549
  111. package/dist/routers/annotations.js +12 -170
  112. package/dist/routers/artifactVersions.d.ts +65 -0
  113. package/dist/routers/artifactVersions.js +14 -0
  114. package/dist/routers/auth.d.ts +0 -6
  115. package/dist/routers/auth.js +37 -422
  116. package/dist/routers/chat.js +15 -229
  117. package/dist/routers/copilot.d.ts +14 -13
  118. package/dist/routers/copilot.js +13 -532
  119. package/dist/routers/flashcards.d.ts +17 -6
  120. package/dist/routers/flashcards.js +23 -349
  121. package/dist/routers/knowledgeBase.d.ts +421 -0
  122. package/dist/routers/knowledgeBase.js +118 -0
  123. package/dist/routers/members.d.ts +0 -41
  124. package/dist/routers/members.js +22 -710
  125. package/dist/routers/notes.d.ts +94 -0
  126. package/dist/routers/notes.js +37 -0
  127. package/dist/routers/notifications.js +7 -109
  128. package/dist/routers/payment.d.ts +2 -12
  129. package/dist/routers/payment.js +11 -393
  130. package/dist/routers/podcast.d.ts +1 -1
  131. package/dist/routers/podcast.js +11 -784
  132. package/dist/routers/studyguide.js +3 -129
  133. package/dist/routers/worksheets.d.ts +29 -14
  134. package/dist/routers/worksheets.js +49 -628
  135. package/dist/routers/workspace.d.ts +27 -71
  136. package/dist/routers/workspace.js +29 -922
  137. package/dist/scripts/purge-deleted-users.js +2 -2
  138. package/dist/server.js +10 -3
  139. package/dist/services/activity/activity-human-description.service.d.ts +13 -0
  140. package/dist/services/activity/activity-human-description.service.js +221 -0
  141. package/dist/services/activity/activity-human-description.service.test.d.ts +1 -0
  142. package/dist/services/activity/activity-human-description.service.test.js +16 -0
  143. package/dist/services/activity/activity-log.service.d.ts +87 -0
  144. package/dist/services/activity/activity-log.service.js +276 -0
  145. package/dist/services/activity/activity-log.service.test.d.ts +1 -0
  146. package/dist/services/activity/activity-log.service.test.js +27 -0
  147. package/dist/services/activity-human-description.service.d.ts +13 -0
  148. package/dist/services/activity-human-description.service.js +221 -0
  149. package/dist/services/activity-human-description.service.test.d.ts +1 -0
  150. package/dist/services/activity-human-description.service.test.js +16 -0
  151. package/dist/services/activity-log.service.d.ts +87 -0
  152. package/dist/services/activity-log.service.js +276 -0
  153. package/dist/services/activity-log.service.test.d.ts +1 -0
  154. package/dist/services/activity-log.service.test.js +27 -0
  155. package/dist/services/admin/admin.service.d.ts +270 -0
  156. package/dist/services/admin/admin.service.js +476 -0
  157. package/dist/services/admin.service.d.ts +270 -0
  158. package/dist/services/admin.service.js +476 -0
  159. package/dist/services/ai/ai-session.service.d.ts +5 -0
  160. package/dist/services/ai/ai-session.service.js +4 -0
  161. package/dist/services/ai-session.service.d.ts +60 -0
  162. package/dist/services/ai-session.service.js +561 -0
  163. package/dist/services/annotation.service.d.ts +177 -0
  164. package/dist/services/annotation.service.js +154 -0
  165. package/dist/services/artifact-notification.service.d.ts +14 -0
  166. package/dist/services/artifact-notification.service.js +20 -0
  167. package/dist/services/artifact-version.service.d.ts +38 -0
  168. package/dist/services/artifact-version.service.js +129 -0
  169. package/dist/services/artifacts/annotation.service.d.ts +177 -0
  170. package/dist/services/artifacts/annotation.service.js +154 -0
  171. package/dist/services/artifacts/artifact-version.service.d.ts +38 -0
  172. package/dist/services/artifacts/artifact-version.service.js +129 -0
  173. package/dist/services/artifacts/chat.service.d.ts +127 -0
  174. package/dist/services/artifacts/chat.service.js +182 -0
  175. package/dist/services/artifacts/study-guide.service.d.ts +18 -0
  176. package/dist/services/artifacts/study-guide.service.js +65 -0
  177. package/dist/services/auth/auth.service.d.ts +94 -0
  178. package/dist/services/auth/auth.service.js +368 -0
  179. package/dist/services/auth.service.d.ts +94 -0
  180. package/dist/services/auth.service.js +368 -0
  181. package/dist/services/base.service.d.ts +14 -0
  182. package/dist/services/base.service.js +14 -0
  183. package/dist/services/billing/payment.service.d.ts +44 -0
  184. package/dist/services/billing/payment.service.js +365 -0
  185. package/dist/services/billing/subscription.service.d.ts +37 -0
  186. package/dist/services/billing/subscription.service.js +654 -0
  187. package/dist/services/billing/usage.service.d.ts +47 -0
  188. package/dist/services/billing/usage.service.js +149 -0
  189. package/dist/services/chat.service.d.ts +127 -0
  190. package/dist/services/chat.service.js +182 -0
  191. package/dist/services/content/copilot.service.d.ts +113 -0
  192. package/dist/services/content/copilot.service.js +439 -0
  193. package/dist/services/content/flashcard-progress.service.d.ts +159 -0
  194. package/dist/services/content/flashcard-progress.service.js +432 -0
  195. package/dist/services/content/flashcard.service.d.ts +184 -0
  196. package/dist/services/content/flashcard.service.js +339 -0
  197. package/dist/services/content/media-analysis.service.d.ts +23 -0
  198. package/dist/services/content/media-analysis.service.js +404 -0
  199. package/dist/services/content/podcast.service.d.ts +267 -0
  200. package/dist/services/content/podcast.service.js +653 -0
  201. package/dist/services/content/worksheet-content.service.d.ts +37 -0
  202. package/dist/services/content/worksheet-content.service.js +84 -0
  203. package/dist/services/content/worksheet-content.service.test.d.ts +1 -0
  204. package/dist/services/content/worksheet-content.service.test.js +69 -0
  205. package/dist/services/content/worksheet-generation.service.d.ts +91 -0
  206. package/dist/services/content/worksheet-generation.service.js +95 -0
  207. package/dist/services/content/worksheet-generation.service.test.d.ts +1 -0
  208. package/dist/services/content/worksheet-generation.service.test.js +20 -0
  209. package/dist/services/content/worksheet.service.d.ts +347 -0
  210. package/dist/services/content/worksheet.service.js +599 -0
  211. package/dist/services/copilot.service.d.ts +116 -0
  212. package/dist/services/copilot.service.js +447 -0
  213. package/dist/services/flashcard-progress.service.d.ts +2 -2
  214. package/dist/services/flashcard-progress.service.js +3 -2
  215. package/dist/services/flashcard.service.d.ts +140 -0
  216. package/dist/services/flashcard.service.js +325 -0
  217. package/dist/services/invitation.service.d.ts +66 -0
  218. package/dist/services/invitation.service.js +348 -0
  219. package/dist/services/knowledge/knowledge-base.service.d.ts +316 -0
  220. package/dist/services/knowledge/knowledge-base.service.js +544 -0
  221. package/dist/services/knowledge-base.service.d.ts +316 -0
  222. package/dist/services/knowledge-base.service.js +536 -0
  223. package/dist/services/media-analysis.service.d.ts +23 -0
  224. package/dist/services/media-analysis.service.js +384 -0
  225. package/dist/services/member.service.d.ts +36 -0
  226. package/dist/services/member.service.js +193 -0
  227. package/dist/services/members/invitation.service.d.ts +66 -0
  228. package/dist/services/members/invitation.service.js +348 -0
  229. package/dist/services/members/member.service.d.ts +36 -0
  230. package/dist/services/members/member.service.js +193 -0
  231. package/dist/services/note.service.d.ts +55 -0
  232. package/dist/services/note.service.js +111 -0
  233. package/dist/services/notification.service.d.ts +214 -0
  234. package/dist/services/notification.service.js +550 -0
  235. package/dist/services/notification.service.test.d.ts +1 -0
  236. package/dist/services/notification.service.test.js +87 -0
  237. package/dist/services/notifications/notification.service.d.ts +214 -0
  238. package/dist/services/notifications/notification.service.js +550 -0
  239. package/dist/services/notifications/notification.service.test.d.ts +1 -0
  240. package/dist/services/notifications/notification.service.test.js +87 -0
  241. package/dist/services/payment.service.d.ts +55 -0
  242. package/dist/services/payment.service.js +368 -0
  243. package/dist/services/podcast.service.d.ts +267 -0
  244. package/dist/services/podcast.service.js +654 -0
  245. package/dist/services/router-module.service.d.ts +7 -0
  246. package/dist/services/router-module.service.js +10 -0
  247. package/dist/services/study-guide.service.d.ts +18 -0
  248. package/dist/services/study-guide.service.js +65 -0
  249. package/dist/services/subscription.service.d.ts +37 -0
  250. package/dist/services/subscription.service.js +654 -0
  251. package/dist/services/usage-limit-policy.service.d.ts +12 -0
  252. package/dist/services/usage-limit-policy.service.js +22 -0
  253. package/dist/services/usage-limit-policy.service.test.d.ts +1 -0
  254. package/dist/services/usage-limit-policy.service.test.js +46 -0
  255. package/dist/services/usage.service.d.ts +27 -0
  256. package/dist/services/usage.service.js +77 -0
  257. package/dist/services/worksheet-content.service.d.ts +42 -0
  258. package/dist/services/worksheet-content.service.js +84 -0
  259. package/dist/services/worksheet-content.service.test.d.ts +1 -0
  260. package/dist/services/worksheet-content.service.test.js +69 -0
  261. package/dist/services/worksheet-generation.service.d.ts +91 -0
  262. package/dist/services/worksheet-generation.service.js +95 -0
  263. package/dist/services/worksheet-generation.service.test.d.ts +1 -0
  264. package/dist/services/worksheet-generation.service.test.js +20 -0
  265. package/dist/services/worksheet.service.d.ts +385 -0
  266. package/dist/services/worksheet.service.js +596 -0
  267. package/dist/services/workspace/workspace-analytics.service.d.ts +24 -0
  268. package/dist/services/workspace/workspace-analytics.service.js +95 -0
  269. package/dist/services/workspace/workspace-kb.service.d.ts +40 -0
  270. package/dist/services/workspace/workspace-kb.service.js +184 -0
  271. package/dist/services/workspace/workspace.service.d.ts +263 -0
  272. package/dist/services/workspace/workspace.service.js +401 -0
  273. package/dist/services/workspace-analytics.service.d.ts +24 -0
  274. package/dist/services/workspace-analytics.service.js +95 -0
  275. package/dist/services/workspace-kb.service.d.ts +40 -0
  276. package/dist/services/workspace-kb.service.js +184 -0
  277. package/dist/services/workspace-progress.service.d.ts +27 -0
  278. package/dist/services/workspace-progress.service.js +56 -0
  279. package/dist/services/workspace-progress.service.test.d.ts +1 -0
  280. package/dist/services/workspace-progress.service.test.js +49 -0
  281. package/dist/services/workspace.service.d.ts +307 -0
  282. package/dist/services/workspace.service.js +390 -0
  283. package/dist/trpc.d.ts +12 -4
  284. package/dist/trpc.js +7 -13
  285. package/package.json +5 -6
  286. package/prisma/migrations/20260509000001_add_knowledge_base/migration.sql +99 -0
  287. package/prisma/migrations/20260509000002_curate_knowledge_base/migration.sql +52 -0
  288. package/prisma/migrations/20260522000000_add_notes/migration.sql +27 -0
  289. package/prisma/migrations/20260524000000_remove_notes/migration.sql +3 -0
  290. package/prisma/schema.prisma +150 -48
  291. package/prisma/seed.mjs +67 -0
  292. package/scripts/debug/README.md +4 -0
  293. package/src/README.md +63 -0
  294. package/src/context.ts +33 -3
  295. package/src/lib/ai/config.ts +34 -0
  296. package/src/lib/ai/embedding-client.ts +47 -0
  297. package/src/lib/ai/index.ts +65 -0
  298. package/src/lib/ai/inference-backend/client.ts +479 -0
  299. package/src/lib/ai/inference-backend/mocks.ts +171 -0
  300. package/src/lib/ai/inference-backend/types.ts +50 -0
  301. package/src/lib/ai/json-parse.ts +35 -0
  302. package/src/lib/ai/llm-client.ts +54 -0
  303. package/src/lib/ai/mock.ts +12 -0
  304. package/src/lib/ai/types.ts +11 -0
  305. package/src/lib/chunking.ts +81 -0
  306. package/src/lib/curated-kb-seed.ts +164 -0
  307. package/src/lib/email.ts +77 -115
  308. package/src/lib/embeddings.ts +9 -0
  309. package/src/lib/ensure-curated-kb-catalog.ts +60 -0
  310. package/src/lib/env.ts +2 -7
  311. package/src/lib/inference.ts +1 -21
  312. package/src/lib/kb-meta.ts +81 -0
  313. package/src/lib/pdf.ts +23 -0
  314. package/src/lib/workspace-kb.ts +7 -0
  315. package/src/repositories/artifact.repository.ts +55 -0
  316. package/src/repositories/base.repository.ts +19 -0
  317. package/src/repositories/invitation.repository.ts +53 -0
  318. package/src/repositories/notification.repository.ts +53 -0
  319. package/src/repositories/user.repository.ts +44 -0
  320. package/src/repositories/workspace-member.repository.ts +38 -0
  321. package/src/repositories/workspace.repository.ts +89 -0
  322. package/src/routers/_app.ts +4 -0
  323. package/src/routers/admin.ts +124 -692
  324. package/src/routers/annotations.ts +25 -203
  325. package/src/routers/artifactVersions.ts +32 -0
  326. package/src/routers/auth.ts +82 -520
  327. package/src/routers/chat.ts +42 -245
  328. package/src/routers/copilot.ts +41 -666
  329. package/src/routers/flashcards.ts +108 -404
  330. package/src/routers/knowledgeBase.ts +216 -0
  331. package/src/routers/members.ts +60 -782
  332. package/src/routers/notifications.ts +15 -117
  333. package/src/routers/payment.ts +37 -446
  334. package/src/routers/podcast.ts +36 -898
  335. package/src/routers/studyguide.ts +5 -144
  336. package/src/routers/worksheets.ts +171 -735
  337. package/src/routers/workspace.ts +141 -1108
  338. package/src/scripts/purge-deleted-users.ts +2 -2
  339. package/src/server.ts +10 -3
  340. package/src/{lib/activity_human_description.test.ts → services/activity/activity-human-description.service.test.ts} +1 -1
  341. package/src/{lib/activity_log_service.test.ts → services/activity/activity-log.service.test.ts} +1 -1
  342. package/src/{lib/activity_log_service.ts → services/activity/activity-log.service.ts} +2 -2
  343. package/src/services/admin/admin.service.ts +612 -0
  344. package/src/services/ai/ai-session.service.ts +5 -0
  345. package/src/services/artifacts/annotation.service.ts +189 -0
  346. package/src/services/artifacts/artifact-version.service.ts +151 -0
  347. package/src/services/artifacts/chat.service.ts +197 -0
  348. package/src/services/artifacts/study-guide.service.ts +72 -0
  349. package/src/services/auth/auth.service.ts +473 -0
  350. package/src/services/base.service.ts +19 -0
  351. package/src/services/billing/payment.service.ts +433 -0
  352. package/src/{lib/subscription_service.ts → services/billing/subscription.service.ts} +5 -5
  353. package/src/services/billing/usage.service.ts +207 -0
  354. package/src/services/content/copilot.service.ts +587 -0
  355. package/src/services/{flashcard-progress.service.ts → content/flashcard-progress.service.ts} +18 -12
  356. package/src/services/content/flashcard.service.ts +417 -0
  357. package/src/services/content/media-analysis.service.ts +561 -0
  358. package/src/services/content/podcast.service.ts +777 -0
  359. package/src/services/content/worksheet-content.service.test.ts +83 -0
  360. package/src/services/content/worksheet-content.service.ts +117 -0
  361. package/src/{lib/worksheet-generation.test.ts → services/content/worksheet-generation.service.test.ts} +3 -3
  362. package/src/services/content/worksheet.service.ts +751 -0
  363. package/src/services/knowledge/knowledge-base.service.ts +705 -0
  364. package/src/services/members/invitation.service.ts +427 -0
  365. package/src/services/members/member.service.ts +241 -0
  366. package/src/{lib/notification-service.test.ts → services/notifications/notification.service.test.ts} +2 -2
  367. package/src/{lib/notification-service.ts → services/notifications/notification.service.ts} +102 -1
  368. package/src/services/workspace/workspace-analytics.service.ts +107 -0
  369. package/src/services/workspace/workspace-kb.service.ts +273 -0
  370. package/src/services/workspace/workspace.service.ts +488 -0
  371. package/src/trpc.ts +7 -15
  372. package/src/lib/ai-session.ts +0 -704
  373. package/src/lib/usage_service.ts +0 -74
  374. package/src/lib/workspace-access.ts +0 -13
  375. /package/{check-difficulty.cjs → scripts/debug/check-difficulty.cjs} +0 -0
  376. /package/{check-questions.cjs → scripts/debug/check-questions.cjs} +0 -0
  377. /package/{db-summary.cjs → scripts/debug/db-summary.cjs} +0 -0
  378. /package/{mcq-test.cjs → scripts/debug/mcq-test.cjs} +0 -0
  379. /package/{test-generate.js → scripts/debug/test-generate.js} +0 -0
  380. /package/{test-ratio.cjs → scripts/debug/test-ratio.cjs} +0 -0
  381. /package/{zod-test.cjs → scripts/debug/zod-test.cjs} +0 -0
  382. /package/src/{lib/activity_human_description.ts → services/activity/activity-human-description.service.ts} +0 -0
  383. /package/src/{lib/worksheet-generation.ts → services/content/worksheet-generation.service.ts} +0 -0
@@ -0,0 +1,427 @@
1
+ import type { PrismaClient } from '@prisma/client';
2
+ import { TRPCError } from '@trpc/server';
3
+ import { BaseService } from '../base.service.js';
4
+ import { sendInvitationEmail } from '../../lib/email.js';
5
+ import PusherService from '../../lib/pusher.js';
6
+ import {
7
+ notifyInviteAccepted,
8
+ notifyInviteRecipient,
9
+ } from '../notifications/notification.service.js';
10
+
11
+ export class InvitationService extends BaseService {
12
+ constructor(db: PrismaClient) {
13
+ super(db);
14
+ }
15
+
16
+ async inviteMember(
17
+ userId: string,
18
+ input: { workspaceId: string; email: string; role: 'admin' | 'member' },
19
+ ) {
20
+ const workspace = await this.db.workspace.findFirst({
21
+ where: { id: input.workspaceId, ownerId: userId },
22
+ });
23
+
24
+ if (!workspace) {
25
+ throw new TRPCError({
26
+ code: 'NOT_FOUND',
27
+ message: 'Workspace not found or insufficient permissions',
28
+ });
29
+ }
30
+
31
+ const existingMember = await this.db.user.findFirst({
32
+ where: {
33
+ email: input.email,
34
+ OR: [
35
+ { id: workspace.ownerId },
36
+ { workspaceMemberships: { some: { workspaceId: input.workspaceId } } },
37
+ ],
38
+ },
39
+ });
40
+
41
+ if (existingMember) {
42
+ throw new TRPCError({
43
+ code: 'BAD_REQUEST',
44
+ message: 'User is already a member of this workspace',
45
+ });
46
+ }
47
+
48
+ const existingInvitation = await this.db.workspaceInvitation.findFirst({
49
+ where: {
50
+ workspaceId: input.workspaceId,
51
+ email: input.email,
52
+ acceptedAt: null,
53
+ expiresAt: { gt: new Date() },
54
+ },
55
+ });
56
+
57
+ if (existingInvitation) {
58
+ throw new TRPCError({
59
+ code: 'BAD_REQUEST',
60
+ message: 'Invitation already sent to this email',
61
+ });
62
+ }
63
+
64
+ const invitation = await this.db.workspaceInvitation.create({
65
+ data: {
66
+ workspaceId: input.workspaceId,
67
+ email: input.email,
68
+ role: input.role,
69
+ invitedById: userId,
70
+ },
71
+ include: {
72
+ workspace: {
73
+ select: {
74
+ title: true,
75
+ owner: { select: { name: true, email: true } },
76
+ },
77
+ },
78
+ },
79
+ });
80
+
81
+ const invitedExistingUser = await this.db.user.findUnique({
82
+ where: { email: input.email },
83
+ select: { id: true, name: true },
84
+ });
85
+
86
+ if (invitedExistingUser) {
87
+ await notifyInviteRecipient(this.db, {
88
+ invitedUserId: invitedExistingUser.id,
89
+ inviterUserId: userId,
90
+ workspaceId: input.workspaceId,
91
+ workspaceTitle: invitation.workspace.title,
92
+ invitationId: invitation.id,
93
+ invitationToken: invitation.token,
94
+ inviterName: invitation.workspace.owner.name || invitation.workspace.owner.email,
95
+ });
96
+ }
97
+
98
+ this.logger.info(
99
+ `🎫 Invitation created for ${input.email} to workspace ${input.workspaceId} with role ${input.role}`,
100
+ 'WORKSPACE',
101
+ {
102
+ invitationId: invitation.id,
103
+ workspaceId: input.workspaceId,
104
+ email: input.email,
105
+ role: input.role,
106
+ invitedBy: userId,
107
+ },
108
+ );
109
+
110
+ await sendInvitationEmail({
111
+ email: invitation.email,
112
+ token: invitation.token,
113
+ role: invitation.role,
114
+ workspaceTitle: invitation.workspace.title,
115
+ invitedByName:
116
+ invitation.workspace.owner.name || invitation.workspace.owner.email || 'Someone',
117
+ });
118
+
119
+ return {
120
+ invitationId: invitation.id,
121
+ token: invitation.token,
122
+ email: invitation.email,
123
+ role: invitation.role,
124
+ expiresAt: invitation.expiresAt,
125
+ workspaceTitle: invitation.workspace.title,
126
+ invitedByName: invitation.workspace.owner.name || invitation.workspace.owner.email,
127
+ };
128
+ }
129
+
130
+ async getInvitations(workspaceId: string) {
131
+ return this.db.workspaceInvitation.findMany({
132
+ where: { workspaceId },
133
+ });
134
+ }
135
+
136
+ async acceptInvite(sessionUserId: string | undefined, token: string) {
137
+ const invitation = await this.db.workspaceInvitation.findFirst({
138
+ where: {
139
+ token,
140
+ acceptedAt: null,
141
+ expiresAt: { gt: new Date() },
142
+ },
143
+ include: {
144
+ workspace: {
145
+ select: {
146
+ id: true,
147
+ title: true,
148
+ ownerId: true,
149
+ owner: { select: { name: true, email: true } },
150
+ },
151
+ },
152
+ },
153
+ });
154
+
155
+ if (!invitation) {
156
+ throw new TRPCError({
157
+ code: 'NOT_FOUND',
158
+ message: 'Invalid or expired invitation',
159
+ });
160
+ }
161
+
162
+ if (!sessionUserId) {
163
+ throw new TRPCError({
164
+ code: 'UNAUTHORIZED',
165
+ message: 'Please log in to accept this invitation',
166
+ });
167
+ }
168
+
169
+ const user = await this.db.user.findFirst({ where: { id: sessionUserId } });
170
+ if (!user || !user.email) throw new TRPCError({ code: 'NOT_FOUND' });
171
+
172
+ this.logger.info(
173
+ `🔍 Verification check for ${user.email} accepting invite for ${invitation.email}`,
174
+ 'WORKSPACE',
175
+ );
176
+
177
+ if (user.email.toLowerCase() !== invitation.email.toLowerCase()) {
178
+ this.logger.warn(
179
+ `❌ Invitation email mismatch: user ${user.email} vs invite ${invitation.email}`,
180
+ 'WORKSPACE',
181
+ );
182
+ throw new TRPCError({
183
+ code: 'BAD_REQUEST',
184
+ message: 'This invitation was sent to a different email address',
185
+ });
186
+ }
187
+
188
+ const isAlreadyMember = await this.db.workspace.findFirst({
189
+ where: {
190
+ id: invitation.workspaceId,
191
+ OR: [
192
+ { ownerId: sessionUserId },
193
+ { members: { some: { userId: sessionUserId } } },
194
+ ],
195
+ },
196
+ });
197
+
198
+ if (isAlreadyMember) {
199
+ this.logger.info(
200
+ `ℹ️ User ${sessionUserId} is already a member of workspace ${invitation.workspaceId}. Marking invite as accepted.`,
201
+ 'WORKSPACE',
202
+ );
203
+ await this.db.workspaceInvitation.update({
204
+ where: { id: invitation.id },
205
+ data: { acceptedAt: new Date() },
206
+ });
207
+
208
+ return {
209
+ workspaceId: invitation.workspaceId,
210
+ workspaceTitle: invitation.workspace.title,
211
+ role: invitation.role,
212
+ ownerName: invitation.workspace.owner.name || invitation.workspace.owner.email,
213
+ message: 'You are already a member of this workspace',
214
+ };
215
+ }
216
+
217
+ let memberAdded = false;
218
+ try {
219
+ await this.db.workspaceMember.create({
220
+ data: {
221
+ workspaceId: invitation.workspaceId,
222
+ userId: sessionUserId,
223
+ role: invitation.role,
224
+ },
225
+ });
226
+ memberAdded = true;
227
+ } catch (error: any) {
228
+ if (error?.code !== 'P2002') {
229
+ throw error;
230
+ }
231
+ this.logger.info(
232
+ `ℹ️ Duplicate invite accept handled for user ${sessionUserId} in workspace ${invitation.workspaceId}`,
233
+ 'WORKSPACE',
234
+ );
235
+ }
236
+
237
+ await this.db.workspaceInvitation.update({
238
+ where: { id: invitation.id },
239
+ data: { acceptedAt: new Date() },
240
+ });
241
+
242
+ if (memberAdded) {
243
+ await notifyInviteAccepted(this.db, {
244
+ recipientUserIds: [invitation.invitedById, invitation.workspace.ownerId],
245
+ actorUserId: sessionUserId,
246
+ workspaceId: invitation.workspaceId,
247
+ workspaceTitle: invitation.workspace.title,
248
+ memberName: user.name || user.email,
249
+ invitationId: invitation.id,
250
+ });
251
+ }
252
+
253
+ this.logger.info(
254
+ `✅ Invitation accepted by ${sessionUserId} (${user.email}) for workspace ${invitation.workspaceId}`,
255
+ 'WORKSPACE',
256
+ {
257
+ invitationId: invitation.id,
258
+ workspaceId: invitation.workspaceId,
259
+ userId: sessionUserId,
260
+ email: invitation.email,
261
+ },
262
+ );
263
+
264
+ try {
265
+ if (memberAdded) {
266
+ await PusherService.emitMemberJoined(invitation.workspaceId, {
267
+ id: sessionUserId,
268
+ name: user.name || 'Member',
269
+ email: user.email,
270
+ role: invitation.role,
271
+ });
272
+ }
273
+ } catch (e) {
274
+ this.logger.error('Failed to emit member joined event', e);
275
+ }
276
+
277
+ return {
278
+ workspaceId: invitation.workspaceId,
279
+ workspaceTitle: invitation.workspace.title,
280
+ role: invitation.role,
281
+ ownerName: invitation.workspace.owner.name || invitation.workspace.owner.email,
282
+ ...(memberAdded ? {} : { message: 'You are already a member of this workspace' }),
283
+ };
284
+ }
285
+
286
+ async getPendingInvitations(userId: string, workspaceId: string) {
287
+ const workspace = await this.db.workspace.findFirst({
288
+ where: { id: workspaceId, ownerId: userId },
289
+ });
290
+
291
+ if (!workspace) {
292
+ throw new TRPCError({
293
+ code: 'NOT_FOUND',
294
+ message: 'Workspace not found or insufficient permissions',
295
+ });
296
+ }
297
+
298
+ const invitations = await this.db.workspaceInvitation.findMany({
299
+ where: {
300
+ workspaceId,
301
+ acceptedAt: null,
302
+ expiresAt: { gt: new Date() },
303
+ },
304
+ include: {
305
+ invitedBy: { select: { name: true, email: true } },
306
+ },
307
+ orderBy: { createdAt: 'desc' },
308
+ });
309
+
310
+ return invitations.map((invitation) => ({
311
+ id: invitation.id,
312
+ email: invitation.email,
313
+ role: invitation.role,
314
+ token: invitation.token,
315
+ expiresAt: invitation.expiresAt,
316
+ createdAt: invitation.createdAt,
317
+ invitedByName: invitation.invitedBy.name || invitation.invitedBy.email,
318
+ }));
319
+ }
320
+
321
+ async cancelInvitation(userId: string, invitationId: string) {
322
+ const invitation = await this.db.workspaceInvitation.findFirst({
323
+ where: {
324
+ id: invitationId,
325
+ acceptedAt: null,
326
+ workspace: { ownerId: userId },
327
+ },
328
+ });
329
+
330
+ if (!invitation) {
331
+ throw new TRPCError({
332
+ code: 'NOT_FOUND',
333
+ message: 'Invitation not found or insufficient permissions',
334
+ });
335
+ }
336
+
337
+ await this.db.workspaceInvitation.delete({ where: { id: invitationId } });
338
+
339
+ this.logger.info(
340
+ `❌ Invitation cancelled for ${invitation.email} to workspace ${invitation.workspaceId}`,
341
+ 'WORKSPACE',
342
+ {
343
+ invitationId,
344
+ workspaceId: invitation.workspaceId,
345
+ email: invitation.email,
346
+ cancelledBy: userId,
347
+ },
348
+ );
349
+
350
+ return {
351
+ invitationId,
352
+ message: 'Invitation cancelled successfully',
353
+ };
354
+ }
355
+
356
+ async resendInvitation(userId: string, invitationId: string) {
357
+ const invitation = await this.db.workspaceInvitation.findFirst({
358
+ where: {
359
+ id: invitationId,
360
+ acceptedAt: null,
361
+ workspace: { ownerId: userId },
362
+ },
363
+ include: {
364
+ workspace: {
365
+ select: {
366
+ title: true,
367
+ owner: { select: { name: true, email: true } },
368
+ },
369
+ },
370
+ },
371
+ });
372
+
373
+ if (!invitation) {
374
+ throw new TRPCError({
375
+ code: 'NOT_FOUND',
376
+ message: 'Invitation not found or insufficient permissions',
377
+ });
378
+ }
379
+
380
+ if (invitation.expiresAt < new Date()) {
381
+ const newExpiry = new Date();
382
+ newExpiry.setDate(newExpiry.getDate() + 7);
383
+ await this.db.workspaceInvitation.update({
384
+ where: { id: invitation.id },
385
+ data: { expiresAt: newExpiry },
386
+ });
387
+ invitation.expiresAt = newExpiry;
388
+ }
389
+
390
+ await sendInvitationEmail({
391
+ email: invitation.email,
392
+ token: invitation.token,
393
+ role: invitation.role,
394
+ workspaceTitle: invitation.workspace.title,
395
+ invitedByName:
396
+ invitation.workspace.owner.name || invitation.workspace.owner.email || 'Someone',
397
+ });
398
+
399
+ this.logger.info(
400
+ `📧 Invitation resent to ${invitation.email} for workspace ${invitation.workspaceId}`,
401
+ 'WORKSPACE',
402
+ {
403
+ invitationId: invitation.id,
404
+ workspaceId: invitation.workspaceId,
405
+ email: invitation.email,
406
+ resentBy: userId,
407
+ },
408
+ );
409
+
410
+ return {
411
+ invitationId: invitation.id,
412
+ message: 'Invitation email resent successfully',
413
+ };
414
+ }
415
+
416
+ async getAllInvitationsDebug(userId: string, workspaceId: string) {
417
+ const workspace = await this.db.workspace.findFirst({
418
+ where: { id: workspaceId, ownerId: userId },
419
+ });
420
+ if (!workspace) throw new TRPCError({ code: 'UNAUTHORIZED' });
421
+
422
+ return this.db.workspaceInvitation.findMany({
423
+ where: { workspaceId },
424
+ orderBy: { createdAt: 'desc' },
425
+ });
426
+ }
427
+ }
@@ -0,0 +1,241 @@
1
+ import type { PrismaClient } from '@prisma/client';
2
+ import { TRPCError } from '@trpc/server';
3
+ import { BaseService } from '../base.service.js';
4
+ import {
5
+ notifyWorkspaceMembershipRemoved,
6
+ notifyWorkspaceRoleChanged,
7
+ } from '../notifications/notification.service.js';
8
+
9
+ export class MemberService extends BaseService {
10
+ constructor(db: PrismaClient) {
11
+ super(db);
12
+ }
13
+
14
+ async getMembers(userId: string, workspaceId: string) {
15
+ const workspace = await this.db.workspace.findFirst({
16
+ where: {
17
+ id: workspaceId,
18
+ OR: [{ ownerId: userId }, { members: { some: { userId } } }],
19
+ },
20
+ include: {
21
+ owner: {
22
+ select: {
23
+ id: true,
24
+ name: true,
25
+ email: true,
26
+ profilePicture: { select: { id: true, name: true, url: true } },
27
+ },
28
+ },
29
+ members: {
30
+ include: {
31
+ user: {
32
+ select: {
33
+ id: true,
34
+ name: true,
35
+ email: true,
36
+ profilePicture: { select: { id: true, name: true, url: true } },
37
+ },
38
+ },
39
+ },
40
+ },
41
+ },
42
+ });
43
+
44
+ if (!workspace) {
45
+ throw new TRPCError({
46
+ code: 'NOT_FOUND',
47
+ message: 'Workspace not found or access denied',
48
+ });
49
+ }
50
+
51
+ const workspaceMembers = [
52
+ {
53
+ id: workspace.owner.id,
54
+ name: workspace.owner.name || 'Unknown',
55
+ email: workspace.owner.email || '',
56
+ role: 'owner' as const,
57
+ joinedAt: workspace.createdAt,
58
+ },
59
+ ...workspace.members.map((membership) => ({
60
+ id: membership.user.id,
61
+ name: membership.user.name || 'Unknown',
62
+ email: membership.user.email || '',
63
+ role: membership.role as 'admin' | 'member',
64
+ joinedAt: membership.joinedAt,
65
+ })),
66
+ ];
67
+
68
+ this.logger.info(
69
+ `👥 Fetched ${workspaceMembers.length} members for workspace ${workspaceId}`,
70
+ 'WORKSPACE',
71
+ { memberEmails: workspaceMembers.map((m) => m.email) },
72
+ );
73
+
74
+ return workspaceMembers;
75
+ }
76
+
77
+ async getCurrentUserRole(userId: string, workspaceId: string) {
78
+ const workspace = await this.db.workspace.findFirst({
79
+ where: { id: workspaceId },
80
+ select: {
81
+ ownerId: true,
82
+ members: { where: { userId }, select: { role: true } },
83
+ },
84
+ });
85
+
86
+ if (!workspace) {
87
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Workspace not found' });
88
+ }
89
+
90
+ if (workspace.ownerId === userId) {
91
+ return 'owner';
92
+ }
93
+
94
+ if (workspace.members.length > 0) {
95
+ return workspace.members[0].role as 'admin' | 'member';
96
+ }
97
+
98
+ throw new TRPCError({
99
+ code: 'FORBIDDEN',
100
+ message: 'Access denied to this workspace',
101
+ });
102
+ }
103
+
104
+ async changeMemberRole(
105
+ userId: string,
106
+ input: { workspaceId: string; memberId: string; role: 'admin' | 'member' },
107
+ ) {
108
+ const workspace = await this.db.workspace.findFirst({
109
+ where: { id: input.workspaceId, ownerId: userId },
110
+ select: { id: true, title: true, ownerId: true },
111
+ });
112
+
113
+ if (!workspace) {
114
+ throw new TRPCError({
115
+ code: 'NOT_FOUND',
116
+ message: 'Workspace not found or insufficient permissions',
117
+ });
118
+ }
119
+
120
+ if (input.memberId === workspace.ownerId) {
121
+ throw new TRPCError({
122
+ code: 'BAD_REQUEST',
123
+ message: 'Cannot change owner role',
124
+ });
125
+ }
126
+
127
+ const member = await this.db.workspaceMember.findFirst({
128
+ where: { workspaceId: input.workspaceId, userId: input.memberId },
129
+ });
130
+
131
+ if (!member) {
132
+ throw new TRPCError({
133
+ code: 'NOT_FOUND',
134
+ message: 'Member not found in this workspace',
135
+ });
136
+ }
137
+
138
+ const updatedMember = await this.db.workspaceMember.update({
139
+ where: { id: member.id },
140
+ data: { role: input.role },
141
+ include: {
142
+ user: { select: { id: true, name: true, email: true } },
143
+ },
144
+ });
145
+
146
+ this.logger.info(
147
+ `🔄 Member role changed for ${input.memberId} from ${member.role} to ${input.role} in workspace ${input.workspaceId}`,
148
+ 'WORKSPACE',
149
+ {
150
+ workspaceId: input.workspaceId,
151
+ memberId: input.memberId,
152
+ oldRole: member.role,
153
+ newRole: input.role,
154
+ changedBy: userId,
155
+ },
156
+ );
157
+
158
+ const actor = await this.db.user.findUnique({
159
+ where: { id: userId },
160
+ select: { name: true, email: true },
161
+ });
162
+ await notifyWorkspaceRoleChanged(this.db, {
163
+ memberUserId: input.memberId,
164
+ workspaceId: input.workspaceId,
165
+ workspaceTitle: workspace.title,
166
+ newRole: input.role,
167
+ oldRole: member.role,
168
+ actorUserId: userId,
169
+ actorName: actor?.name || actor?.email || 'A workspace admin',
170
+ }).catch(() => {});
171
+
172
+ return {
173
+ memberId: input.memberId,
174
+ role: input.role,
175
+ memberName: updatedMember.user.name || updatedMember.user.email,
176
+ message: 'Role changed successfully',
177
+ };
178
+ }
179
+
180
+ async removeMember(userId: string, input: { workspaceId: string; memberId: string }) {
181
+ const workspace = await this.db.workspace.findFirst({
182
+ where: { id: input.workspaceId, ownerId: userId },
183
+ select: { id: true, title: true, ownerId: true },
184
+ });
185
+
186
+ if (!workspace) {
187
+ throw new TRPCError({
188
+ code: 'NOT_FOUND',
189
+ message: 'Workspace not found or insufficient permissions',
190
+ });
191
+ }
192
+
193
+ if (input.memberId === workspace.ownerId) {
194
+ throw new TRPCError({
195
+ code: 'BAD_REQUEST',
196
+ message: 'Cannot remove workspace owner',
197
+ });
198
+ }
199
+
200
+ const member = await this.db.workspaceMember.findFirst({
201
+ where: { workspaceId: input.workspaceId, userId: input.memberId },
202
+ include: { user: { select: { name: true, email: true } } },
203
+ });
204
+
205
+ if (!member) {
206
+ throw new TRPCError({
207
+ code: 'NOT_FOUND',
208
+ message: 'Member not found in this workspace',
209
+ });
210
+ }
211
+
212
+ const actor = await this.db.user.findUnique({
213
+ where: { id: userId },
214
+ select: { name: true, email: true },
215
+ });
216
+ await notifyWorkspaceMembershipRemoved(this.db, {
217
+ memberUserId: input.memberId,
218
+ workspaceId: input.workspaceId,
219
+ workspaceTitle: workspace.title,
220
+ actorUserId: userId,
221
+ actorName: actor?.name || actor?.email || 'A workspace admin',
222
+ }).catch(() => {});
223
+
224
+ await this.db.workspaceMember.delete({ where: { id: member.id } });
225
+
226
+ this.logger.info(
227
+ `🗑️ Member ${input.memberId} removed from workspace ${input.workspaceId}`,
228
+ 'WORKSPACE',
229
+ {
230
+ workspaceId: input.workspaceId,
231
+ memberId: input.memberId,
232
+ removedBy: userId,
233
+ },
234
+ );
235
+
236
+ return {
237
+ memberId: input.memberId,
238
+ message: 'Member removed successfully',
239
+ };
240
+ }
241
+ }
@@ -1,7 +1,7 @@
1
1
  import test from 'node:test';
2
2
  import assert from 'node:assert/strict';
3
- import PusherService from './pusher.js';
4
- import { NotificationType, createNotification } from './notification-service.js';
3
+ import PusherService from '../../lib/pusher.js';
4
+ import { NotificationType, createNotification } from './notification.service.js';
5
5
 
6
6
  type FakeNotification = {
7
7
  id: string;