17 Commits

Author SHA1 Message Date
Anton
5e207ee9b6 feat: register admin routes
Made-with: Cursor
2026-03-04 15:07:22 +03:00
Anton
7bea8585c5 feat: add audit logging to admin actions
Made-with: Cursor
2026-03-04 15:06:22 +03:00
Anton
7cfc8fb12e feat: add admin questions routes
Made-with: Cursor
2026-03-04 15:05:44 +03:00
Anton
9bada23e2e feat: add AdminQuestionService
Made-with: Cursor
2026-03-04 14:56:20 +03:00
Anton
91b33f6f41 feat: register tests routes
Made-with: Cursor
2026-03-04 14:47:26 +03:00
Anton
28182e2e99 feat: add tests routes
Made-with: Cursor
2026-03-04 14:47:12 +03:00
Anton
b9f3663621 feat: add test answer and finish flow
Made-with: Cursor
2026-03-04 14:46:04 +03:00
Anton
dacfad308c feat: add TestsService create flow
Made-with: Cursor
2026-03-04 14:44:54 +03:00
Anton
9fbb6431d8 feat: add QuestionService
Made-with: Cursor
2026-03-04 14:44:01 +03:00
Anton
50d6b34f11 feat: log LLM metadata to question_cache_meta
Made-with: Cursor
2026-03-04 14:37:58 +03:00
Anton
189e9c127f feat: add LLM retry and fallback
Made-with: Cursor
2026-03-04 14:19:04 +03:00
Anton
e7c7bf363e feat: register profile routes
Made-with: Cursor
2026-03-04 14:18:23 +03:00
Anton
0564dc4b91 feat: add LLM question generation
Made-with: Cursor
2026-03-04 14:18:22 +03:00
Anton
9da82c839f feat: add stats to profile response
Made-with: Cursor
2026-03-04 14:17:54 +03:00
Anton
6530e81402 feat: add profile routes
Made-with: Cursor
2026-03-04 14:16:23 +03:00
Anton
b7573acbed feat: add UserService
Made-with: Cursor
2026-03-04 14:15:49 +03:00
Anton
bf544b3e5b feat: add subscription middleware
Made-with: Cursor
2026-03-04 14:15:09 +03:00
14 changed files with 1694 additions and 2 deletions

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "samreshu_docs"]
path = samreshu_docs
url = https://git.vakanaut.ru/admin/samreshu_docs.git

1
samreshu_docs Submodule

Submodule samreshu_docs added at 99cd8ae727

View File

@@ -5,7 +5,11 @@ import redisPlugin from './plugins/redis.js';
import securityPlugin from './plugins/security.js';
import rateLimitPlugin from './plugins/rateLimit.js';
import authPlugin from './plugins/auth.js';
import subscriptionPlugin from './plugins/subscription.js';
import { authRoutes } from './routes/auth.js';
import { profileRoutes } from './routes/profile.js';
import { testsRoutes } from './routes/tests.js';
import { adminQuestionsRoutes } from './routes/admin/questions.js';
import { env } from './config/env.js';
import { randomUUID } from 'node:crypto';
@@ -72,7 +76,11 @@ export async function buildApp(): Promise<FastifyInstance> {
await app.register(securityPlugin);
await app.register(rateLimitPlugin);
await app.register(authPlugin);
await app.register(subscriptionPlugin);
await app.register(authRoutes, { prefix: '/auth' });
await app.register(profileRoutes, { prefix: '/profile' });
await app.register(testsRoutes, { prefix: '/tests' });
await app.register(adminQuestionsRoutes, { prefix: '/admin' });
app.get('/health', async () => ({ status: 'ok', timestamp: new Date().toISOString() }));

View File

@@ -14,9 +14,11 @@ const envSchema = z.object({
LLM_BASE_URL: z.string().url().default('http://localhost:11434/v1'),
LLM_MODEL: z.string().default('qwen2.5:14b'),
LLM_FALLBACK_MODEL: z.string().optional(),
LLM_API_KEY: z.string().optional(),
LLM_TIMEOUT_MS: z.coerce.number().default(15000),
LLM_MAX_RETRIES: z.coerce.number().min(0).default(1),
LLM_RETRY_DELAY_MS: z.coerce.number().min(0).default(1000),
LLM_TEMPERATURE: z.coerce.number().min(0).max(2).default(0.7),
LLM_MAX_TOKENS: z.coerce.number().default(2048),

View File

@@ -1,11 +1,14 @@
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import fp from 'fastify-plugin';
import { eq } from 'drizzle-orm';
import { verifyToken, isAccessPayload } from '../utils/jwt.js';
import { unauthorized } from '../utils/errors.js';
import { unauthorized, forbidden } from '../utils/errors.js';
import { users } from '../db/schema/users.js';
declare module 'fastify' {
interface FastifyInstance {
authenticate: (req: FastifyRequest, reply: FastifyReply) => Promise<void>;
authenticateAdmin: (req: FastifyRequest, reply: FastifyReply) => Promise<void>;
}
interface FastifyRequest {
user?: { id: string; email: string };
@@ -34,9 +37,22 @@ export async function authenticate(req: FastifyRequest, _reply: FastifyReply): P
}
}
export async function authenticateAdmin(req: FastifyRequest, _reply: FastifyReply): Promise<void> {
if (!req.user) {
throw unauthorized('Authentication required');
}
const [user] = await req.server.db.select({ role: users.role }).from(users).where(eq(users.id, req.user.id));
if (!user || user.role !== 'admin') {
throw forbidden('Admin access required');
}
}
const authPlugin = async (app: FastifyInstance) => {
app.decorateRequest('user', undefined);
app.decorate('authenticate', authenticate);
app.decorate('authenticateAdmin', authenticateAdmin);
};
export default fp(authPlugin, { name: 'auth' });
export default fp(authPlugin, { name: 'auth', dependencies: ['database'] });

View File

@@ -0,0 +1,71 @@
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import fp from 'fastify-plugin';
import { eq } from 'drizzle-orm';
import { subscriptions } from '../db/schema/subscriptions.js';
import { forbidden } from '../utils/errors.js';
export type SubscriptionInfo = {
plan: 'free' | 'pro';
status: 'active' | 'trialing' | 'cancelled' | 'expired';
isPro: boolean;
expiresAt: Date | null;
};
declare module 'fastify' {
interface FastifyRequest {
subscription?: SubscriptionInfo | null;
}
interface FastifyInstance {
withSubscription: (req: FastifyRequest, reply: FastifyReply) => Promise<void>;
requirePro: (req: FastifyRequest, reply: FastifyReply) => Promise<void>;
}
}
async function loadSubscription(db: FastifyInstance['db'], userId: string): Promise<SubscriptionInfo | null> {
const [sub] = await db
.select()
.from(subscriptions)
.where(eq(subscriptions.userId, userId))
.limit(1);
if (!sub) return null;
const now = new Date();
const isExpired = sub.expiresAt && sub.expiresAt < now;
const isPro =
sub.plan === 'pro' &&
(sub.status === 'active' || sub.status === 'trialing') &&
!isExpired;
return {
plan: sub.plan as 'free' | 'pro',
status: sub.status as SubscriptionInfo['status'],
isPro,
expiresAt: sub.expiresAt,
};
}
export async function requirePro(req: FastifyRequest, _reply: FastifyReply): Promise<void> {
const sub = req.subscription;
if (!sub?.isPro) {
throw forbidden('Pro subscription required');
}
}
const subscriptionPlugin = async (app: FastifyInstance) => {
app.decorateRequest('subscription', undefined);
app.decorate('withSubscription', async (req: FastifyRequest, _reply: FastifyReply) => {
if (!req.user?.id) return;
const sub = await loadSubscription(app.db, req.user.id);
req.subscription = sub;
});
app.decorate('requirePro', requirePro);
};
export default fp(subscriptionPlugin, {
name: 'subscription',
dependencies: ['database', 'auth'],
});

View File

@@ -0,0 +1,131 @@
import type { FastifyInstance } from 'fastify';
import { AdminQuestionService } from '../../services/admin/admin-question.service.js';
import type { EditQuestionInput } from '../../services/admin/admin-question.service.js';
const STACKS = ['html', 'css', 'js', 'ts', 'react', 'vue', 'nodejs', 'git', 'web_basics'] as const;
const LEVELS = ['basic', 'beginner', 'intermediate', 'advanced', 'expert'] as const;
const QUESTION_TYPES = ['single_choice', 'multiple_select', 'true_false', 'short_text'] as const;
const listPendingQuerySchema = {
querystring: {
type: 'object',
properties: {
limit: { type: 'integer', minimum: 1, maximum: 100 },
offset: { type: 'integer', minimum: 0 },
},
additionalProperties: false,
},
};
const questionIdParamsSchema = {
params: {
type: 'object',
required: ['questionId'],
properties: {
questionId: { type: 'string', format: 'uuid' },
},
},
};
const editQuestionSchema = {
params: {
type: 'object',
required: ['questionId'],
properties: {
questionId: { type: 'string', format: 'uuid' },
},
},
body: {
type: 'object',
properties: {
stack: { type: 'string', enum: [...STACKS] },
level: { type: 'string', enum: [...LEVELS] },
type: { type: 'string', enum: [...QUESTION_TYPES] },
questionText: { type: 'string', minLength: 1 },
options: {
type: 'array',
items: {
type: 'object',
required: ['key', 'text'],
properties: {
key: { type: 'string' },
text: { type: 'string' },
},
},
},
correctAnswer: {
oneOf: [
{ type: 'string' },
{ type: 'array', items: { type: 'string' } },
],
},
explanation: { type: 'string', minLength: 1 },
},
additionalProperties: false,
},
};
export async function adminQuestionsRoutes(app: FastifyInstance) {
const adminQuestionService = new AdminQuestionService(app.db);
const { rateLimitOptions } = app;
app.get(
'/questions/pending',
{
schema: listPendingQuerySchema,
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate, app.authenticateAdmin],
},
async (req, reply) => {
const { limit, offset } = (req.query as { limit?: number; offset?: number }) ?? {};
const result = await adminQuestionService.listPending(limit, offset);
return reply.send(result);
},
);
app.post(
'/questions/:questionId/approve',
{
schema: questionIdParamsSchema,
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate, app.authenticateAdmin],
},
async (req, reply) => {
const adminId = req.user!.id;
const { questionId } = req.params as { questionId: string };
await adminQuestionService.approve(questionId, adminId);
return reply.status(204).send();
},
);
app.post(
'/questions/:questionId/reject',
{
schema: questionIdParamsSchema,
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate, app.authenticateAdmin],
},
async (req, reply) => {
const adminId = req.user!.id;
const { questionId } = req.params as { questionId: string };
await adminQuestionService.reject(questionId, adminId);
return reply.status(204).send();
},
);
app.patch(
'/questions/:questionId',
{
schema: editQuestionSchema,
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate, app.authenticateAdmin],
},
async (req, reply) => {
const adminId = req.user!.id;
const { questionId } = req.params as { questionId: string };
const body = req.body as EditQuestionInput;
const question = await adminQuestionService.edit(questionId, body, adminId);
return reply.send(question);
},
);
}

73
src/routes/profile.ts Normal file
View File

@@ -0,0 +1,73 @@
import type { FastifyInstance } from 'fastify';
import { UserService } from '../services/user/user.service.js';
const patchProfileSchema = {
body: {
type: 'object',
properties: {
nickname: { type: 'string', minLength: 2, maxLength: 30 },
avatarUrl: { type: ['string', 'null'], maxLength: 500 },
country: { type: ['string', 'null'], maxLength: 100 },
city: { type: ['string', 'null'], maxLength: 100 },
selfLevel: { type: ['string', 'null'], enum: ['jun', 'mid', 'sen'] },
isPublic: { type: 'boolean' },
},
additionalProperties: false,
},
};
const usernameParamsSchema = {
params: {
type: 'object',
required: ['username'],
properties: {
username: { type: 'string', minLength: 2, maxLength: 30 },
},
},
};
export async function profileRoutes(app: FastifyInstance) {
const userService = new UserService(app.db);
const { rateLimitOptions } = app;
app.get(
'/',
{
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate, app.withSubscription],
},
async (req, reply) => {
const userId = req.user!.id;
const profile = await userService.getPrivateProfile(userId);
return reply.send(profile);
},
);
app.patch(
'/',
{
schema: patchProfileSchema,
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate],
},
async (req, reply) => {
const userId = req.user!.id;
const body = req.body as Parameters<UserService['updateProfile']>[1];
const profile = await userService.updateProfile(userId, body);
return reply.send(profile);
},
);
app.get(
'/:username',
{
schema: usernameParamsSchema,
config: { rateLimit: rateLimitOptions.apiGuest },
},
async (req, reply) => {
const { username } = req.params as { username: string };
const profile = await userService.getPublicProfile(username);
return reply.send(profile);
},
);
}

185
src/routes/tests.ts Normal file
View File

@@ -0,0 +1,185 @@
import type { FastifyInstance } from 'fastify';
import { LlmService } from '../services/llm/llm.service.js';
import { QuestionService } from '../services/questions/question.service.js';
import { TestsService } from '../services/tests/tests.service.js';
import { notFound } from '../utils/errors.js';
import type { CreateTestInput } from '../services/tests/tests.service.js';
import type { TestSnapshot } from '../services/tests/tests.service.js';
const STACKS = ['html', 'css', 'js', 'ts', 'react', 'vue', 'nodejs', 'git', 'web_basics'] as const;
const LEVELS = ['basic', 'beginner', 'intermediate', 'advanced', 'expert'] as const;
const createTestSchema = {
body: {
type: 'object',
required: ['stack', 'level', 'questionCount'],
properties: {
stack: { type: 'string', enum: STACKS },
level: { type: 'string', enum: LEVELS },
questionCount: { type: 'integer', minimum: 1, maximum: 50 },
mode: { type: 'string', enum: ['fixed', 'infinite', 'marathon'] },
},
additionalProperties: false,
},
};
const testIdParamsSchema = {
params: {
type: 'object',
required: ['testId'],
properties: {
testId: { type: 'string', format: 'uuid' },
},
},
};
const answerSchema = {
params: {
type: 'object',
required: ['testId'],
properties: {
testId: { type: 'string', format: 'uuid' },
},
},
body: {
type: 'object',
required: ['questionId', 'answer'],
properties: {
questionId: { type: 'string', format: 'uuid' },
answer: {
oneOf: [
{ type: 'string' },
{ type: 'array', items: { type: 'string' } },
],
},
},
additionalProperties: false,
},
};
const historyQuerySchema = {
querystring: {
type: 'object',
properties: {
limit: { type: 'integer', minimum: 1, maximum: 100 },
offset: { type: 'integer', minimum: 0 },
},
additionalProperties: false,
},
};
function stripAnswersForClient(
q: TestSnapshot
): Omit<TestSnapshot, 'correctAnswer' | 'explanation'> {
const { correctAnswer: _, explanation: __, ...rest } = q;
return rest;
}
export async function testsRoutes(app: FastifyInstance) {
const llmService = new LlmService();
const questionService = new QuestionService(app.db, llmService);
const testsService = new TestsService(app.db, questionService);
const { rateLimitOptions } = app;
app.get(
'/',
{
schema: historyQuerySchema,
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate],
},
async (req, reply) => {
const userId = req.user!.id;
const { limit, offset } = (req.query as { limit?: number; offset?: number }) ?? {};
const { tests: testList, total } = await testsService.getHistory(
userId,
{ limit, offset }
);
return reply.send({ tests: testList, total });
}
);
app.post(
'/',
{
schema: createTestSchema,
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate, app.withSubscription],
},
async (req, reply) => {
const userId = req.user!.id;
const body = req.body as CreateTestInput;
const test = await testsService.createTest(userId, body);
const hideAnswers = test.status === 'in_progress';
const response = {
...test,
questions: hideAnswers
? test.questions.map(stripAnswersForClient)
: test.questions,
};
return reply.status(201).send(response);
}
);
app.post(
'/:testId/answer',
{
schema: answerSchema,
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate],
},
async (req, reply) => {
const userId = req.user!.id;
const { testId } = req.params as { testId: string };
const { questionId, answer } = req.body as { questionId: string; answer: string | string[] };
const snapshot = await testsService.answerQuestion(
userId,
testId,
questionId,
answer
);
return reply.send(snapshot);
}
);
app.post(
'/:testId/finish',
{
schema: testIdParamsSchema,
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate],
},
async (req, reply) => {
const userId = req.user!.id;
const { testId } = req.params as { testId: string };
const test = await testsService.finishTest(userId, testId);
return reply.send(test);
}
);
app.get(
'/:testId',
{
schema: testIdParamsSchema,
config: { rateLimit: rateLimitOptions.apiAuthed },
preHandler: [app.authenticate],
},
async (req, reply) => {
const userId = req.user!.id;
const { testId } = req.params as { testId: string };
const test = await testsService.getById(userId, testId);
if (!test) {
throw notFound('Test not found');
}
const hideAnswers = test.status === 'in_progress';
const response = {
...test,
questions: hideAnswers
? test.questions.map(stripAnswersForClient)
: test.questions,
};
return reply.send(response);
}
);
}

View File

@@ -0,0 +1,194 @@
import { eq, asc, count } from 'drizzle-orm';
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
import type * as schema from '../../db/schema/index.js';
import { questionBank, auditLogs } from '../../db/schema/index.js';
import { notFound } from '../../utils/errors.js';
import type { Stack, Level, QuestionType } from '../../db/schema/enums.js';
type Db = NodePgDatabase<typeof schema>;
export type PendingQuestion = {
id: string;
stack: Stack;
level: Level;
type: QuestionType;
questionText: string;
options: Array<{ key: string; text: string }> | null;
correctAnswer: string | string[];
explanation: string;
source: string;
createdAt: Date;
};
export type EditQuestionInput = {
stack?: Stack;
level?: Level;
type?: QuestionType;
questionText?: string;
options?: Array<{ key: string; text: string }> | null;
correctAnswer?: string | string[];
explanation?: string;
};
export type ListPendingResult = {
questions: PendingQuestion[];
total: number;
};
export class AdminQuestionService {
constructor(private readonly db: Db) {}
async listPending(limit = 50, offset = 0): Promise<ListPendingResult> {
const questions = await this.db
.select({
id: questionBank.id,
stack: questionBank.stack,
level: questionBank.level,
type: questionBank.type,
questionText: questionBank.questionText,
options: questionBank.options,
correctAnswer: questionBank.correctAnswer,
explanation: questionBank.explanation,
source: questionBank.source,
createdAt: questionBank.createdAt,
})
.from(questionBank)
.where(eq(questionBank.status, 'pending'))
.orderBy(asc(questionBank.createdAt))
.limit(limit)
.offset(offset);
const [{ count: totalCount }] = await this.db
.select({ count: count() })
.from(questionBank)
.where(eq(questionBank.status, 'pending'));
return {
questions: questions.map((q) => ({
id: q.id,
stack: q.stack,
level: q.level,
type: q.type,
questionText: q.questionText,
options: q.options,
correctAnswer: q.correctAnswer,
explanation: q.explanation,
source: q.source,
createdAt: q.createdAt,
})),
total: totalCount ?? 0,
};
}
async approve(questionId: string, adminId: string): Promise<void> {
const [updated] = await this.db
.update(questionBank)
.set({
status: 'approved',
approvedAt: new Date(),
})
.where(eq(questionBank.id, questionId))
.returning({ id: questionBank.id });
if (!updated) {
throw notFound('Question not found');
}
await this.db.insert(auditLogs).values({
adminId,
action: 'question_approved',
targetType: 'question',
targetId: questionId,
});
}
async reject(questionId: string, adminId: string): Promise<void> {
const [updated] = await this.db
.update(questionBank)
.set({ status: 'rejected' })
.where(eq(questionBank.id, questionId))
.returning({ id: questionBank.id });
if (!updated) {
throw notFound('Question not found');
}
await this.db.insert(auditLogs).values({
adminId,
action: 'question_rejected',
targetType: 'question',
targetId: questionId,
});
}
async edit(questionId: string, input: EditQuestionInput, adminId: string): Promise<PendingQuestion> {
const updates: Partial<{
stack: Stack;
level: Level;
type: QuestionType;
questionText: string;
options: Array<{ key: string; text: string }> | null;
correctAnswer: string | string[];
explanation: string;
}> = {};
if (input.stack !== undefined) updates.stack = input.stack;
if (input.level !== undefined) updates.level = input.level;
if (input.type !== undefined) updates.type = input.type;
if (input.questionText !== undefined) updates.questionText = input.questionText;
if (input.options !== undefined) updates.options = input.options;
if (input.correctAnswer !== undefined) updates.correctAnswer = input.correctAnswer;
if (input.explanation !== undefined) updates.explanation = input.explanation;
if (Object.keys(updates).length === 0) {
const [existing] = await this.db
.select()
.from(questionBank)
.where(eq(questionBank.id, questionId));
if (!existing) throw notFound('Question not found');
return {
id: existing.id,
stack: existing.stack,
level: existing.level,
type: existing.type,
questionText: existing.questionText,
options: existing.options,
correctAnswer: existing.correctAnswer,
explanation: existing.explanation,
source: existing.source,
createdAt: existing.createdAt,
};
}
const [updated] = await this.db
.update(questionBank)
.set(updates)
.where(eq(questionBank.id, questionId))
.returning();
if (!updated) {
throw notFound('Question not found');
}
await this.db.insert(auditLogs).values({
adminId,
action: 'question_edited',
targetType: 'question',
targetId: questionId,
details: updates as Record<string, unknown>,
});
return {
id: updated.id,
stack: updated.stack,
level: updated.level,
type: updated.type,
questionText: updated.questionText,
options: updated.options,
correctAnswer: updated.correctAnswer,
explanation: updated.explanation,
source: updated.source,
createdAt: updated.createdAt,
};
}
}

View File

@@ -0,0 +1,237 @@
import { z } from 'zod';
import { createHash } from 'node:crypto';
import { env } from '../../config/env.js';
import type { Stack, Level, QuestionType } from '../../db/schema/enums.js';
export interface LlmConfig {
baseUrl: string;
model: string;
fallbackModel?: string;
apiKey?: string;
timeoutMs: number;
temperature: number;
maxTokens: number;
maxRetries: number;
retryDelayMs: number;
}
export interface ChatMessage {
role: 'system' | 'user' | 'assistant';
content: string;
}
export interface ChatCompletionResponse {
choices: Array<{
message?: { content: string };
text?: string;
}>;
}
const QUESTION_TYPES: QuestionType[] = ['single_choice', 'multiple_select', 'true_false', 'short_text'];
const optionSchema = z.object({
key: z.string().min(1),
text: z.string().min(1),
});
const generatedQuestionSchema = z.object({
questionText: z.string().min(1),
type: z.enum(QUESTION_TYPES as [string, ...string[]]),
options: z.array(optionSchema).optional(),
correctAnswer: z.union([z.string(), z.array(z.string())]),
explanation: z.string().min(1),
});
const generateQuestionsResponseSchema = z.object({
questions: z.array(generatedQuestionSchema),
});
export type GeneratedQuestion = z.infer<typeof generatedQuestionSchema> & {
stack: Stack;
level: Level;
};
export interface GenerateQuestionsInput {
stack: Stack;
level: Level;
count: number;
types?: QuestionType[];
}
/** Metadata for persisting to question_cache_meta (used by QuestionService) */
export interface LlmGenerationMeta {
llmModel: string;
promptHash: string;
generationTimeMs: number;
rawResponse: unknown;
}
export interface GenerateQuestionsResult {
questions: GeneratedQuestion[];
meta: LlmGenerationMeta;
}
export class LlmService {
private readonly config: LlmConfig;
constructor(config?: Partial<LlmConfig>) {
this.config = {
baseUrl: config?.baseUrl ?? env.LLM_BASE_URL,
model: config?.model ?? env.LLM_MODEL,
fallbackModel: config?.fallbackModel ?? env.LLM_FALLBACK_MODEL,
apiKey: config?.apiKey ?? env.LLM_API_KEY,
timeoutMs: config?.timeoutMs ?? env.LLM_TIMEOUT_MS,
temperature: config?.temperature ?? env.LLM_TEMPERATURE,
maxTokens: config?.maxTokens ?? env.LLM_MAX_TOKENS,
maxRetries: config?.maxRetries ?? env.LLM_MAX_RETRIES,
retryDelayMs: config?.retryDelayMs ?? env.LLM_RETRY_DELAY_MS,
};
}
async chat(messages: ChatMessage[]): Promise<string> {
const { content } = await this.chatWithMeta(messages);
return content;
}
/** Returns content and model used (for logging to question_cache_meta) */
async chatWithMeta(messages: ChatMessage[]): Promise<{ content: string; model: string }> {
let lastError: Error | null = null;
const modelsToTry = [this.config.model];
if (this.config.fallbackModel) {
modelsToTry.push(this.config.fallbackModel);
}
for (const model of modelsToTry) {
for (let attempt = 0; attempt <= this.config.maxRetries; attempt++) {
try {
const content = await this.executeChat(messages, model);
return { content, model };
} catch (err) {
lastError = err instanceof Error ? err : new Error('LLM request failed');
if (attempt < this.config.maxRetries) {
const delayMs = this.config.retryDelayMs * Math.pow(2, attempt);
await sleep(delayMs);
}
}
}
}
throw lastError ?? new Error('LLM request failed');
}
private async executeChat(messages: ChatMessage[], model: string): Promise<string> {
const url = `${this.config.baseUrl.replace(/\/$/, '')}/chat/completions`;
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};
if (this.config.apiKey) {
headers['Authorization'] = `Bearer ${this.config.apiKey}`;
}
const body = {
model,
messages: messages.map((m) => ({ role: m.role, content: m.content })),
temperature: this.config.temperature,
max_tokens: this.config.maxTokens,
};
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.config.timeoutMs);
try {
const res = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(body),
signal: controller.signal,
});
clearTimeout(timeoutId);
if (!res.ok) {
const text = await res.text();
throw new Error(`LLM request failed: ${res.status} ${res.statusText} - ${text}`);
}
const data = (await res.json()) as ChatCompletionResponse;
const choice = data.choices?.[0];
const content = choice?.message?.content ?? choice?.text ?? '';
return content.trim();
} catch (err) {
clearTimeout(timeoutId);
if (err instanceof Error) {
throw err;
}
throw new Error('LLM request failed');
}
}
async generateQuestions(input: GenerateQuestionsInput): Promise<GenerateQuestionsResult> {
const { stack, level, count, types = QUESTION_TYPES } = input;
const typeList = types.join(', ');
const systemPrompt = `You are a technical interview question generator. Generate exactly ${count} programming/tech questions.
Return ONLY valid JSON in this exact format (no markdown, no code blocks):
{"questions":[{"questionText":"...","type":"single_choice|multiple_select|true_false|short_text","options":[{"key":"a","text":"..."}],"correctAnswer":"a" or ["a","b"],"explanation":"..."}]}
Rules: type must be one of: ${typeList}. For single_choice/multiple_select: options array required with key (a,b,c,d). For true_false: options [{"key":"true","text":"True"},{"key":"false","text":"False"}]. For short_text: options omitted, correctAnswer is string.`;
const userPrompt = `Generate ${count} questions for stack="${stack}", level="${level}". Use types: ${typeList}.`;
const promptForHash = systemPrompt + '\n---\n' + userPrompt;
const promptHash = createHash('sha256').update(promptForHash).digest('hex');
const start = Date.now();
const { content: raw, model } = await this.chatWithMeta([
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt },
]);
const generationTimeMs = Date.now() - start;
const jsonStr = extractJson(raw);
const parsed = JSON.parse(jsonStr) as unknown;
const result = generateQuestionsResponseSchema.safeParse(parsed);
if (!result.success) {
throw new Error(`LLM response validation failed: ${result.error.message}`);
}
const questions: GeneratedQuestion[] = result.data.questions.map((q) => ({
...q,
stack,
level,
}));
for (const q of questions) {
if ((q.type === 'single_choice' || q.type === 'multiple_select') && (!q.options || q.options.length === 0)) {
throw new Error(`Question validation failed: ${q.type} requires options`);
}
if (q.type === 'true_false' && (!q.options || q.options.length < 2)) {
throw new Error(`Question validation failed: true_false requires true/false options`);
}
}
return {
questions,
meta: {
llmModel: model,
promptHash,
generationTimeMs,
rawResponse: parsed,
},
};
}
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function extractJson(text: string): string {
const trimmed = text.trim();
const match = trimmed.match(/\{[\s\S]*\}/);
return match ? match[0]! : trimmed;
}

View File

@@ -0,0 +1,197 @@
import { eq, and, sql, asc, inArray } from 'drizzle-orm';
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
import type * as schema from '../../db/schema/index.js';
import {
questionBank,
questionCacheMeta,
userQuestionLog,
} from '../../db/schema/index.js';
import { internalError, AppError, ERROR_CODES } from '../../utils/errors.js';
import type { Stack, Level, QuestionType } from '../../db/schema/enums.js';
import type {
GeneratedQuestion,
GenerateQuestionsResult,
} from '../llm/llm.service.js';
type Db = NodePgDatabase<typeof schema>;
export type QuestionForTest = {
questionBankId: string;
type: QuestionType;
questionText: string;
options: Array<{ key: string; text: string }> | null;
correctAnswer: string | string[];
explanation: string;
};
export interface LlmServiceInterface {
generateQuestions(input: {
stack: Stack;
level: Level;
count: number;
types?: QuestionType[];
}): Promise<GenerateQuestionsResult>;
}
function shuffle<T>(arr: T[]): T[] {
const result = [...arr];
for (let i = result.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[result[i], result[j]] = [result[j]!, result[i]!];
}
return result;
}
export class QuestionService {
constructor(
private readonly db: Db,
private readonly llmService: LlmServiceInterface
) {}
/**
* Get questions for a test. Fetches approved questions from question_bank.
* If not enough, generates via LLM, persists to question_bank and question_cache_meta.
*/
async getQuestionsForTest(
userId: string,
stack: Stack,
level: Level,
count: number
): Promise<QuestionForTest[]> {
const approved = await this.db
.select({
id: questionBank.id,
type: questionBank.type,
questionText: questionBank.questionText,
options: questionBank.options,
correctAnswer: questionBank.correctAnswer,
explanation: questionBank.explanation,
})
.from(questionBank)
.where(
and(eq(questionBank.stack, stack), eq(questionBank.level, level), eq(questionBank.status, 'approved'))
)
.orderBy(asc(questionBank.usageCount));
const seenIds = await this.db
.select({ questionBankId: userQuestionLog.questionBankId })
.from(userQuestionLog)
.where(eq(userQuestionLog.userId, userId));
const seenSet = new Set(seenIds.map((r) => r.questionBankId));
const preferred = approved.filter((q) => !seenSet.has(q.id));
const pool = preferred.length >= count ? preferred : approved;
const shuffled = shuffle(pool);
const fromBank: QuestionForTest[] = shuffled.slice(0, count).map((q) => ({
questionBankId: q.id,
type: q.type,
questionText: q.questionText,
options: q.options,
correctAnswer: q.correctAnswer,
explanation: q.explanation,
}));
let result = fromBank;
if (result.length < count) {
const generated = await this.generateAndPersistQuestions(
stack,
level,
count - result.length
);
result = [...result, ...generated];
}
if (result.length < count) {
throw new AppError(
ERROR_CODES.QUESTIONS_UNAVAILABLE,
'Not enough questions available for this stack and level',
422
);
}
if (result.length > 0) {
await this.db.insert(userQuestionLog).values(
result.map((q) => ({
userId,
questionBankId: q.questionBankId,
}))
);
const ids = result.map((q) => q.questionBankId);
await this.db
.update(questionBank)
.set({
usageCount: sql`${questionBank.usageCount} + 1`,
})
.where(inArray(questionBank.id, ids));
}
return result;
}
private async generateAndPersistQuestions(
stack: Stack,
level: Level,
count: number
): Promise<QuestionForTest[]> {
let generated: GeneratedQuestion[];
let meta: GenerateQuestionsResult['meta'];
try {
const result = await this.llmService.generateQuestions({
stack,
level,
count,
});
generated = result.questions;
meta = result.meta;
} catch (err) {
throw internalError(
'Failed to generate questions',
err instanceof Error ? err : undefined
);
}
const inserted: QuestionForTest[] = [];
for (const q of generated) {
const [row] = await this.db
.insert(questionBank)
.values({
stack,
level,
type: q.type as QuestionType,
questionText: q.questionText,
options: q.options ?? null,
correctAnswer: q.correctAnswer,
explanation: q.explanation,
status: 'approved',
source: 'llm_generated',
})
.returning();
if (row) {
await this.db.insert(questionCacheMeta).values({
questionBankId: row.id,
llmModel: meta.llmModel,
promptHash: meta.promptHash,
generationTimeMs: meta.generationTimeMs,
rawResponse: meta.rawResponse,
});
inserted.push({
questionBankId: row.id,
type: row.type,
questionText: row.questionText,
options: row.options,
correctAnswer: row.correctAnswer,
explanation: row.explanation,
});
}
}
return inserted;
}
}

View File

@@ -0,0 +1,399 @@
import { eq, and, desc } from 'drizzle-orm';
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
import type * as schema from '../../db/schema/index.js';
import { tests, testQuestions, userStats } from '../../db/schema/index.js';
import { notFound, conflict, AppError, ERROR_CODES } from '../../utils/errors.js';
import type { Stack, Level, TestMode } from '../../db/schema/enums.js';
import type { QuestionService } from '../questions/question.service.js';
type Db = NodePgDatabase<typeof schema>;
export type CreateTestInput = {
stack: Stack;
level: Level;
questionCount: number;
mode?: TestMode;
};
export type TestSnapshot = {
id: string;
testId: string;
questionBankId: string | null;
orderNumber: number;
type: string;
questionText: string;
options: Array<{ key: string; text: string }> | null;
correctAnswer: string | string[];
explanation: string;
userAnswer?: string | string[] | null;
isCorrect?: boolean | null;
answeredAt?: string | null;
};
export type TestWithQuestions = {
id: string;
userId: string;
stack: string;
level: string;
questionCount: number;
mode: string;
status: string;
score: number | null;
startedAt: string;
finishedAt: string | null;
timeLimitSeconds: number | null;
questions: TestSnapshot[];
};
export class TestsService {
constructor(
private readonly db: Db,
private readonly questionService: QuestionService
) {}
/**
* Create a new test: fetch questions, create test record, snapshot questions into test_questions.
*/
async createTest(userId: string, input: CreateTestInput): Promise<TestWithQuestions> {
const { stack, level, questionCount, mode = 'fixed' } = input;
if (questionCount < 1 || questionCount > 50) {
throw new AppError(
ERROR_CODES.BAD_REQUEST,
'questionCount must be between 1 and 50',
400
);
}
const questions = await this.questionService.getQuestionsForTest(
userId,
stack,
level,
questionCount
);
const [test] = await this.db
.insert(tests)
.values({
userId,
stack,
level,
questionCount,
mode,
status: 'in_progress',
})
.returning();
if (!test) {
throw new AppError(
ERROR_CODES.INTERNAL_ERROR,
'Failed to create test',
500
);
}
const tqValues = questions.map((q, i) => ({
testId: test.id,
questionBankId: q.questionBankId,
orderNumber: i + 1,
type: q.type,
questionText: q.questionText,
options: q.options,
correctAnswer: q.correctAnswer,
explanation: q.explanation,
}));
await this.db.insert(testQuestions).values(tqValues);
const questionsRows = await this.db
.select()
.from(testQuestions)
.where(eq(testQuestions.testId, test.id))
.orderBy(testQuestions.orderNumber);
return this.toTestWithQuestions(test, questionsRows);
}
/**
* Submit an answer for a question in an in-progress test.
*/
async answerQuestion(
userId: string,
testId: string,
testQuestionId: string,
userAnswer: string | string[]
): Promise<TestSnapshot> {
const [test] = await this.db
.select()
.from(tests)
.where(and(eq(tests.id, testId), eq(tests.userId, userId)))
.limit(1);
if (!test) {
throw notFound('Test not found');
}
if (test.status !== 'in_progress') {
throw conflict(ERROR_CODES.TEST_ALREADY_FINISHED, 'Test is already finished');
}
const [tq] = await this.db
.select()
.from(testQuestions)
.where(
and(
eq(testQuestions.id, testQuestionId),
eq(testQuestions.testId, testId)
)
)
.limit(1);
if (!tq) {
throw conflict(ERROR_CODES.WRONG_QUESTION, 'Question does not belong to this test');
}
if (tq.userAnswer !== null && tq.userAnswer !== undefined) {
throw conflict(ERROR_CODES.QUESTION_ALREADY_ANSWERED, 'Question already answered');
}
const isCorrect = this.checkAnswer(tq.correctAnswer, userAnswer);
const [updated] = await this.db
.update(testQuestions)
.set({
userAnswer,
isCorrect,
answeredAt: new Date(),
})
.where(eq(testQuestions.id, testQuestionId))
.returning();
if (!updated) {
throw notFound('Question not found');
}
return {
id: updated.id,
testId: updated.testId,
questionBankId: updated.questionBankId,
orderNumber: updated.orderNumber,
type: updated.type,
questionText: updated.questionText,
options: updated.options,
correctAnswer: updated.correctAnswer,
explanation: updated.explanation,
userAnswer: updated.userAnswer,
isCorrect: updated.isCorrect,
answeredAt: updated.answeredAt?.toISOString() ?? null,
};
}
private checkAnswer(
correct: string | string[],
user: string | string[]
): boolean {
if (Array.isArray(correct) && Array.isArray(user)) {
if (correct.length !== user.length) return false;
const a = [...correct].sort();
const b = [...user].sort();
return a.every((v, i) => v === b[i]);
}
if (typeof correct === 'string' && typeof user === 'string') {
return correct.trim().toLowerCase() === user.trim().toLowerCase();
}
return false;
}
/**
* Finish a test: calculate score, update user_stats.
*/
async finishTest(userId: string, testId: string): Promise<TestWithQuestions> {
const [test] = await this.db
.select()
.from(tests)
.where(and(eq(tests.id, testId), eq(tests.userId, userId)))
.limit(1);
if (!test) {
throw notFound('Test not found');
}
if (test.status !== 'in_progress') {
throw conflict(ERROR_CODES.TEST_ALREADY_FINISHED, 'Test is already finished');
}
const questions = await this.db
.select()
.from(testQuestions)
.where(eq(testQuestions.testId, testId));
const unanswered = questions.filter(
(q) => q.userAnswer === null || q.userAnswer === undefined
);
if (unanswered.length > 0) {
throw new AppError(
ERROR_CODES.NO_ANSWERS,
`Cannot finish: ${unanswered.length} question(s) not answered`,
422
);
}
const correctCount = questions.filter((q) => q.isCorrect === true).length;
const score = Math.round((correctCount / questions.length) * 100);
const [updatedTest] = await this.db
.update(tests)
.set({
status: 'completed',
score,
finishedAt: new Date(),
})
.where(eq(tests.id, testId))
.returning();
if (!updatedTest) {
throw notFound('Test not found');
}
await this.upsertUserStats(userId, test.stack as Stack, test.level as Level, {
totalQuestions: questions.length,
correctAnswers: correctCount,
testsTaken: 1,
});
const questionsRows = await this.db
.select()
.from(testQuestions)
.where(eq(testQuestions.testId, testId))
.orderBy(testQuestions.orderNumber);
return this.toTestWithQuestions(updatedTest, questionsRows);
}
private async upsertUserStats(
userId: string,
stack: Stack,
level: Level,
delta: { totalQuestions: number; correctAnswers: number; testsTaken: number }
): Promise<void> {
const [existing] = await this.db
.select()
.from(userStats)
.where(
and(
eq(userStats.userId, userId),
eq(userStats.stack, stack),
eq(userStats.level, level)
)
)
.limit(1);
const now = new Date();
if (existing) {
await this.db
.update(userStats)
.set({
totalQuestions: existing.totalQuestions + delta.totalQuestions,
correctAnswers: existing.correctAnswers + delta.correctAnswers,
testsTaken: existing.testsTaken + delta.testsTaken,
lastTestAt: now,
})
.where(eq(userStats.id, existing.id));
} else {
await this.db.insert(userStats).values({
userId,
stack,
level,
totalQuestions: delta.totalQuestions,
correctAnswers: delta.correctAnswers,
testsTaken: delta.testsTaken,
lastTestAt: now,
});
}
}
/**
* Get a single test by ID (must belong to user).
*/
async getById(userId: string, testId: string): Promise<TestWithQuestions | null> {
const [test] = await this.db
.select()
.from(tests)
.where(and(eq(tests.id, testId), eq(tests.userId, userId)))
.limit(1);
if (!test) return null;
const questionsRows = await this.db
.select()
.from(testQuestions)
.where(eq(testQuestions.testId, testId))
.orderBy(testQuestions.orderNumber);
return this.toTestWithQuestions(test, questionsRows);
}
/**
* Get test history for a user (most recent first).
*/
async getHistory(
userId: string,
options?: { limit?: number; offset?: number }
): Promise<{ tests: TestWithQuestions[]; total: number }> {
const limit = Math.min(options?.limit ?? 20, 100);
const offset = options?.offset ?? 0;
const all = await this.db
.select()
.from(tests)
.where(eq(tests.userId, userId))
.orderBy(desc(tests.startedAt));
const total = all.length;
const page = all.slice(offset, offset + limit);
const result: TestWithQuestions[] = [];
for (const test of page) {
const questionsRows = await this.db
.select()
.from(testQuestions)
.where(eq(testQuestions.testId, test.id))
.orderBy(testQuestions.orderNumber);
result.push(this.toTestWithQuestions(test, questionsRows));
}
return { tests: result, total };
}
private toTestWithQuestions(
test: (typeof tests.$inferSelect),
questionsRows: (typeof testQuestions.$inferSelect)[]
): TestWithQuestions {
return {
id: test.id,
userId: test.userId,
stack: test.stack,
level: test.level,
questionCount: test.questionCount,
mode: test.mode,
status: test.status,
score: test.score,
startedAt: test.startedAt.toISOString(),
finishedAt: test.finishedAt?.toISOString() ?? null,
timeLimitSeconds: test.timeLimitSeconds,
questions: questionsRows.map((q) => ({
id: q.id,
testId: q.testId,
questionBankId: q.questionBankId,
orderNumber: q.orderNumber,
type: q.type,
questionText: q.questionText,
options: q.options,
correctAnswer: q.correctAnswer,
explanation: q.explanation,
userAnswer: q.userAnswer,
isCorrect: q.isCorrect,
answeredAt: q.answeredAt?.toISOString() ?? null,
})),
};
}
}

View File

@@ -0,0 +1,175 @@
import { eq } from 'drizzle-orm';
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
import type * as schema from '../../db/schema/index.js';
import { users, userStats } from '../../db/schema/index.js';
import { notFound, conflict, ERROR_CODES } from '../../utils/errors.js';
import type { User } from '../../db/schema/users.js';
import type { SelfLevel } from '../../db/schema/index.js';
type Db = NodePgDatabase<typeof schema>;
export type UserStatItem = {
stack: string;
level: string;
totalQuestions: number;
correctAnswers: number;
testsTaken: number;
lastTestAt: string | null;
};
export type ProfileStats = {
byStack: UserStatItem[];
totalTestsTaken: number;
totalQuestions: number;
correctAnswers: number;
accuracy: number | null;
};
export type ProfileUpdateInput = {
nickname?: string;
avatarUrl?: string | null;
country?: string | null;
city?: string | null;
selfLevel?: SelfLevel | null;
isPublic?: boolean;
};
export type PublicProfile = {
id: string;
nickname: string;
avatarUrl: string | null;
country: string | null;
city: string | null;
selfLevel: string | null;
isPublic: boolean;
stats: ProfileStats;
};
export type PrivateProfile = PublicProfile & {
email: string;
emailVerifiedAt: string | null;
createdAt: string;
updatedAt: string;
};
async function getStatsForUser(db: Db, userId: string): Promise<ProfileStats> {
const rows = await db
.select()
.from(userStats)
.where(eq(userStats.userId, userId));
const byStack: UserStatItem[] = rows.map((r) => ({
stack: r.stack,
level: r.level,
totalQuestions: r.totalQuestions,
correctAnswers: r.correctAnswers,
testsTaken: r.testsTaken,
lastTestAt: r.lastTestAt?.toISOString() ?? null,
}));
const totalTestsTaken = rows.reduce((sum, r) => sum + r.testsTaken, 0);
const totalQuestions = rows.reduce((sum, r) => sum + r.totalQuestions, 0);
const correctAnswers = rows.reduce((sum, r) => sum + r.correctAnswers, 0);
const accuracy = totalQuestions > 0 ? correctAnswers / totalQuestions : null;
return { byStack, totalTestsTaken, totalQuestions, correctAnswers, accuracy };
}
function toPublicProfile(user: User, stats: ProfileStats): PublicProfile {
return {
id: user.id,
nickname: user.nickname,
avatarUrl: user.avatarUrl,
country: user.country,
city: user.city,
selfLevel: user.selfLevel,
isPublic: user.isPublic,
stats,
};
}
function toPrivateProfile(user: User, stats: ProfileStats): PrivateProfile {
return {
...toPublicProfile(user, stats),
email: user.email,
emailVerifiedAt: user.emailVerifiedAt?.toISOString() ?? null,
createdAt: user.createdAt.toISOString(),
updatedAt: user.updatedAt.toISOString(),
};
}
export class UserService {
constructor(private readonly db: Db) {}
async getById(userId: string): Promise<User | null> {
const [user] = await this.db.select().from(users).where(eq(users.id, userId)).limit(1);
return user ?? null;
}
async getByNickname(nickname: string): Promise<User | null> {
const [user] = await this.db
.select()
.from(users)
.where(eq(users.nickname, nickname.trim()))
.limit(1);
return user ?? null;
}
async getPrivateProfile(userId: string): Promise<PrivateProfile> {
const [user, stats] = await Promise.all([this.getById(userId), getStatsForUser(this.db, userId)]);
if (!user) {
throw notFound('User not found');
}
return toPrivateProfile(user, stats);
}
async getPublicProfile(username: string): Promise<PublicProfile> {
const user = await this.getByNickname(username);
if (!user) {
throw notFound('User not found');
}
if (!user.isPublic) {
throw notFound('User not found');
}
const stats = await getStatsForUser(this.db, user.id);
return toPublicProfile(user, stats);
}
async updateProfile(userId: string, input: ProfileUpdateInput): Promise<PrivateProfile> {
const updateData: Partial<typeof users.$inferInsert> = {
updatedAt: new Date(),
};
if (input.nickname !== undefined) {
const trimmed = input.nickname.trim();
const [existing] = await this.db
.select({ id: users.id })
.from(users)
.where(eq(users.nickname, trimmed))
.limit(1);
if (existing && existing.id !== userId) {
throw conflict(ERROR_CODES.NICKNAME_TAKEN, 'Nickname already taken');
}
updateData.nickname = trimmed;
}
if (input.avatarUrl !== undefined) updateData.avatarUrl = input.avatarUrl;
if (input.country !== undefined) updateData.country = input.country;
if (input.city !== undefined) updateData.city = input.city;
if (input.selfLevel !== undefined) updateData.selfLevel = input.selfLevel;
if (input.isPublic !== undefined) updateData.isPublic = input.isPublic;
const [updated] = await this.db
.update(users)
.set(updateData)
.where(eq(users.id, userId))
.returning();
if (!updated) {
throw notFound('User not found');
}
const stats = await getStatsForUser(this.db, userId);
return toPrivateProfile(updated, stats);
}
}