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

    Class ExerciseService

    Service class for comprehensive exercise management operations within the WayrApp content system.

    Provides complete CRUD functionality for exercises with type-specific validation, data integrity enforcement, and comprehensive business logic for all supported exercise types in the platform.

    Index

    Constructors

    Methods

    • Creates a new exercise with comprehensive validation and type-specific data verification.

      Validates exercise ID uniqueness and performs type-specific data validation to ensure the exercise data conforms to the expected schema for the specified exercise type.

      Parameters

      • data: CreateExerciseDto

        Exercise creation data including ID, type, and type-specific data

        • id

          Unique exercise identifier

        • exercise_type

          Type of exercise

        • data

          Type-specific exercise data object

      Returns Promise<Exercise>

      The newly created exercise object

      When exercise ID already exists

      When exercise data validation fails for the specified type

      // Create a fill-in-the-blank exercise
      const exercise = await exerciseService.createExercise({
      id: 'exercise-fill-greeting',
      exercise_type: 'fill-in-the-blank',
      data: {
      text: 'Hello, my name is _____ and I am from _____.',
      blanks: [
      { position: 18, correct_answers: ['John', 'Jane'], hints: ['common name'] },
      { position: 40, correct_answers: ['Spain', 'Mexico'], hints: ['country'] }
      ]
      }
      });
    • Retrieves a single exercise by its unique identifier.

      Parameters

      • id: string

        The unique exercise identifier

      Returns Promise<Exercise>

      The exercise object with all its properties and type-specific data

      When exercise with the specified ID is not found

      const exercise = await exerciseService.getExercise('exercise-translate-hello');
      console.log(exercise.exercise_type); // 'translation'
      console.log(exercise.data.source_text); // 'Hello'
    • Retrieves paginated exercises across all types in the system.

      Parameters

      • Optionaloptions: QueryOptions = {}

        Query options for pagination, sorting, and filtering

        • page

          Page number for pagination

        • limit

          Number of items per page

        • sortBy

          Field to sort by

        • sortOrder

          Sort direction

        • filters

          Additional filters to apply

      Returns Promise<PaginatedResult<Exercise>>

      Paginated exercise results with metadata

      const allExercises = await exerciseService.getExercises({
      page: 1,
      limit: 20,
      sortBy: 'created_at',
      sortOrder: 'desc',
      filters: { exercise_type: 'translation' }
      });
    • Retrieves paginated exercises filtered by a specific exercise type.

      Validates the exercise type against supported types before querying and returns exercises matching the specified type with pagination support.

      Parameters

      • exerciseType: string

        The exercise type to filter by

      • Optionaloptions: QueryOptions = {}

        Query options for pagination, sorting, and filtering

        • page

          Page number for pagination

        • limit

          Number of items per page

        • sortBy

          Field to sort by

        • sortOrder

          Sort direction

      Returns Promise<PaginatedResult<Exercise>>

      Paginated exercise results filtered by type

      When exercise type is not one of the valid supported types

      const translationExercises = await exerciseService.getExercisesByType('translation', {
      page: 1,
      limit: 10,
      sortBy: 'created_at',
      sortOrder: 'asc'
      });
      // Returns only translation exercises
    • Updates an existing exercise with partial data and comprehensive validation.

      Validates exercise existence and performs type-specific data validation when exercise data is updated. If only data is provided without type, validates against the current exercise type to maintain data consistency.

      Parameters

      • id: string

        The unique exercise identifier

      • data: Partial<CreateExerciseDto>

        Partial exercise data for update

        • exercise_type

          Updated exercise type

        • data

          Updated type-specific exercise data

      Returns Promise<Exercise>

      The updated exercise object

      When exercise with the specified ID is not found

      When exercise data validation fails for the specified or current type

      const updatedExercise = await exerciseService.updateExercise('exercise-translate-hello', {
      data: {
      source_text: 'Hello world',
      target_text: 'Hola mundo',
      hints: ['greeting', 'common phrase', 'world']
      }
      });
    • Permanently deletes an exercise and removes it from all lesson assignments.

      Validates exercise existence before deletion and ensures the operation completes successfully. This operation may cascade to remove exercise assignments from lessons.

      Parameters

      • id: string

        The unique exercise identifier

      Returns Promise<void>

      Resolves when deletion is complete

      When exercise with the specified ID is not found

      When deletion operation fails

      await exerciseService.deleteExercise('exercise-translate-hello');
      // Exercise is now permanently deleted from the system
    • Retrieves multiple exercises by their unique identifiers in a single query.

      Efficiently fetches multiple exercises by their IDs, returning them in the order found in the database. Returns an empty array if no IDs are provided.

      Parameters

      • ids: string[]

        Array of unique exercise identifiers

      Returns Promise<Exercise[]>

      Array of exercise objects matching the provided IDs

      const exercises = await exerciseService.getExercisesByIds([
      'exercise-translate-hello',
      'exercise-fill-greeting',
      'exercise-vof-statement'
      ]);
      console.log(exercises.length); // Number of found exercises (may be less than requested)