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

    Class UserRepository

    UserRepository - Data access layer for user operations using Prisma ORM

    UserRepository Provides comprehensive data access operations for user management including CRUD operations, authentication queries, and pagination. Acts as the data access layer between the business logic service and the PostgreSQL database through Prisma ORM.

    Index

    Constructors

    Methods

    • Creates a new user without password authentication

      Parameters

      • userData: CreateUserDto

        User data for creation

        • email

          User's email address (required, unique)

        • username

          User's username (optional, unique if provided)

        • country_code

          Two-letter country code (optional)

        • profile_picture_url

          URL to user's profile picture (optional)

        • role

          User's role in the system (defaults to 'student')

      Returns Promise<User>

      Promise resolving to the created user object

      Throws CONFLICT error if email or username already exists

      Throws DATABASE_ERROR for other database-related failures

      const userData = {
      email: 'user@example.com',
      username: 'johndoe',
      country_code: 'US',
      role: 'student'
      };
      const user = await userRepository.create(userData);
    • Creates a new user with password hash for authentication

      Parameters

      • userData: CreateUserDto & { passwordHash: string }

        User data including password hash

        • email

          User's email address (required, unique)

        • username

          User's username (optional, unique if provided)

        • country_code

          Two-letter country code (optional)

        • profile_picture_url

          URL to user's profile picture (optional)

        • role

          User's role in the system (defaults to 'student')

        • passwordHash

          Hashed password for authentication

        • passwordHash: string

          Hashed password for authentication

      Returns Promise<User>

      Promise resolving to the created user object

      Throws CONFLICT error if email or username already exists

      Throws DATABASE_ERROR for other database-related failures

      Uses type-safe Prisma operations without unsafe type assertions, ensuring data integrity and compile-time type checking for all user creation operations.

    • Retrieves a user by their unique identifier

      Parameters

      • id: string

        User's unique identifier (UUID)

      Returns Promise<null | User>

      Promise resolving to user object or null if not found

      Throws DATABASE_ERROR for database-related failures

    • Retrieves minimal user data with password hash by their unique identifier for authentication

      Parameters

      • id: string

        User's unique identifier (UUID)

      Returns Promise<null | UserAuthData>

      Promise resolving to minimal user auth data or null if not found

      Throws DATABASE_ERROR for database-related failures

      This method uses Prisma's select to fetch only essential authentication fields, reducing data exposure and improving performance. The returned UserAuthData contains only the minimum information needed for password verification workflows.

      const authData = await userRepository.findByIdWithPassword('user-123');
      if (authData && authData.passwordHash) {
      const isValid = await comparePassword(plainPassword, authData.passwordHash);
      }
    • Retrieves a user by their email address

      Parameters

      • email: string

        User's email address

      Returns Promise<null | User>

      Promise resolving to user object or null if not found

      Throws DATABASE_ERROR for database-related failures

    • Retrieves minimal user data with password hash by their email address for authentication

      Parameters

      • email: string

        User's email address

      Returns Promise<null | UserAuthData>

      Promise resolving to minimal user auth data or null if not found

      Throws DATABASE_ERROR for database-related failures

      This method uses Prisma's select to fetch only essential authentication fields, reducing data exposure and improving performance. The returned UserAuthData contains only the minimum information needed for login workflows.

      const authData = await userRepository.findByEmailWithPassword('user@example.com');
      if (authData && authData.isActive && authData.passwordHash) {
      const isValid = await comparePassword(plainPassword, authData.passwordHash);
      if (isValid) {
      // Authentication successful, fetch full user data if needed
      const fullUser = await userRepository.findById(authData.id);
      }
      }
    • Retrieves a user by their username

      Parameters

      • username: string

        User's username

      Returns Promise<null | User>

      Promise resolving to user object or null if not found

      Throws DATABASE_ERROR for database-related failures

    • Updates existing user information with validation

      Parameters

      • id: string

        User's unique identifier (UUID)

      • updates: UpdateUserDto

        Object containing fields to update

        • username

          New username (optional, must be unique if provided)

        • country_code

          New country code (optional)

        • profile_picture_url

          New profile picture URL (optional)

        • is_active

          New active status (optional)

      Returns Promise<User>

      Promise resolving to the updated user object

      Throws NOT_FOUND error if user doesn't exist

      Throws CONFLICT error if username already exists

      Throws DATABASE_ERROR for other database-related failures

    • Performs soft delete by deactivating user account

      Parameters

      • id: string

        User's unique identifier (UUID)

      Returns Promise<boolean>

      Promise resolving to true if successful, false if user not found

      Throws DATABASE_ERROR for database-related failures

    • Retrieves paginated list of users with filtering and sorting capabilities

      Parameters

      • Optionaloptions: QueryOptions = {}

        Query options for pagination, filtering, and sorting

        • page

          Page number for pagination

        • limit

          Number of items per page

        • sortBy

          Field to sort by

        • sortOrder

          Sort order

        • filters

          Filters to apply

        • filters.role

          Filter by user role

        • filters.is_active

          Filter by active status

        • filters.search

          Search in email and username fields

      Returns Promise<PaginatedResult<User>>

      Promise resolving to paginated result with users and pagination metadata

      Throws DATABASE_ERROR for database-related failures

    • Updates user password hash securely

      Parameters

      • id: string

        User's unique identifier (UUID)

      • passwordHash: string

        New hashed password

      Returns Promise<User>

      Promise resolving to the updated user object

      Throws NOT_FOUND error if user doesn't exist

      Throws DATABASE_ERROR for other database-related failures

      Uses type-safe Prisma operations without unsafe type assertions, ensuring secure password updates with proper type checking and validation.

    • Updates user's last login timestamp for tracking purposes

      Parameters

      • id: string

        User's unique identifier (UUID)

      • Optionaldate: Date = ...

        Login timestamp (defaults to current date/time)

      Returns Promise<User>

      Promise resolving to the updated user object

      Throws NOT_FOUND error if user doesn't exist

      Throws DATABASE_ERROR for other database-related failures

      Uses type-safe Prisma operations without unsafe type assertions, ensuring reliable timestamp updates with proper type validation.