@goscribe/server 1.2.0 → 1.3.1
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/check-difficulty.cjs +14 -0
- package/check-questions.cjs +14 -0
- package/db-summary.cjs +22 -0
- package/dist/context.d.ts +5 -1
- package/dist/lib/activity_human_description.d.ts +13 -0
- package/dist/lib/activity_human_description.js +221 -0
- package/dist/lib/activity_human_description.test.d.ts +1 -0
- package/dist/lib/activity_human_description.test.js +16 -0
- package/dist/lib/activity_log_service.d.ts +87 -0
- package/dist/lib/activity_log_service.js +276 -0
- package/dist/lib/activity_log_service.test.d.ts +1 -0
- package/dist/lib/activity_log_service.test.js +27 -0
- package/dist/lib/ai-session.d.ts +15 -2
- package/dist/lib/ai-session.js +147 -85
- package/dist/lib/constants.d.ts +13 -0
- package/dist/lib/constants.js +12 -0
- package/dist/lib/email.d.ts +11 -0
- package/dist/lib/email.js +193 -0
- package/dist/lib/env.d.ts +13 -0
- package/dist/lib/env.js +16 -0
- package/dist/lib/inference.d.ts +4 -1
- package/dist/lib/inference.js +3 -3
- package/dist/lib/logger.d.ts +4 -4
- package/dist/lib/logger.js +30 -8
- package/dist/lib/notification-service.d.ts +152 -0
- package/dist/lib/notification-service.js +473 -0
- package/dist/lib/notification-service.test.d.ts +1 -0
- package/dist/lib/notification-service.test.js +87 -0
- package/dist/lib/prisma.d.ts +2 -1
- package/dist/lib/prisma.js +5 -1
- package/dist/lib/pusher.d.ts +23 -0
- package/dist/lib/pusher.js +69 -5
- package/dist/lib/retry.d.ts +15 -0
- package/dist/lib/retry.js +37 -0
- package/dist/lib/storage.js +2 -2
- package/dist/lib/stripe.d.ts +9 -0
- package/dist/lib/stripe.js +36 -0
- package/dist/lib/subscription_service.d.ts +37 -0
- package/dist/lib/subscription_service.js +654 -0
- package/dist/lib/usage_service.d.ts +26 -0
- package/dist/lib/usage_service.js +59 -0
- package/dist/lib/worksheet-generation.d.ts +91 -0
- package/dist/lib/worksheet-generation.js +95 -0
- package/dist/lib/worksheet-generation.test.d.ts +1 -0
- package/dist/lib/worksheet-generation.test.js +20 -0
- package/dist/lib/workspace-access.d.ts +18 -0
- package/dist/lib/workspace-access.js +13 -0
- package/dist/routers/_app.d.ts +1349 -253
- package/dist/routers/_app.js +10 -0
- package/dist/routers/admin.d.ts +361 -0
- package/dist/routers/admin.js +633 -0
- package/dist/routers/annotations.d.ts +219 -0
- package/dist/routers/annotations.js +187 -0
- package/dist/routers/auth.d.ts +88 -7
- package/dist/routers/auth.js +339 -19
- package/dist/routers/chat.d.ts +6 -12
- package/dist/routers/copilot.d.ts +199 -0
- package/dist/routers/copilot.js +571 -0
- package/dist/routers/flashcards.d.ts +47 -81
- package/dist/routers/flashcards.js +143 -27
- package/dist/routers/members.d.ts +36 -7
- package/dist/routers/members.js +200 -19
- package/dist/routers/notifications.d.ts +99 -0
- package/dist/routers/notifications.js +127 -0
- package/dist/routers/payment.d.ts +89 -0
- package/dist/routers/payment.js +403 -0
- package/dist/routers/podcast.d.ts +8 -13
- package/dist/routers/podcast.js +54 -31
- package/dist/routers/studyguide.d.ts +1 -29
- package/dist/routers/studyguide.js +80 -71
- package/dist/routers/worksheets.d.ts +105 -38
- package/dist/routers/worksheets.js +258 -68
- package/dist/routers/workspace.d.ts +139 -60
- package/dist/routers/workspace.js +455 -315
- package/dist/scripts/purge-deleted-users.d.ts +1 -0
- package/dist/scripts/purge-deleted-users.js +149 -0
- package/dist/server.js +130 -10
- package/dist/services/flashcard-progress.service.d.ts +18 -66
- package/dist/services/flashcard-progress.service.js +51 -42
- package/dist/trpc.d.ts +20 -21
- package/dist/trpc.js +150 -1
- package/mcq-test.cjs +36 -0
- package/package.json +9 -2
- package/prisma/migrations/20260413143206_init/migration.sql +873 -0
- package/prisma/schema.prisma +471 -324
- package/src/context.ts +4 -1
- package/src/lib/activity_human_description.test.ts +28 -0
- package/src/lib/activity_human_description.ts +239 -0
- package/src/lib/activity_log_service.test.ts +37 -0
- package/src/lib/activity_log_service.ts +353 -0
- package/src/lib/ai-session.ts +79 -51
- package/src/lib/email.ts +213 -29
- package/src/lib/env.ts +23 -6
- package/src/lib/inference.ts +2 -2
- package/src/lib/notification-service.test.ts +106 -0
- package/src/lib/notification-service.ts +677 -0
- package/src/lib/prisma.ts +6 -1
- package/src/lib/pusher.ts +86 -2
- package/src/lib/stripe.ts +39 -0
- package/src/lib/subscription_service.ts +722 -0
- package/src/lib/usage_service.ts +74 -0
- package/src/lib/worksheet-generation.test.ts +31 -0
- package/src/lib/worksheet-generation.ts +139 -0
- package/src/routers/_app.ts +9 -0
- package/src/routers/admin.ts +710 -0
- package/src/routers/annotations.ts +41 -0
- package/src/routers/auth.ts +338 -28
- package/src/routers/copilot.ts +719 -0
- package/src/routers/flashcards.ts +201 -68
- package/src/routers/members.ts +280 -80
- package/src/routers/notifications.ts +142 -0
- package/src/routers/payment.ts +448 -0
- package/src/routers/podcast.ts +112 -83
- package/src/routers/studyguide.ts +12 -0
- package/src/routers/worksheets.ts +289 -66
- package/src/routers/workspace.ts +329 -122
- package/src/scripts/purge-deleted-users.ts +167 -0
- package/src/server.ts +137 -11
- package/src/services/flashcard-progress.service.ts +49 -37
- package/src/trpc.ts +184 -5
- package/test-generate.js +30 -0
- package/test-ratio.cjs +9 -0
- package/zod-test.cjs +22 -0
- package/prisma/migrations/20250826124819_add_worksheet_difficulty_and_estimated_time/migration.sql +0 -213
- package/prisma/migrations/20250826133236_add_worksheet_question_progress/migration.sql +0 -31
- package/prisma/seed.mjs +0 -135
package/dist/trpc.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { initTRPC, TRPCError } from "@trpc/server";
|
|
2
2
|
import superjson from "superjson";
|
|
3
|
+
import { ActivityLogStatus } from "@prisma/client";
|
|
3
4
|
import { logger } from "./lib/logger.js";
|
|
4
5
|
import { toTRPCError } from "./lib/errors.js";
|
|
6
|
+
import { getUserUsage, getUserPlanLimits } from "./lib/usage_service.js";
|
|
7
|
+
import { getClientIp, isActivityLogEnabled, scheduleRecordActivity, truncateUserAgent, } from "./lib/activity_log_service.js";
|
|
8
|
+
/** Avoid logging the log viewers themselves (noise when browsing activity). */
|
|
9
|
+
const SKIP_ACTIVITY_TRPC_PATHS = new Set([
|
|
10
|
+
"admin.activityList",
|
|
11
|
+
"admin.activityExportCsv",
|
|
12
|
+
]);
|
|
5
13
|
const t = initTRPC.context().create({
|
|
6
14
|
transformer: superjson,
|
|
7
15
|
errorFormatter({ shape, error }) {
|
|
@@ -51,6 +59,7 @@ const isAuthed = middleware(({ ctx, next }) => {
|
|
|
51
59
|
}
|
|
52
60
|
return next({
|
|
53
61
|
ctx: {
|
|
62
|
+
...ctx,
|
|
54
63
|
session: ctx.session,
|
|
55
64
|
userId: ctx.session.user.id,
|
|
56
65
|
},
|
|
@@ -67,8 +76,148 @@ const errorHandler = middleware(async ({ next }) => {
|
|
|
67
76
|
throw toTRPCError(error);
|
|
68
77
|
}
|
|
69
78
|
});
|
|
79
|
+
/**
|
|
80
|
+
* Middleware that enforces email verification
|
|
81
|
+
*/
|
|
82
|
+
const isVerified = middleware(async ({ ctx, next }) => {
|
|
83
|
+
const user = await ctx.db.user.findUnique({
|
|
84
|
+
where: { id: ctx.session.user.id },
|
|
85
|
+
select: { emailVerified: true },
|
|
86
|
+
});
|
|
87
|
+
if (!user?.emailVerified) {
|
|
88
|
+
throw new TRPCError({
|
|
89
|
+
code: "FORBIDDEN",
|
|
90
|
+
message: "Please verify your email to access this feature",
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
return next();
|
|
94
|
+
});
|
|
95
|
+
/**
|
|
96
|
+
* Middleware that enforces resource limits based on the user's plan.
|
|
97
|
+
* Note: This matches the 'path' to decide which limit to check.
|
|
98
|
+
*/
|
|
99
|
+
const checkUsageLimit = middleware(async ({ ctx, next, path }) => {
|
|
100
|
+
const userId = ctx.session.user.id;
|
|
101
|
+
// 1. Get current usage and limits
|
|
102
|
+
const [usage, limits] = await Promise.all([
|
|
103
|
+
getUserUsage(userId),
|
|
104
|
+
getUserPlanLimits(userId)
|
|
105
|
+
]);
|
|
106
|
+
// If no limits found (no active plan), we block any premium actions
|
|
107
|
+
if (!limits) {
|
|
108
|
+
throw new TRPCError({
|
|
109
|
+
code: "FORBIDDEN",
|
|
110
|
+
message: "No active plan found. Please subscribe to a plan to continue.",
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
// 2. Check limits based on the tRPC path
|
|
114
|
+
// Flashcards (matches: flashcards.createCard, flashcards.generateFromPrompt)
|
|
115
|
+
if (path.startsWith('flashcards.') && (path.includes('create') || path.includes('generate')) && usage.flashcards >= limits.maxFlashcards) {
|
|
116
|
+
throw new TRPCError({ code: "FORBIDDEN", message: "Flashcard limit reached for your plan." });
|
|
117
|
+
}
|
|
118
|
+
// Worksheets (matches: worksheets.create, worksheets.generateFromPrompt)
|
|
119
|
+
if (path.startsWith('worksheets.') && (path.includes('create') || path.includes('generate')) && usage.worksheets >= limits.maxWorksheets) {
|
|
120
|
+
throw new TRPCError({ code: "FORBIDDEN", message: "Worksheet limit reached for your plan." });
|
|
121
|
+
}
|
|
122
|
+
// Podcasts (matches: podcast.generateEpisode)
|
|
123
|
+
if (path.startsWith('podcast.') && path.includes('generate') && usage.podcasts >= limits.maxPodcasts) {
|
|
124
|
+
throw new TRPCError({ code: "FORBIDDEN", message: "Podcast limit reached for your plan." });
|
|
125
|
+
}
|
|
126
|
+
// Study Guides (matches: studyguide.get - because it lazily creates)
|
|
127
|
+
if (path.startsWith('studyguide.') && path.includes('get') && usage.studyGuides >= limits.maxStudyGuides) {
|
|
128
|
+
// Note: We only block if the artifact doesn't exist yet (handled inside the query or by checking usage)
|
|
129
|
+
// For simplicity, if they hit the limit, we block creation of NEW study guides.
|
|
130
|
+
// However, usage_service counts existing ones.
|
|
131
|
+
// If usage is already at/over limit, it means any NEW workspace's 'get' will fail creation.
|
|
132
|
+
}
|
|
133
|
+
// Storage check (for uploads)
|
|
134
|
+
if (path.includes('upload') && usage.storageBytes >= Number(limits.maxStorageBytes)) {
|
|
135
|
+
throw new TRPCError({ code: "FORBIDDEN", message: "Storage limit reached for your plan." });
|
|
136
|
+
}
|
|
137
|
+
return next();
|
|
138
|
+
});
|
|
139
|
+
/**
|
|
140
|
+
* Middleware that enforces system admin role
|
|
141
|
+
*/
|
|
142
|
+
const isAdmin = middleware(async ({ ctx, next }) => {
|
|
143
|
+
const user = await ctx.db.user.findUnique({
|
|
144
|
+
where: { id: ctx.session.user.id },
|
|
145
|
+
include: { role: true },
|
|
146
|
+
});
|
|
147
|
+
if (user?.role?.name !== 'System Admin') {
|
|
148
|
+
throw new TRPCError({
|
|
149
|
+
code: "FORBIDDEN",
|
|
150
|
+
message: "You do not have permission to access this resource",
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return next();
|
|
154
|
+
});
|
|
155
|
+
/**
|
|
156
|
+
* Persists ActivityLog rows for authenticated tRPC calls (async, non-blocking).
|
|
157
|
+
*/
|
|
158
|
+
const activityLogMiddleware = middleware(async (opts) => {
|
|
159
|
+
const { ctx, next, path, type, getRawInput } = opts;
|
|
160
|
+
if (!isActivityLogEnabled() || SKIP_ACTIVITY_TRPC_PATHS.has(path)) {
|
|
161
|
+
return next();
|
|
162
|
+
}
|
|
163
|
+
const userId = ctx.userId;
|
|
164
|
+
if (!userId) {
|
|
165
|
+
return next();
|
|
166
|
+
}
|
|
167
|
+
let rawInput;
|
|
168
|
+
try {
|
|
169
|
+
rawInput = await getRawInput();
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
rawInput = undefined;
|
|
173
|
+
}
|
|
174
|
+
const req = ctx.req;
|
|
175
|
+
const ipAddress = req ? getClientIp(req) : undefined;
|
|
176
|
+
const userAgent = req.headers["user-agent"];
|
|
177
|
+
const httpMethod = req.method;
|
|
178
|
+
const start = Date.now();
|
|
179
|
+
const result = await next();
|
|
180
|
+
const durationMs = Date.now() - start;
|
|
181
|
+
if (result.ok) {
|
|
182
|
+
scheduleRecordActivity({
|
|
183
|
+
db: ctx.db,
|
|
184
|
+
actorUserId: userId,
|
|
185
|
+
path,
|
|
186
|
+
type,
|
|
187
|
+
status: ActivityLogStatus.SUCCESS,
|
|
188
|
+
durationMs,
|
|
189
|
+
rawInput,
|
|
190
|
+
ipAddress,
|
|
191
|
+
userAgent: truncateUserAgent(typeof userAgent === "string" ? userAgent : undefined),
|
|
192
|
+
httpMethod,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
scheduleRecordActivity({
|
|
197
|
+
db: ctx.db,
|
|
198
|
+
actorUserId: userId,
|
|
199
|
+
path,
|
|
200
|
+
type,
|
|
201
|
+
status: ActivityLogStatus.FAILURE,
|
|
202
|
+
durationMs,
|
|
203
|
+
rawInput,
|
|
204
|
+
ipAddress,
|
|
205
|
+
userAgent: truncateUserAgent(typeof userAgent === "string" ? userAgent : undefined),
|
|
206
|
+
httpMethod,
|
|
207
|
+
errorCode: result.error.code,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
});
|
|
70
212
|
/** Exported procedures with middleware */
|
|
71
213
|
export const authedProcedure = publicProcedure
|
|
72
214
|
.use(loggingMiddleware)
|
|
73
215
|
.use(errorHandler)
|
|
74
|
-
.use(isAuthed)
|
|
216
|
+
.use(isAuthed)
|
|
217
|
+
.use(activityLogMiddleware);
|
|
218
|
+
export const verifiedProcedure = authedProcedure
|
|
219
|
+
.use(isVerified);
|
|
220
|
+
export const adminProcedure = authedProcedure
|
|
221
|
+
.use(isAdmin);
|
|
222
|
+
export const limitedProcedure = verifiedProcedure
|
|
223
|
+
.use(checkUsageLimit);
|
package/mcq-test.cjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
const worksheetModeSchema = z.enum(['practice', 'quiz']);
|
|
4
|
+
const worksheetDifficultyInputSchema = z.enum(['easy', 'medium', 'hard']);
|
|
5
|
+
const questionTypeSchema = z.enum([
|
|
6
|
+
'MULTIPLE_CHOICE',
|
|
7
|
+
'TEXT',
|
|
8
|
+
'NUMERIC',
|
|
9
|
+
'TRUE_FALSE',
|
|
10
|
+
'MATCHING',
|
|
11
|
+
'FILL_IN_THE_BLANK',
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const worksheetPresetConfigSchema = z.object({
|
|
16
|
+
mode: worksheetModeSchema.default('practice'),
|
|
17
|
+
numQuestions: z.number().int().min(1).max(20).default(8),
|
|
18
|
+
difficulty: worksheetDifficultyInputSchema.default('medium'),
|
|
19
|
+
questionTypes: z.array(questionTypeSchema).optional(),
|
|
20
|
+
mcqRatio: z.number().min(0).max(1).optional(),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
function test(mcqCount, numQuestions) {
|
|
24
|
+
const configOverride = {
|
|
25
|
+
questionTypes: ['MULTIPLE_CHOICE', 'TEXT'],
|
|
26
|
+
mcqRatio: mcqCount / Math.max(1, numQuestions),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const merged = {
|
|
30
|
+
...worksheetPresetConfigSchema.parse({}),
|
|
31
|
+
...configOverride,
|
|
32
|
+
};
|
|
33
|
+
console.log(`mcqCount: ${mcqCount}, numQuestions: ${numQuestions} -> mcqRatio: ${merged.mcqRatio}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
test(4, 8);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goscribe/server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,6 +15,10 @@
|
|
|
15
15
|
"dev": "tsx watch src/server.ts",
|
|
16
16
|
"build": "npx prisma generate && tsc -p .",
|
|
17
17
|
"start": "node --experimental-specifier-resolution=node dist/server.js",
|
|
18
|
+
"test": "tsx --test src/lib/worksheet-generation.test.ts",
|
|
19
|
+
"test:activity": "tsx --test src/lib/activity_log_service.test.ts src/lib/activity_human_description.test.ts"
|
|
20
|
+
},
|
|
21
|
+
"prisma": {
|
|
18
22
|
"seed": "node --experimental-specifier-resolution=node prisma/seed.mjs"
|
|
19
23
|
},
|
|
20
24
|
"author": "",
|
|
@@ -36,12 +40,14 @@
|
|
|
36
40
|
"express": "^5.1.0",
|
|
37
41
|
"helmet": "^8.1.0",
|
|
38
42
|
"morgan": "^1.10.1",
|
|
43
|
+
"nodemailer": "^8.0.1",
|
|
39
44
|
"openai": "^6.3.0",
|
|
40
45
|
"prisma": "^6.14.0",
|
|
41
46
|
"pusher": "^5.2.0",
|
|
42
47
|
"pusher-js": "^8.4.0",
|
|
43
48
|
"resend": "^6.9.2",
|
|
44
49
|
"socket.io": "^4.8.1",
|
|
50
|
+
"stripe": "^20.4.1",
|
|
45
51
|
"superjson": "^2.2.2",
|
|
46
52
|
"uuid": "^13.0.0",
|
|
47
53
|
"zod": "^4.1.1"
|
|
@@ -53,6 +59,7 @@
|
|
|
53
59
|
"@types/express": "^5.0.3",
|
|
54
60
|
"@types/morgan": "^1.9.10",
|
|
55
61
|
"@types/node": "^24.3.0",
|
|
62
|
+
"@types/nodemailer": "^7.0.11",
|
|
56
63
|
"ts-node": "^10.9.2",
|
|
57
64
|
"ts-node-dev": "^2.0.0",
|
|
58
65
|
"tsc-alias": "^1.8.16",
|
|
@@ -61,4 +68,4 @@
|
|
|
61
68
|
"typescript": "^5.9.2",
|
|
62
69
|
"typescript-transform-paths": "^3.5.5"
|
|
63
70
|
}
|
|
64
|
-
}
|
|
71
|
+
}
|