InstitutionsAPI.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const logger = require('logger-sharelatex')
  15. const metrics = require('metrics-sharelatex')
  16. const settings = require('settings-sharelatex')
  17. const request = require('request')
  18. const { promisifyAll } = require('../../util/promises')
  19. const NotificationsBuilder = require('../Notifications/NotificationsBuilder')
  20. const { V1ConnectionError } = require('../Errors/Errors')
  21. const 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. getInstitutionNewLicences(institutionId, startDate, endDate, lag, callback) {
  50. if (callback == null) {
  51. callback = function(error, body) {}
  52. }
  53. return makeAffiliationRequest(
  54. {
  55. method: 'GET',
  56. path: `/api/v2/institutions/${institutionId.toString()}/new_institution_licences`,
  57. body: { start_date: startDate, end_date: endDate, lag },
  58. defaultErrorMessage: "Couldn't get institution new licences"
  59. },
  60. callback
  61. )
  62. },
  63. getUserAffiliations(userId, callback) {
  64. if (callback == null) {
  65. callback = function(error, body) {}
  66. }
  67. return makeAffiliationRequest(
  68. {
  69. method: 'GET',
  70. path: `/api/v2/users/${userId.toString()}/affiliations`,
  71. defaultErrorMessage: "Couldn't get user affiliations"
  72. },
  73. (error, body) => callback(error, body || [])
  74. )
  75. },
  76. addAffiliation(userId, email, affiliationOptions, callback) {
  77. if (callback == null) {
  78. // affiliationOptions is optional
  79. callback = affiliationOptions
  80. affiliationOptions = {}
  81. }
  82. const { university, department, role, confirmedAt } = affiliationOptions
  83. return makeAffiliationRequest(
  84. {
  85. method: 'POST',
  86. path: `/api/v2/users/${userId.toString()}/affiliations`,
  87. body: { email, university, department, role, confirmedAt },
  88. defaultErrorMessage: "Couldn't create affiliation"
  89. },
  90. function(error, body) {
  91. if (error) {
  92. return callback(error, body)
  93. }
  94. // have notifications delete any ip matcher notifications for this university
  95. return NotificationsBuilder.ipMatcherAffiliation(userId).read(
  96. university != null ? university.id : undefined,
  97. function(err) {
  98. if (err) {
  99. // log and ignore error
  100. logger.err(
  101. { err },
  102. 'Something went wrong marking ip notifications read'
  103. )
  104. }
  105. return callback(null, body)
  106. }
  107. )
  108. }
  109. )
  110. },
  111. removeAffiliation(userId, email, callback) {
  112. if (callback == null) {
  113. callback = function(error) {}
  114. }
  115. return makeAffiliationRequest(
  116. {
  117. method: 'POST',
  118. path: `/api/v2/users/${userId.toString()}/affiliations/remove`,
  119. body: { email },
  120. extraSuccessStatusCodes: [404], // `Not Found` responses are considered successful
  121. defaultErrorMessage: "Couldn't remove affiliation"
  122. },
  123. callback
  124. )
  125. },
  126. endorseAffiliation(userId, email, role, department, callback) {
  127. if (callback == null) {
  128. callback = function(error) {}
  129. }
  130. return makeAffiliationRequest(
  131. {
  132. method: 'POST',
  133. path: `/api/v2/users/${userId.toString()}/affiliations/endorse`,
  134. body: { email, role, department },
  135. defaultErrorMessage: "Couldn't endorse affiliation"
  136. },
  137. callback
  138. )
  139. },
  140. deleteAffiliations(userId, callback) {
  141. if (callback == null) {
  142. callback = function(error) {}
  143. }
  144. return makeAffiliationRequest(
  145. {
  146. method: 'DELETE',
  147. path: `/api/v2/users/${userId.toString()}/affiliations`,
  148. defaultErrorMessage: "Couldn't delete affiliations"
  149. },
  150. callback
  151. )
  152. },
  153. addEntitlement(userId, email, callback) {
  154. return makeAffiliationRequest(
  155. {
  156. method: 'POST',
  157. path: `/api/v2/users/${userId}/affiliations/add_entitlement`,
  158. body: { email },
  159. defaultErrorMessage: "Couldn't add entitlement"
  160. },
  161. callback
  162. )
  163. },
  164. removeEntitlement(userId, email, callback) {
  165. return makeAffiliationRequest(
  166. {
  167. method: 'POST',
  168. path: `/api/v2/users/${userId}/affiliations/remove_entitlement`,
  169. body: { email },
  170. defaultErrorMessage: "Couldn't remove entitlement"
  171. },
  172. callback
  173. )
  174. }
  175. }
  176. var makeAffiliationRequest = function(requestOptions, callback) {
  177. if (callback == null) {
  178. callback = function(error) {}
  179. }
  180. if (!settings.apis.v1.url) {
  181. return callback(null)
  182. } // service is not configured
  183. if (!requestOptions.extraSuccessStatusCodes) {
  184. requestOptions.extraSuccessStatusCodes = []
  185. }
  186. return request(
  187. {
  188. method: requestOptions.method,
  189. url: `${settings.apis.v1.url}${requestOptions.path}`,
  190. body: requestOptions.body,
  191. auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
  192. json: true,
  193. timeout: 20 * 1000
  194. },
  195. function(error, response, body) {
  196. if (error != null) {
  197. return callback(
  198. new V1ConnectionError('error getting affiliations from v1').withCause(
  199. error
  200. )
  201. )
  202. }
  203. if (response && response.statusCode >= 500) {
  204. return callback(
  205. new V1ConnectionError({
  206. message: 'error getting affiliations from v1',
  207. info: {
  208. status: response.statusCode,
  209. body: body
  210. }
  211. })
  212. )
  213. }
  214. let isSuccess = response.statusCode >= 200 && response.statusCode < 300
  215. if (!isSuccess) {
  216. isSuccess = Array.from(requestOptions.extraSuccessStatusCodes).includes(
  217. response.statusCode
  218. )
  219. }
  220. if (!isSuccess) {
  221. let errorMessage
  222. if (body != null ? body.errors : undefined) {
  223. errorMessage = `${response.statusCode}: ${body.errors}`
  224. } else {
  225. errorMessage = `${requestOptions.defaultErrorMessage}: ${
  226. response.statusCode
  227. }`
  228. }
  229. logger.warn(
  230. { path: requestOptions.path, body: requestOptions.body },
  231. errorMessage
  232. )
  233. return callback(new Error(errorMessage))
  234. }
  235. return callback(null, body)
  236. }
  237. )
  238. }
  239. ;[
  240. 'getInstitutionAffiliations',
  241. 'getUserAffiliations',
  242. 'addAffiliation',
  243. 'removeAffiliation'
  244. ].map(method =>
  245. metrics.timeAsyncMethod(
  246. InstitutionsAPI,
  247. method,
  248. 'mongo.InstitutionsAPI',
  249. logger
  250. )
  251. )
  252. InstitutionsAPI.promises = promisifyAll(InstitutionsAPI)
  253. module.exports = InstitutionsAPI