All files / lib/agent/batch Manager.js

100% Statements 19/19
80% Branches 8/10
100% Functions 8/8
100% Lines 19/19

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    11x 11x 11x                               81x                       1x 1x                           1x                           3x                               5x   5x 1x     4x 4x 4x 7x       4x       5x       1x       11x  
"use strict";
 
const Batch = require("./Batch");
const Base = require("./Base");
const _ = require("lodash");
 
/**
 * The class manages the batches defined by the OData client
 *
 * @see http://docs.oasis-open.org/odata/odata/v4.01/cs01/part1-protocol/odata-v4.01-cs01-part1-protocol.html#_Toc505771274
 *
 * @class Manager
 */
class Manager extends Base {
  /**
   * Creates an instance of <code>Manager</code>.
   *
   * @memberof Manager
   */
  constructor() {
    super("batches");
  }
 
  /**
   * Add new batch object to the list of the batches
   *
   * @return {Object} newly created the batch object which represents future batch request
   *
   * @public
   * @memberof Manager
   */
  add() {
    let batch = super.add(Batch);
    return batch;
  }
 
  /**
   * Remove batch from the currently registered batch objects
   *
   * @param {Object} batch object for remove
   *
   * @return {Object} removed batch object
   *
   * @private
   * @memberof Manager
   */
  remove(batch) {
    return this.batches.splice(this.indexOf(batch), 1);
  }
 
  /**
   * Check existency of passed batch in registered batches
   *
   * @param {Object} batch to check
   *
   * @return {Boolean} true if batch object exists
   *
   * @public
   * @memberof Manager
   */
  has(batch) {
    return this.batches.length > 0 && !!this.batches[this.indexOf(batch)];
  }
 
  /**
   * Find index of batch passed as parameter. Raise error if batch is not
   * Batch type
   *
   * @param {Batch} batch to find
   *
   * @return {Number} index of the found batch or -1 if does not exists
   *
   * @public
   * @memberof Manager
   */
  indexOf(batch) {
    let id;
    let index = -1;
 
    if (!(batch instanceof Batch)) {
      throw new Error("Only instance of batch has to be found.");
    }
 
    id = _.get(batch, "id");
    Eif (_.isString(id)) {
      index = _.findIndex(this.batches, (processedBatch) => {
        return _.get(processedBatch, "id") === id;
      });
    }
 
    return index;
  }
 
  get defaultBatch() {
    return this.batches.length > 0 ? this.batches[0] : undefined;
  }
 
  get defaultChangeSet() {
    return this.defaultBatch ? this.defaultBatch.defaultChangeSet : undefined;
  }
}
 
module.exports = Manager;