{"size": 1686, "ext": "js", "max_stars_repo_path": "server/v1/user/user.service.js", "max_stars_repo_name": "jonty007/mern-base", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/v1/user/user.service.js", "max_issues_repo_name": "jonty007/mern-base", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/v1/user/user.service.js", "max_forks_repo_name": "jonty007/mern-base", "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": 23.7464788732, "max_line_length": 81, "alphanum_fraction": 0.6892052195}
import { User } from '../../db/models';
import { getProfilePicture } from '../file/file.service';
import moment from 'moment';

function convertToUserDTO(user) {
  return {
    _id: user._id.toString(),
    firstName: user.firstName,
    lastName: user.lastName,
    email: user.email,
    phone: user.phone,
    dob: user.dob,
    profileImageId: user.profileImageId.toString()
  };
}

export async function getUserDetails(userId) {
  const user = await User.findById(userId);
  return convertToUserDTO(user);
}

export async function getUserProfilePicture(userId) {
  const user = await User.findById(userId);

  const info = await getProfilePicture(user.profileImageId.toString());
  return info;
}

export async function updateUserProfile(userId, data) {
  const { firstName, lastName, phoneNumber, dob, profileImageId } = data;

  if (!firstName) {
    throw new Error('USER.UPDATE.REQUIRED_FIRST_NAME');
  }

  if (!dob || !moment(dob, 'YYYY-MM-DD').isValid()) {
    throw new Error('USER.UPDATE.REQUIRED_DOB');
  }

  if (!profileImageId) {
    throw new Error('USER.UPDATE.REQUIRED_PROFILE_IMAGE');
  }

  const updatedUser = await User.findByIdAndUpdate(
    userId,
    {
      firstName,
      lastName,
      phone: phoneNumber,
      dob,
      profileImageId
    },
    { new: true }
  );

  return convertToUserDTO(updatedUser);
}

export async function updatePassword(userId, { existingPassword, newPassword }) {
  const user = await User.findById(userId);

  const passwordMatch = await user.isPasswordMatch(existingPassword);
  if (!passwordMatch) {
    throw new Error('USER.UPDATE.PASSWORD_NOT_MATCH');
  }
  
  await user.update({
    password: newPassword
  });
}
