Source

sites.js

  1. const common = require('./common');
  2. /**
  3. * A module related to xMatters sites.<br><br>
  4. * {@link https://help.xmatters.com/xmapi/index.html#sites}
  5. *
  6. * @module sites
  7. */
  8. /**
  9. * Get a site from xMatters<br><br>
  10. *
  11. * {@link https://help.xmatters.com/xmapi/index.html#get-a-site}
  12. *
  13. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  14. * @param {string} siteId The unique identifier (id) or name (name) of the site.<br><br>
  15. * Examples:<br>
  16. * - London Building B2
  17. * - b2341d69-8b83-4660-b8c8-f2e728f675f9<br>
  18. * @param {Object} query A json object representing the query string parameters for this request.
  19. * @returns {Promise<Site>} Site Object Requested
  20. */
  21. async function get(env, siteId, query, throwError) {
  22. return common.get(env, '/api/xm/1/sites/', siteId, query, 'Site', throwError);
  23. }
  24. /**
  25. * Get all sites from xMatters matching the query. Please refer to the link below for the available query parameters.<br><br>
  26. *
  27. * {@link https://help.xmatters.com/xmapi/index.html#get-sites}
  28. *
  29. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  30. * @param {Object} query A json object representing the query string parameters for this request.
  31. * @returns {Promise<Site[]>} Array of Site Objects Requested
  32. */
  33. async function getMany(env, query) {
  34. return common.getMany(env, '/api/xm/1/sites', query, 'Sites');
  35. }
  36. /**
  37. * Create a site in xMatters<br><br>
  38. *
  39. * {@link https://help.xmatters.com/xmapi/index.html#create-a-site}
  40. *
  41. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  42. * @param {Site} site {@link https://help.xmatters.com/xmapi/index.html#site-object}
  43. * @returns {Promise<Site>} Site Object Created
  44. */
  45. async function create(env, site) {
  46. return common.create(env, '/api/xm/1/sites', site, 'Site', true);
  47. }
  48. /**
  49. * Update a site in xMatters<br><br>
  50. *
  51. * {@link https://help.xmatters.com/xmapi/index.html#modify-or-update-a-site}
  52. *
  53. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  54. * @param {Site} site {@link https://help.xmatters.com/xmapi/index.html#site-object}
  55. * @param {string} siteId The unique identifier (id) for the site.<br><br>
  56. * Examples:<br>
  57. * - b2341d69-8b83-4660-b8c8-f2e728f675f9<br>
  58. * @returns {Promise<Site>} Site Object Updated
  59. */
  60. async function update(env, site, siteId) {
  61. return common.update(env, '/api/xm/1/sites/', site, siteId, 'Site');
  62. }
  63. /**
  64. * Delete a site in xMatters<br><br>
  65. *
  66. * {@link https://help.xmatters.com/xmapi/index.html#delete-a-site}
  67. * @param {module:environments.xMattersEnvironment} env The xmtoolbox representation of an xMatters instance.
  68. * @param {string} siteId The unique identifier (id) or name (name) of the site.<br><br>
  69. * Examples:<br>
  70. * - London Building B2
  71. * - b2341d69-8b83-4660-b8c8-f2e728f675f9<br>
  72. * @returns {Promise}
  73. * @name delete
  74. */
  75. async function _delete(env, siteId) {
  76. return common.delete(env, '/api/xm/1/sites/', siteId, 'Site');
  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 {Site[]} sites Array of site objects to transform.
  82. * @returns {Promise}
  83. */
  84. async function exportToImport(destination, sites) {
  85. return common.convertDefaultInitial(sites, convert);
  86. function convert(site) {
  87. {
  88. {
  89. if (site.latitude) site.latitude = Number(site.latitude);
  90. if (site.longitude) site.longitude = Number(site.longitude);
  91. return site;
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * The key values from the object that can be synchronized.
  98. */
  99. const fields = [
  100. //'id',
  101. 'name',
  102. 'country',
  103. 'language',
  104. 'timezone',
  105. //'externallyOwned',
  106. 'status',
  107. 'address1',
  108. 'address2',
  109. 'postalCode',
  110. 'postalCode',
  111. 'city',
  112. 'state',
  113. 'country',
  114. 'latitude',
  115. 'longitude',
  116. ];
  117. /**
  118. * Synchronizes an array of objects from a source with destination objects and updates the destination as necessary.
  119. * @param {module:environments.xMattersEnvironment} destination The xmtoolbox representation of the target or destination xMatters instance.
  120. * @param {Site[]} sourceSites An array of the site objects to synchronize from the source data.
  121. * @param {Site[]} destinationSites An array of the site objects to synchronize from the destination data.
  122. * @param {Object} options
  123. */
  124. async function sync(destination, sourceSites, destinationSites, options) {
  125. return common.syncObject(
  126. 'Site',
  127. sourceSites,
  128. destinationSites,
  129. destination,
  130. 'name',
  131. fields,
  132. create,
  133. update,
  134. _delete,
  135. options
  136. );
  137. }
  138. module.exports = {
  139. get,
  140. getMany,
  141. create,
  142. update,
  143. delete: _delete,
  144. exportToImport,
  145. fields,
  146. sync,
  147. };