WayrApp Backend & Ecosystem Documentation - v1.0.0
    Preparing search index...

    Module ExerciseService

    Exercise management service for the WayrApp language learning platform.

    This service provides CRUD operations for exercises within the content management system, handling exercise lifecycle management, type-specific validation, and data integrity enforcement. It serves as the primary business logic layer for exercise-related operations, ensuring that all exercise data conforms to type-specific schemas and maintaining consistency across the platform.

    The service manages six distinct exercise types: translation (source-to-target language conversion), fill-in-the-blank (text completion with multiple blanks), vof (true/false verification), pairs (matching left-right associations), informative (content display), and ordering (sequence arrangement). Each exercise type has its own validation rules and data structure requirements that are enforced during creation and updates.

    Key architectural responsibilities include exercise creation with type-specific validation, exercise retrieval with pagination and filtering support, exercise updates with data consistency checks, exercise deletion with dependency validation, bulk exercise retrieval by IDs, and comprehensive data validation for all supported exercise types. The service integrates with the repository layer for data persistence and provides detailed error handling for all business rule violations.

    Exercise data validation is performed through private methods that ensure each exercise type contains the required fields and follows the correct data structure. This includes validating translation pairs, blank positions and answers, boolean statements, matching pairs, informative content, and ordering sequences with proper numbering.

    Exequiel Trujillo

    1.0.0

    // Initialize service with Prisma client
    import { PrismaClient } from '@prisma/client';
    import { ExerciseService } from './ExerciseService';

    const prisma = new PrismaClient();
    const exerciseService = new ExerciseService(prisma);

    // Create a translation exercise
    const translationExercise = await exerciseService.createExercise({
    id: 'exercise-translate-hello',
    exercise_type: 'translation',
    data: {
    source_text: 'Hello',
    target_text: 'Hola',
    hints: ['greeting', 'common phrase']
    }
    });

    // Get exercises by type with pagination
    const fillBlankExercises = await exerciseService.getExercisesByType('fill-in-the-blank', {
    page: 1,
    limit: 10,
    sortBy: 'created_at',
    sortOrder: 'desc'
    });

    // Bulk retrieve exercises by IDs
    const exercises = await exerciseService.getExercisesByIds([
    'exercise-translate-hello',
    'exercise-fill-greeting',
    'exercise-vof-statement'
    ]);

    Classes

    ExerciseService