|
|
@@ -12,39 +12,50 @@ module.exports = V1SubscriptionManager =
|
|
|
# - 'v1_free'
|
|
|
getPlanCodeFromV1: (userId, callback=(err, planCode)->) ->
|
|
|
logger.log {userId}, "[V1SubscriptionManager] fetching v1 plan for user"
|
|
|
+ V1SubscriptionManager._v1Request userId, {
|
|
|
+ method: 'GET',
|
|
|
+ url: (v1Id) -> "/api/v1/sharelatex/users/#{v1Id}/plan_code"
|
|
|
+ }, (error, body) ->
|
|
|
+ return callback(error) if error?
|
|
|
+ planName = body?.plan_name
|
|
|
+ logger.log {userId, planName, body}, "[V1SubscriptionManager] fetched v1 plan for user"
|
|
|
+ if planName in ['pro', 'pro_plus', 'student', 'free']
|
|
|
+ planName = "v1_#{planName}"
|
|
|
+ else
|
|
|
+ # Throw away 'anonymous', etc as being equivalent to null
|
|
|
+ planName = null
|
|
|
+ return callback(null, planName)
|
|
|
+
|
|
|
+ notifyV1OfFeaturesChange: (userId, callback = (error) ->) ->
|
|
|
+ V1SubscriptionManager._v1Request userId, {
|
|
|
+ method: 'POST',
|
|
|
+ url: (v1Id) -> "/api/v1/sharelatex/users/#{v1Id}/sync"
|
|
|
+ }, callback
|
|
|
+
|
|
|
+ _v1Request: (userId, options, callback=(err, body)->) ->
|
|
|
+ if !settings?.apis?.v1
|
|
|
+ return callback null, null
|
|
|
UserGetter.getUser userId, {'overleaf.id': 1}, (err, user) ->
|
|
|
return callback(err) if err?
|
|
|
v1Id = user?.overleaf?.id
|
|
|
if !v1Id?
|
|
|
logger.log {userId}, "[V1SubscriptionManager] no v1 id found for user"
|
|
|
return callback(null, null)
|
|
|
- V1SubscriptionManager._v1PlanRequest v1Id, (err, body) ->
|
|
|
- return callback(err) if err?
|
|
|
- planName = body?.plan_name
|
|
|
- logger.log {userId, planName, body}, "[V1SubscriptionManager] fetched v1 plan for user"
|
|
|
- if planName in ['pro', 'pro_plus', 'student', 'free']
|
|
|
- planName = "v1_#{planName}"
|
|
|
+ options.url = options.url
|
|
|
+ request {
|
|
|
+ baseUrl: settings.apis.v1.url
|
|
|
+ url: options.url(v1Id)
|
|
|
+ method: options.method
|
|
|
+ auth:
|
|
|
+ user: settings.apis.v1.user
|
|
|
+ pass: settings.apis.v1.pass
|
|
|
+ sendImmediately: true
|
|
|
+ json: true,
|
|
|
+ timeout: 5 * 1000
|
|
|
+ }, (error, response, body) ->
|
|
|
+ return callback(error) if error?
|
|
|
+ if 200 <= response.statusCode < 300
|
|
|
+ return callback null, body
|
|
|
else
|
|
|
- # Throw away 'anonymous', etc as being equivalent to null
|
|
|
- planName = null
|
|
|
- return callback(null, planName)
|
|
|
+ return callback new Error("non-success code from v1: #{response.statusCode}")
|
|
|
|
|
|
- _v1PlanRequest: (v1Id, callback=(err, body)->) ->
|
|
|
- if !settings?.apis?.v1
|
|
|
- return callback null, null
|
|
|
- request {
|
|
|
- method: 'GET',
|
|
|
- url: settings.apis.v1.url +
|
|
|
- "/api/v1/sharelatex/users/#{v1Id}/plan_code"
|
|
|
- auth:
|
|
|
- user: settings.apis.v1.user
|
|
|
- pass: settings.apis.v1.pass
|
|
|
- sendImmediately: true
|
|
|
- json: true,
|
|
|
- timeout: 5 * 1000
|
|
|
- }, (error, response, body) ->
|
|
|
- return callback(error) if error?
|
|
|
- if 200 <= response.statusCode < 300
|
|
|
- return callback null, body
|
|
|
- else
|
|
|
- return callback new Error("non-success code from v1: #{response.statusCode}")
|