InstitutionsAPI.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS103: Rewrite code to no longer use __guard__
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let InstitutionsAPI
  16. const logger = require('logger-sharelatex')
  17. const metrics = require('metrics-sharelatex')
  18. const settings = require('settings-sharelatex')
  19. const request = require('request')
  20. const NotificationsBuilder = require('../Notifications/NotificationsBuilder')
  21. module.exports = InstitutionsAPI = {
  22. getInstitutionAffiliations(institutionId, callback) {
  23. if (callback == null) {
  24. callback = function(error, body) {}
  25. }
  26. return makeAffiliationRequest(
  27. {
  28. method: 'GET',
  29. path: `/api/v2/institutions/${institutionId.toString()}/affiliations`,
  30. defaultErrorMessage: "Couldn't get institution affiliations"
  31. },
  32. (error, body) => callback(error, body || [])
  33. )
  34. },
  35. getInstitutionLicences(institutionId, startDate, endDate, lag, callback) {
  36. if (callback == null) {
  37. callback = function(error, body) {}
  38. }
  39. return makeAffiliationRequest(
  40. {
  41. method: 'GET',
  42. path: `/api/v2/institutions/${institutionId.toString()}/institution_licences`,
  43. body: { start_date: startDate, end_date: endDate, lag },
  44. defaultErrorMessage: "Couldn't get institution licences"
  45. },
  46. callback
  47. )
  48. },
  49. getUserAffiliations(userId, callback) {
  50. if (callback == null) {
  51. callback = function(error, body) {}
  52. }
  53. return makeAffiliationRequest(
  54. {
  55. method: 'GET',
  56. path: `/api/v2/users/${userId.toString()}/affiliations`,
  57. defaultErrorMessage: "Couldn't get user affiliations"
  58. },
  59. (error, body) => callback(error, body || [])
  60. )
  61. },
  62. addAffiliation(userId, email, affiliationOptions, callback) {
  63. if (callback == null) {
  64. // affiliationOptions is optional
  65. callback = affiliationOptions
  66. affiliationOptions = {}
  67. }
  68. const { university, department, role, confirmedAt } = affiliationOptions
  69. return makeAffiliationRequest(
  70. {
  71. method: 'POST',
  72. path: `/api/v2/users/${userId.toString()}/affiliations`,
  73. body: { email, university, department, role, confirmedAt },
  74. defaultErrorMessage: "Couldn't create affiliation"
  75. },
  76. function(error, body) {
  77. if (error) {
  78. return callback(error, body)
  79. }
  80. // have notifications delete any ip matcher notifications for this university
  81. return NotificationsBuilder.ipMatcherAffiliation(userId).read(
  82. university != null ? university.id : undefined,
  83. function(err) {
  84. if (err) {
  85. // log and ignore error
  86. logger.err(
  87. { err },
  88. 'Something went wrong marking ip notifications read'
  89. )
  90. }
  91. return callback(null, body)
  92. }
  93. )
  94. }
  95. )
  96. },
  97. removeAffiliation(userId, email, callback) {
  98. if (callback == null) {
  99. callback = function(error) {}
  100. }
  101. return makeAffiliationRequest(
  102. {
  103. method: 'POST',
  104. path: `/api/v2/users/${userId.toString()}/affiliations/remove`,
  105. body: { email },
  106. extraSuccessStatusCodes: [404], // `Not Found` responses are considered successful
  107. defaultErrorMessage: "Couldn't remove affiliation"
  108. },
  109. callback
  110. )
  111. },
  112. endorseAffiliation(userId, email, role, department, callback) {
  113. if (callback == null) {
  114. callback = function(error) {}
  115. }
  116. return makeAffiliationRequest(
  117. {
  118. method: 'POST',
  119. path: `/api/v2/users/${userId.toString()}/affiliations/endorse`,
  120. body: { email, role, department },
  121. defaultErrorMessage: "Couldn't endorse affiliation"
  122. },
  123. callback
  124. )
  125. },
  126. deleteAffiliations(userId, callback) {
  127. if (callback == null) {
  128. callback = function(error) {}
  129. }
  130. return makeAffiliationRequest(
  131. {
  132. method: 'DELETE',
  133. path: `/api/v2/users/${userId.toString()}/affiliations`,
  134. defaultErrorMessage: "Couldn't delete affiliations"
  135. },
  136. callback
  137. )
  138. }
  139. }
  140. var makeAffiliationRequest = function(requestOptions, callback) {
  141. if (callback == null) {
  142. callback = function(error) {}
  143. }
  144. if (
  145. !__guard__(
  146. __guard__(settings != null ? settings.apis : undefined, x1 => x1.v1),
  147. x => x.url
  148. )
  149. ) {
  150. return callback(null)
  151. } // service is not configured
  152. if (!requestOptions.extraSuccessStatusCodes) {
  153. requestOptions.extraSuccessStatusCodes = []
  154. }
  155. return request(
  156. {
  157. method: requestOptions.method,
  158. url: `${settings.apis.v1.url}${requestOptions.path}`,
  159. body: requestOptions.body,
  160. auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
  161. json: true,
  162. timeout: 20 * 1000
  163. },
  164. function(error, response, body) {
  165. if (error != null) {
  166. return callback(error)
  167. }
  168. let isSuccess = response.statusCode >= 200 && response.statusCode < 300
  169. if (!isSuccess) {
  170. isSuccess = Array.from(requestOptions.extraSuccessStatusCodes).includes(
  171. response.statusCode
  172. )
  173. }
  174. if (!isSuccess) {
  175. let errorMessage
  176. if (body != null ? body.errors : undefined) {
  177. errorMessage = `${response.statusCode}: ${body.errors}`
  178. } else {
  179. errorMessage = `${requestOptions.defaultErrorMessage}: ${
  180. response.statusCode
  181. }`
  182. }
  183. logger.warn(
  184. { path: requestOptions.path, body: requestOptions.body },
  185. errorMessage
  186. )
  187. return callback(new Error(errorMessage))
  188. }
  189. return callback(null, body)
  190. }
  191. )
  192. }
  193. ;[
  194. 'getInstitutionAffiliations',
  195. 'getUserAffiliations',
  196. 'addAffiliation',
  197. 'removeAffiliation'
  198. ].map(method =>
  199. metrics.timeAsyncMethod(
  200. InstitutionsAPI,
  201. method,
  202. 'mongo.InstitutionsAPI',
  203. logger
  204. )
  205. )
  206. function __guard__(value, transform) {
  207. return typeof value !== 'undefined' && value !== null
  208. ? transform(value)
  209. : undefined
  210. }