Merge branch 'subscriptionimprovements'

This commit is contained in:
Henry Oswald 2016-03-31 11:58:56 +01:00
commit cb3eb88af2
6 changed files with 108 additions and 15 deletions

View file

@ -4,6 +4,7 @@ SubscriptionLocator = require("./SubscriptionLocator")
ErrorsController = require("../Errors/ErrorController")
SubscriptionDomainHandler = require("./SubscriptionDomainHandler")
_ = require("underscore")
async = require("async")
module.exports =
@ -53,19 +54,26 @@ module.exports =
subscription: subscription
renderGroupInvitePage: (req, res)->
subscription_id = req.params.subscription_id
group_subscription_id = req.params.subscription_id
user_id = req.session.user._id
licence = SubscriptionDomainHandler.findDomainLicenceBySubscriptionId(subscription_id)
licence = SubscriptionDomainHandler.findDomainLicenceBySubscriptionId(group_subscription_id)
if !licence?
return ErrorsController.notFound(req, res)
SubscriptionGroupHandler.isUserPartOfGroup user_id, licence.subscription_id, (err, partOfGroup)->
jobs =
partOfGroup: (cb)->
SubscriptionGroupHandler.isUserPartOfGroup user_id, licence.group_subscription_id, cb
subscription: (cb)->
SubscriptionLocator.getUsersSubscription user_id, cb
async.series jobs, (err, results)->
{partOfGroup, subscription} = results
if partOfGroup
return res.redirect("/user/subscription/custom_account")
else
res.render "subscriptions/group/invite",
title: "Group Invitation"
subscription_id:subscription_id
group_subscription_id:group_subscription_id
licenceName:licence.name
has_personal_subscription: subscription?
beginJoinGroup: (req, res)->
subscription_id = req.params.subscription_id

View file

@ -22,4 +22,7 @@ module.exports =
Subscription.findOne _id:subscription_id, callback
getSubscriptionByMemberIdAndId: (user_id, subscription_id, callback)->
Subscription.findOne member_ids: user_id, _id:subscription_id, {_id:1}, callback
Subscription.findOne {member_ids: user_id, _id:subscription_id}, {_id:1}, callback
getGroupSubscriptionMemberOf: (user_id, callback)->
Subscription.findOne {member_ids: user_id}, {_id:1}, callback

View file

@ -16,10 +16,24 @@ module.exports =
syncSubscription: (recurlySubscription, adminUser_id, callback) ->
self = @
logger.log adminUser_id:adminUser_id, recurlySubscription:recurlySubscription, "syncSubscription, creating new if subscription does not exist"
SubscriptionLocator.getUsersSubscription adminUser_id, (err, subscription)->
jobs =
subscription: (cb)->
SubscriptionLocator.getUsersSubscription adminUser_id, cb
groupSubscription: (cb)->
SubscriptionLocator.getGroupSubscriptionMemberOf adminUser_id, cb
async.series jobs, (err, results)->
{subscription, groupSubscription} = results
if subscription?
logger.log adminUser_id:adminUser_id, recurlySubscription:recurlySubscription, "subscription does exist"
self._updateSubscription recurlySubscription, subscription, callback
self._updateSubscription recurlySubscription, subscription, (err)->
if err?
logger.err err:err, adminUser_id:adminUser_id, "error syncing subscription"
return callback(err)
if groupSubscription? and recurlySubscription.state == "expired"
logger.log adminUser_id:adminUser_id, "subscription does exist"
UserFeaturesUpdater.updateFeatures adminUser_id, groupSubscription.planCode, callback
else
callback()
else
logger.log adminUser_id:adminUser_id, recurlySubscription:recurlySubscription, "subscription does not exist, creating a new one"
self._createNewSubscription adminUser_id, (err, subscription)->

View file

@ -2,7 +2,8 @@ extends ../../layout
block scripts
script(type='text/javascript').
window.subscription_id = '#{subscription_id}'
window.group_subscription_id = '#{group_subscription_id}'
window.has_personal_subscription = '#{has_personal_subscription}'
block content
.content.content-alt
@ -11,12 +12,26 @@ block content
.col-md-8.col-md-offset-2
-if (query.expired)
.alert.alert-warning #{translate("email_link_expired")}
.row
div  
.row
.col-md-8.col-md-offset-2(ng-cloak)
.card(ng-controller="GroupSubscriptionInviteController")
.page-header
h1.text-centered #{translate("you_are_invited_to_group", {groupName:licenceName})}
div(ng-show="!requestSent").row.text-centered
div(ng-show="view =='personalSubscription'").row.text-centered
div #{translate("cancel_personal_subscription_first")}
.row
.col-md-12  
.row
.col-md-12
a.btn.btn.btn-default(ng-click="keepPersonalSubscription()", ng-disabled="inflight") #{translate("not_now")}
span  
a.btn.btn.btn-primary(ng-click="cancelSubscription()", ng-disabled="inflight") #{translate("cancel_your_subscription")}
div(ng-show="view =='groupSubscriptionInvite'").row.text-centered
.row
.col-md-12 #{translate("group_provides_you_with_premium_account", {groupName:licenceName})}
.row
@ -26,10 +41,10 @@ block content
.text-center
a.btn.btn-default(href="/project") #{translate("not_now")}
span  
a.btn.btn.btn-primary(ng-click="joinGroup()") #{translate("verify_email_address")}
a.btn.btn.btn-primary(ng-click="joinGroup()", ng-disabled="inflight") #{translate("verify_email_address")}
span(ng-show="requestSent").row.text-centered.text-center
span(ng-show="view =='requestSent'").row.text-centered.text-center
.row
.col-md-12 #{translate("check_email_to_complete_the_upgrade")}
.row

View file

@ -3,12 +3,31 @@ define [
], (App) ->
App.controller "GroupSubscriptionInviteController", ($scope, $http) ->
$scope.requestSent = false
$scope.inflight = false
if has_personal_subscription
$scope.view = "personalSubscription"
else
$scope.view = "groupSubscriptionInvite"
$scope.keepPersonalSubscription = ->
$scope.view = "groupSubscriptionInvite"
$scope.cancelSubscription = ->
$scope.inflight = true
request = $http.post "/user/subscription/cancel", {_csrf:window.csrfToken}
request.success (data, status)->
$scope.inflight = false
$scope.view = "groupSubscriptionInvite"
request.error (data, status)->
console.log "the request failed"
$scope.joinGroup = ->
$scope.requestSent = true
request = $http.post "/user/subscription/#{subscription_id}/group/begin-join", {_csrf:window.csrfToken}
$scope.view = "requestSent"
$scope.inflight = true
request = $http.post "/user/subscription/#{group_subscription_id}/group/begin-join", {_csrf:window.csrfToken}
request.success (data, status)->
$scope.inflight = false
if status != 200 # assume request worked
$scope.requestSent = false
request.error (data, status)->

View file

@ -23,6 +23,14 @@ describe "Subscription Updater", ->
freeTrial:{}
plan_code:"student_or_something"
@groupSubscription =
admin_id: @adminUser._id
members_id: @allUserIds
save: sinon.stub().callsArgWith(0)
freeTrial:{}
plan_code:"group_subscription"
@updateStub = sinon.stub().callsArgWith(2, null)
@findAndModifyStub = sinon.stub().callsArgWith(2, null, @subscription)
@SubscriptionModel = class
@ -34,6 +42,7 @@ describe "Subscription Updater", ->
@SubscriptionLocator =
getUsersSubscription: sinon.stub()
getGroupSubscriptionMemberOf:sinon.stub()
@Settings =
freeTrialPlanCode: "collaborator"
@ -58,16 +67,41 @@ describe "Subscription Updater", ->
describe "syncSubscription", ->
it "should update the subscription if the user already is admin of one", (done)->
beforeEach ->
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
@SubscriptionLocator.getGroupSubscriptionMemberOf.callsArgWith(1, null, @groupSubscription)
@SubscriptionUpdater._updateSubscription = sinon.stub().callsArgWith(2)
it "should update the subscription if the user already is admin of one", (done)->
@SubscriptionUpdater._createNewSubscription = sinon.stub()
@SubscriptionUpdater.syncSubscription @recurlySubscription, @adminUser._id, (err)=>
@SubscriptionLocator.getUsersSubscription.calledWith(@adminUser._id).should.equal true
@SubscriptionUpdater._updateSubscription.called.should.equal true
@SubscriptionUpdater._updateSubscription.calledWith(@recurlySubscription, @subscription).should.equal true
done()
it "should sync with the group subscription if the recurly subscription is expired", (done)->
@recurlySubscription.state = "expired"
@SubscriptionUpdater.syncSubscription @recurlySubscription, @adminUser._id, (err)=>
@SubscriptionLocator.getUsersSubscription.calledWith(@adminUser._id).should.equal true
@SubscriptionUpdater._updateSubscription.called.should.equal true
@SubscriptionUpdater._updateSubscription.calledWith(@recurlySubscription, @subscription).should.equal true
@UserFeaturesUpdater.updateFeatures.calledWith(@adminUser._id, @groupSubscription.planCode).should.equal true
done()
it "should not call updateFeatures with group subscription if recurly subscription is not expired", (done)->
@SubscriptionUpdater.syncSubscription @recurlySubscription, @adminUser._id, (err)=>
@SubscriptionLocator.getUsersSubscription.calledWith(@adminUser._id).should.equal true
@SubscriptionUpdater._updateSubscription.called.should.equal true
@SubscriptionUpdater._updateSubscription.calledWith(@recurlySubscription, @subscription).should.equal true
@UserFeaturesUpdater.updateFeatures.called.should.equal false
done()
describe "_updateSubscription", ->