All files / Zapcard_API/services stripper.service.js

95.24% Statements 20/21
66.67% Branches 4/6
100% Functions 4/4
100% Lines 16/16
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                1x                 2x 2x 2x 1x   1x                         3x 3x 3x 3x 3x 2x 2x 2x   1x             1x        
/**
 *  @fileOverview A service that handles cleanup operations on objects called
 *
 *  @author       Ralfs Lagzda
 *
 *  @requires     NPM:lodash
 */
 
const _ = require('lodash');
/**
 * Strip irrelevant or even sensitive information
 *
 * @memberOf StripperService
 * @function defaultStrip
 * @param {Object} modelObject the object to strip the default properties from
 * @returns {Promise<Object>} eventually strips and resolves the object or throws error
 */
const defaultStrip = modelObject => new Promise(async (resolve, reject) => {
  try {
    if (typeof (modelObject) !== 'object') throw (Error('Invalid parameters'));
    resolve(_.omit(modelObject, ['createdBy', '__v']));
  } catch (err) {
    reject(err);
  }
});
 
/**
 * Build a request query body that conforms to the protocol
 *
 * @memberOf StripperService
 * @function buildDefaultQuery
 * @param {Object} queryObject the query object to build upon
 * @returns {Promise<Object>}
 * eventually builds and resoles the query object or throws error
 */
const buildDefaultQuery = (userId, queryObject) => new Promise(async (resolve, reject) => {
  try {
    let newQueryObject = _.omit(queryObject, ['createdBy', '__v', '_id']);
    const paramMap = { createdBy: userId };
    Iif (queryObject.name) paramMap.name = new RegExp(`${queryObject.name}`, 'i');
    Eif (queryObject.telephone) paramMap.telephone = new RegExp(`${queryObject.telephone}`, 'i');
    newQueryObject = await Object.assign(newQueryObject, paramMap);
    resolve(newQueryObject);
  } catch (err) {
    reject(err);
  }
});
/**
 * A service that handles cleanup operations on objects called
 * @namespace StripperService
 */
module.exports = {
  defaultStrip,
  buildDefaultQuery,
};