InstitutionsAPI.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. const logger = require('logger-sharelatex')
  2. const metrics = require('metrics-sharelatex')
  3. const settings = require('settings-sharelatex')
  4. const request = require('request')
  5. const { promisifyAll } = require('../../util/promises')
  6. const NotificationsBuilder = require('../Notifications/NotificationsBuilder')
  7. const { V1ConnectionError } = require('../Errors/Errors')
  8. const InstitutionsAPI = {
  9. getInstitutionAffiliations(institutionId, callback) {
  10. makeAffiliationRequest(
  11. {
  12. method: 'GET',
  13. path: `/api/v2/institutions/${institutionId.toString()}/affiliations`,
  14. defaultErrorMessage: "Couldn't get institution affiliations"
  15. },
  16. (error, body) => callback(error, body || [])
  17. )
  18. },
  19. getInstitutionLicences(institutionId, startDate, endDate, lag, callback) {
  20. makeAffiliationRequest(
  21. {
  22. method: 'GET',
  23. path: `/api/v2/institutions/${institutionId.toString()}/institution_licences`,
  24. body: { start_date: startDate, end_date: endDate, lag },
  25. defaultErrorMessage: "Couldn't get institution licences"
  26. },
  27. callback
  28. )
  29. },
  30. getInstitutionNewLicences(institutionId, startDate, endDate, lag, callback) {
  31. makeAffiliationRequest(
  32. {
  33. method: 'GET',
  34. path: `/api/v2/institutions/${institutionId.toString()}/new_institution_licences`,
  35. body: { start_date: startDate, end_date: endDate, lag },
  36. defaultErrorMessage: "Couldn't get institution new licences"
  37. },
  38. callback
  39. )
  40. },
  41. getUserAffiliations(userId, callback) {
  42. makeAffiliationRequest(
  43. {
  44. method: 'GET',
  45. path: `/api/v2/users/${userId.toString()}/affiliations`,
  46. defaultErrorMessage: "Couldn't get user affiliations"
  47. },
  48. (error, body) => callback(error, body || [])
  49. )
  50. },
  51. addAffiliation(userId, email, affiliationOptions, callback) {
  52. if (!callback) {
  53. // affiliationOptions is optional
  54. callback = affiliationOptions
  55. affiliationOptions = {}
  56. }
  57. const {
  58. university,
  59. department,
  60. role,
  61. confirmedAt,
  62. entitlement
  63. } = affiliationOptions
  64. makeAffiliationRequest(
  65. {
  66. method: 'POST',
  67. path: `/api/v2/users/${userId.toString()}/affiliations`,
  68. body: { email, university, department, role, confirmedAt, entitlement },
  69. defaultErrorMessage: "Couldn't create affiliation"
  70. },
  71. function(error, body) {
  72. if (error) {
  73. return callback(error, body)
  74. }
  75. if (!university) {
  76. return callback(null, body)
  77. }
  78. // have notifications delete any ip matcher notifications for this university
  79. NotificationsBuilder.ipMatcherAffiliation(userId).read(
  80. university.id,
  81. function(err) {
  82. if (err) {
  83. // log and ignore error
  84. logger.err(
  85. { err },
  86. 'Something went wrong marking ip notifications read'
  87. )
  88. }
  89. callback(null, body)
  90. }
  91. )
  92. }
  93. )
  94. },
  95. removeAffiliation(userId, email, callback) {
  96. makeAffiliationRequest(
  97. {
  98. method: 'POST',
  99. path: `/api/v2/users/${userId.toString()}/affiliations/remove`,
  100. body: { email },
  101. extraSuccessStatusCodes: [404], // `Not Found` responses are considered successful
  102. defaultErrorMessage: "Couldn't remove affiliation"
  103. },
  104. callback
  105. )
  106. },
  107. endorseAffiliation(userId, email, role, department, callback) {
  108. makeAffiliationRequest(
  109. {
  110. method: 'POST',
  111. path: `/api/v2/users/${userId.toString()}/affiliations/endorse`,
  112. body: { email, role, department },
  113. defaultErrorMessage: "Couldn't endorse affiliation"
  114. },
  115. callback
  116. )
  117. },
  118. deleteAffiliations(userId, callback) {
  119. makeAffiliationRequest(
  120. {
  121. method: 'DELETE',
  122. path: `/api/v2/users/${userId.toString()}/affiliations`,
  123. defaultErrorMessage: "Couldn't delete affiliations"
  124. },
  125. callback
  126. )
  127. },
  128. addEntitlement(userId, email, callback) {
  129. makeAffiliationRequest(
  130. {
  131. method: 'POST',
  132. path: `/api/v2/users/${userId}/affiliations/add_entitlement`,
  133. body: { email },
  134. defaultErrorMessage: "Couldn't add entitlement"
  135. },
  136. callback
  137. )
  138. },
  139. removeEntitlement(userId, email, callback) {
  140. makeAffiliationRequest(
  141. {
  142. method: 'POST',
  143. path: `/api/v2/users/${userId}/affiliations/remove_entitlement`,
  144. body: { email },
  145. defaultErrorMessage: "Couldn't remove entitlement"
  146. },
  147. callback
  148. )
  149. }
  150. }
  151. var makeAffiliationRequest = function(requestOptions, callback) {
  152. if (!settings.apis.v1.url) {
  153. return callback(null)
  154. } // service is not configured
  155. if (!requestOptions.extraSuccessStatusCodes) {
  156. requestOptions.extraSuccessStatusCodes = []
  157. }
  158. request(
  159. {
  160. method: requestOptions.method,
  161. url: `${settings.apis.v1.url}${requestOptions.path}`,
  162. body: requestOptions.body,
  163. auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
  164. json: true,
  165. timeout: 20 * 1000
  166. },
  167. function(error, response, body) {
  168. if (error) {
  169. return callback(
  170. new V1ConnectionError('error getting affiliations from v1').withCause(
  171. error
  172. )
  173. )
  174. }
  175. if (response && response.statusCode >= 500) {
  176. return callback(
  177. new V1ConnectionError({
  178. message: 'error getting affiliations from v1',
  179. info: {
  180. status: response.statusCode,
  181. body: body
  182. }
  183. })
  184. )
  185. }
  186. let isSuccess = response.statusCode >= 200 && response.statusCode < 300
  187. if (!isSuccess) {
  188. isSuccess = requestOptions.extraSuccessStatusCodes.includes(
  189. response.statusCode
  190. )
  191. }
  192. if (!isSuccess) {
  193. let errorMessage
  194. if (body && body.errors) {
  195. errorMessage = `${response.statusCode}: ${body.errors}`
  196. } else {
  197. errorMessage = `${requestOptions.defaultErrorMessage}: ${
  198. response.statusCode
  199. }`
  200. }
  201. logger.warn(
  202. { path: requestOptions.path, body: requestOptions.body },
  203. errorMessage
  204. )
  205. return callback(new Error(errorMessage))
  206. }
  207. callback(null, body)
  208. }
  209. )
  210. }
  211. ;[
  212. 'getInstitutionAffiliations',
  213. 'getUserAffiliations',
  214. 'addAffiliation',
  215. 'removeAffiliation'
  216. ].map(method =>
  217. metrics.timeAsyncMethod(
  218. InstitutionsAPI,
  219. method,
  220. 'mongo.InstitutionsAPI',
  221. logger
  222. )
  223. )
  224. InstitutionsAPI.promises = promisifyAll(InstitutionsAPI)
  225. module.exports = InstitutionsAPI