V1SubscriptionManager.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. let V1SubscriptionManager
  2. const UserGetter = require('../User/UserGetter')
  3. const request = require('requestretry')
  4. const settings = require('@overleaf/settings')
  5. const { V1ConnectionError, NotFoundError } = require('../Errors/Errors')
  6. const { promisifyAll } = require('@overleaf/promise-utils')
  7. module.exports = V1SubscriptionManager = {
  8. cancelV1Subscription(userId, callback) {
  9. V1SubscriptionManager._v1Request(
  10. userId,
  11. {
  12. method: 'DELETE',
  13. url(v1Id) {
  14. return `/api/v1/overleaf/users/${v1Id}/subscription`
  15. },
  16. },
  17. callback
  18. )
  19. },
  20. v1IdForUser(userId, callback) {
  21. UserGetter.getUser(userId, { 'overleaf.id': 1 }, function (err, user) {
  22. if (err) {
  23. return callback(err)
  24. }
  25. const v1Id = user?.overleaf?.id
  26. callback(null, v1Id)
  27. })
  28. },
  29. // v1 accounts created before migration to v2 had github and mendeley for free
  30. // but these are now paid-for features for new accounts (v1id > cutoff)
  31. getGrandfatheredFeaturesForV1User(v1Id) {
  32. const cutoff = settings.v1GrandfatheredFeaturesUidCutoff
  33. if (!cutoff) {
  34. return {}
  35. }
  36. if (!v1Id) {
  37. return {}
  38. }
  39. if (v1Id < cutoff) {
  40. return settings.v1GrandfatheredFeatures || {}
  41. } else {
  42. return {}
  43. }
  44. },
  45. _v1Request(userId, options, callback) {
  46. if (!settings.apis.v1.url) {
  47. return callback(null, null)
  48. }
  49. V1SubscriptionManager.v1IdForUser(userId, function (err, v1Id) {
  50. if (err) {
  51. return callback(err)
  52. }
  53. if (!v1Id) {
  54. return callback(null, null, null)
  55. }
  56. const url = options.url(v1Id)
  57. const requestOptions = {
  58. baseUrl: settings.apis.v1.url,
  59. url,
  60. method: options.method,
  61. auth: {
  62. user: settings.apis.v1.user,
  63. pass: settings.apis.v1.pass,
  64. sendImmediately: true,
  65. },
  66. json: true,
  67. timeout: settings.apis.v1.timeout,
  68. }
  69. if (options.method === 'GET') {
  70. requestOptions.maxAttempts = 3
  71. requestOptions.retryDelay = 500
  72. } else {
  73. requestOptions.maxAttempts = 0
  74. }
  75. request(requestOptions, function (error, response, body) {
  76. if (error) {
  77. return callback(
  78. new V1ConnectionError({
  79. message: 'no v1 connection',
  80. info: { url },
  81. }).withCause(error)
  82. )
  83. }
  84. if (response && response.statusCode >= 500) {
  85. return callback(
  86. new V1ConnectionError({
  87. message: 'error from v1',
  88. info: {
  89. status: response.statusCode,
  90. body,
  91. },
  92. })
  93. )
  94. }
  95. if (response.statusCode >= 200 && response.statusCode < 300) {
  96. return callback(null, body, v1Id)
  97. } else {
  98. if (response.statusCode === 404) {
  99. return callback(new NotFoundError(`v1 user not found: ${userId}`))
  100. } else {
  101. return callback(
  102. new Error(
  103. `non-success code from v1: ${response.statusCode} ${
  104. options.method
  105. } ${options.url(v1Id)}`
  106. )
  107. )
  108. }
  109. }
  110. })
  111. })
  112. },
  113. }
  114. module.exports.promises = promisifyAll(module.exports, {
  115. without: ['getGrandfatheredFeaturesForV1User'],
  116. })