All files / Zapcard_API/services user.service.js

83.33% Statements 50/60
50% Branches 2/4
75% Functions 15/20
84.31% Lines 43/51
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171                        1x 1x 1x 1x 1x                 3x 3x 3x                         4x 4x 4x   3x                     2x 2x 2x 1x   1x                       2x 2x 2x   1x                     2x 2x 2x 1x   1x                     1x                         1x                               3x 3x 3x   1x 1x 1x 1x 1x     1x 1x 1x 1x     1x       1x             1x                    
/**
 *  @fileOverview A service that handles CRUD operations on users and picture uploads.
 *
 *  @author       Ralfs Lagzda
 *
 *  @requires     NPM:fs
 *  @requires     NPM:mkdirp
 *  @requires     NPM:checksum
 *  @requires     NPM:path
 *  @requires     ../models/user
 */
 
const fs = require('fs');
const mkdirp = require('mkdirp');
const checksum = require('checksum');
const path = require('path');
const User = require('../models/user');
 
/**
 * List all users
 *
 * @memberOf UserService
 * @function list
 * @returns {Promise<Card[]>} Promise that eventually resolves an array of users or throws error
 */
const list = () => new Promise(async (resolve, reject) => {
  try {
    resolve(await User.find({}));
  } catch (err) {
    reject(err);
  }
});
/**
 * Create a user
 *
 * @memberOf UserService
 * @function create
 * @param {Object} user the user object to create
 * @returns {Promise<User>} Promise that eventually creates and resolves a user or throws error
 */
const create = user => new Promise(async (resolve, reject) => {
  try {
    resolve(await new User(user).save());
  } catch (err) {
    reject(err);
  }
});
/**
 * Retrieves a single user by ID
 *
 * @memberOf UserService
 * @function read
 * @param {String} userId the ID of the user
 * @returns {Promise<User>} Promise that eventually resolves a user or throws error
 */
const read = userId => new Promise(async (resolve, reject) => {
  try {
    const user = await User.findById(userId);
    resolve(user);
  } catch (err) {
    reject(err);
  }
});
/**
 * Updates a single user
 *
 * @memberOf UserService
 * @function update
 * @param {String} userId the ID of the user
 * @param {Object} body key/value pairs of the fields that should be changed
 * @returns {Promise<User>} Promise that eventually updates and resolves a user or throws error
 */
const update = (userId, body) => new Promise(async (resolve, reject) => {
  try {
    resolve(await Object.assign(await User.findById(userId), body).save());
  } catch (err) {
    reject(err);
  }
});
/**
 * Deletes a single user
 *
 * @memberOf UserService
 * @function del
 * @param {String} userId the ID of the user
 * @returns {Promise} Promise that eventually deletes a user or throws error
 */
const del = userId => new Promise(async (resolve, reject) => {
  try {
    await User.remove({ _id: userId });
    resolve();
  } catch (err) {
    reject(err);
  }
});
/**
 * Removes sensitive information from the user object
 *
 * @memberOf UserService
 * @function stripSensitiveInformation
 * @param {Object} user the user object to be stripped
 * @returns {Object} the stripped user object
 */
const stripSensitiveInformation = (user) => {
  const publicUser = user;
  delete publicUser.password;
  return publicUser;
};
/**
 * Retrieve the profile picture for the user
 *
 * @memberOf UserService
 * @function getProfilePicture
 * @param {String} userId the userId folder path where to look for the picture
 * @returns {Buffer} the profile picture
 */
const getProfilePicture = userId => new Promise(async (resolve, reject) => {
  fs.readFile(path.resolve(`./uploads/${userId}/image.png`), (err, data) => {
    if (err) reject(err);
    else resolve(data);
  });
});
/**
 * Upload a picture for the user
 *
 * @memberOf UserService
 * @function modifyProfilePicture
 * @param {Object} user the user to which picture needs to be added
 * @param {Object} files the userId folder path where to look for the picture
 * @param {String} files.path the path where the temporary picture is currently stored
 * @returns {Buffer} the profile picture
 */
const modifyProfilePicture = (user, files) => new Promise(async (resolve, reject) => {
  try {
    if (!files.path) reject(Error('Path to file was not provided'));
    else {
      const oldPath = files.path;
      const newPath = path.resolve(`./uploads/${user.id}`);
      mkdirp(newPath, async () => {
        const readStream = fs.createReadStream(oldPath);
        readStream.once('error', (err) => {
          reject(err);
        });
        readStream.once('end', () => {
          checksum.file(`${newPath}/image.png`, async (err, sum) => {
            await Object.assign(user, { profilePicture: sum }).save();
            resolve(sum);
          });
        });
        readStream.pipe(fs.createWriteStream(`${newPath}/image.png`));
      });
    }
  } catch (err) {
    reject(err);
  }
});
/**
 * A service that handles CRUD operations on users and picture uploads.
 * @namespace UserService
 */
module.exports = {
  list,
  create,
  read,
  update,
  del,
  stripSensitiveInformation,
  modifyProfilePicture,
  getProfilePicture,
};