# Code coverage report for infrastructure/repositories/UserRepository.js

# All files / infrastructure/repositories UserRepository.js

0% Statements 0/25
0% Branches 0/4
0% Functions 0/9
0% Lines 0/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131

     const IUserRepository = require('../../domain/irepositories/IUserRepository');
    const User = require('../../domain/models/User');

    /**
     * User Repository Implementation
     * Prisma-based data access for User entity
     */
    class UserRepository extends IUserRepository {
      constructor(prisma) {
        super();
        this.prisma = prisma;
      }

      /**
       * Find user by ID
       * @param {number} id
       * @returns {Promise<User|null>}
       */
      async findById(id) {
        const userData = await this.prisma.user.findUnique({
          where: { id: parseInt(id) }
        });

        if (!userData) {
          return null;
        }

        return this._toDomain(userData);
      }

      /**
       * Find user by email
       * @param {string} email
       * @returns {Promise<User|null>}
       */
      async findByEmail(email) {
        const userData = await this.prisma.user.findUnique({
          where: { email: email.toLowerCase() }
        });

        if (!userData) {
          return null;
        }

        return this._toDomain(userData);
      }

      /**
       * Find all users
       * @returns {Promise<User[]>}
       */
      async findAll() {
        const usersData = await this.prisma.user.findMany({
          orderBy: { createdAt: 'desc' }
        });

        return usersData.map(userData => this._toDomain(userData));
      }

      /**
       * Create new user
       * @param {User} user
       * @returns {Promise<User>}
       */
      async create(user) {
        const userData = await this.prisma.user.create({
          data: {
            name: user.name,
            email: user.email,
            password: user.password
          }
        });

        return this._toDomain(userData);
      }

      /**
       * Update existing user
       * @param {User} user
       * @returns {Promise<User>}
       */
      async update(user) {
        const userData = await this.prisma.user.update({
          where: { id: user.id },
          data: {
            name: user.name,
            email: user.email,
            password: user.password,
            updatedAt: new Date()
          }
        });

        return this._toDomain(userData);
      }

      /**
       * Delete user by ID
       * @param {number} id
       * @returns {Promise<boolean>}
       */
      async delete(id) {
        try {
          await this.prisma.user.delete({
            where: { id: parseInt(id) }
          });
          return true;
        } catch (error) {
          return false;
        }
      }

      /**
       * Convert Prisma data to Domain model
       * @private
       * @param {Object} userData - Prisma user data
       * @returns {User}
       */
      _toDomain(userData) {
        return new User(
          userData.id,
          userData.name,
          userData.email,
          userData.password,
          userData.createdAt,
          userData.updatedAt
        );
      }
    }

    module.exports = UserRepository;