All files / lib/model/oasis/schema Action.js

100% Statements 41/41
100% Branches 18/18
100% Functions 13/13
100% Lines 40/40

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    1x 1x 1x 1x                                 19x 19x 16x 16x 3x       19x 20x   19x 5x     19x 2x     19x 1x     19x   5x     5x   10x   5x       19x     3x 3x 2x   3x       19x                       2x 1x     2x 2x                 19x 1x     18x 1x                         1x       1x 1x   1x  
"use strict";
 
const _ = require("lodash");
const AnnotationTarget = require("../annotations/AnnotationTarget");
const Parameter = require("./Parameter");
const ReturnType = require("./ReturnType");
 
/**
 * Action - service-defined operation.
 *
 * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_Action
 *
 * @class Action
 * @extends {AnnotationTarget}
 */
class Action extends AnnotationTarget {
  /**
   * Creates an instance of Action
   * @param {Object} rawMetadata raw metadata object for an action
   * @memberof Action
   */
  constructor(rawMetadata) {
    super(rawMetadata);
    if (_.isArray(rawMetadata.ReturnType)) {
      let returnType = new ReturnType(rawMetadata.ReturnType[0]);
      Object.defineProperty(this, "returnType", {
        get: () => returnType,
      });
    }
 
    let parameters = (rawMetadata.Parameter || []).map(
      (md) => new Parameter(md)
    );
    Object.defineProperty(this, "parameters", {
      get: () => parameters,
    });
 
    Object.defineProperty(this, "isBound", {
      get: () => rawMetadata.$.IsBound === "true",
    });
 
    Object.defineProperty(this, "entitySetPath", {
      get: () => rawMetadata.$.EntitySetPath,
    });
 
    Object.defineProperty(this, "entityTypePath", {
      get: () => {
        const entitySetPath = _.has(rawMetadata, "$.EntitySetPath")
          ? rawMetadata.$.EntitySetPath
          : Action.DEFAULT_ENTITY_SET_PATH;
        let parameterWithType = _.filter(
          rawMetadata.Parameter,
          (parameter) => parameter.$.Name === entitySetPath
        );
        return _.get(parameterWithType, "0.$.Type");
      },
    });
 
    Object.defineProperty(this, "entityType", {
      get: () => {
        let matchCollection;
        let entityType = this.entityTypePath;
        if (_.isString(entityType)) {
          matchCollection = entityType.match(Action.COLLECTION_TYPE_REGEXP);
        }
        return matchCollection ? matchCollection[1] : entityType;
      },
    });
 
    this._checkConsistency();
  }
 
  /**
   * Initializes schema dependent properties. Decoupled from constructor,
   * because it needs to resolve schema (type) references.
   *
   * @param {CsdlSchema} schema to resolve references
   * @returns {Action} this to allow methods chaining
   * @memberof Action
   */
  initSchemaDependentProperties(schema) {
    if (_.has(this, "returnType")) {
      this.returnType.initSchemaDependentProperties(schema);
    }
 
    this.parameters.forEach((p) => p.initSchemaDependentProperties(schema));
    return this;
  }
 
  /**
   * Checks properties consistency, i.e. mandatory properties, return type.
   *
   * @memberof Action
   */
  _checkConsistency() {
    if (!this.name) {
      throw new Error("Name attribute is mandatory for action.");
    }
 
    if (_.isArray(this.raw.ReturnType) && this.raw.ReturnType.length !== 1) {
      throw new Error(
        `Function ${this.name} may contain at most one ReturnType element`
      );
    }
  }
 
  /**
   * Resolves model path within this type.
   *
   * @returns {Object} itself
   * @memberof Function
   */
  resolveModelPath() {
    return this;
  }
}
 
Action.DEFAULT_ENTITY_SET_PATH = "_it";
Action.COLLECTION_TYPE_REGEXP = /^Collection\(([^)]+)\)$/;
 
module.exports = Action;