@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,433 @@
1
+ import type { PrismaClient } from '@prisma/client';
2
+ import type { ArtifactType } from '@prisma/client';
3
+ import { TRPCError } from '@trpc/server';
4
+ import { Stripe } from 'stripe';
5
+ import { BaseService } from '../base.service.js';
6
+ import { stripe } from '../../lib/stripe.js';
7
+ import { env } from '../../lib/env.js';
8
+ import { getAccountSummary, getUserUsage, getUserPlanLimits } from './usage.service.js';
9
+ import {
10
+ notifyPaymentSucceeded,
11
+ notifySubscriptionActivated,
12
+ notifySubscriptionPaymentSucceeded,
13
+ } from '../notifications/notification.service.js';
14
+ import { upsertSubscriptionFromStripe } from './subscription.service.js';
15
+
16
+ /** Stripe Checkout URLs contain the session id (cs_…); we only store the URL in DB. */
17
+ const CHECKOUT_SESSION_ID_IN_URL = /cs_[a-zA-Z0-9]+/;
18
+
19
+ async function reuseCheckoutUrlIfSessionStillOpen(
20
+ stripeClient: Stripe,
21
+ db: PrismaClient,
22
+ existing: { id: string; stripeSessionId: string | null },
23
+ lockKey: string,
24
+ ): Promise<string | null> {
25
+ const url = existing.stripeSessionId;
26
+ if (!url) return null;
27
+
28
+ const idMatch = url.match(CHECKOUT_SESSION_ID_IN_URL);
29
+ if (!idMatch) {
30
+ await db.idempotencyRecord.updateMany({
31
+ where: { id: existing.id, activeLockKey: lockKey },
32
+ data: { activeLockKey: null, status: 'expired' },
33
+ });
34
+ return null;
35
+ }
36
+
37
+ try {
38
+ const session = await stripeClient.checkout.sessions.retrieve(idMatch[0]);
39
+ if (session.status === 'open') {
40
+ return url;
41
+ }
42
+ await db.idempotencyRecord.updateMany({
43
+ where: { id: existing.id, activeLockKey: lockKey },
44
+ data: {
45
+ activeLockKey: null,
46
+ status: session.status === 'complete' ? 'completed' : 'expired',
47
+ },
48
+ });
49
+ return null;
50
+ } catch {
51
+ await db.idempotencyRecord.updateMany({
52
+ where: { id: existing.id, activeLockKey: lockKey },
53
+ data: { activeLockKey: null, status: 'expired' },
54
+ });
55
+ return null;
56
+ }
57
+ }
58
+
59
+ export class PaymentService extends BaseService {
60
+ constructor(db: PrismaClient) {
61
+ super(db);
62
+ }
63
+
64
+ async getPlans(userId?: string) {
65
+ const plans = await this.db.plan.findMany({
66
+ where: { active: true },
67
+ include: { limit: true },
68
+ orderBy: { price: 'asc' },
69
+ });
70
+
71
+ // Anonymous callers (pricing page) don't get per-user `isActive`.
72
+ if (!userId) {
73
+ return plans.map((plan: any) => ({ ...plan, isActive: false }));
74
+ }
75
+
76
+ const activeSubscriptions = await this.db.subscription.findMany({
77
+ where: { userId, status: 'active' },
78
+ });
79
+
80
+ return plans.map((plan: any) => ({
81
+ ...plan,
82
+ isActive: activeSubscriptions.some((sub: any) => sub.planId === plan.id),
83
+ }));
84
+ }
85
+
86
+ async createCheckoutSession(userId: string, input: { planId: string }) {
87
+ if (!stripe) {
88
+ throw new TRPCError({
89
+ code: 'INTERNAL_SERVER_ERROR',
90
+ message: 'Stripe not configured',
91
+ });
92
+ }
93
+
94
+ const user = await this.db.user.findUnique({ where: { id: userId } });
95
+ if (!user) throw new TRPCError({ code: 'NOT_FOUND', message: 'User not found' });
96
+
97
+ const lockKey = `pending_${userId}_${input.planId}`;
98
+ let attempt = null;
99
+ let retryCount = 0;
100
+
101
+ while (retryCount < 3) {
102
+ try {
103
+ attempt = await this.db.idempotencyRecord.create({
104
+ data: {
105
+ userId,
106
+ planId: input.planId,
107
+ activeLockKey: lockKey,
108
+ status: 'pending',
109
+ },
110
+ });
111
+ break;
112
+ } catch (err: any) {
113
+ if (err.code === 'P2002') {
114
+ const existing = await this.db.idempotencyRecord.findUnique({
115
+ where: { activeLockKey: lockKey },
116
+ });
117
+ if (!existing) {
118
+ retryCount++;
119
+ continue;
120
+ }
121
+
122
+ const isStale = Date.now() - existing.updatedAt.getTime() > 24 * 60 * 60 * 1000;
123
+ if (isStale) {
124
+ const result = await this.db.idempotencyRecord.updateMany({
125
+ where: { id: existing.id, activeLockKey: lockKey },
126
+ data: { activeLockKey: null, status: 'expired' },
127
+ });
128
+ if (result.count > 0) {
129
+ retryCount++;
130
+ continue;
131
+ }
132
+ }
133
+
134
+ if (existing.stripeSessionId && stripe) {
135
+ const reusable = await reuseCheckoutUrlIfSessionStillOpen(
136
+ stripe,
137
+ this.db,
138
+ existing,
139
+ lockKey,
140
+ );
141
+ if (reusable) return { url: reusable };
142
+ retryCount++;
143
+ continue;
144
+ }
145
+ await new Promise((resolve) => setTimeout(resolve, 800));
146
+ retryCount++;
147
+ continue;
148
+ }
149
+ throw err;
150
+ }
151
+ }
152
+
153
+ if (!attempt) {
154
+ throw new TRPCError({ code: 'CONFLICT', message: 'Concurrent request' });
155
+ }
156
+
157
+ const plan = await this.db.plan.findUnique({ where: { id: input.planId } });
158
+ if (!plan) throw new TRPCError({ code: 'NOT_FOUND', message: 'Plan not found' });
159
+
160
+ try {
161
+ const successUrl = env.STRIPE_SUCCESS_URL.includes('session_id=')
162
+ ? env.STRIPE_SUCCESS_URL
163
+ : `${env.STRIPE_SUCCESS_URL}${env.STRIPE_SUCCESS_URL.includes('?') ? '&' : '?'}session_id={CHECKOUT_SESSION_ID}`;
164
+
165
+ const session = await stripe.checkout.sessions.create(
166
+ {
167
+ customer: user.stripe_customer_id || undefined,
168
+ customer_email: user.stripe_customer_id ? undefined : user.email || undefined,
169
+ line_items: [{ price: plan.stripePriceId, quantity: 1 }],
170
+ mode: plan.interval ? 'subscription' : 'payment',
171
+ subscription_data: plan.interval
172
+ ? {
173
+ metadata: {
174
+ userId,
175
+ planId: plan.id,
176
+ attemptId: attempt.id,
177
+ },
178
+ }
179
+ : undefined,
180
+ success_url: successUrl,
181
+ cancel_url: env.STRIPE_CANCEL_URL,
182
+ metadata: {
183
+ userId,
184
+ planId: plan.id,
185
+ attemptId: attempt.id,
186
+ },
187
+ },
188
+ {
189
+ idempotencyKey: attempt.id,
190
+ },
191
+ );
192
+
193
+ await this.db.idempotencyRecord.update({
194
+ where: { id: attempt.id },
195
+ data: { stripeSessionId: session.url },
196
+ });
197
+
198
+ return { url: session.url };
199
+ } catch (error: any) {
200
+ await this.db.idempotencyRecord.update({
201
+ where: { id: attempt.id },
202
+ data: { status: 'failed', activeLockKey: null },
203
+ });
204
+ throw new TRPCError({
205
+ code: 'INTERNAL_SERVER_ERROR',
206
+ message: error.message,
207
+ });
208
+ }
209
+ }
210
+
211
+ async confirmCheckoutSuccess(userId: string, input: { sessionId: string }) {
212
+ if (!stripe) {
213
+ throw new TRPCError({
214
+ code: 'INTERNAL_SERVER_ERROR',
215
+ message: 'Stripe not configured',
216
+ });
217
+ }
218
+
219
+ const session = await stripe.checkout.sessions.retrieve(input.sessionId, {
220
+ expand: ['line_items', 'subscription'],
221
+ });
222
+
223
+ const metadata = session.metadata || {};
224
+ if (metadata.userId !== userId) {
225
+ throw new TRPCError({
226
+ code: 'FORBIDDEN',
227
+ message: 'This checkout session does not belong to the current user',
228
+ });
229
+ }
230
+
231
+ if (session.status !== 'complete') {
232
+ return { confirmed: false, reason: 'checkout_not_complete' };
233
+ }
234
+
235
+ const plan = metadata.planId
236
+ ? await this.db.plan.findUnique({ where: { id: metadata.planId } })
237
+ : null;
238
+
239
+ if (session.mode === 'payment' && session.payment_status === 'paid') {
240
+ await notifyPaymentSucceeded(this.db, {
241
+ userId,
242
+ planId: metadata.planId,
243
+ planName: plan?.name || metadata.planType,
244
+ stripeSessionId: session.id,
245
+ amountPaid: session.amount_total ?? undefined,
246
+ });
247
+ return { confirmed: true, kind: 'payment' as const };
248
+ }
249
+
250
+ if (session.mode === 'subscription') {
251
+ const stripeSubscriptionId =
252
+ typeof session.subscription === 'string'
253
+ ? session.subscription
254
+ : session.subscription?.id;
255
+
256
+ if (stripeSubscriptionId) {
257
+ await upsertSubscriptionFromStripe(stripeSubscriptionId);
258
+ await notifySubscriptionActivated(this.db, {
259
+ userId,
260
+ planId: metadata.planId,
261
+ planName: plan?.name || metadata.planType,
262
+ stripeSubscriptionId,
263
+ });
264
+
265
+ if (session.payment_status === 'paid') {
266
+ await notifySubscriptionPaymentSucceeded(this.db, {
267
+ userId,
268
+ planId: metadata.planId,
269
+ planName: plan?.name || metadata.planType,
270
+ stripeInvoiceId: `checkout_${session.id}`,
271
+ amountPaid: session.amount_total ?? undefined,
272
+ });
273
+ }
274
+ }
275
+ return { confirmed: true, kind: 'subscription' as const };
276
+ }
277
+
278
+ return { confirmed: false, reason: 'unsupported_mode' };
279
+ }
280
+
281
+ async createResourcePurchaseSession(
282
+ userId: string,
283
+ input: { resourceType: ArtifactType; quantity: number },
284
+ ) {
285
+ if (!stripe) {
286
+ throw new TRPCError({
287
+ code: 'INTERNAL_SERVER_ERROR',
288
+ message: 'Stripe is not configured on the server',
289
+ });
290
+ }
291
+
292
+ const user = await this.db.user.findUnique({ where: { id: userId } });
293
+ if (!user) {
294
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'User not found' });
295
+ }
296
+
297
+ const resourcePrice = await this.db.resourcePrice.findUnique({
298
+ where: { resourceType: input.resourceType },
299
+ });
300
+
301
+ if (!resourcePrice) {
302
+ throw new TRPCError({
303
+ code: 'PRECONDITION_FAILED',
304
+ message: 'Price not set',
305
+ });
306
+ }
307
+
308
+ const lockKey = `topup_${userId}_${input.resourceType}`;
309
+ let attempt = null;
310
+ let retryCount = 0;
311
+
312
+ while (retryCount < 3) {
313
+ try {
314
+ attempt = await this.db.idempotencyRecord.create({
315
+ data: {
316
+ userId,
317
+ resourceType: input.resourceType,
318
+ activeLockKey: lockKey,
319
+ status: 'pending',
320
+ },
321
+ });
322
+ break;
323
+ } catch (err: any) {
324
+ if (err.code === 'P2002') {
325
+ const existing = await this.db.idempotencyRecord.findUnique({
326
+ where: { activeLockKey: lockKey },
327
+ });
328
+ if (!existing) {
329
+ retryCount++;
330
+ continue;
331
+ }
332
+
333
+ const isStale = Date.now() - existing.updatedAt.getTime() > 24 * 60 * 60 * 1000;
334
+ if (isStale) {
335
+ const result = await this.db.idempotencyRecord.updateMany({
336
+ where: { id: existing.id, activeLockKey: lockKey },
337
+ data: { activeLockKey: null, status: 'expired' },
338
+ });
339
+ if (result.count > 0) {
340
+ retryCount++;
341
+ continue;
342
+ }
343
+ }
344
+
345
+ if (existing.stripeSessionId && stripe) {
346
+ const reusable = await reuseCheckoutUrlIfSessionStillOpen(
347
+ stripe,
348
+ this.db,
349
+ existing,
350
+ lockKey,
351
+ );
352
+ if (reusable) return { url: reusable };
353
+ retryCount++;
354
+ continue;
355
+ }
356
+ await new Promise((resolve) => setTimeout(resolve, 800));
357
+ retryCount++;
358
+ continue;
359
+ }
360
+ throw err;
361
+ }
362
+ }
363
+
364
+ if (!attempt) {
365
+ throw new TRPCError({ code: 'CONFLICT', message: 'Concurrent request' });
366
+ }
367
+
368
+ try {
369
+ const resourceName = input.resourceType
370
+ .split('_')
371
+ .map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
372
+ .join(' ');
373
+
374
+ const session = await stripe.checkout.sessions.create(
375
+ {
376
+ customer: user.stripe_customer_id || undefined,
377
+ line_items: [
378
+ {
379
+ price_data: {
380
+ currency: 'usd',
381
+ product_data: {
382
+ name: `Add-on: extra ${resourceName}s`,
383
+ description: `Purchase of ${input.quantity} additional ${resourceName}(s)`,
384
+ },
385
+ unit_amount: resourcePrice.priceCents,
386
+ },
387
+ quantity: input.quantity,
388
+ },
389
+ ],
390
+ mode: 'payment',
391
+ success_url: `${env.STRIPE_SUCCESS_URL}?success=true`,
392
+ cancel_url: env.STRIPE_CANCEL_URL,
393
+ metadata: {
394
+ userId,
395
+ resourceType: input.resourceType,
396
+ quantity: input.quantity.toString(),
397
+ isPurchase: 'true',
398
+ attemptId: attempt.id,
399
+ },
400
+ invoice_creation: { enabled: true },
401
+ },
402
+ {
403
+ idempotencyKey: attempt.id,
404
+ },
405
+ );
406
+
407
+ await this.db.idempotencyRecord.update({
408
+ where: { id: attempt.id },
409
+ data: { stripeSessionId: session.url },
410
+ });
411
+
412
+ return { url: session.url };
413
+ } catch (error: any) {
414
+ await this.db.idempotencyRecord.update({
415
+ where: { id: attempt.id },
416
+ data: { status: 'failed', activeLockKey: null },
417
+ });
418
+ throw new TRPCError({
419
+ code: 'INTERNAL_SERVER_ERROR',
420
+ message: error.message,
421
+ });
422
+ }
423
+ }
424
+
425
+ async getUsageOverview(userId: string) {
426
+ const { usage, limits, hasActivePlan } = await getAccountSummary(userId);
427
+ return { usage, limits, hasActivePlan };
428
+ }
429
+
430
+ getResourcePrices() {
431
+ return this.db.resourcePrice.findMany();
432
+ }
433
+ }
@@ -1,6 +1,6 @@
1
- import { prisma } from './prisma.js';
2
- import { logger } from './logger.js';
3
- import { stripe } from './stripe.js';
1
+ import { prisma } from '../../lib/prisma.js';
2
+ import { logger } from '../../lib/logger.js';
3
+ import { stripe } from '../../lib/stripe.js';
4
4
  import Stripe from 'stripe';
5
5
  import {
6
6
  notifyPaymentFailed,
@@ -8,9 +8,9 @@ import {
8
8
  notifySubscriptionActivated,
9
9
  notifySubscriptionCanceled,
10
10
  notifySubscriptionPaymentSucceeded,
11
- } from './notification-service.js';
11
+ } from '../notifications/notification.service.js';
12
12
  import { ArtifactType } from '@prisma/client';
13
- import PusherService from './pusher.js';
13
+ import PusherService from '../../lib/pusher.js';
14
14
 
15
15
  /**
16
16
  * Handle checkout.session.completed event
@@ -0,0 +1,207 @@
1
+ import type { Prisma } from '@prisma/client';
2
+ import { prisma } from '../../lib/prisma.js';
3
+ import { workspaceAccessWhere } from '../../repositories/workspace.repository.js';
4
+
5
+ export interface UserUsage {
6
+ flashcards: number;
7
+ worksheets: number;
8
+ studyGuides: number;
9
+ podcasts: number;
10
+ storageBytes: number;
11
+ }
12
+
13
+ export interface AccountStats {
14
+ workspaces: number;
15
+ folders: number;
16
+ lastUpdated: Date | null;
17
+ spaceUsed: number;
18
+ spaceTotal: number;
19
+ }
20
+
21
+ export interface UserPlanLimits {
22
+ id: string;
23
+ planId: string;
24
+ maxStorageBytes: bigint;
25
+ maxWorksheets: number;
26
+ maxFlashcards: number;
27
+ maxPodcasts: number;
28
+ maxStudyGuides: number;
29
+ createdAt: Date;
30
+ updatedAt: Date;
31
+ isFallbackPlan: boolean;
32
+ }
33
+
34
+ export interface AccountSummary {
35
+ stats: AccountStats;
36
+ usage: UserUsage;
37
+ limits: UserPlanLimits;
38
+ hasActivePlan: boolean;
39
+ }
40
+
41
+ const FALLBACK_PLAN_LIMITS = {
42
+ id: 'fallback-free',
43
+ planId: 'fallback-free',
44
+ maxStorageBytes: BigInt(1024 * 1024 * 1024), // 1GB
45
+ maxWorksheets: 3,
46
+ maxFlashcards: 20,
47
+ maxPodcasts: 0,
48
+ maxStudyGuides: 1,
49
+ } as const;
50
+
51
+ const CACHE_TTL_MS = 30_000;
52
+
53
+ type CacheEntry<T> = { value: T; expiresAt: number };
54
+
55
+ const usageCache = new Map<string, CacheEntry<UserUsage>>();
56
+ const limitsCache = new Map<string, CacheEntry<UserPlanLimits>>();
57
+ const accountSummaryCache = new Map<string, CacheEntry<AccountSummary>>();
58
+
59
+ function readCache<T>(map: Map<string, CacheEntry<T>>, userId: string): T | null {
60
+ const entry = map.get(userId);
61
+ if (!entry || entry.expiresAt <= Date.now()) {
62
+ map.delete(userId);
63
+ return null;
64
+ }
65
+ return entry.value;
66
+ }
67
+
68
+ function writeCache<T>(map: Map<string, CacheEntry<T>>, userId: string, value: T) {
69
+ map.set(userId, { value, expiresAt: Date.now() + CACHE_TTL_MS });
70
+ }
71
+
72
+ /** Bust cached usage/limit reads after creates, deletes, or billing changes. */
73
+ export function invalidateUserBillingCache(userId: string) {
74
+ usageCache.delete(userId);
75
+ limitsCache.delete(userId);
76
+ accountSummaryCache.delete(userId);
77
+ }
78
+
79
+ function workspaceAccessFilter(userId: string): Prisma.WorkspaceWhereInput {
80
+ return workspaceAccessWhere(userId);
81
+ }
82
+
83
+ /**
84
+ * Counts all resources consumed by a user across the platform.
85
+ */
86
+ export async function getUserUsage(userId: string): Promise<UserUsage> {
87
+ const cached = readCache(usageCache, userId);
88
+ if (cached) return cached;
89
+
90
+ const [flashcards, worksheets, studyGuides, podcasts, storageResult] = await Promise.all([
91
+ prisma.artifact.count({ where: { createdById: userId, type: 'FLASHCARD_SET' } }),
92
+ prisma.artifact.count({ where: { createdById: userId, type: 'WORKSHEET' } }),
93
+ prisma.artifact.count({ where: { createdById: userId, type: 'STUDY_GUIDE' } }),
94
+ prisma.artifact.count({ where: { createdById: userId, type: 'PODCAST_EPISODE' } }),
95
+ prisma.fileAsset.aggregate({
96
+ _sum: { size: true },
97
+ where: { userId },
98
+ }),
99
+ ]);
100
+
101
+ const usage: UserUsage = {
102
+ flashcards,
103
+ worksheets,
104
+ studyGuides,
105
+ podcasts,
106
+ storageBytes: Number(storageResult._sum.size || 0),
107
+ };
108
+ writeCache(usageCache, userId, usage);
109
+ return usage;
110
+ }
111
+
112
+ /**
113
+ * Retrieves the specific plan limits for a user based on their active subscription
114
+ * PLUS any extra credits purchased.
115
+ */
116
+ export async function getUserPlanLimits(userId: string): Promise<UserPlanLimits> {
117
+ const cached = readCache(limitsCache, userId);
118
+ if (cached) return cached;
119
+
120
+ const [activeSub, freePlan, userCredits] = await Promise.all([
121
+ prisma.subscription.findFirst({
122
+ where: { userId, status: 'active' },
123
+ include: { plan: { include: { limit: true } } },
124
+ orderBy: { createdAt: 'desc' },
125
+ }),
126
+ prisma.plan.findFirst({
127
+ where: {
128
+ active: true,
129
+ price: 0,
130
+ limit: { isNot: null },
131
+ },
132
+ include: { limit: true },
133
+ orderBy: { createdAt: 'asc' },
134
+ }),
135
+ prisma.userCredit.groupBy({
136
+ by: ['resourceType'],
137
+ where: { userId },
138
+ _sum: { amount: true },
139
+ }),
140
+ ]);
141
+
142
+ const baseLimit = activeSub?.plan?.limit ?? freePlan?.limit;
143
+ const base = baseLimit ?? FALLBACK_PLAN_LIMITS;
144
+
145
+ const getCreditSum = (type: string) =>
146
+ userCredits.find((c) => c.resourceType === type)?._sum.amount || 0;
147
+
148
+ const limits: UserPlanLimits = {
149
+ id: base.id,
150
+ planId: base.planId,
151
+ maxStorageBytes: BigInt(Number(base.maxStorageBytes) + getCreditSum('STORAGE') * 1024 * 1024),
152
+ maxWorksheets: base.maxWorksheets + getCreditSum('WORKSHEET'),
153
+ maxFlashcards: base.maxFlashcards + getCreditSum('FLASHCARD_SET'),
154
+ maxPodcasts: base.maxPodcasts + getCreditSum('PODCAST_EPISODE'),
155
+ maxStudyGuides: base.maxStudyGuides + getCreditSum('STUDY_GUIDE'),
156
+ createdAt: baseLimit?.createdAt || new Date(),
157
+ updatedAt: baseLimit?.updatedAt || new Date(),
158
+ isFallbackPlan: !baseLimit,
159
+ };
160
+
161
+ writeCache(limitsCache, userId, limits);
162
+ return limits;
163
+ }
164
+
165
+ /**
166
+ * Sidebar/dashboard summary: stats + usage + limits in one parallel DB round-trip batch.
167
+ */
168
+ export async function getAccountSummary(userId: string): Promise<AccountSummary> {
169
+ const cached = readCache(accountSummaryCache, userId);
170
+ if (cached) return cached;
171
+
172
+ const workspaceWhere = workspaceAccessFilter(userId);
173
+
174
+ const [workspaceMeta, folderCount, usage, limits, storageUsed] = await Promise.all([
175
+ prisma.workspace.aggregate({
176
+ where: workspaceWhere,
177
+ _count: { _all: true },
178
+ _max: { updatedAt: true },
179
+ }),
180
+ prisma.folder.count({ where: { ownerId: userId } }),
181
+ getUserUsage(userId),
182
+ getUserPlanLimits(userId),
183
+ prisma.fileAsset.aggregate({
184
+ where: {
185
+ userId,
186
+ workspace: workspaceWhere,
187
+ },
188
+ _sum: { size: true },
189
+ }),
190
+ ]);
191
+
192
+ const summary: AccountSummary = {
193
+ stats: {
194
+ workspaces: workspaceMeta._count._all,
195
+ folders: folderCount,
196
+ lastUpdated: workspaceMeta._max.updatedAt,
197
+ spaceUsed: Number(storageUsed._sum.size ?? 0),
198
+ spaceTotal: Number(limits.maxStorageBytes),
199
+ },
200
+ usage,
201
+ limits,
202
+ hasActivePlan: !limits.isFallbackPlan,
203
+ };
204
+
205
+ writeCache(accountSummaryCache, userId, summary);
206
+ return summary;
207
+ }