All files / src/security SecurityDocument.js

100% Statements 47/47
80.56% Branches 29/36
100% Functions 8/8
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149    112x 7x       105x                                                   105x 103x       105x 103x     1603x   1603x                         1x 107x 107x               1x   5x   5x 5x     5x 5x   5x                 1x   6x   6x 6x 6x     6x 6x 3x     3x 3x                         1x   9x 9x   9x 3x     6x 6x 6x     6x 6x   6x 6x 3x     3x   3x 3x       6x     1x  
function SecurityDocument(Security, id, content, meta) {
 
  if (!id) {
    throw new Error('A security document must have an id');
  }
 
  // Define properties
  Object.defineProperties(this, {
    // private properties
    kuzzle: {
      value: Security.kuzzle
    },
    Security: {
      value: Security
    },
    // read-only properties
    // writable properties
    id: {
      value: id,
      enumerable: true
    },
    content: {
      value: {},
      writable: true,
      enumerable: true
    },
    meta: {
      value: meta || {},
      writable: true,
      enumerable: true
    }
  });
 
  if (content) {
    this.setContent(content, true);
  }
 
  // promisifying
  if (Security.kuzzle.bluebird) {
    return Security.kuzzle.bluebird.promisifyAll(this, {
      suffix: 'Promise',
      filter: function (name, func, target, passes) {
        var whitelist = ['delete', 'update'];
 
        return passes && whitelist.indexOf(name) !== -1;
      }
    });
  }
}
 
/**
 * Replaces the current content with new data.
 * Changes made by this function won’t be applied until the save method is called.
 *
 * @param {Object} data - New securityDocument content
 * @return {SecurityDocument} this
 */
SecurityDocument.prototype.setContent = function (data) {
  this.content = data;
  return this;
};
 
/**
 * Serialize this object into a pojo
 *
 * @return {object} pojo representing this securityDocument
 */
SecurityDocument.prototype.serialize = function () {
  var
    data = {};
 
  Eif (this.id) {
    data._id = this.id;
  }
 
  data.body = this.content;
  data.meta = this.meta;
 
  return data;
};
 
/**
 * Delete the current KuzzleSecurityDocument into Kuzzle.
 *
 * @param {object} [options] - Optional parameters
 * @param {responseCallback} [cb] - Handles the query response
 */
SecurityDocument.prototype.delete = function (options, cb) {
  var
    self = this;
 
  Eif (options && cb === undefined && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.query(this.Security.buildQueryArgs(this.deleteActionName), {_id: this.id}, options, function (error, res) {
    if (error) {
      return cb ? cb(error) : false;
    }
 
    Eif (cb) {
      cb(null, res.result._id);
    }
  });
};
 
/**
 * Update the current KuzzleSecurityDocument into Kuzzle.
 *
 * @param {object} content - Content to add to KuzzleSecurityDocument
 * @param {object} [options] - Optional parameters
 * @param {responseCallback} [cb] - Handles the query response
 * @returns {SecurityDocument} this
 */
SecurityDocument.prototype.update = function (content, options, cb) {
  var
    data = {},
    self = this;
 
  if (typeof content !== 'object') {
    throw new Error('Parameter "content" must be a object');
  }
 
  Eif (options && cb === undefined && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  data._id = self.id;
  data.body = content;
 
  self.kuzzle.query(this.Security.buildQueryArgs(this.updateActionName), data, options, function (error, response) {
    if (error) {
      return cb ? cb(error) : false;
    }
 
    self.setContent(response.result._source);
 
    Eif (cb) {
      cb(null, self);
    }
  });
 
  return this;
};
 
module.exports = SecurityDocument;