Source

planConstants.js

  1. const common = require('./common');
  2. /**
  3. * A module related to xMatters communication plan constants.<br><br>
  4. * {@link https://help.xmatters.com/xmapi/index.html#plan-constants}
  5. *
  6. * @module planConstants
  7. */
  8. /**
  9. * Get a plan constant from xMatters<br><br>
  10. *
  11. * {@link https://help.xmatters.com/xmapi/index.html#get-plan-constants}
  12. *
  13. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  14. * @param {string} constantId
  15. * @param {Object} query A json object representing the query string parameters for this request.
  16. * @param {string} planId The unique identifier (id) or name of the plan you want to list the constants for.
  17. * @returns {Promise<PlanConstant>} Plan Constant Object Requested
  18. */
  19. async function get(env, constantId, query, planId) {
  20. return common.get(env, `/api/xm/1/plans/${planId}/constants/`, constantId, query, 'Plan Constant');
  21. }
  22. /**
  23. * Get all plan constants from xMatters matching the query. Please refer to the link below for the available query parameters.<br><br>
  24. *
  25. * {@link https://help.xmatters.com/xmapi/index.html#get-plan-constants}
  26. *
  27. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  28. * @param {Object} query A json object representing the query string parameters for this request.
  29. * @param {string} planId The unique identifier (id) or name of the plan you want to list the constants for.
  30. * @returns {Promise<PlanConstant[]>} Array of Plan Constant Objects Requested
  31. */
  32. async function getMany(env, query, planId) {
  33. return common.getMany(env, `/api/xm/1/plans/${planId}/constants`, query, 'Plan Constants');
  34. }
  35. /**
  36. * Create a plan constant in xMatters<br><br>
  37. *
  38. * {@link https://help.xmatters.com/xmapi/index.html#create-a-plan-constant}
  39. *
  40. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  41. * @param {PlanConstant} constant {@link https://help.xmatters.com/xmapi/index.html#constant-object}
  42. * @param {string} planId The unique identifier (id) or of the plan that will contain the constant.
  43. * @returns {Promise<PlanConstant>} Plan Constant Object Created
  44. */
  45. async function create(env, constant, planId) {
  46. return common.create(env, `/api/xm/1/plans/${planId}/constants`, constant, `Plan Constant`, true);
  47. }
  48. /**
  49. * Update a plan constant in xMatters<br><br>
  50. *
  51. * {@link https://help.xmatters.com/xmapi/index.html#modify-a-plan-constant}
  52. *
  53. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  54. * @param {PlanConstant} constant {@link https://help.xmatters.com/xmapi/index.html#constant-object}
  55. * @param {string} constantId The unique identifier (id) of the constant.<br><br>
  56. * Examples:<br>
  57. * - b2341d69-8b83-4660-b8c8-f2e728f675f9<br>
  58. * @param {string} planId The unique identifier (id) or name of the plan containing the constant.
  59. * @returns {Promise<PlanConstant>} Plan Constant Object Updated
  60. */
  61. async function update(env, constant, constantId, planId) {
  62. return common.update(env, `/api/xm/1/plans/${planId}/constants`, constant, constantId, 'Plan Constant');
  63. }
  64. /**
  65. * Delete a plan constant in xMatters<br><br>
  66. *
  67. * {@link https://help.xmatters.com/xmapi/index.html#delete-a-plan-constant}
  68. *
  69. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  70. * @param {string} constantId The unique identifier (id) of of the constant to delete.
  71. * @param {string} planId The unique identifier (id) or name of the plan.
  72. * @returns {Promise}
  73. * @name delete
  74. */
  75. async function _delete(env, constantId, planId) {
  76. await common.delete(env, `/api/xm/1/plans/${planId}/constants`, constantId, 'Plan Constant');
  77. }
  78. /**
  79. * Transforms an array of records exported from xMatters to the format needed to import into xMatters.
  80. * @param {module:environments.xMattersEnvironment} destination The xmtoolbox representation of the target or destination xMatters instance.
  81. * @param {PlanConstant[]} planConstants Array of plan constant objects to transform.
  82. * @param {xMattersObjects} destinationData The xMatters data for the destination.
  83. * @returns {Promise}
  84. */
  85. async function exportToImport(destination, planConstants, destinationData) {
  86. const destinationPlans = (destinationData.all ? destinationData.all.plans : null) || destinationData.plans;
  87. return common.convertDefaultInitial(await planConstants, convert);
  88. function convert(planConstant) {
  89. {
  90. //set plan
  91. //plan can be supplied as a string representing the name of the plan or an object with name key.
  92. planConstant.plan = common.AssignParentObject(planConstant.plan, destinationPlans, 'name');
  93. return planConstant;
  94. }
  95. }
  96. }
  97. /**
  98. * The key values from the object that can be synchronized.
  99. */
  100. const fields = ['name', 'value', 'description'];
  101. /**
  102. * Synchronizes an array of objects from a source with destination objects and updates the destination as necessary.
  103. * @param {module:environments.xMattersEnvironment} destination The xmtoolbox representation of the target or destination xMatters instance.
  104. * @param {PlanConstant[]} sourcePlanConstants An array of the plan constants objects to synchronize from the source data.
  105. * @param {PlanConstant[]} destinationPlanConstants An array of the plan constants objects to synchronize from the destination data.
  106. * @param {Object} options
  107. * @returns {Promise<module:sync.SyncResults>}
  108. */
  109. async function sync(destination, sourcePlanConstants, destinationPlanConstants, options) {
  110. return common.syncObject(
  111. 'Plan Constant',
  112. sourcePlanConstants,
  113. destinationPlanConstants,
  114. destination,
  115. ['name', 'plan'],
  116. fields,
  117. create,
  118. update,
  119. _delete,
  120. options,
  121. 'plan'
  122. );
  123. }
  124. exports.get = get;
  125. exports.getMany = getMany;
  126. exports.create = create;
  127. exports.update = update;
  128. exports.delete = _delete;
  129. exports.exportToImport = exportToImport;
  130. exports.fields = fields;
  131. exports.sync = sync;