All files / lib/agent/batch ChangeSet.js

96.96% Statements 32/33
100% Branches 10/10
90% Functions 9/10
96.96% Lines 32/33

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    7x 7x 7x 7x       15x 15x                                 1x                           1x 1x         2x                                         2x 2x 2x   48x   48x           2x 1x     30x 3x 3x 2x       3x 2x   1x   27x 24x   30x       1x   1x   2x                   4x       7x  
"use strict";
 
const Base = require("./Base");
const Request = require("./Request");
const Response = require("./Response");
const _ = require("lodash");
 
class ChangeSet extends Base {
  constructor() {
    super("requests", "changeset");
    this.commited = false;
  }
 
  /**
   * Add new item to the batch list object
   *
   * @param {String} httpMethod name of the HTTP method
   * @param {String} inputUrl relative path in the service
   * @param {Object} headers object which contains headers used for the post request
   * @param {Object} payload data which is converted to the JSON string and passed as body of POST request
   *
   * @returns {Object} new instance of Request class
   *
   * @private
   * @memberof ChangeSet
   */
  addRequest(...args) {
    return this.add(Request, ...args);
  }
 
  /**
   * Generate HTTP request which is part of thh multipart/mixed content for the OData batch
   *
   * @param {String} csrfToken passed to request headers
   *
   * @returns {String} changeset converted to the string
   *
   * @private
   * @memberof ChangeSet
   */
  payload(csrfToken) {
    let boundary = `--${this.boundary()}`;
    return _.concat(
      [],
      ...[
        `Content-Type: multipart/mixed; boundary=${this.boundary()}\n`,
        boundary,
        _.map(this.requests, (request) => request.payload(csrfToken)).join(
          `\n${boundary}\n`
        ),
        `${boundary}--`,
      ]
    ).join("\n");
  }
 
  /**
   * Parse response from OData response and resolve/reject promises of the particular
   * changeset requests.
   *
   * @param {String} changeSetResponse - content of the changeset from http batch request
   *
   * @returns {Promise} promise which is resolved by the particular responses inside the changeset
   *
   * @private
   * @memberof ChangeSet
   */
  process(changeSetResponse) {
    let promise;
    let promises = [];
    let insideChangeSet = false;
    let boundary = _.chain(changeSetResponse)
      .map((line) => {
        return line.match(/^Content-Type: multipart\/mixed; boundary=([^\s]+)/);
      })
      .filter((match) => match)
      .reduce((acc, match) => {
        return match[1];
      })
      .get(1)
      .value();
    if (boundary) {
      _.reduce(
        changeSetResponse,
        (acc, row) => {
          if (row.indexOf(`--${boundary}`) > -1) {
            insideChangeSet = true;
            if (acc.length) {
              promises.push(
                this.requests[acc.length - 1].process(acc[acc.length - 1])
              );
            }
            if (row !== `--${boundary}--`) {
              acc.push([]);
            } else {
              insideChangeSet = false;
            }
          } else if (insideChangeSet) {
            acc[acc.length - 1].push(row);
          }
          return acc;
        },
        []
      );
      promise = Promise.all(promises);
    } else {
      promise = new Response(changeSetResponse).promise;
    }
    return promise;
  }
 
  /**
   * Mark changeset as committed
   *
   * @public
   * @memberof ChangeSet
   */
  commit() {
    this.commited = true;
  }
}
 
module.exports = ChangeSet;