10 Commits

Author SHA1 Message Date
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
7 changed files with 1076 additions and 8 deletions

View File

@@ -7,6 +7,8 @@ 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 { env } from './config/env.js';
import { randomUUID } from 'node:crypto';
@@ -75,6 +77,8 @@ export async function buildApp(): Promise<FastifyInstance> {
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' });
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),

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,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

@@ -1,13 +1,30 @@
import { eq } from 'drizzle-orm';
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
import type * as schema from '../../db/schema/index.js';
import { users } 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;
@@ -25,6 +42,7 @@ export type PublicProfile = {
city: string | null;
selfLevel: string | null;
isPublic: boolean;
stats: ProfileStats;
};
export type PrivateProfile = PublicProfile & {
@@ -34,7 +52,30 @@ export type PrivateProfile = PublicProfile & {
updatedAt: string;
};
function toPublicProfile(user: User): PublicProfile {
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,
@@ -43,12 +84,13 @@ function toPublicProfile(user: User): PublicProfile {
city: user.city,
selfLevel: user.selfLevel,
isPublic: user.isPublic,
stats,
};
}
function toPrivateProfile(user: User): PrivateProfile {
function toPrivateProfile(user: User, stats: ProfileStats): PrivateProfile {
return {
...toPublicProfile(user),
...toPublicProfile(user, stats),
email: user.email,
emailVerifiedAt: user.emailVerifiedAt?.toISOString() ?? null,
createdAt: user.createdAt.toISOString(),
@@ -74,11 +116,11 @@ export class UserService {
}
async getPrivateProfile(userId: string): Promise<PrivateProfile> {
const user = await this.getById(userId);
const [user, stats] = await Promise.all([this.getById(userId), getStatsForUser(this.db, userId)]);
if (!user) {
throw notFound('User not found');
}
return toPrivateProfile(user);
return toPrivateProfile(user, stats);
}
async getPublicProfile(username: string): Promise<PublicProfile> {
@@ -89,7 +131,8 @@ export class UserService {
if (!user.isPublic) {
throw notFound('User not found');
}
return toPublicProfile(user);
const stats = await getStatsForUser(this.db, user.id);
return toPublicProfile(user, stats);
}
async updateProfile(userId: string, input: ProfileUpdateInput): Promise<PrivateProfile> {
@@ -126,6 +169,7 @@ export class UserService {
throw notFound('User not found');
}
return toPrivateProfile(updated);
const stats = await getStatsForUser(this.db, userId);
return toPrivateProfile(updated, stats);
}
}