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

    Class LessonRepository

    Lesson repository class providing comprehensive data access operations for lesson management and exercise assignment operations. Implements standardized CRUD operations with advanced querying, filtering, pagination, and sophisticated exercise relationship management.

    Index

    Constructors

    Methods

    • Creates a new lesson in the database with automatic field mapping and default value assignment. Transforms DTO field names to match database schema and applies default experience points value.

      Parameters

      • data: CreateLessonDto

        Lesson creation data with all required fields

        • id

          Unique identifier for the lesson

        • module_id

          Parent module identifier

        • experience_points

          Experience points awarded for lesson completion (defaults to 10)

        • order

          Order position within the parent module

      Returns Promise<Lesson>

      Promise resolving to the created lesson with transformed field names

      When database operation fails or constraint violations occur

    • Retrieves a single lesson by its unique identifier and module ID with complete exercise information. Includes all assigned exercises with their details, ordered by exercise sequence within the lesson. Uses composite key lookup to prevent horizontal access vulnerabilities.

      Parameters

      • id: string

        Unique identifier of the lesson to retrieve

      • moduleId: string

        Module identifier to ensure lesson belongs to the correct module

      Returns Promise<null | Lesson>

      Promise resolving to lesson with exercises or null if not found

    • Retrieves a paginated list of lessons within a specific module with advanced filtering capabilities. Supports experience points range filtering, comprehensive sorting options, and includes complete exercise information for each lesson ordered by sequence within lessons.

      Parameters

      • moduleId: string

        Parent module identifier to filter lessons

      • Optionaloptions: QueryOptions = {}

        Query configuration options for filtering, pagination, and sorting

        • page

          Page number for pagination (1-based)

        • limit

          Maximum number of lessons per page

        • filters

          Filter conditions (supports 'experience_points_min' and 'experience_points_max')

        • sortBy

          Field to sort by (must be in allowed sort fields)

        • sortOrder

          Sort direction

      Returns Promise<PaginatedResult<Lesson>>

      Promise resolving to paginated lesson results with exercises

    • Retrieves a paginated list of all lessons with optional module filtering and comprehensive sorting. Supports cross-module lesson querying with module-based filtering and includes complete exercise information for each lesson ordered by sequence within lessons.

      Parameters

      • Optionaloptions: QueryOptions = {}

        Query configuration options for filtering, pagination, and sorting

        • page

          Page number for pagination (1-based)

        • limit

          Maximum number of lessons per page

        • filters

          Filter conditions (supports 'module_id' filter)

        • sortBy

          Field to sort by (must be in allowed sort fields)

        • sortOrder

          Sort direction

      Returns Promise<PaginatedResult<Lesson>>

      Promise resolving to paginated lesson results with exercises

    • Updates an existing lesson with partial data and field mapping transformation. Supports updating experience points and order position with complete exercise information returned in the updated lesson object. Uses composite key lookup to prevent horizontal access vulnerabilities.

      Parameters

      • id: string

        Unique identifier of the lesson to update

      • moduleId: string

        Module identifier to ensure lesson belongs to the correct module

      • data: Partial<CreateLessonDto>

        Partial lesson data with fields to update

        • experience_points

          Updated experience points value

        • order

          Updated order position within the module

      Returns Promise<Lesson>

      Promise resolving to updated lesson with complete exercise information

      When lesson is not found or database operation fails

    • Deletes a lesson from the database by its unique identifier and module ID. Handles deletion gracefully with error catching to prevent application crashes. Cascade deletion will automatically remove associated lesson-exercise relationships. Uses composite key lookup to prevent horizontal access vulnerabilities.

      Parameters

      • id: string

        Unique identifier of the lesson to delete

      • moduleId: string

        Module identifier to ensure lesson belongs to the correct module

      Returns Promise<boolean>

      Promise resolving to true if deletion succeeded, false if failed or lesson not found

    • Checks if a lesson exists in the database by its unique identifier. Uses efficient count query to determine existence without retrieving full lesson data.

      Parameters

      • id: string

        Unique identifier of the lesson to check

      Returns Promise<boolean>

      Promise resolving to true if lesson exists, false otherwise

    • Assigns an exercise to a lesson at a specific order position. Creates a new lesson-exercise relationship with the specified order for exercise sequencing.

      Parameters

      • lessonId: string

        Unique identifier of the lesson

      • exerciseId: string

        Unique identifier of the exercise to assign

      • order: number

        Order position of the exercise within the lesson sequence

      Returns Promise<LessonExercise>

      Promise resolving to the created lesson-exercise assignment with exercise details

      When lesson or exercise doesn't exist, or database operation fails

    • Removes an exercise assignment from a lesson. Deletes the lesson-exercise relationship while preserving both the lesson and exercise entities.

      Parameters

      • lessonId: string

        Unique identifier of the lesson

      • exerciseId: string

        Unique identifier of the exercise to unassign

      Returns Promise<boolean>

      Promise resolving to true if unassignment succeeded, false if failed or assignment not found

    • Retrieves all exercises assigned to a specific lesson ordered by their sequence. Returns complete exercise information with assignment details for lesson content delivery.

      Parameters

      • lessonId: string

        Unique identifier of the lesson

      Returns Promise<LessonExercise[]>

      Promise resolving to array of lesson-exercise assignments with exercise details, ordered by sequence

    • Reorders exercises within a lesson using atomic transaction operations. Updates the order positions of all specified exercises to match the provided sequence, ensuring data consistency through database transaction isolation.

      Parameters

      • lessonId: string

        Unique identifier of the lesson

      • exerciseIds: string[]

        Array of exercise IDs in the desired order sequence

      Returns Promise<boolean>

      Promise resolving to true if reordering succeeded, false if failed

      When transaction fails or exercise assignments don't exist

    • Reorders lessons within a module using atomic transaction operations. Updates the order positions of all specified lessons to match the provided sequence, ensuring data consistency through database transaction isolation.

      This method performs atomic updates of lesson order values within a single transaction, preventing race conditions and maintaining referential integrity. Each lesson's order is updated based on its position in the provided lesson IDs array, starting from 1.

      Parameters

      • moduleId: string

        The unique module identifier containing the lessons

      • lessonIds: string[]

        Array of lesson IDs in their new desired order sequence

      Returns Promise<boolean>

      Promise resolving to true if reordering succeeded, false if failed

      When transaction fails or lesson assignments don't exist

      // Reorder lessons within a module
      const success = await lessonRepository.reorderLessons('module-basic-conversation', [
      'lesson-greetings',
      'lesson-introductions',
      'lesson-farewells'
      ]);
      // Lessons are now ordered: greetings (1), introductions (2), farewells (3)