{"size": 2039, "ext": "js", "max_stars_repo_path": "src/api/users/users.da.js", "max_stars_repo_name": "segalzeyalz/linkedin", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/api/users/users.da.js", "max_issues_repo_name": "segalzeyalz/linkedin", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/api/users/users.da.js", "max_forks_repo_name": "segalzeyalz/linkedin", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 18.0442477876, "max_line_length": 96, "alphanum_fraction": 0.5713585091}
import Q from 'q';
import users from './users.model';

const getAll = () => {
  const deferred = Q.defer();

  users.find({}, (err, users) => {
    if (err) deferred.reject(err);
    deferred.resolve(users);
  });
  return deferred.promise;
}
const getFindUsers = (query) => {
  const deferred = Q.defer();
  const {
    company,
    name,
    title
  } = query;

  const finderObj = {};
  if (company) finderObj.company = company;
  if (name) finderObj.name = name;
  if (title) finderObj.title = title;
  users.find(finderObj, (err, users) => {
    if (err) deferred.reject(err);
    deferred.resolve(users);
  });
  return deferred.promise;
}
const getUser = (id) => {
  const deferred = Q.defer();

  users.find({
    _id: id
  }, (err, users) => {
    if (err) deferred.reject(err);
    deferred.resolve(users);
  });
  return deferred.promise;
}

const update = (
  id,
  name,
  title = '',
  company = '',
  skills = []) => () => {
  const deferred = Q.defer();
  const query = {
    id,
    name,
    title,
    company,
    skills
  };

  if (Object.keys(query).length > 0) {
    users.update({
      _id: id
    }, {
      $set: query
    }, (err, user) => {
      if (err) deferred.reject(err);

      deferred.resolve(user);
    });
  } else {
    // reject promise if name and completed information is missing
    deferred.reject({});
  }

  return deferred.promise;
}

const create = (name = '', title = '', company = '', skills = [], created = new Date().now) => {
  const deferred = Q.defer();
  const user = new users({
    name,
    title,
    company,
    skills,
    created
  });
  user.save((err, savedUser) => {
    if (err) deferred.reject(err);

    deferred.resolve(savedUser);
  });

  return deferred.promise;
}

const remove = () => (id) => {
  const deferred = Q.defer();
  users.remove({
    _id: id
  }, (err, user) => {
    if (err) deferred.reject(err);

    deferred.resolve(user);
  });

  return deferred.promise;
}
export default {
  getAll,
  getFindUsers,
  getUser,
  update,
  create,
  remove
};