Source

dynamicTeams.js

  1. const common = require('./common');
  2. /**
  3. * A module related to xMatters dynamic teams.<br><br>
  4. * {@link https://help.xmatters.com/xmapi/index.html#dynamic-teams}
  5. *
  6. * @module dynamicTeams
  7. */
  8. /**
  9. * Get a dynamic team from xMatters<br><br>
  10. *
  11. * {@link https://help.xmatters.com/xmapi/index.html#get-a-dynamic-team}
  12. *
  13. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  14. * @param {string} dynamicTeamId The unique id of the dynamic team.
  15. * @param {Object} query A json object representing the query string parameters for this request.
  16. * @returns {Promise<DynamicTeam>} Dynamic Team Object Requested
  17. */
  18. async function get(env, dynamicTeamId, query) {
  19. return common.get(env, '/api/xm/1/dynamic-teams/', dynamicTeamId, query, 'Dynamic Team');
  20. }
  21. /**
  22. * Get all dynamic teams from xMatters matching the query. Please refer to the link below for the available query parameters.<br><br>
  23. *
  24. * {@link https://help.xmatters.com/xmapi/index.html#get-dynamic-teams}
  25. *
  26. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  27. * @param {Object} query A json object representing the query string parameters for this request.
  28. * @returns {Promise<DynamicTeam[]>} Array of Dynamic Team Objects Requested
  29. */
  30. async function getMany(env, query) {
  31. return common.getMany(env, '/api/xm/1/dynamic-teams', query, 'Dynamic Teams');
  32. }
  33. /**
  34. * Get all dynamic teams members from xMatters matching the query. Please refer to the link below for the available query parameters.<br><br>
  35. *
  36. * {@link https://help.xmatters.com/xmapi/index.html#get-dynamic-team-members}
  37. *
  38. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  39. * @param {Object} query A json object representing the query string parameters for this request.
  40. * @param {string} dynamicTeamId The unique id or targetNameof the dynamic team.
  41. * @returns {Promise<module:people.Person[]>} Array of Person Objects from the Dynamic Team Requested
  42. */
  43. async function getMembers(env, query, dynamicTeamId) {
  44. return common.getMany(
  45. env,
  46. `/api/xm/1/dynamic-teams/${dynamicTeamId}/members`,
  47. query,
  48. 'Dynamic Team Members'
  49. );
  50. }
  51. /**
  52. * Create a dynamic team in xMatters<br><br>
  53. *
  54. * {@link https://help.xmatters.com/xmapi/index.html#create-a-dynamic-team}
  55. *
  56. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  57. * @param {DynamicTeam} dynamicTeam
  58. * @returns {Promise<DynamicTeam>} Dynamic Team Object Created
  59. */
  60. async function create(env, dynamicTeam) {
  61. return common.create(env, '/api/xm/1/dynamic-teams', dynamicTeam, 'Dynamic Team', true);
  62. }
  63. /**
  64. * Update a dynamic team in xMatters<br><br>
  65. *
  66. * {@link https://help.xmatters.com/xmapi/index.html#update-a-dynamic-team}
  67. *
  68. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  69. * @param {DynamicTeam} dynamicTeam
  70. * @param {string} dynamicTeamId The unique identifier (id) of the dynamic team you want to modify.
  71. * @returns {Promise<DynamicTeam>} Dynamic Team Object Updated
  72. */
  73. async function update(env, dynamicTeam, dynamicTeamId) {
  74. return common.update(env, '/api/xm/1/dynamic-teams/', dynamicTeam, dynamicTeamId, 'Dynamic Team');
  75. }
  76. /**
  77. * Delete a dynamic team in xMatters<br><br>
  78. *
  79. * {@link https://help.xmatters.com/xmapi/index.html#delete-a-dynamic-team}
  80. *
  81. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  82. * @param {string} dynamicTeamId The unique identifier (id) or name (targetName) of the dynamic team.<br><br>
  83. * Examples:<br>
  84. * - MIMTeam1<br>
  85. * - a6341d69-5683-4621-b8c8-f2e728f6752q
  86. * @returns {Promise}
  87. * @name delete
  88. */
  89. async function _delete(env, dynamicTeamId) {
  90. await common.delete(env, '/api/xm/1/dynamic-teams/', dynamicTeamId, 'Dynamic Team');
  91. }
  92. /**
  93. * Transforms an array of records exported from xMatters to the format needed to import into xMatters.
  94. * @param {module:environments.xMattersEnvironment} destination The xmtoolbox representation of the target or destination xMatters instance.
  95. * @param {DynamicTeam[]} dynamicTeams Array of Dynamic Teams to transform.
  96. * @param {xMattersObjects} destinationData The xMatters data for the destination.
  97. * @param {Object} options
  98. */
  99. async function exportToImport(destination, dynamicTeams, destinationData, options) {
  100. const destinationPeople =
  101. (destinationData.all ? destinationData.all.people : null) || destinationData.people;
  102. const { defaultSupervisorId } = options;
  103. return common.convertDefaultInitial(dynamicTeams, convert);
  104. function convert(dynamicTeam) {
  105. {
  106. delete dynamicTeam.links;
  107. if (dynamicTeam.criteria && dynamicTeam.criteria.criterion && dynamicTeam.criteria.criterion.data) {
  108. dynamicTeam.criteria.criterion = dynamicTeam.criteria.criterion.data.map(c => c);
  109. }
  110. if (dynamicTeam.observers && dynamicTeam.observers.data) {
  111. dynamicTeam.observers = dynamicTeam.observers.data;
  112. dynamicTeam.observers = dynamicTeam.observers.map(observer => {
  113. return { name: observer.name };
  114. });
  115. }
  116. //BUG IN XMAPI #152283 remove this code if empty array is allowed to unset observers
  117. if (dynamicTeam.observers && dynamicTeam.observers.length === 0) {
  118. //delete record.observers;
  119. }
  120. if (dynamicTeam.supervisors && dynamicTeam.supervisors.data) {
  121. dynamicTeam.supervisors = dynamicTeam.supervisors.data;
  122. dynamicTeam.supervisors = dynamicTeam.supervisors.map(supervisor => {
  123. const destinationSupervisor =
  124. destinationPeople &&
  125. destinationPeople.find(({ targetName }) => targetName === supervisor.targetName);
  126. if (!destinationSupervisor) {
  127. destination.log.warn(
  128. `DATA INTEGRITY ISSUE: Supervisor mapping failed. Dynamic Team [${dynamicTeam.targetName}] has a supervisor with targetName [${supervisor.targetName}] but this supervisor was not found in the provided destination data.`
  129. );
  130. } else {
  131. return destinationSupervisor.id;
  132. }
  133. });
  134. }
  135. //pull any null values from the supervisor array. will be null if matches are not found.
  136. if (dynamicTeam.supervisors) {
  137. dynamicTeam.supervisors = dynamicTeam.supervisors.filter(function (el) {
  138. return el != null;
  139. });
  140. //groups require at least one supervisor. If supervisors were removed then add a default.
  141. if (dynamicTeam.supervisors.length === 0 && defaultSupervisorId) {
  142. dynamicTeam.supervisors = [defaultSupervisorId];
  143. }
  144. }
  145. return dynamicTeam;
  146. }
  147. }
  148. }
  149. /**
  150. * The key values from the object that can be synchronized.
  151. */
  152. const fields = [
  153. //'id',
  154. 'targetName',
  155. 'recipientType',
  156. //'externallyOwned',
  157. 'supervisors',
  158. 'observers',
  159. 'criteria',
  160. ];
  161. /**
  162. * Synchronizes an array of objects from a source with destination objects and updates the destination as necessary.
  163. * @param {module:environments.xMattersEnvironment} destination The xmtoolbox representation of the target or destination xMatters instance.
  164. * @param {DynamicTeam[]} sourceDynamicTeams An array of the dynamic team objects to synchronize from the source data.
  165. * @param {DynamicTeam[]} destination An array of the dynamic team objects to synchronize from the destination data.
  166. * @param {Object} options
  167. * @returns {Promise<module:sync.SyncResults>}
  168. */
  169. async function sync(destination, sourceDynamicTeams, destinationDynamicTeams, options) {
  170. return common.syncObject(
  171. 'Dynamic Teams',
  172. sourceDynamicTeams,
  173. destinationDynamicTeams,
  174. destination,
  175. 'targetName',
  176. fields,
  177. create,
  178. update,
  179. _delete,
  180. options
  181. );
  182. }
  183. exports.get = get;
  184. exports.getMany = getMany;
  185. exports.getMembers = getMembers;
  186. exports.create = create;
  187. exports.update = update;
  188. exports.delete = _delete;
  189. exports.exportToImport = exportToImport;
  190. exports.fields = fields;
  191. exports.sync = sync;