All files / Zapcard_API/services card.service.js

100% Statements 35/35
100% Branches 4/4
100% Functions 10/10
100% Lines 29/29
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                1x 1x                 4x 4x 4x 3x       3x   1x                       4x 4x 4x 3x   1x                     2x 2x 2x       1x   1x                       2x 2x 2x 1x   1x                     2x 2x 2x 1x   1x             1x              
/**
 *  @fileOverview A service that handles CRUD operations for user's cards
 *
 *  @author       Ralfs Lagzda
 *
 *  @requires     NPM:lodash
 *  @requires     ../models/card
 */
const _ = require('lodash');
const Card = require('../models/card');
/**
 * List all cards
 *
 * @memberOf CardService
 * @function list
 * @param {Object} params the parameters to limit the query to desired results
 * @returns {Promise<Card[]>} Promise that eventually resolves an array of cards or throws error
 */
const list = params => new Promise(async (resolve, reject) => {
  try {
    if (typeof (params) !== 'object' || _.isEmpty(params)) throw (Error('Invalid parameters'));
    const cards = await Card.find(params)
      .populate('description')
      .populate('email')
      .populate('telephone');
    resolve(cards);
  } catch (err) {
    reject(err);
  }
});
/**
 * Create a card
 *
 * @memberOf CardService
 * @function create
 * @param {String} userId the owner of the card
 * @param {Object} card the card object to create
 * @returns {Promise<Card>} Promise that eventually creates and resolves a card or throws error
 */
const create = (userId, card) => new Promise(async (resolve, reject) => {
  try {
    const creatableCard = await Object.assign(card, { createdBy: userId });
    resolve(new Card(creatableCard).save());
  } catch (err) {
    reject(err);
  }
});
/**
 * Retrieves a single card by ID
 *
 * @memberOf CardService
 * @function read
 * @param {String} cardId the ID of the card
 * @returns {Promise<Card>} Promise that eventually resolves a card or throws error
 */
const read = cardId => new Promise(async (resolve, reject) => {
  try {
    const card = await Card.findById(cardId).lean()
      .populate('description')
      .populate('email')
      .populate('telephone');
    resolve(card);
  } catch (err) {
    reject(err);
  }
});
/**
 * Updates a single card
 *
 * @memberOf CardService
 * @function update
 * @param {String} cardId the ID of the card
 * @param {Object} body key/value pairs of the fields that should be changed
 * @returns {Promise<Card>} Promise that eventually updates and resolves a card or throws error
 */
const update = (cardId, body) => new Promise(async (resolve, reject) => {
  try {
    const updatedCard = await Object.assign(await Card.findById(cardId), body);
    resolve(updatedCard.save());
  } catch (err) {
    reject(err);
  }
});
/**
 * Deletes a single card
 *
 * @memberOf CardService
 * @function del
 * @param {String} cardId the ID of the card
 * @returns {Promise} Promise that eventually deletes a card or throws error
 */
const del = cardId => new Promise(async (resolve, reject) => {
  try {
    await Card.remove({ _id: cardId });
    resolve();
  } catch (err) {
    reject(err);
  }
});
/**
 * A service that handles CRUD operations for user's cards
 * @namespace CardService
 */
module.exports = {
  list,
  create,
  read,
  update,
  del,
};