mcp-quiz-server
Version:
🧠AI-Powered Quiz Management via Model Context Protocol (MCP) - Create, manage, and take quizzes directly from VS Code, Claude, and other AI agents.
197 lines (196 loc) • 5.74 kB
JavaScript
"use strict";
/**
* @fileoverview Get Quiz Query - Application Layer
* @version 1.0.0
* @since 2025-07-29
* @lastUpdated 2025-07-29
* @module GetQuizQuery Application Query
* @description Query for retrieving quiz data with optional inclusions.
* Part of CQRS pattern implementation in the application layer.
* @contributors Claude Code Agent
* @dependencies Domain value objects
* @requirements REQ-ARCH-001 (Clean Architecture Application Layer)
* @testCoverage Unit tests for query validation and execution
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvalidQueryError = exports.GetQuizQuery = void 0;
const QuizId_1 = require("../../domain/value-objects/QuizId");
/**
* Get Quiz Query
*
* @description Query that encapsulates all parameters needed to retrieve a quiz.
* Queries are immutable data structures that represent data requests.
* This query will be processed by GetQuizQueryHandler.
*
* @example
* ```typescript
* const query = new GetQuizQuery({
* quizId: 'quiz-123',
* includeQuestions: true,
* includeStatistics: false,
* userId: 'user-456' // for permission checking
* });
* ```
*
* @since 2025-07-29
* @author Claude Code Agent
* @requirements REQ-ARCH-001 (Clean Architecture Application Layer)
*/
class GetQuizQuery {
constructor(data) {
this.data = data;
this.queryId = this.generateQueryId();
this.timestamp = new Date();
this.quizId = QuizId_1.QuizId.fromString(data.quizId);
// Validate query data on construction
this.validate();
}
/**
* Generate unique query ID
*/
generateQueryId() {
return `query_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
/**
* Validate query data
*/
validate() {
var _a;
if (!((_a = this.data.quizId) === null || _a === void 0 ? void 0 : _a.trim())) {
throw new InvalidQueryError('Quiz ID is required');
}
// QuizId constructor will validate the format
try {
QuizId_1.QuizId.fromString(this.data.quizId);
}
catch (error) {
throw new InvalidQueryError(`Invalid quiz ID format: ${this.data.quizId}`);
}
// Additional validations can go here
if (this.data.version !== undefined && this.data.version < 1) {
throw new InvalidQueryError('Version must be a positive integer');
}
}
/**
* Check if questions should be included in response
*/
get shouldIncludeQuestions() {
return this.data.includeQuestions === true;
}
/**
* Check if statistics should be included in response
*/
get shouldIncludeStatistics() {
return this.data.includeStatistics === true;
}
/**
* Check if results should be included in response
*/
get shouldIncludeResults() {
return this.data.includeResults === true;
}
/**
* Check if metadata should be included in response
*/
get shouldIncludeMetadata() {
return this.data.includeMetadata === true;
}
/**
* Check if this is a personalized query for a specific user
*/
get isPersonalized() {
return !!this.data.userId;
}
/**
* Get user ID for personalization
*/
get userId() {
return this.data.userId;
}
/**
* Get requested version
*/
get version() {
return this.data.version;
}
/**
* Convert to plain object for serialization/logging
*/
toPlainObject() {
return {
queryId: this.queryId,
timestamp: this.timestamp.toISOString(),
queryType: 'GetQuizQuery',
data: {
...this.data,
// Mask sensitive data in logs
userId: this.data.userId ? '[MASKED]' : undefined,
},
};
}
/**
* Create a summary for logging
*/
toSummary() {
const inclusions = [];
if (this.shouldIncludeQuestions)
inclusions.push('questions');
if (this.shouldIncludeStatistics)
inclusions.push('statistics');
if (this.shouldIncludeResults)
inclusions.push('results');
if (this.shouldIncludeMetadata)
inclusions.push('metadata');
const inclusionsText = inclusions.length > 0 ? ` including ${inclusions.join(', ')}` : '';
const userText = this.isPersonalized ? ' (personalized)' : '';
return `GetQuizQuery(${this.queryId}): Quiz ${this.data.quizId}${inclusionsText}${userText}`;
}
/**
* Check if query is still valid
*/
isValid() {
try {
this.validate();
return true;
}
catch (_a) {
return false;
}
}
/**
* Create a copy with different inclusions
*/
withInclusions(inclusions) {
return new GetQuizQuery({
...this.data,
...inclusions,
});
}
/**
* Create a copy for a different user (useful for permission changes)
*/
forUser(userId) {
return new GetQuizQuery({
...this.data,
userId,
});
}
/**
* Create a copy without personalization
*/
asAnonymous() {
const { userId, ...dataWithoutUser } = this.data;
return new GetQuizQuery(dataWithoutUser);
}
}
exports.GetQuizQuery = GetQuizQuery;
/**
* Query validation error
*/
class InvalidQueryError extends Error {
constructor(message) {
super(message);
this.name = 'InvalidQueryError';
}
}
exports.InvalidQueryError = InvalidQueryError;