<commit_before>'use strict';

const Promise = require('bluebird');
const User = require('../models/User');
const UserStats = require('../models/UserStats');

module.exports = function() {
  const create = function(userData) {
    return new Promise((resolve, reject) => {
      const user = new User(userData);
      user.createdAt = Date.now();
      user.updatedAt = Date.now();
      user.save()
        .then((createdUser) => {
          User.findById(createdUser.id)
            .select('-__v')
            .exec()
            .then((newUser) => {
              resolve(newUser.toObject());
            });
        })
        .catch((err) => {
          console.log(err);
          reject;
        });
    });
  };

  const createUserStats = function(userId) {
    return new Promise((resolve, reject) => {
      UserStats.create({ userId: userId })
        .then((newUserStats) => {
          resolve(newUserStats);
        })
        .catch((err) => {
          console.log(err);
          reject;
        });
    })
  };

  const getByEmailOrUsername = function(emailOrUsername) {
    return new Promise((resolve, reject) => {
      User.findOne({
        $or: [
          { email: emailOrUsername },
          { username: emailOrUsername },
        ],
      })
        .select('-__v')
        .exec()
        .then((user) => {
          resolve(user.toObject());
        })
        .catch(reject);
    });
  };

  const updateUser = function(emailOrUsername, newData) {
    return new Promise((resolve, reject) => {
      User.findOneAndUpdate({
        $or: [
          { email: emailOrUsername },
          { username: emailOrUsername },
        ],
      }, newData)
        .select('-__v -password')
        .exec()
        .then(() => {
          getByEmailOrUsername(emailOrUsername)
            .then((user) => {
              delete user.password;
              resolve(user);
            });
        })
        .catch(reject);
    });
  };

  const updateManyById = function(usersArray, updateQuery) {
    return new Promise((resolve, reject) => {
      User.updateMany({ _id: { $in: usersArray }}, updateQuery)
      .then((res) => {
        console.log(res);
        resolve(res);
      })
      .catch((err) => {
        console.log('error updating many in dao');
        console.log(err);
      });
    });
  };

  const getAllUsers = function(emailOrUsername) {
    return new Promise((resolve, reject) => {
      User.find({
        $or: [
          { email: { $ne: emailOrUsername }},
          { username: { $ne: emailOrUsername }},<commit_msg>Remove deleting user password in Dao. Should be handled on the service layer.<commit_after>'use strict';

const Promise = require('bluebird');
const User = require('../models/User');
const UserStats = require('../models/UserStats');

module.exports = function() {
  const create = function(userData) {
    return new Promise((resolve, reject) => {
      const user = new User(userData);
      user.createdAt = Date.now();
      user.updatedAt = Date.now();
      user.save()
        .then((createdUser) => {
          User.findById(createdUser.id)
            .select('-__v')
            .exec()
            .then((newUser) => {
              resolve(newUser.toObject());
            });
        })
        .catch((err) => {
          console.log(err);
          reject;
        });
    });
  };

  const createUserStats = function(userId) {
    return new Promise((resolve, reject) => {
      UserStats.create({ userId: userId })
        .then((newUserStats) => {
          resolve(newUserStats);
        })
        .catch((err) => {
          console.log(err);
          reject;
        });
    })
  };

  const getByEmailOrUsername = function(emailOrUsername) {
    return new Promise((resolve, reject) => {
      User.findOne({
        $or: [
          { email: emailOrUsername },
          { username: emailOrUsername },
        ],
      })
        .select('-__v')
        .exec()
        .then((user) => {
          resolve(user.toObject());
        })
        .catch(reject);
    });
  };

  const updateUser = function(emailOrUsername, newData) {
    return new Promise((resolve, reject) => {
      User.findOneAndUpdate({
        $or: [
          { email: emailOrUsername },
          { username: emailOrUsername },
        ],
      }, newData)
        .select('-__v -password')
        .exec()
        .then(() => {
          getByEmailOrUsername(emailOrUsername)
            .then((user) => {
              resolve(user);
            });
        })
        .catch(reject);
    });
  };

  const updateManyById = function(usersArray, updateQuery) {
    return new Promise((resolve, reject) => {
      User.updateMany({ _id: { $in: usersArray }}, updateQuery)
      .then((res) => {
        console.log(res);
        resolve(res);
      })
      .catch((err) => {
        console.log('error updating many in dao');
        console.log(err);
      });
    });
  };

  const getAllUsers = function(emailOrUsername) {
    return new Promise((resolve, reject) => {
      User.find({
        $or: [
          { email: { $ne: emailOrUsername }},
          { username: { $ne: emailOrUsername }},
{'fraction_non_alphanumeric': 0.15795586527293845, 'fraction_numerical': 0.0, 'mean_word_length': 1.8327850877192982, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '336928', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1481, 'n_words': 278}