All files / Zapcard_API/services email.service.js

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