@goscribe/server 1.3.3 → 1.5.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.
- package/.env.example +12 -0
- package/.vscode/settings.json +3 -0
- package/REFACTOR_NOTES.md +60 -0
- package/TESTING_PROMPT.md +225 -0
- package/dist/controllers/admin.controller.d.ts +715 -0
- package/dist/controllers/admin.controller.js +9 -0
- package/dist/controllers/annotations.controller.d.ts +439 -0
- package/dist/controllers/annotations.controller.js +9 -0
- package/dist/controllers/app-router.controller.d.ts +3011 -0
- package/dist/controllers/app-router.controller.js +38 -0
- package/dist/controllers/app-router.controller.test.d.ts +1 -0
- package/dist/controllers/app-router.controller.test.js +36 -0
- package/dist/controllers/auth.controller.d.ts +323 -0
- package/dist/controllers/auth.controller.js +9 -0
- package/dist/controllers/base.controller.d.ts +4 -0
- package/dist/controllers/base.controller.js +5 -0
- package/dist/controllers/chat.controller.d.ts +341 -0
- package/dist/controllers/chat.controller.js +9 -0
- package/dist/controllers/copilot.controller.d.ts +397 -0
- package/dist/controllers/copilot.controller.js +9 -0
- package/dist/controllers/flashcards.controller.d.ts +651 -0
- package/dist/controllers/flashcards.controller.js +9 -0
- package/dist/controllers/members.controller.d.ts +339 -0
- package/dist/controllers/members.controller.js +9 -0
- package/dist/controllers/notifications.controller.d.ts +199 -0
- package/dist/controllers/notifications.controller.js +9 -0
- package/dist/controllers/payment.controller.d.ts +181 -0
- package/dist/controllers/payment.controller.js +9 -0
- package/dist/controllers/podcast.controller.d.ts +575 -0
- package/dist/controllers/podcast.controller.js +9 -0
- package/dist/controllers/router-module.controller.d.ts +5 -0
- package/dist/controllers/router-module.controller.js +6 -0
- package/dist/controllers/studyguide.controller.d.ts +73 -0
- package/dist/controllers/studyguide.controller.js +9 -0
- package/dist/controllers/worksheets.controller.d.ts +829 -0
- package/dist/controllers/worksheets.controller.js +9 -0
- package/dist/controllers/workspace.controller.d.ts +1207 -0
- package/dist/controllers/workspace.controller.js +9 -0
- package/dist/lib/activity_human_description.test.js +16 -15
- package/dist/lib/activity_log_service.test.js +28 -23
- package/dist/lib/ai/config.d.ts +20 -0
- package/dist/lib/ai/config.js +31 -0
- package/dist/lib/ai/embedding-client.d.ts +8 -0
- package/dist/lib/ai/embedding-client.js +30 -0
- package/dist/lib/ai/index.d.ts +47 -0
- package/dist/lib/ai/index.js +28 -0
- package/dist/lib/ai/inference-backend/client.d.ts +28 -0
- package/dist/lib/ai/inference-backend/client.js +301 -0
- package/dist/lib/ai/inference-backend/mocks.d.ts +12 -0
- package/dist/lib/ai/inference-backend/mocks.js +133 -0
- package/dist/lib/ai/inference-backend/types.d.ts +44 -0
- package/dist/lib/ai/inference-backend/types.js +1 -0
- package/dist/lib/ai/json-parse.d.ts +2 -0
- package/dist/lib/ai/json-parse.js +34 -0
- package/dist/lib/ai/llm-client.d.ts +6 -0
- package/dist/lib/ai/llm-client.js +19 -0
- package/dist/lib/ai/mock.d.ts +2 -0
- package/dist/lib/ai/mock.js +10 -0
- package/dist/lib/ai/types.d.ts +9 -0
- package/dist/lib/ai/types.js +1 -0
- package/dist/lib/chunking.d.ts +19 -0
- package/dist/lib/chunking.js +47 -0
- package/dist/lib/curated-kb-seed.d.ts +12 -0
- package/dist/lib/curated-kb-seed.js +155 -0
- package/dist/lib/email.js +67 -108
- package/dist/lib/embeddings.d.ts +2 -0
- package/dist/lib/embeddings.js +1 -0
- package/dist/lib/ensure-curated-kb-catalog.d.ts +6 -0
- package/dist/lib/ensure-curated-kb-catalog.js +53 -0
- package/dist/lib/env.d.ts +1 -5
- package/dist/lib/env.js +2 -7
- package/dist/lib/inference.d.ts +1 -8
- package/dist/lib/inference.js +1 -19
- package/dist/lib/kb-meta.d.ts +8 -0
- package/dist/lib/kb-meta.js +77 -0
- package/dist/lib/note-text.d.ts +1 -0
- package/dist/lib/note-text.js +47 -0
- package/dist/lib/notification-service.test.js +37 -36
- package/dist/lib/pdf.d.ts +11 -0
- package/dist/lib/pdf.js +11 -0
- package/dist/lib/usage_service.d.ts +2 -1
- package/dist/lib/usage_service.js +30 -12
- package/dist/lib/worksheet-generation.js +4 -4
- package/dist/lib/worksheet-generation.test.js +32 -17
- package/dist/lib/workspace-kb.d.ts +5 -0
- package/dist/lib/workspace-kb.js +7 -0
- package/dist/models/controller-context.model.d.ts +8 -0
- package/dist/models/controller-context.model.js +1 -0
- package/dist/repositories/artifact.repository.d.ts +60 -0
- package/dist/repositories/artifact.repository.js +40 -0
- package/dist/repositories/base.repository.d.ts +14 -0
- package/dist/repositories/base.repository.js +14 -0
- package/dist/repositories/invitation.repository.d.ts +94 -0
- package/dist/repositories/invitation.repository.js +44 -0
- package/dist/repositories/notification.repository.d.ts +72 -0
- package/dist/repositories/notification.repository.js +44 -0
- package/dist/repositories/router-module.repository.d.ts +10 -0
- package/dist/repositories/router-module.repository.js +14 -0
- package/dist/repositories/user.repository.d.ts +74 -0
- package/dist/repositories/user.repository.js +37 -0
- package/dist/repositories/workspace-member.repository.d.ts +31 -0
- package/dist/repositories/workspace-member.repository.js +31 -0
- package/dist/repositories/workspace.repository.d.ts +97 -0
- package/dist/repositories/workspace.repository.js +79 -0
- package/dist/routers/_app.d.ts +528 -33
- package/dist/routers/_app.js +4 -0
- package/dist/routers/admin.d.ts +0 -4
- package/dist/routers/admin.js +21 -549
- package/dist/routers/annotations.js +12 -170
- package/dist/routers/artifactVersions.d.ts +65 -0
- package/dist/routers/artifactVersions.js +14 -0
- package/dist/routers/auth.d.ts +0 -6
- package/dist/routers/auth.js +36 -421
- package/dist/routers/chat.js +15 -229
- package/dist/routers/copilot.d.ts +14 -13
- package/dist/routers/copilot.js +13 -532
- package/dist/routers/flashcards.d.ts +5 -5
- package/dist/routers/flashcards.js +23 -349
- package/dist/routers/knowledgeBase.d.ts +421 -0
- package/dist/routers/knowledgeBase.js +118 -0
- package/dist/routers/members.d.ts +0 -41
- package/dist/routers/members.js +22 -710
- package/dist/routers/notes.d.ts +94 -0
- package/dist/routers/notes.js +37 -0
- package/dist/routers/notifications.js +7 -109
- package/dist/routers/payment.d.ts +3 -2
- package/dist/routers/payment.js +11 -393
- package/dist/routers/podcast.d.ts +1 -1
- package/dist/routers/podcast.js +11 -784
- package/dist/routers/studyguide.js +3 -129
- package/dist/routers/worksheets.d.ts +29 -14
- package/dist/routers/worksheets.js +49 -628
- package/dist/routers/workspace.d.ts +0 -4
- package/dist/routers/workspace.js +28 -922
- package/dist/scripts/purge-deleted-users.js +2 -2
- package/dist/server.js +10 -3
- package/dist/services/activity/activity-human-description.service.d.ts +13 -0
- package/dist/services/activity/activity-human-description.service.js +221 -0
- package/dist/services/activity/activity-human-description.service.test.d.ts +1 -0
- package/dist/services/activity/activity-human-description.service.test.js +16 -0
- package/dist/services/activity/activity-log.service.d.ts +87 -0
- package/dist/services/activity/activity-log.service.js +276 -0
- package/dist/services/activity/activity-log.service.test.d.ts +1 -0
- package/dist/services/activity/activity-log.service.test.js +27 -0
- package/dist/services/activity-human-description.service.d.ts +13 -0
- package/dist/services/activity-human-description.service.js +221 -0
- package/dist/services/activity-human-description.service.test.d.ts +1 -0
- package/dist/services/activity-human-description.service.test.js +16 -0
- package/dist/services/activity-log.service.d.ts +87 -0
- package/dist/services/activity-log.service.js +276 -0
- package/dist/services/activity-log.service.test.d.ts +1 -0
- package/dist/services/activity-log.service.test.js +27 -0
- package/dist/services/admin/admin.service.d.ts +270 -0
- package/dist/services/admin/admin.service.js +476 -0
- package/dist/services/admin.service.d.ts +270 -0
- package/dist/services/admin.service.js +476 -0
- package/dist/services/ai/ai-session.service.d.ts +5 -0
- package/dist/services/ai/ai-session.service.js +4 -0
- package/dist/services/ai-session.service.d.ts +60 -0
- package/dist/services/ai-session.service.js +561 -0
- package/dist/services/annotation.service.d.ts +177 -0
- package/dist/services/annotation.service.js +154 -0
- package/dist/services/artifact-notification.service.d.ts +14 -0
- package/dist/services/artifact-notification.service.js +20 -0
- package/dist/services/artifact-version.service.d.ts +38 -0
- package/dist/services/artifact-version.service.js +129 -0
- package/dist/services/artifacts/annotation.service.d.ts +177 -0
- package/dist/services/artifacts/annotation.service.js +154 -0
- package/dist/services/artifacts/artifact-version.service.d.ts +38 -0
- package/dist/services/artifacts/artifact-version.service.js +129 -0
- package/dist/services/artifacts/chat.service.d.ts +127 -0
- package/dist/services/artifacts/chat.service.js +182 -0
- package/dist/services/artifacts/study-guide.service.d.ts +18 -0
- package/dist/services/artifacts/study-guide.service.js +65 -0
- package/dist/services/auth/auth.service.d.ts +94 -0
- package/dist/services/auth/auth.service.js +368 -0
- package/dist/services/auth.service.d.ts +94 -0
- package/dist/services/auth.service.js +368 -0
- package/dist/services/base.service.d.ts +14 -0
- package/dist/services/base.service.js +14 -0
- package/dist/services/billing/payment.service.d.ts +55 -0
- package/dist/services/billing/payment.service.js +368 -0
- package/dist/services/billing/subscription.service.d.ts +37 -0
- package/dist/services/billing/subscription.service.js +654 -0
- package/dist/services/billing/usage.service.d.ts +27 -0
- package/dist/services/billing/usage.service.js +77 -0
- package/dist/services/chat.service.d.ts +127 -0
- package/dist/services/chat.service.js +182 -0
- package/dist/services/content/copilot.service.d.ts +113 -0
- package/dist/services/content/copilot.service.js +453 -0
- package/dist/services/content/flashcard-progress.service.d.ts +159 -0
- package/dist/services/content/flashcard-progress.service.js +432 -0
- package/dist/services/content/flashcard.service.d.ts +140 -0
- package/dist/services/content/flashcard.service.js +326 -0
- package/dist/services/content/media-analysis.service.d.ts +23 -0
- package/dist/services/content/media-analysis.service.js +404 -0
- package/dist/services/content/podcast.service.d.ts +267 -0
- package/dist/services/content/podcast.service.js +653 -0
- package/dist/services/content/worksheet-content.service.d.ts +37 -0
- package/dist/services/content/worksheet-content.service.js +84 -0
- package/dist/services/content/worksheet-content.service.test.d.ts +1 -0
- package/dist/services/content/worksheet-content.service.test.js +69 -0
- package/dist/services/content/worksheet-generation.service.d.ts +91 -0
- package/dist/services/content/worksheet-generation.service.js +95 -0
- package/dist/services/content/worksheet-generation.service.test.d.ts +1 -0
- package/dist/services/content/worksheet-generation.service.test.js +20 -0
- package/dist/services/content/worksheet.service.d.ts +347 -0
- package/dist/services/content/worksheet.service.js +599 -0
- package/dist/services/copilot.service.d.ts +116 -0
- package/dist/services/copilot.service.js +447 -0
- package/dist/services/flashcard-progress.service.d.ts +2 -2
- package/dist/services/flashcard-progress.service.js +3 -2
- package/dist/services/flashcard.service.d.ts +140 -0
- package/dist/services/flashcard.service.js +325 -0
- package/dist/services/invitation.service.d.ts +66 -0
- package/dist/services/invitation.service.js +348 -0
- package/dist/services/knowledge/knowledge-base.service.d.ts +316 -0
- package/dist/services/knowledge/knowledge-base.service.js +544 -0
- package/dist/services/knowledge-base.service.d.ts +316 -0
- package/dist/services/knowledge-base.service.js +536 -0
- package/dist/services/media-analysis.service.d.ts +23 -0
- package/dist/services/media-analysis.service.js +384 -0
- package/dist/services/member.service.d.ts +36 -0
- package/dist/services/member.service.js +193 -0
- package/dist/services/members/invitation.service.d.ts +66 -0
- package/dist/services/members/invitation.service.js +348 -0
- package/dist/services/members/member.service.d.ts +36 -0
- package/dist/services/members/member.service.js +193 -0
- package/dist/services/note.service.d.ts +55 -0
- package/dist/services/note.service.js +111 -0
- package/dist/services/notification.service.d.ts +214 -0
- package/dist/services/notification.service.js +550 -0
- package/dist/services/notification.service.test.d.ts +1 -0
- package/dist/services/notification.service.test.js +87 -0
- package/dist/services/notifications/notification.service.d.ts +214 -0
- package/dist/services/notifications/notification.service.js +550 -0
- package/dist/services/notifications/notification.service.test.d.ts +1 -0
- package/dist/services/notifications/notification.service.test.js +87 -0
- package/dist/services/payment.service.d.ts +55 -0
- package/dist/services/payment.service.js +368 -0
- package/dist/services/podcast.service.d.ts +267 -0
- package/dist/services/podcast.service.js +654 -0
- package/dist/services/router-module.service.d.ts +7 -0
- package/dist/services/router-module.service.js +10 -0
- package/dist/services/study-guide.service.d.ts +18 -0
- package/dist/services/study-guide.service.js +65 -0
- package/dist/services/subscription.service.d.ts +37 -0
- package/dist/services/subscription.service.js +654 -0
- package/dist/services/usage-limit-policy.service.d.ts +12 -0
- package/dist/services/usage-limit-policy.service.js +22 -0
- package/dist/services/usage-limit-policy.service.test.d.ts +1 -0
- package/dist/services/usage-limit-policy.service.test.js +46 -0
- package/dist/services/usage.service.d.ts +27 -0
- package/dist/services/usage.service.js +77 -0
- package/dist/services/worksheet-content.service.d.ts +42 -0
- package/dist/services/worksheet-content.service.js +84 -0
- package/dist/services/worksheet-content.service.test.d.ts +1 -0
- package/dist/services/worksheet-content.service.test.js +69 -0
- package/dist/services/worksheet-generation.service.d.ts +91 -0
- package/dist/services/worksheet-generation.service.js +95 -0
- package/dist/services/worksheet-generation.service.test.d.ts +1 -0
- package/dist/services/worksheet-generation.service.test.js +20 -0
- package/dist/services/worksheet.service.d.ts +385 -0
- package/dist/services/worksheet.service.js +596 -0
- package/dist/services/workspace/workspace-analytics.service.d.ts +24 -0
- package/dist/services/workspace/workspace-analytics.service.js +95 -0
- package/dist/services/workspace/workspace-kb.service.d.ts +40 -0
- package/dist/services/workspace/workspace-kb.service.js +184 -0
- package/dist/services/workspace/workspace.service.d.ts +307 -0
- package/dist/services/workspace/workspace.service.js +394 -0
- package/dist/services/workspace-analytics.service.d.ts +24 -0
- package/dist/services/workspace-analytics.service.js +95 -0
- package/dist/services/workspace-kb.service.d.ts +40 -0
- package/dist/services/workspace-kb.service.js +184 -0
- package/dist/services/workspace-progress.service.d.ts +27 -0
- package/dist/services/workspace-progress.service.js +56 -0
- package/dist/services/workspace-progress.service.test.d.ts +1 -0
- package/dist/services/workspace-progress.service.test.js +49 -0
- package/dist/services/workspace.service.d.ts +307 -0
- package/dist/services/workspace.service.js +390 -0
- package/dist/trpc.js +2 -2
- package/package.json +5 -6
- package/prisma/migrations/20260509000001_add_knowledge_base/migration.sql +99 -0
- package/prisma/migrations/20260509000002_curate_knowledge_base/migration.sql +52 -0
- package/prisma/migrations/20260522000000_add_notes/migration.sql +27 -0
- package/prisma/migrations/20260524000000_remove_notes/migration.sql +3 -0
- package/prisma/schema.prisma +150 -48
- package/prisma/seed.mjs +67 -0
- package/scripts/debug/README.md +4 -0
- package/src/README.md +63 -0
- package/src/lib/ai/config.ts +34 -0
- package/src/lib/ai/embedding-client.ts +47 -0
- package/src/lib/ai/index.ts +62 -0
- package/src/lib/ai/inference-backend/client.ts +479 -0
- package/src/lib/ai/inference-backend/mocks.ts +171 -0
- package/src/lib/ai/inference-backend/types.ts +50 -0
- package/src/lib/ai/json-parse.ts +35 -0
- package/src/lib/ai/llm-client.ts +31 -0
- package/src/lib/ai/mock.ts +12 -0
- package/src/lib/ai/types.ts +11 -0
- package/src/lib/chunking.ts +81 -0
- package/src/lib/curated-kb-seed.ts +164 -0
- package/src/lib/email.ts +77 -115
- package/src/lib/embeddings.ts +9 -0
- package/src/lib/ensure-curated-kb-catalog.ts +60 -0
- package/src/lib/env.ts +2 -7
- package/src/lib/inference.ts +1 -21
- package/src/lib/kb-meta.ts +81 -0
- package/src/lib/pdf.ts +23 -0
- package/src/lib/workspace-kb.ts +7 -0
- package/src/repositories/artifact.repository.ts +55 -0
- package/src/repositories/base.repository.ts +19 -0
- package/src/repositories/invitation.repository.ts +53 -0
- package/src/repositories/notification.repository.ts +53 -0
- package/src/repositories/user.repository.ts +44 -0
- package/src/repositories/workspace-member.repository.ts +38 -0
- package/src/repositories/workspace.repository.ts +89 -0
- package/src/routers/_app.ts +4 -0
- package/src/routers/admin.ts +124 -692
- package/src/routers/annotations.ts +25 -203
- package/src/routers/artifactVersions.ts +32 -0
- package/src/routers/auth.ts +81 -519
- package/src/routers/chat.ts +42 -245
- package/src/routers/copilot.ts +41 -666
- package/src/routers/flashcards.ts +108 -404
- package/src/routers/knowledgeBase.ts +216 -0
- package/src/routers/members.ts +60 -782
- package/src/routers/notifications.ts +15 -117
- package/src/routers/payment.ts +37 -446
- package/src/routers/podcast.ts +36 -898
- package/src/routers/studyguide.ts +5 -144
- package/src/routers/worksheets.ts +171 -735
- package/src/routers/workspace.ts +138 -1109
- package/src/scripts/purge-deleted-users.ts +2 -2
- package/src/server.ts +10 -3
- package/src/{lib/activity_human_description.test.ts → services/activity/activity-human-description.service.test.ts} +1 -1
- package/src/{lib/activity_log_service.test.ts → services/activity/activity-log.service.test.ts} +1 -1
- package/src/{lib/activity_log_service.ts → services/activity/activity-log.service.ts} +2 -2
- package/src/services/admin/admin.service.ts +612 -0
- package/src/services/ai/ai-session.service.ts +5 -0
- package/src/services/artifacts/annotation.service.ts +189 -0
- package/src/services/artifacts/artifact-version.service.ts +151 -0
- package/src/services/artifacts/chat.service.ts +197 -0
- package/src/services/artifacts/study-guide.service.ts +72 -0
- package/src/services/auth/auth.service.ts +473 -0
- package/src/services/base.service.ts +19 -0
- package/src/services/billing/payment.service.ts +436 -0
- package/src/{lib/subscription_service.ts → services/billing/subscription.service.ts} +5 -5
- package/src/{lib/usage_service.ts → services/billing/usage.service.ts} +32 -12
- package/src/services/content/copilot.service.ts +596 -0
- package/src/services/{flashcard-progress.service.ts → content/flashcard-progress.service.ts} +6 -3
- package/src/services/content/flashcard.service.ts +394 -0
- package/src/services/content/media-analysis.service.ts +556 -0
- package/src/services/content/podcast.service.ts +777 -0
- package/src/services/content/worksheet-content.service.test.ts +83 -0
- package/src/services/content/worksheet-content.service.ts +117 -0
- package/src/{lib/worksheet-generation.test.ts → services/content/worksheet-generation.service.test.ts} +1 -1
- package/src/services/content/worksheet.service.ts +751 -0
- package/src/services/knowledge/knowledge-base.service.ts +705 -0
- package/src/services/members/invitation.service.ts +427 -0
- package/src/services/members/member.service.ts +241 -0
- package/src/{lib/notification-service.test.ts → services/notifications/notification.service.test.ts} +2 -2
- package/src/{lib/notification-service.ts → services/notifications/notification.service.ts} +102 -1
- package/src/services/workspace/workspace-analytics.service.ts +107 -0
- package/src/services/workspace/workspace-kb.service.ts +273 -0
- package/src/services/workspace/workspace.service.ts +481 -0
- package/src/trpc.ts +2 -2
- package/src/lib/ai-session.ts +0 -704
- package/src/lib/workspace-access.ts +0 -13
- /package/{check-difficulty.cjs → scripts/debug/check-difficulty.cjs} +0 -0
- /package/{check-questions.cjs → scripts/debug/check-questions.cjs} +0 -0
- /package/{db-summary.cjs → scripts/debug/db-summary.cjs} +0 -0
- /package/{mcq-test.cjs → scripts/debug/mcq-test.cjs} +0 -0
- /package/{test-generate.js → scripts/debug/test-generate.js} +0 -0
- /package/{test-ratio.cjs → scripts/debug/test-ratio.cjs} +0 -0
- /package/{zod-test.cjs → scripts/debug/zod-test.cjs} +0 -0
- /package/src/{lib/activity_human_description.ts → services/activity/activity-human-description.service.ts} +0 -0
- /package/src/{lib/worksheet-generation.ts → services/content/worksheet-generation.service.ts} +0 -0
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
import { BaseService } from '../base.service.js';
|
|
2
|
+
import { stripe } from '../../lib/stripe.js';
|
|
3
|
+
import { buildActivityLogWhere, deleteActivityLogsOlderThan, getActivityRetentionDays, } from '../activity/activity-log.service.js';
|
|
4
|
+
import { getActivityHumanDescription } from '../activity/activity-human-description.service.js';
|
|
5
|
+
function csvEscapeCell(value) {
|
|
6
|
+
if (value === null || value === undefined)
|
|
7
|
+
return '';
|
|
8
|
+
const s = String(value);
|
|
9
|
+
if (/[",\n\r]/.test(s))
|
|
10
|
+
return `"${s.replace(/"/g, '""')}"`;
|
|
11
|
+
return s;
|
|
12
|
+
}
|
|
13
|
+
export class AdminService extends BaseService {
|
|
14
|
+
constructor(db) {
|
|
15
|
+
super(db);
|
|
16
|
+
}
|
|
17
|
+
async getSystemStats() {
|
|
18
|
+
const totalUsers = await this.db.user.count();
|
|
19
|
+
const totalWorkspaces = await this.db.workspace.count();
|
|
20
|
+
const totalSubscriptions = await this.db.subscription.count({
|
|
21
|
+
where: { status: 'active' },
|
|
22
|
+
});
|
|
23
|
+
const subRevenueResult = await this.db.invoice.aggregate({
|
|
24
|
+
where: { status: 'paid', type: 'SUBSCRIPTION' },
|
|
25
|
+
_sum: { amountPaid: true },
|
|
26
|
+
});
|
|
27
|
+
const topupRevenueResult = await this.db.invoice.aggregate({
|
|
28
|
+
where: { status: 'paid', type: 'TOPUP' },
|
|
29
|
+
_sum: { amountPaid: true },
|
|
30
|
+
});
|
|
31
|
+
const subRevenue = Number(subRevenueResult._sum.amountPaid || 0) / 100;
|
|
32
|
+
const topupRevenue = Number(topupRevenueResult._sum.amountPaid || 0) / 100;
|
|
33
|
+
return {
|
|
34
|
+
totalUsers,
|
|
35
|
+
totalWorkspaces,
|
|
36
|
+
totalSubscriptions,
|
|
37
|
+
revenue: subRevenue + topupRevenue,
|
|
38
|
+
subscriptionRevenue: subRevenue,
|
|
39
|
+
topupRevenue,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
async listUsers(input) {
|
|
43
|
+
const search = input.search?.trim();
|
|
44
|
+
const baseRoleWhere = {
|
|
45
|
+
role: { name: { not: 'System Admin' } },
|
|
46
|
+
};
|
|
47
|
+
const searchWhere = search && search.length > 0
|
|
48
|
+
? {
|
|
49
|
+
OR: [
|
|
50
|
+
{ email: { contains: search, mode: 'insensitive' } },
|
|
51
|
+
{ name: { contains: search, mode: 'insensitive' } },
|
|
52
|
+
{ id: { contains: search } },
|
|
53
|
+
],
|
|
54
|
+
}
|
|
55
|
+
: undefined;
|
|
56
|
+
const verifiedWhere = input.emailVerified === 'yes'
|
|
57
|
+
? { emailVerified: { not: null } }
|
|
58
|
+
: input.emailVerified === 'no'
|
|
59
|
+
? { emailVerified: null }
|
|
60
|
+
: undefined;
|
|
61
|
+
const joinedWhere = input.joinedFrom || input.joinedTo
|
|
62
|
+
? {
|
|
63
|
+
createdAt: {
|
|
64
|
+
...(input.joinedFrom ? { gte: input.joinedFrom } : {}),
|
|
65
|
+
...(input.joinedTo ? { lte: input.joinedTo } : {}),
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
: undefined;
|
|
69
|
+
const where = {
|
|
70
|
+
AND: [
|
|
71
|
+
baseRoleWhere,
|
|
72
|
+
...(searchWhere ? [searchWhere] : []),
|
|
73
|
+
...(verifiedWhere ? [verifiedWhere] : []),
|
|
74
|
+
...(joinedWhere ? [joinedWhere] : []),
|
|
75
|
+
],
|
|
76
|
+
};
|
|
77
|
+
const totalCount = await this.db.user.count({ where });
|
|
78
|
+
const pageSize = input.pageSize;
|
|
79
|
+
const totalPages = Math.max(1, Math.ceil(totalCount / pageSize));
|
|
80
|
+
const page = Math.min(Math.max(1, input.page), totalPages);
|
|
81
|
+
const skip = (page - 1) * pageSize;
|
|
82
|
+
const users = await this.db.user.findMany({
|
|
83
|
+
where,
|
|
84
|
+
skip,
|
|
85
|
+
take: pageSize,
|
|
86
|
+
orderBy: { createdAt: 'desc' },
|
|
87
|
+
include: {
|
|
88
|
+
role: true,
|
|
89
|
+
profilePicture: true,
|
|
90
|
+
subscriptions: {
|
|
91
|
+
orderBy: { createdAt: 'desc' },
|
|
92
|
+
take: 1,
|
|
93
|
+
include: { plan: true },
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
return {
|
|
98
|
+
users: users.map((user) => ({
|
|
99
|
+
...user,
|
|
100
|
+
profilePicture: user.profilePicture?.objectKey
|
|
101
|
+
? `/profile-picture/${user.profilePicture.objectKey}?t=${new Date(user.updatedAt).getTime()}`
|
|
102
|
+
: null,
|
|
103
|
+
})),
|
|
104
|
+
page,
|
|
105
|
+
pageSize,
|
|
106
|
+
totalCount,
|
|
107
|
+
totalPages,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
async listInvoices(input) {
|
|
111
|
+
const search = input.search?.trim();
|
|
112
|
+
const searchWhere = search && search.length > 0
|
|
113
|
+
? {
|
|
114
|
+
OR: [
|
|
115
|
+
{ id: { contains: search } },
|
|
116
|
+
{ stripeInvoiceId: { contains: search, mode: 'insensitive' } },
|
|
117
|
+
{ user: { email: { contains: search, mode: 'insensitive' } } },
|
|
118
|
+
{ user: { name: { contains: search, mode: 'insensitive' } } },
|
|
119
|
+
],
|
|
120
|
+
}
|
|
121
|
+
: undefined;
|
|
122
|
+
const userIdWhere = input.userId?.trim() ? { userId: input.userId.trim() } : undefined;
|
|
123
|
+
const statusWhere = input.status?.trim() ? { status: input.status.trim() } : undefined;
|
|
124
|
+
const typeWhere = input.type ? { type: input.type } : undefined;
|
|
125
|
+
const dateWhere = input.from || input.to
|
|
126
|
+
? {
|
|
127
|
+
createdAt: {
|
|
128
|
+
...(input.from ? { gte: input.from } : {}),
|
|
129
|
+
...(input.to ? { lte: input.to } : {}),
|
|
130
|
+
},
|
|
131
|
+
}
|
|
132
|
+
: undefined;
|
|
133
|
+
const andParts = [
|
|
134
|
+
...(searchWhere ? [searchWhere] : []),
|
|
135
|
+
...(userIdWhere ? [userIdWhere] : []),
|
|
136
|
+
...(statusWhere ? [statusWhere] : []),
|
|
137
|
+
...(typeWhere ? [typeWhere] : []),
|
|
138
|
+
...(dateWhere ? [dateWhere] : []),
|
|
139
|
+
];
|
|
140
|
+
const where = andParts.length > 0 ? { AND: andParts } : {};
|
|
141
|
+
const totalCount = await this.db.invoice.count({ where });
|
|
142
|
+
const pageSize = input.pageSize;
|
|
143
|
+
const totalPages = Math.max(1, Math.ceil(totalCount / pageSize));
|
|
144
|
+
const page = Math.min(Math.max(1, input.page), totalPages);
|
|
145
|
+
const skip = (page - 1) * pageSize;
|
|
146
|
+
const invoices = await this.db.invoice.findMany({
|
|
147
|
+
where,
|
|
148
|
+
skip,
|
|
149
|
+
take: pageSize,
|
|
150
|
+
orderBy: { createdAt: 'desc' },
|
|
151
|
+
include: {
|
|
152
|
+
user: { include: { profilePicture: true } },
|
|
153
|
+
subscription: { include: { plan: true } },
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
const items = invoices.map((invoice) => ({
|
|
157
|
+
...invoice,
|
|
158
|
+
user: invoice.user
|
|
159
|
+
? {
|
|
160
|
+
...invoice.user,
|
|
161
|
+
profilePicture: invoice.user.profilePicture?.objectKey
|
|
162
|
+
? `/profile-picture/${invoice.user.profilePicture.objectKey}?t=${new Date(invoice.user.updatedAt).getTime()}`
|
|
163
|
+
: null,
|
|
164
|
+
}
|
|
165
|
+
: invoice.user,
|
|
166
|
+
}));
|
|
167
|
+
return { items, page, pageSize, totalCount, totalPages };
|
|
168
|
+
}
|
|
169
|
+
async listWorkspaces(input) {
|
|
170
|
+
const workspaces = await this.db.workspace.findMany({
|
|
171
|
+
take: input.limit + 1,
|
|
172
|
+
cursor: input.cursor ? { id: input.cursor } : undefined,
|
|
173
|
+
orderBy: { createdAt: 'desc' },
|
|
174
|
+
include: {
|
|
175
|
+
owner: { select: { name: true, email: true } },
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
let nextCursor = undefined;
|
|
179
|
+
if (workspaces.length > input.limit) {
|
|
180
|
+
const nextItem = workspaces.pop();
|
|
181
|
+
nextCursor = nextItem.id;
|
|
182
|
+
}
|
|
183
|
+
return { workspaces, nextCursor };
|
|
184
|
+
}
|
|
185
|
+
async updateUserRole(actorId, input) {
|
|
186
|
+
const role = await this.db.role.findUnique({ where: { name: input.roleName } });
|
|
187
|
+
if (!role) {
|
|
188
|
+
throw new Error(`Role ${input.roleName} not found`);
|
|
189
|
+
}
|
|
190
|
+
const updatedUser = await this.db.user.update({
|
|
191
|
+
where: { id: input.userId },
|
|
192
|
+
data: { roleId: role.id },
|
|
193
|
+
});
|
|
194
|
+
this.logger.info(`User ${input.userId} role updated to ${input.roleName} by admin ${actorId}`, 'ADMIN');
|
|
195
|
+
return updatedUser;
|
|
196
|
+
}
|
|
197
|
+
listPlans() {
|
|
198
|
+
return this.db.plan.findMany({
|
|
199
|
+
include: { limit: true },
|
|
200
|
+
orderBy: { price: 'asc' },
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
async upsertPlan(actorId, input) {
|
|
204
|
+
let { limits, id, stripePriceId, ...planData } = input;
|
|
205
|
+
if (!stripePriceId && !id) {
|
|
206
|
+
if (!stripe) {
|
|
207
|
+
throw new Error('Stripe is not configured on the server');
|
|
208
|
+
}
|
|
209
|
+
try {
|
|
210
|
+
const product = await stripe.products.create({
|
|
211
|
+
name: planData.name,
|
|
212
|
+
description: planData.description,
|
|
213
|
+
});
|
|
214
|
+
const price = await stripe.prices.create({
|
|
215
|
+
product: product.id,
|
|
216
|
+
unit_amount: Math.round(planData.price * 100),
|
|
217
|
+
currency: 'usd',
|
|
218
|
+
recurring: planData.type === 'subscription'
|
|
219
|
+
? { interval: planData.interval || 'month' }
|
|
220
|
+
: undefined,
|
|
221
|
+
});
|
|
222
|
+
stripePriceId = price.id;
|
|
223
|
+
this.logger.info(`Automatically created Stripe Product (${product.id}) and Price (${price.id}) for plan: ${planData.name}`, 'STRIPE');
|
|
224
|
+
}
|
|
225
|
+
catch (err) {
|
|
226
|
+
this.logger.error(`Failed to automate Stripe creation: ${err.message}`, 'STRIPE');
|
|
227
|
+
throw new Error(`Stripe error: ${err.message}`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (id) {
|
|
231
|
+
const updatedPlan = await this.db.plan.update({
|
|
232
|
+
where: { id },
|
|
233
|
+
data: {
|
|
234
|
+
...planData,
|
|
235
|
+
stripePriceId,
|
|
236
|
+
limit: {
|
|
237
|
+
update: {
|
|
238
|
+
maxStorageBytes: BigInt(limits.maxStorageBytes),
|
|
239
|
+
maxWorksheets: limits.maxWorksheets,
|
|
240
|
+
maxFlashcards: limits.maxFlashcards,
|
|
241
|
+
maxPodcasts: limits.maxPodcasts,
|
|
242
|
+
maxStudyGuides: limits.maxStudyGuides,
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
this.logger.info(`Plan ${id} updated by admin ${actorId}`, 'ADMIN');
|
|
248
|
+
return updatedPlan;
|
|
249
|
+
}
|
|
250
|
+
const newPlan = await this.db.plan.create({
|
|
251
|
+
data: {
|
|
252
|
+
...planData,
|
|
253
|
+
stripePriceId: stripePriceId || '',
|
|
254
|
+
limit: {
|
|
255
|
+
create: {
|
|
256
|
+
maxStorageBytes: BigInt(limits.maxStorageBytes),
|
|
257
|
+
maxWorksheets: limits.maxWorksheets,
|
|
258
|
+
maxFlashcards: limits.maxFlashcards,
|
|
259
|
+
maxPodcasts: limits.maxPodcasts,
|
|
260
|
+
maxStudyGuides: limits.maxStudyGuides,
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
this.logger.info(`New plan ${newPlan.id} created by admin ${actorId}`, 'ADMIN');
|
|
266
|
+
return newPlan;
|
|
267
|
+
}
|
|
268
|
+
async deletePlan(actorId, id) {
|
|
269
|
+
const activeSubs = await this.db.subscription.count({
|
|
270
|
+
where: { planId: id, status: 'active' },
|
|
271
|
+
});
|
|
272
|
+
if (activeSubs > 0) {
|
|
273
|
+
await this.db.plan.update({ where: { id }, data: { active: false } });
|
|
274
|
+
return {
|
|
275
|
+
success: true,
|
|
276
|
+
message: 'Plan deactivated because it has active subscribers.',
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
await this.db.plan.delete({ where: { id } });
|
|
280
|
+
this.logger.info(`Plan ${id} deleted by admin ${actorId}`, 'ADMIN');
|
|
281
|
+
return { success: true, message: 'Plan deleted.' };
|
|
282
|
+
}
|
|
283
|
+
getUserInvoices(input) {
|
|
284
|
+
return this.db.invoice.findMany({
|
|
285
|
+
where: { userId: input.userId },
|
|
286
|
+
orderBy: { createdAt: 'desc' },
|
|
287
|
+
take: input.limit,
|
|
288
|
+
include: {
|
|
289
|
+
subscription: { include: { plan: true } },
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
async getUserDetailedInfo(userId) {
|
|
294
|
+
const user = await this.db.user.findUnique({
|
|
295
|
+
where: { id: userId },
|
|
296
|
+
include: {
|
|
297
|
+
role: true,
|
|
298
|
+
profilePicture: true,
|
|
299
|
+
subscriptions: {
|
|
300
|
+
orderBy: { createdAt: 'desc' },
|
|
301
|
+
take: 1,
|
|
302
|
+
include: { plan: true },
|
|
303
|
+
},
|
|
304
|
+
_count: {
|
|
305
|
+
select: { workspaces: true, artifacts: true },
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
});
|
|
309
|
+
if (!user)
|
|
310
|
+
throw new Error('User not found');
|
|
311
|
+
const totalSpentResult = await this.db.invoice.aggregate({
|
|
312
|
+
where: { userId, status: 'paid' },
|
|
313
|
+
_sum: { amountPaid: true },
|
|
314
|
+
});
|
|
315
|
+
const totalSpent = Number(totalSpentResult._sum.amountPaid || 0) / 100;
|
|
316
|
+
const purchasedCredits = await this.db.userCredit.groupBy({
|
|
317
|
+
by: ['resourceType'],
|
|
318
|
+
where: { userId },
|
|
319
|
+
_sum: { amount: true },
|
|
320
|
+
});
|
|
321
|
+
const profilePictureUrl = user.profilePicture?.objectKey
|
|
322
|
+
? `/profile-picture/${user.profilePicture.objectKey}?t=${new Date(user.updatedAt).getTime()}`
|
|
323
|
+
: null;
|
|
324
|
+
return {
|
|
325
|
+
...user,
|
|
326
|
+
totalSpent,
|
|
327
|
+
purchasedCredits: purchasedCredits.map((c) => ({
|
|
328
|
+
type: c.resourceType,
|
|
329
|
+
amount: c._sum.amount || 0,
|
|
330
|
+
})),
|
|
331
|
+
profilePicture: profilePictureUrl,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
async debugInvoices() {
|
|
335
|
+
const count = await this.db.invoice.count();
|
|
336
|
+
const all = await this.db.invoice.findMany({ take: 10 });
|
|
337
|
+
return { count, all };
|
|
338
|
+
}
|
|
339
|
+
listResourcePrices() {
|
|
340
|
+
return this.db.resourcePrice.findMany({
|
|
341
|
+
orderBy: { resourceType: 'asc' },
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
async upsertResourcePrice(actorId, input) {
|
|
345
|
+
const price = await this.db.resourcePrice.upsert({
|
|
346
|
+
where: { resourceType: input.resourceType },
|
|
347
|
+
update: { priceCents: input.priceCents },
|
|
348
|
+
create: {
|
|
349
|
+
resourceType: input.resourceType,
|
|
350
|
+
priceCents: input.priceCents,
|
|
351
|
+
},
|
|
352
|
+
});
|
|
353
|
+
this.logger.info(`Resource price for ${input.resourceType} updated to ${input.priceCents} by admin ${actorId}`, 'ADMIN');
|
|
354
|
+
return price;
|
|
355
|
+
}
|
|
356
|
+
async listRecentInvoices(limit) {
|
|
357
|
+
const invoices = await this.db.invoice.findMany({
|
|
358
|
+
take: limit,
|
|
359
|
+
orderBy: { createdAt: 'desc' },
|
|
360
|
+
include: {
|
|
361
|
+
user: { include: { profilePicture: true } },
|
|
362
|
+
},
|
|
363
|
+
});
|
|
364
|
+
return invoices.map((invoice) => ({
|
|
365
|
+
...invoice,
|
|
366
|
+
user: {
|
|
367
|
+
...invoice.user,
|
|
368
|
+
profilePicture: invoice.user.profilePicture?.objectKey
|
|
369
|
+
? `/profile-picture/${invoice.user.profilePicture.objectKey}?t=${new Date(invoice.user.updatedAt).getTime()}`
|
|
370
|
+
: null,
|
|
371
|
+
},
|
|
372
|
+
}));
|
|
373
|
+
}
|
|
374
|
+
async activityList(input) {
|
|
375
|
+
const where = buildActivityLogWhere({
|
|
376
|
+
from: input.from,
|
|
377
|
+
to: input.to,
|
|
378
|
+
actorUserId: input.actorUserId,
|
|
379
|
+
workspaceId: input.workspaceId,
|
|
380
|
+
category: input.category,
|
|
381
|
+
status: input.status,
|
|
382
|
+
search: input.search,
|
|
383
|
+
});
|
|
384
|
+
const totalCount = await this.db.activityLog.count({ where });
|
|
385
|
+
const totalPages = Math.max(1, Math.ceil(totalCount / input.limit));
|
|
386
|
+
const page = Math.min(Math.max(1, input.page), totalPages);
|
|
387
|
+
const skip = (page - 1) * input.limit;
|
|
388
|
+
const rows = await this.db.activityLog.findMany({
|
|
389
|
+
where,
|
|
390
|
+
skip,
|
|
391
|
+
take: input.limit,
|
|
392
|
+
orderBy: { createdAt: 'desc' },
|
|
393
|
+
include: {
|
|
394
|
+
actor: { select: { id: true, email: true, name: true } },
|
|
395
|
+
workspace: { select: { id: true, title: true } },
|
|
396
|
+
},
|
|
397
|
+
});
|
|
398
|
+
return {
|
|
399
|
+
items: rows.map((r) => ({
|
|
400
|
+
id: r.id,
|
|
401
|
+
createdAt: r.createdAt,
|
|
402
|
+
action: r.action,
|
|
403
|
+
description: getActivityHumanDescription(r.trpcPath, r.action),
|
|
404
|
+
category: r.category,
|
|
405
|
+
trpcPath: r.trpcPath,
|
|
406
|
+
status: r.status,
|
|
407
|
+
durationMs: r.durationMs,
|
|
408
|
+
errorCode: r.errorCode,
|
|
409
|
+
ipAddress: r.ipAddress,
|
|
410
|
+
actor: r.actor,
|
|
411
|
+
workspace: r.workspace,
|
|
412
|
+
metadata: r.metadata,
|
|
413
|
+
})),
|
|
414
|
+
page,
|
|
415
|
+
pageSize: input.limit,
|
|
416
|
+
totalCount,
|
|
417
|
+
totalPages,
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
async activityExportCsv(input) {
|
|
421
|
+
const where = buildActivityLogWhere({
|
|
422
|
+
from: input.from,
|
|
423
|
+
to: input.to,
|
|
424
|
+
actorUserId: input.actorUserId,
|
|
425
|
+
workspaceId: input.workspaceId,
|
|
426
|
+
category: input.category,
|
|
427
|
+
status: input.status,
|
|
428
|
+
search: input.search,
|
|
429
|
+
});
|
|
430
|
+
const rows = await this.db.activityLog.findMany({
|
|
431
|
+
where,
|
|
432
|
+
orderBy: { createdAt: 'desc' },
|
|
433
|
+
take: input.maxRows,
|
|
434
|
+
include: {
|
|
435
|
+
actor: { select: { email: true, name: true } },
|
|
436
|
+
workspace: { select: { title: true } },
|
|
437
|
+
},
|
|
438
|
+
});
|
|
439
|
+
const header = [
|
|
440
|
+
'createdAt',
|
|
441
|
+
'actorEmail',
|
|
442
|
+
'actorName',
|
|
443
|
+
'description',
|
|
444
|
+
'trpcPath',
|
|
445
|
+
'category',
|
|
446
|
+
'status',
|
|
447
|
+
'durationMs',
|
|
448
|
+
'workspaceTitle',
|
|
449
|
+
'errorCode',
|
|
450
|
+
].join(',');
|
|
451
|
+
const lines = rows.map((r) => [
|
|
452
|
+
csvEscapeCell(r.createdAt.toISOString()),
|
|
453
|
+
csvEscapeCell(r.actor?.email),
|
|
454
|
+
csvEscapeCell(r.actor?.name),
|
|
455
|
+
csvEscapeCell(getActivityHumanDescription(r.trpcPath, r.action)),
|
|
456
|
+
csvEscapeCell(r.trpcPath),
|
|
457
|
+
csvEscapeCell(r.category),
|
|
458
|
+
csvEscapeCell(r.status),
|
|
459
|
+
csvEscapeCell(r.durationMs),
|
|
460
|
+
csvEscapeCell(r.workspace?.title),
|
|
461
|
+
csvEscapeCell(r.errorCode),
|
|
462
|
+
].join(','));
|
|
463
|
+
return {
|
|
464
|
+
csv: [header, ...lines].join('\n'),
|
|
465
|
+
count: rows.length,
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
async activityPurgeRetention() {
|
|
469
|
+
const days = getActivityRetentionDays();
|
|
470
|
+
const cutoff = new Date();
|
|
471
|
+
cutoff.setDate(cutoff.getDate() - days);
|
|
472
|
+
const { deleted } = await deleteActivityLogsOlderThan(this.db, cutoff);
|
|
473
|
+
this.logger.info(`ActivityLog retention purge: deleted ${deleted} rows older than ${days} days (cutoff ${cutoff.toISOString()})`, 'ADMIN');
|
|
474
|
+
return { deleted, retentionDays: days, cutoff };
|
|
475
|
+
}
|
|
476
|
+
}
|