All files / Zapcard_API/services telephone.service.js

97.73% Statements 43/44
100% Branches 8/8
100% Functions 12/12
97.22% Lines 35/36
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                1x 1x                   7x 7x 7x 6x   1x                         4x 4x 4x 2x   3x                     5x 5x 5x   1x                         2x 2x 2x 1x   1x                       2x 2x 2x 1x   1x                           4x 4x 4x 3x 3x 2x 2x   1x                 1x                
/**
 *  @fileOverview A service that handles CRUD operations for user's telephones
 *
 *  @author       Ralfs Lagzda
 *
 *  @requires     NPM:lodash
 *  @requires     ../models/telephone
 */
const _ = require('lodash');
const Telephone = require('../models/telephone');
/**
 * List all telephones
 *
 * @memberOf TelephoneService
 * @function list
 * @param {Object} params the parameters to limit the query to desired results
 * @returns {Promise<Telephone[]>} Promise that eventually resolves an array
 * of telephones or throws error
 */
const list = params => new Promise(async (resolve, reject) => {
  try {
    if (typeof (params) !== 'object' || _.isEmpty(params)) throw (Error('Invalid parameters'));
    resolve(await Telephone.find(params));
  } catch (err) {
    reject(err);
  }
});
/**
 * Create a telephone
 *
 * @memberOf TelephoneService
 * @function create
 * @param {String} userId the owner of the telephone
 * @param {Object} telephone the telephone object to create
 * @returns {Promise<Telephone>} Promise that eventually creates and resolves
 * a telephone or throws error
 */
const create = (userId, telephone) => new Promise(async (resolve, reject) => {
  try {
    const creatableTelephone = await Object.assign(telephone, { createdBy: userId });
    resolve(await new Telephone(creatableTelephone).save());
  } catch (err) {
    reject(err);
  }
});
/**
 * Retrieves a single telephone by ID
 *
 * @memberOf TelephoneService
 * @function read
 * @param {String} telephoneId the ID of the telephone
 * @returns {Promise<Telephone>} Promise that eventually resolves a telephone or throws error
 */
const read = telephoneId => new Promise(async (resolve, reject) => {
  try {
    resolve(await Telephone.findById(telephoneId));
  } catch (err) {
    reject(err);
  }
});
/**
 * Updates a single telephone
 *
 * @memberOf TelephoneService
 * @function update
 * @param {String} telephoneId the ID of the telephone
 * @param {Object} body key/value pairs of the fields that should be changed
 * @returns {Promise<Telephone>} Promise that eventually updates and
 * resolves a telephone or throws error
 */
const update = (telephoneId, body) => new Promise(async (resolve, reject) => {
  try {
    const updatedTelephone = await Object.assign(await Telephone.findById(telephoneId), body);
    resolve(await updatedTelephone.save());
  } catch (err) {
    reject(err);
  }
});
/**
 * Deletes a single telephone
 *
 * @memberOf TelephoneService
 * @function del
 * @param {String} telephoneId the ID of the telephone
 * @returns {Promise} Promise that eventually deletes
 * a telephone or throws error
 */
const del = telephoneId => new Promise(async (resolve, reject) => {
  try {
    await Telephone.remove({ _id: telephoneId });
    resolve();
  } catch (err) {
    reject(err);
  }
});
/**
 * Find or Create a telephone
 *
 * @memberOf TelephoneService
 * @function findOrCreate
 * @param {String} userId the owner of the telephone
 * @param {Object} telephone the telephone object to create
 * @param {String} telephone.telephone the telephone string in the object
 * @returns {Promise<Telephone>} eventually creates and returns a
 * telephone or throws error
 */
const findOrCreate = (userId, telephone) => new Promise(async (resolve, reject) => {
  try {
    if (!telephone) return resolve('');
    const existingTelephone = await Telephone.findOne({ telephone });
    if (!existingTelephone) {
      const creatableTelephone = await Object.assign({ telephone }, { createdBy: userId });
      return resolve(await new Telephone(creatableTelephone).save());
    }
    return resolve(existingTelephone);
  } catch (err) {
    return reject(err);
  }
});
/**
 * A service that handles CRUD operations for user's telephones
 * @namespace TelephoneService
 */
module.exports = {
  list,
  create,
  read,
  update,
  del,
  findOrCreate,
};