All files / Zapcard_API/services description.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                     2x 2x 2x   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 descriptions
 *
 *  @author       Ralfs Lagzda
 *
 *  @requires     NPM:lodash
 *  @requires     ../models/description
 */
const _ = require('lodash');
const Description = require('../models/description');
/**
 * List all descriptions
 *
 * @memberOf DescriptionService
 * @function list
 * @param {Object} params the parameters to limit the query to desired results
 * @returns {Promise<Description[]>} Promise that eventually resolves an array
 * of descriptions or throws error
 */
const list = params => new Promise(async (resolve, reject) => {
  try {
    if (typeof (params) !== 'object' || _.isEmpty(params)) throw (Error('Invalid parameters'));
    resolve(await Description.find(params));
  } catch (err) {
    reject(err);
  }
});
/**
 * Create a description
 *
 * @memberOf DescriptionService
 * @function create
 * @param {String} userId the owner of the description
 * @param {Object} description the description object to create
 * @returns {Promise<Description>} Promise that eventually creates and resolves
 * a description or throws error
 */
const create = (userId, description) => new Promise(async (resolve, reject) => {
  try {
    const creatableDescription = await Object.assign(description, { createdBy: userId });
    resolve(await new Description(creatableDescription).save());
  } catch (err) {
    reject(err);
  }
});
/**
 * Retrieves a single description by ID
 *
 * @memberOf DescriptionService
 * @function read
 * @param {String} descriptionId the ID of the description
 * @returns {Promise<Description>} Promise that eventually resolves a description or throws error
 */
const read = descriptionId => new Promise(async (resolve, reject) => {
  try {
    resolve(await Description.findById(descriptionId));
  } catch (err) {
    reject(err);
  }
});
/**
 * Updates a single description
 *
 * @memberOf DescriptionService
 * @function update
 * @param {String} descriptionId the ID of the description
 * @param {Object} body key/value pairs of the fields that should be changed
 * @returns {Promise<Description>} Promise that eventually updates and
 * resolves a description or throws error
 */
const update = (descriptionId, body) => new Promise(async (resolve, reject) => {
  try {
    const updatedDescription = await Object.assign(await Description.findById(descriptionId), body);
    resolve(await updatedDescription.save());
  } catch (err) {
    reject(err);
  }
});
/**
 * Deletes a single description
 *
 * @memberOf DescriptionService
 * @function del
 * @param {String} descriptionId the ID of the description
 * @returns {Promise} Promise that eventually deletes
 * a description or throws error
 */
const del = descriptionId => new Promise(async (resolve, reject) => {
  try {
    await Description.remove({ _id: descriptionId });
    resolve();
  } catch (err) {
    reject(err);
  }
});
/**
 * Find or Create a description
 *
 * @memberOf DescriptionService
 * @function findOrCreate
 * @param {String} userId the owner of the description
 * @param {Object} description the description object to create
 * @param {String} description.description the description string in the object
 * @returns {Promise<Description>} eventually creates and returns a
 * description or throws error
 */
const findOrCreate = (userId, description) => new Promise(async (resolve, reject) => {
  try {
    if (!description) return resolve('');
    const existingDescription = await Description.findOne({ description });
    if (!existingDescription) {
      const creatableDescription = await Object.assign({ description }, { createdBy: userId });
      return resolve(await new Description(creatableDescription).save());
    }
    return resolve(existingDescription);
  } catch (err) {
    return reject(err);
  }
});
/**
 * A service that handles CRUD operations for user's descriptions
 * @namespace DescriptionService
 */
module.exports = {
  list,
  create,
  read,
  update,
  del,
  findOrCreate,
};