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

    Class ContentService

    Main service class implementing comprehensive educational content management operations.

    This class provides the complete business logic layer for managing the four-tier content hierarchy with built-in validation, intelligent caching, and specialized features like packaged content generation. All operations maintain referential integrity and automatically invalidate related caches when content is modified.

    ContentService

    Index

    Constructors

    • Creates a new ContentService instance with initialized repositories for all content entities.

      Initializes all repository instances (CourseRepository, LevelRepository, SectionRepository, ModuleRepository) with the provided Prisma client, establishing the foundation for all content management operations with consistent database access patterns.

      Parameters

      • prisma: PrismaClient

        Prisma database client for data access operations across all repositories

      Returns ContentService

    Methods

    • Creates a new course with automatic ID generation if not provided.

      Parameters

      • data: CreateCourseDto

        Course creation data including optional ID, languages, name, and optional description

        • id

          Optional unique identifier for the course (auto-generated if not provided)

        • source_language

          Source language code (e.g., 'en')

        • target_language

          Target language code (e.g., 'es')

        • name

          Display name of the course

        • description

          Optional course description

        • is_public

          Whether the course is publicly accessible

      Returns Promise<Course>

      The created course object with timestamps

      When a course with the same ID already exists

    • Retrieves a single course by its unique identifier.

      Parameters

      • id: string

        The unique identifier of the course to retrieve

      Returns Promise<Course>

      The course object with all its properties

      When no course is found with the specified ID

    • Retrieves a paginated list of courses with optional filtering and sorting.

      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 order

        • filters

          Additional filters to apply

        • search

          Search term for text-based filtering

      Returns Promise<PaginatedResult<Course>>

      Paginated result containing courses and pagination metadata

    • Updates an existing course with partial data and invalidates related caches.

      Parameters

      • id: string

        The unique identifier of the course to update

      • data: Partial<CreateCourseDto>

        Partial course data to update

        • source_language

          Updated source language code

        • target_language

          Updated target language code

        • name

          Updated course name

        • description

          Updated course description

        • is_public

          Updated public visibility status

      Returns Promise<Course>

      The updated course object

      When no course is found with the specified ID

    • Deletes a course and all its associated content, with cache invalidation.

      Parameters

      • id: string

        The unique identifier of the course to delete

      Returns Promise<void>

      Resolves when the course is successfully deleted

      When no course is found with the specified ID or deletion fails

    • Creates a new level within a course with comprehensive validation.

      Validates parent course existence, unique level ID, unique code within course, and unique order within course before creation.

      Parameters

      • data: CreateLevelDto

        Level creation data

        • id

          Unique identifier for the level

        • course_id

          ID of the parent course

        • code

          Unique code within the course (e.g., 'A1', 'B2')

        • name

          Display name of the level

        • order

          Ordering position within the course

      Returns Promise<Level>

      The created level object with timestamps

      When parent course doesn't exist, level ID/code/order conflicts exist

    • Retrieves a single level by its unique identifier.

      Parameters

      • id: string

        The unique identifier of the level to retrieve

      Returns Promise<Level>

      The level object with all its properties

      When no level is found with the specified ID

    • Retrieves all levels belonging to a specific course with pagination support.

      Parameters

      • courseId: string

        The unique identifier of the parent course

      • Optionaloptions: QueryOptions = {}

        Query options for pagination and sorting

        • page

          Page number for pagination

        • limit

          Number of items per page

        • sortBy

          Field to sort by (typically 'order')

        • sortOrder

          Sort order

      Returns Promise<PaginatedResult<Level>>

      Paginated result containing levels and pagination metadata

      When the parent course doesn't exist

    • Updates an existing level with validation for code and order uniqueness within the course.

      Parameters

      • id: string

        The unique identifier of the level to update

      • data: Partial<Omit<CreateLevelDto, "id" | "course_id">>

        Partial level data to update

        • code

          Updated level code (must be unique within course)

        • name

          Updated level name

        • order

          Updated order position (must be unique within course)

      Returns Promise<Level>

      The updated level object

      When level doesn't exist or code/order conflicts occur

    • Deletes a level and all its associated content with cache invalidation.

      Parameters

      • id: string

        The unique identifier of the level to delete

      Returns Promise<void>

      Resolves when the level is successfully deleted

      When no level is found with the specified ID or deletion fails

    • Creates a new section within a level with validation for parent existence and order uniqueness.

      Parameters

      • data: CreateSectionDto

        Section creation data

        • id

          Unique identifier for the section

        • level_id

          ID of the parent level

        • name

          Display name of the section

        • order

          Ordering position within the level

      Returns Promise<Section>

      The created section object with timestamps

      When parent level doesn't exist, section ID conflicts, or order conflicts within level

    • Retrieves a single section by its unique identifier.

      Parameters

      • id: string

        The unique identifier of the section to retrieve

      Returns Promise<Section>

      The section object with all its properties

      When no section is found with the specified ID

    • Retrieves all sections belonging to a specific level with pagination support.

      Parameters

      • levelId: string

        The unique identifier of the parent level

      • Optionaloptions: QueryOptions = {}

        Query options for pagination and sorting

        • page

          Page number for pagination

        • limit

          Number of items per page

        • sortBy

          Field to sort by (typically 'order')

        • sortOrder

          Sort order

      Returns Promise<PaginatedResult<Section>>

      Paginated result containing sections and pagination metadata

      When the parent level doesn't exist

    • Updates an existing section with validation for order uniqueness within the level.

      Parameters

      • id: string

        The unique identifier of the section to update

      • data: Partial<Omit<CreateSectionDto, "id" | "level_id">>

        Partial section data to update

        • name

          Updated section name

        • order

          Updated order position (must be unique within level)

      Returns Promise<Section>

      The updated section object

      When section doesn't exist, parent level doesn't exist, or order conflicts occur

    • Deletes a section and all its associated content with cache invalidation.

      Parameters

      • id: string

        The unique identifier of the section to delete

      Returns Promise<void>

      Resolves when the section is successfully deleted

      When no section is found, parent level doesn't exist, or deletion fails

    • Creates a new module within a section with comprehensive validation.

      Parameters

      • data: CreateModuleDto

        Module creation data

        • id

          Unique identifier for the module

        • section_id

          ID of the parent section

        • module_type

          Type of module content

        • name

          Display name of the module

        • order

          Ordering position within the section

      Returns Promise<Module>

      The created module object with timestamps

      When parent section/level doesn't exist, module ID conflicts, or order conflicts within section

    • Retrieves a single module by its unique identifier.

      Parameters

      • id: string

        The unique identifier of the module to retrieve

      Returns Promise<Module>

      The module object with all its properties

      When no module is found with the specified ID

    • Retrieves all modules belonging to a specific section with pagination support.

      Parameters

      • sectionId: string

        The unique identifier of the parent section

      • 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 (typically 'order')

        • sortOrder

          Sort order

        • filters

          Additional filters (e.g., module_type)

      Returns Promise<PaginatedResult<Module>>

      Paginated result containing modules and pagination metadata

      When the parent section doesn't exist

    • Updates an existing module with validation for order uniqueness within the section.

      Parameters

      • id: string

        The unique identifier of the module to update

      • data: Partial<Omit<CreateModuleDto, "id" | "section_id">>

        Partial module data to update

        • module_type

          Updated module type

        • name

          Updated module name

        • order

          Updated order position (must be unique within section)

      Returns Promise<Module>

      The updated module object

      When module doesn't exist, parent section/level doesn't exist, or order conflicts occur

    • Deletes a module and all its associated content with cache invalidation.

      Parameters

      • id: string

        The unique identifier of the module to delete

      Returns Promise<void>

      Resolves when the module is successfully deleted

      When no module is found, parent section/level doesn't exist, or deletion fails

    • Generates a complete packaged course with all hierarchical content for offline support.

      This method creates a comprehensive package containing the entire course hierarchy (course → levels → sections → modules → lessons → exercises) optimized for offline use. Implements intelligent caching with 15-minute TTL and supports conditional requests via If-Modified-Since headers for efficient bandwidth usage.

      Parameters

      • courseId: string

        The unique identifier of the course to package

      • OptionalifModifiedSince: string

        Optional If-Modified-Since header value for conditional requests

      Returns Promise<null | PackagedCourse>

      Complete packaged course or null if not modified since specified date

      When course doesn't exist or packaging fails

      // Get full packaged course
      const packagedCourse = await contentService.getPackagedCourse('spanish-101');

      // Conditional request to check for updates
      const updated = await contentService.getPackagedCourse('spanish-101', '2024-01-01T00:00:00Z');
      if (updated === null) {
      console.log('Course not modified since specified date');
      }
    • Reorders modules within a section by updating their position sequence.

      Validates section existence, ensures all provided module IDs are currently assigned to the section, validates completeness of the reorder list, and prevents duplicates before applying the new module order.

      Parameters

      • sectionId: string

        The unique section identifier

      • moduleIds: string[]

        Array of module IDs in their new desired order

      Returns Promise<void>

      Resolves when reordering is complete

      When section with the specified ID is not found

      When provided module IDs are not assigned to the section

      When reorder list is incomplete (missing currently assigned modules)

      When duplicate module IDs are provided in the reorder list

      When reordering operation fails

      // Reorder modules in a section
      await contentService.reorderModules('section-basic-grammar', [
      'module-present-tense',
      'module-past-tense',
      'module-future-tense'
      ]);
      // Modules are now in the specified order
    • Reorders sections within a level by updating their position sequence.

      Validates level existence, ensures all provided section IDs are currently assigned to the level, validates completeness of the reorder list, and prevents duplicates before applying the new section order.

      Parameters

      • levelId: string

        The unique level identifier

      • sectionIds: string[]

        Array of section IDs in their new desired order

      Returns Promise<void>

      Resolves when reordering is complete

      When level with the specified ID is not found

      When provided section IDs are not assigned to the level

      When reorder list is incomplete (missing currently assigned sections)

      When duplicate section IDs are provided in the reorder list

      When reordering operation fails

      // Reorder sections in a level
      await contentService.reorderSections('level-basic-grammar', [
      'section-present-tense',
      'section-past-tense',
      'section-future-tense'
      ]);
      // Sections are now in the specified order
    • Reorders levels within a course by updating their position sequence.

      Validates course existence, ensures all provided level IDs are currently assigned to the course, validates completeness of the reorder list, and prevents duplicates before applying the new level order.

      Parameters

      • courseId: string

        The unique course identifier

      • levelIds: string[]

        Array of level IDs in their new desired order

      Returns Promise<void>

      Resolves when reordering is complete

      When course with the specified ID is not found

      When provided level IDs are not assigned to the course

      When reorder list is incomplete (missing currently assigned levels)

      When duplicate level IDs are provided in the reorder list

      When reordering operation fails

      // Reorder levels in a course
      await contentService.reorderLevels('course-spanish-101', [
      'level-beginner',
      'level-intermediate',
      'level-advanced'
      ]);
      // Levels are now in the specified order
    • Invalidates the packaged course cache when content is updated.

      This method is automatically called by all content modification operations (create, update, delete) to ensure cache consistency across the content hierarchy.

      Parameters

      • courseId: string

        The unique identifier of the course whose cache should be invalidated

      Returns Promise<void>

      Resolves when cache invalidation is complete