const BaseData = require('../base/base.data');
const User = require('../../models/user.model');
const bcrypt = require('bcrypt');

class UsersData extends BaseData {
    constructor(db) {
        super(db, User, User);
    }

    findByUsername(username) {
        return this
            .filterBy({ username: new RegExp(username, 'i') })
            .then(([user]) => user);
    }

    create(model) {
        let newInstance;

        try {
            newInstance = new User(model);
        } catch (error) {
            return Promise.reject(error);
        }

        return this._generateHash(newInstance)
            .then((user) => {
                return this.collection.insert(user);
            })
            .then((inserted) => {
                return inserted;
            });
    }

    update(model) {
        let newInstance;

        try {
            newInstance = new User(model);
        } catch (error) {
            return Promise.reject(error);
        }

        return this._generateHash(newInstance)
            .then((user) => {
                return this.collection.update(user);
            })
            .then((updated) => {
                return updated;
            });
    }

    _generateHash(bodyUser) {
        const promise = new Promise((res, rej) => {
            bcrypt.genSalt(10, (err, salt) => {
                if (err) {
                    return rej(err);
                }
                return bcrypt.hash(bodyUser.password, salt, (error, hash) => {
                    if (err) {
                        return rej(error);
                    }

                    bodyUser.passHash = hash;
                    delete bodyUser.password;

                    if (bodyUser['repeat-password']) {
                        delete bodyUser['repeat-password'];
                    }
                    return res(bodyUser);
                });
            });
        });
        return promise;
    }
}

module.exports = UsersData;

@@ -87,18 +87,124 @@
 .model')
+.User;%0A// eslint-disable-next-line prefer-const%0Alet initUser = require('../../models/user.model').initUser
 ;%0A
-
 const bc
@@ -308,14 +308,8 @@
 (db,
- User,
  Use
@@ -555,36 +555,36 @@
   newInstance = 
-new 
+init
 User(model);%0A   
@@ -980,20 +980,20 @@
 tance = 
-new 
+init
 User(mod

change new User with initUser in users.data.js