Add proper messaging if an organization has gone over the repo limit. This change also moves plan information into the server
This commit is contained in:
parent
1f0b142535
commit
9fa77aaa48
6 changed files with 226 additions and 99 deletions
124
static/js/app.js
124
static/js/app.js
|
@ -10,6 +10,7 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics',
|
|||
}
|
||||
|
||||
var userService = {}
|
||||
var currentSubscription = null;
|
||||
|
||||
userService.load = function() {
|
||||
var userFetch = Restangular.one('user/');
|
||||
|
@ -30,6 +31,20 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics',
|
|||
});
|
||||
};
|
||||
|
||||
userService.resetCurrentSubscription = function() {
|
||||
currentSubscription = null;
|
||||
};
|
||||
|
||||
userService.getCurrentSubscription = function(callback, failure) {
|
||||
if (currentSubscription) { callback(currentSubscription); }
|
||||
|
||||
var getSubscription = Restangular.one('user/plan');
|
||||
getSubscription.get().then(function(sub) {
|
||||
currentSubscription = sub;
|
||||
callback(sub);
|
||||
}, failure);
|
||||
};
|
||||
|
||||
userService.currentUser = function() {
|
||||
return userResponse;
|
||||
}
|
||||
|
@ -55,62 +70,48 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics',
|
|||
}]);
|
||||
|
||||
$provide.factory('PlanService', ['Restangular', 'KeyService', function(Restangular, KeyService) {
|
||||
var plans = [
|
||||
{
|
||||
title: 'Open Source',
|
||||
price: 0,
|
||||
privateRepos: 0,
|
||||
stripeId: 'free',
|
||||
audience: 'Share with the world',
|
||||
},
|
||||
{
|
||||
title: 'Micro',
|
||||
price: 700,
|
||||
privateRepos: 5,
|
||||
stripeId: 'micro',
|
||||
audience: 'For smaller teams',
|
||||
},
|
||||
{
|
||||
title: 'Basic',
|
||||
price: 1200,
|
||||
privateRepos: 10,
|
||||
stripeId: 'small',
|
||||
audience: 'For your basic team',
|
||||
},
|
||||
{
|
||||
title: 'Medium',
|
||||
price: 2200,
|
||||
privateRepos: 20,
|
||||
stripeId: 'medium',
|
||||
audience: 'For medium-sized teams',
|
||||
},
|
||||
];
|
||||
|
||||
var plans = null;
|
||||
var planDict = {};
|
||||
var i;
|
||||
for(i = 0; i < plans.length; i++) {
|
||||
planDict[plans[i].stripeId] = plans[i];
|
||||
}
|
||||
|
||||
var planService = {}
|
||||
|
||||
planService.planList = function() {
|
||||
return plans;
|
||||
};
|
||||
|
||||
planService.getPlan = function(planId) {
|
||||
return planDict[planId];
|
||||
};
|
||||
|
||||
planService.getMinimumPlan = function(privateCount) {
|
||||
for (var i = 0; i < plans.length; i++) {
|
||||
var plan = plans[i];
|
||||
if (plan.privateRepos >= privateCount) {
|
||||
return plan;
|
||||
}
|
||||
planService.verifyLoaded = function(callback) {
|
||||
if (plans) {
|
||||
callback(plans);
|
||||
return;
|
||||
}
|
||||
|
||||
return null;
|
||||
var getPlans = Restangular.one('plans');
|
||||
getPlans.get().then(function(data) {
|
||||
for(var i = 0; i < data.plans.length; i++) {
|
||||
planDict[data.plans[i].stripeId] = data.plans[i];
|
||||
}
|
||||
plans = data.plans;
|
||||
callback(plans);
|
||||
}, function() { callback([]); });
|
||||
};
|
||||
|
||||
planService.getPlanList = function(callback) {
|
||||
planService.verifyLoaded(callback);
|
||||
};
|
||||
|
||||
planService.getPlan = function(planId, callback) {
|
||||
planService.verifyLoaded(function() {
|
||||
callback(planDict[planId]);
|
||||
});
|
||||
};
|
||||
|
||||
planService.getMinimumPlan = function(privateCount, callback) {
|
||||
planService.verifyLoaded(function() {
|
||||
for (var i = 0; i < plans.length; i++) {
|
||||
var plan = plans[i];
|
||||
if (plan.privateRepos >= privateCount) {
|
||||
callback(plan);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
callback(null);
|
||||
});
|
||||
};
|
||||
|
||||
planService.showSubscribeDialog = function($scope, planId, started, success, failed) {
|
||||
|
@ -132,16 +133,17 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics',
|
|||
});
|
||||
};
|
||||
|
||||
var planDetails = planService.getPlan(planId)
|
||||
StripeCheckout.open({
|
||||
key: KeyService.stripePublishableKey,
|
||||
address: false, // TODO change to true
|
||||
amount: planDetails.price,
|
||||
currency: 'usd',
|
||||
name: 'Quay ' + planDetails.title + ' Subscription',
|
||||
description: 'Up to ' + planDetails.privateRepos + ' private repositories',
|
||||
panelLabel: 'Subscribe',
|
||||
token: submitToken
|
||||
planService.getPlan(planId, function(planDetails) {
|
||||
StripeCheckout.open({
|
||||
key: KeyService.stripePublishableKey,
|
||||
address: false,
|
||||
amount: planDetails.price,
|
||||
currency: 'usd',
|
||||
name: 'Quay ' + planDetails.title + ' Subscription',
|
||||
description: 'Up to ' + planDetails.privateRepos + ' private repositories',
|
||||
panelLabel: 'Subscribe',
|
||||
token: submitToken
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -156,7 +156,11 @@ function SigninCtrl($scope, $location, $timeout, Restangular, KeyService, UserSe
|
|||
};
|
||||
|
||||
function PlansCtrl($scope, UserService, PlanService) {
|
||||
$scope.plans = PlanService.planList();
|
||||
// Load the list of plans.
|
||||
PlanService.getPlanList(function(plans) {
|
||||
$scope.plans = plans;
|
||||
$scope.status = 'ready';
|
||||
});
|
||||
|
||||
$scope.$watch( function () { return UserService.currentUser(); }, function (currentUser) {
|
||||
$scope.user = currentUser;
|
||||
|
@ -169,8 +173,6 @@ function PlansCtrl($scope, UserService, PlanService) {
|
|||
$('#signinModal').modal({});
|
||||
}
|
||||
};
|
||||
|
||||
$scope.status = 'ready';
|
||||
}
|
||||
|
||||
function GuideCtrl($scope) {
|
||||
|
@ -743,7 +745,10 @@ function RepoAdminCtrl($scope, Restangular, $routeParams, $rootScope) {
|
|||
}
|
||||
|
||||
function UserAdminCtrl($scope, $timeout, Restangular, PlanService, UserService, KeyService, $routeParams) {
|
||||
$scope.plans = PlanService.planList();
|
||||
// Load the list of plans.
|
||||
PlanService.getPlanList(function(plans) {
|
||||
$scope.plans = plans;
|
||||
});
|
||||
|
||||
$scope.$watch(function () { return UserService.currentUser(); }, function (currentUser) {
|
||||
$scope.askForPassword = currentUser.askForPassword;
|
||||
|
@ -751,28 +756,29 @@ function UserAdminCtrl($scope, $timeout, Restangular, PlanService, UserService,
|
|||
|
||||
var subscribedToPlan = function(sub) {
|
||||
$scope.subscription = sub;
|
||||
$scope.subscribedPlan = PlanService.getPlan(sub.plan);
|
||||
$scope.planUsagePercent = sub.usedPrivateRepos * 100 / $scope.subscribedPlan.privateRepos;
|
||||
PlanService.getPlan(sub.plan, function(subscribedPlan) {
|
||||
$scope.subscribedPlan = subscribedPlan;
|
||||
$scope.planUsagePercent = sub.usedPrivateRepos * 100 / $scope.subscribedPlan.privateRepos;
|
||||
|
||||
if (sub.usedPrivateRepos > $scope.subscribedPlan.privateRepos) {
|
||||
$scope.errorMessage = 'You are using more private repositories than your plan allows, please upgrate your subscription to avoid disruptions in your service.';
|
||||
} else {
|
||||
$scope.errorMessage = null;
|
||||
}
|
||||
if (sub.usedPrivateRepos > $scope.subscribedPlan.privateRepos) {
|
||||
$scope.errorMessage = 'You are using more private repositories than your plan allows, please upgrate your subscription to avoid disruptions in your service.';
|
||||
} else {
|
||||
$scope.errorMessage = null;
|
||||
}
|
||||
|
||||
$scope.planLoading = false;
|
||||
$scope.planChanging = false;
|
||||
$scope.planLoading = false;
|
||||
$scope.planChanging = false;
|
||||
|
||||
mixpanel.people.set({
|
||||
'plan': sub.plan
|
||||
});
|
||||
mixpanel.people.set({
|
||||
'plan': sub.plan
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
$scope.planLoading = true;
|
||||
var getSubscription = Restangular.one('user/plan');
|
||||
getSubscription.get().then(subscribedToPlan, function() {
|
||||
UserService.getCurrentSubscription(subscribedToPlan, function() {
|
||||
// User has no subscription
|
||||
$scope.planLoading = false;
|
||||
$scope.planChanging = false;
|
||||
});
|
||||
|
||||
$scope.planChanging = false;
|
||||
|
@ -782,6 +788,7 @@ function UserAdminCtrl($scope, $timeout, Restangular, PlanService, UserService,
|
|||
$scope.planChanging = true;
|
||||
}, function(plan) {
|
||||
// Subscribed.
|
||||
UserService.resetCurrentSubscription();
|
||||
subscribedToPlan(plan);
|
||||
}, function() {
|
||||
// Failure.
|
||||
|
@ -798,6 +805,7 @@ function UserAdminCtrl($scope, $timeout, Restangular, PlanService, UserService,
|
|||
plan: planId,
|
||||
};
|
||||
|
||||
UserService.resetCurrentSubscription();
|
||||
var changeSubscriptionRequest = Restangular.one('user/plan');
|
||||
changeSubscriptionRequest.customPUT(subscriptionDetails).then(subscribedToPlan, function() {
|
||||
// Failure
|
||||
|
@ -813,9 +821,11 @@ function UserAdminCtrl($scope, $timeout, Restangular, PlanService, UserService,
|
|||
// Show the subscribe dialog if a plan was requested.
|
||||
var requested = $routeParams['plan']
|
||||
if (requested !== undefined && requested !== 'free') {
|
||||
if (PlanService.getPlan(requested) !== undefined) {
|
||||
$scope.subscribe(requested);
|
||||
}
|
||||
PlanService.getPlan(requested, function(found) {
|
||||
if (found) {
|
||||
$scope.subscribe(requested);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$scope.updatingUser = false;
|
||||
|
@ -1039,11 +1049,21 @@ function NewRepoCtrl($scope, $location, $http, UserService, Restangular, PlanSer
|
|||
var subscribedToPlan = function(sub) {
|
||||
$scope.planChanging = false;
|
||||
$scope.subscription = sub;
|
||||
$scope.subscribedPlan = PlanService.getPlan(sub.plan);
|
||||
$scope.planRequired = null;
|
||||
if ($scope.subscription.usedPrivateRepos >= $scope.subscribedPlan.privateRepos) {
|
||||
$scope.planRequired = PlanService.getMinimumPlan($scope.subscription.usedPrivateRepos);
|
||||
}
|
||||
|
||||
PlanService.getPlan(sub.plan, function(subscribedPlan) {
|
||||
$scope.subscribedPlan = subscribedPlan;
|
||||
$scope.planRequired = null;
|
||||
|
||||
// Check to see if the current plan allows for an additional private repository to
|
||||
// be created.
|
||||
var privateAllowed = $scope.subscription.usedPrivateRepos < $scope.subscribedPlan.privateRepos;
|
||||
if (!privateAllowed) {
|
||||
// If not, find the minimum repository that does.
|
||||
PlanService.getMinimumPlan($scope.subscription.usedPrivateRepos + 1, function(minimum) {
|
||||
$scope.planRequired = minimum;
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
$scope.editDescription = function() {
|
||||
|
@ -1109,6 +1129,7 @@ function NewRepoCtrl($scope, $location, $http, UserService, Restangular, PlanSer
|
|||
$scope.planChanging = true;
|
||||
}, function(plan) {
|
||||
// Subscribed.
|
||||
UserService.resetCurrentSubscription();
|
||||
subscribedToPlan(plan);
|
||||
}, function() {
|
||||
// Failure.
|
||||
|
@ -1116,15 +1137,37 @@ function NewRepoCtrl($scope, $location, $http, UserService, Restangular, PlanSer
|
|||
$scope.planChanging = false;
|
||||
});
|
||||
};
|
||||
|
||||
// Watch the namespace on the repo. If it changes, we update the plan and the public/private
|
||||
// accordingly.
|
||||
$scope.$watch('repo.namespace', function(namespace) {
|
||||
// Note: Can initially be undefined.
|
||||
if (!namespace) { return; }
|
||||
|
||||
var isUserNamespace = (namespace == $scope.user.username);
|
||||
|
||||
$scope.plans = PlanService.planList();
|
||||
$scope.planRequired = null;
|
||||
$scope.isUserNamespace = isUserNamespace;
|
||||
|
||||
// Load the user's subscription information in case they want to create a private
|
||||
// repository.
|
||||
var getSubscription = Restangular.one('user/plan');
|
||||
getSubscription.get().then(subscribedToPlan, function() {
|
||||
// User has no subscription
|
||||
$scope.planRequired = PlanService.getMinimumPlan(1);
|
||||
if (isUserNamespace) {
|
||||
// Load the user's subscription information in case they want to create a private
|
||||
// repository.
|
||||
UserService.getCurrentSubscription(subscribedToPlan, function() {
|
||||
PlanService.getMinimumPlan(1, function(minimum) { $scope.planRequired = minimum; });
|
||||
});
|
||||
} else {
|
||||
$scope.planRequired = null;
|
||||
|
||||
var checkPrivateAllowed = Restangular.one('organization/' + namespace + '/private');
|
||||
checkPrivateAllowed.get().then(function(resp) {
|
||||
$scope.planRequired = resp.privateAllowed ? null : {};
|
||||
}, function() {
|
||||
$scope.planRequired = {};
|
||||
});
|
||||
|
||||
// Auto-set to private repo.
|
||||
$scope.repo.is_public = '0';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue