Source

subscriptionForms.js

  1. const common = require('./common');
  2. /**
  3. * A module related to xMatters subscription forms.<br><br>
  4. * {@link https://help.xmatters.com/xmapi/index.html#subscription-forms}
  5. *
  6. * @module subscriptionForms
  7. */
  8. /**
  9. * Get a subscription form from xMatters<br><br>
  10. *
  11. * {@link https://help.xmatters.com/xmapi/index.html#get-a-subscription-form}
  12. *
  13. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  14. * @param {string} subscriptionFormId The unique identifier (id) of the subscription form.<br><br>
  15. * Examples:<br>
  16. * - b2341d69-8b83-4660-b8c8-f2e728f675f9<br>
  17. * @param {Object} query A json object representing the query string parameters for this request.
  18. * @returns {SubscriptionForm} Subscription Form Object Requested
  19. */
  20. async function get(env, subscriptionFormId, query) {
  21. return common.get(env, '/api/xm/1/subscription-forms/', subscriptionFormId, query, 'Subscription Form');
  22. }
  23. /**
  24. * Get all subscription forms from xMatters matching the query. Please refer to the link below for the available query parameters.<br><br>
  25. *
  26. * {@link https://help.xmatters.com/xmapi/index.html#get-subscription-forms}
  27. *
  28. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  29. * @param {Object} query A json object representing the query string parameters for this request.
  30. * @returns {Promise<SubscriptionForm[]>} Array of Subscription Form Objects Requested
  31. */
  32. async function getMany(env, query) {
  33. return common.getMany(env, '/api/xm/1/subscription-forms', query, 'Subscription Forms');
  34. }
  35. /**
  36. * Get all subscription forms in a communication plan from xMatters matching the query. Please refer to the link below for the available query parameters.<br><br>
  37. *
  38. * {@link https://help.xmatters.com/xmapi/index.html#get-subscription-forms-in-a-plan}
  39. *
  40. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  41. * @param {Object} query A json object representing the query string parameters for this request.
  42. * @param {string} planId The unique identifier (id) or name of the communication plan.<br><br>
  43. * Examples:<br>
  44. * - b2341d69-8b83-4660-b8c8-f2e728f675f9<br>
  45. * - MIM
  46. * @returns {Promise<SubscriptionForm[]>} Array of Subscription Form Objects Requested
  47. */
  48. async function getManyInPlan(env, query, planId) {
  49. return common.getMany(
  50. env,
  51. `/api/xm/1/plans/${planId}/subscription-forms`,
  52. query,
  53. 'Subscription Forms In Plan'
  54. );
  55. }
  56. /**
  57. * Create a subscription form in xMatters<br><br>
  58. *
  59. * {@link https://help.xmatters.com/xmapi/index.html#create-a-subscription-form}
  60. *
  61. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  62. * @param {SubscriptionForm} subscriptionForm {@link https://help.xmatters.com/xmapi/index.html#subscription-form-objects}
  63. * @param {*} planId The UUID of the communication plan that is associated with the integration.
  64. * @returns {Promise<Site>} Site Object Created
  65. */
  66. async function create(env, subscriptionForm, planId) {
  67. return common.create(
  68. env,
  69. `/api/xm/1/plans/${planId}/subscription-forms`,
  70. subscriptionForm,
  71. 'Subscription Form',
  72. true
  73. );
  74. }
  75. /**
  76. * Update an subscription form in xMatters<br><br>
  77. *
  78. * {@link https://help.xmatters.com/xmapi/index.html#modify-a-subscription-form}
  79. *
  80. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  81. * @param {*} integration {@link https://help.xmatters.com/xmapi/index.html#subscription-form-objects}
  82. * @param {*} subscriptionFormId The unique identifier (id) of the subscription form you want to modify.<br><br>
  83. * Examples:<br>
  84. * - 345c95ee-4abe-47ea-ae7c-ae84fb4bee4f<br>
  85. * @param {*} planId The UUID of the communication plan that is associated with the integration.
  86. * @returns {Promise<Integration>} Integration Object Updated
  87. */
  88. async function update(env, subscriptionForm, subscriptionFormId, planId) {
  89. return common.update(
  90. env,
  91. `/api/xm/1/plans/${planId}/subscription-forms/`,
  92. subscriptionForm,
  93. subscriptionFormId,
  94. 'Subscription Form'
  95. );
  96. }
  97. /**
  98. * Transforms an array of records exported from xMatters to the format needed to import into xMatters.
  99. * @param {module:environments.xMattersEnvironment} destination The xmtoolbox representation of the target or destination xMatters instance.
  100. * @param {SubscriptionForm[]} subscriptionForms Array of subscription form objects to transform.
  101. * @returns {Promise}
  102. */
  103. async function exportToImport(destination, subscriptionForms, destinationData) {
  104. const destinationPlans = (destinationData.all ? destinationData.all.plans : null) || destinationData.plans;
  105. const destinationForms = (destinationData.all ? destinationData.all.forms : null) || destinationData.forms;
  106. return common.convertDefaultInitial(subscriptionForms, convert);
  107. function convert(subscriptionForm) {
  108. {
  109. //if plan, set plan
  110. if (subscriptionForm.plan) {
  111. //plan can be supplied as a string representing the name of the plan or an object with name key.
  112. const planName = subscriptionForm.plan.name;
  113. subscriptionForm.plan = common.AssignParentObject(subscriptionForm.plan, destinationPlans, 'name');
  114. //if the planName was returned rather than the id.
  115. if (planName === subscriptionForm.plan) {
  116. destination.log.warn(
  117. `DATA INTEGRITY ISSUE: Subscription Plan [${subscriptionForm.name}] has plan [${planName}] but a plan with that name was not found in the provided destination data.`
  118. );
  119. }
  120. }
  121. //if form, set form
  122. if (subscriptionForm.form) {
  123. //form can be supplied as a string representing the name of the form or an object with name key.
  124. const formName = subscriptionForm.form.name;
  125. const id = common.AssignParentObject(subscriptionForm.form, destinationForms, 'name', ['plan']);
  126. subscriptionForm.form = { id };
  127. //if the formName was returned rather than the id.
  128. if (formName === subscriptionForm.form) {
  129. destination.log.warn(
  130. `DATA INTEGRITY ISSUE: Subscription Form [${subscriptionForm.name}] has form [${formName}] but a form with that name was not found in the provided destination data.`
  131. );
  132. }
  133. }
  134. if (subscriptionForm.roles && subscriptionForm.roles.data) {
  135. subscriptionForm.roles = subscriptionForm.roles.data.map(({ name }) => ({ name }));
  136. }
  137. if (subscriptionForm.propertyDefinitions && subscriptionForm.propertyDefinitions.data) {
  138. subscriptionForm.propertyDefinitions = subscriptionForm.propertyDefinitions.data.map(({ name }) => ({
  139. name,
  140. }));
  141. }
  142. if (subscriptionForm.notificationDelay) {
  143. subscriptionForm.notificationDelay = Number(subscriptionForm.notificationDelay);
  144. }
  145. if (subscriptionForm.targetDeviceNames) {
  146. subscriptionForm.targetDeviceNames = subscriptionForm.targetDeviceNames.data.map(
  147. ({ name, visible, selected }) => ({ name, visible, selected })
  148. );
  149. }
  150. {
  151. return subscriptionForm;
  152. }
  153. }
  154. }
  155. }
  156. /**
  157. * The key values from the object that can be synchronized.
  158. */
  159. const fields = [
  160. //'id',
  161. 'name',
  162. 'description',
  163. 'devicesSectionCollapsed',
  164. 'devicesSectionVisible',
  165. 'notificationDelay',
  166. 'propertyDefinitions',
  167. 'oneWay',
  168. 'roles',
  169. 'scope',
  170. 'form',
  171. 'subscribeOthers',
  172. 'targetDeviceNames',
  173. ];
  174. /**
  175. * Synchronizes an array of objects from a source with destination objects and updates the destination as necessary.
  176. * @param {module:environments.xMattersEnvironment} destination The xmtoolbox representation of the target or destination xMatters instance.
  177. * @param {SubscriptionForm[]} sourceSubscriptionForms An array of the subscription form objects to synchronize from the source data.
  178. * @param {SubscriptionForm[]} destinationSubscriptionForm An array of the subscription form objects to synchronize from the destination data.
  179. * @param {Object} options
  180. */
  181. async function sync(destination, sourceSubscriptionForms, destinationSubscriptionForms, options) {
  182. return common.syncObject(
  183. 'Subscription Form',
  184. sourceSubscriptionForms,
  185. destinationSubscriptionForms,
  186. destination,
  187. 'name',
  188. fields,
  189. create,
  190. update,
  191. undefined,
  192. options,
  193. 'plan'
  194. );
  195. }
  196. module.exports = {
  197. get,
  198. getMany,
  199. getManyInPlan,
  200. create,
  201. update,
  202. exportToImport,
  203. fields,
  204. sync,
  205. };