1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/assets/javascripts/controllers/plans.js.erb

384 lines
14 KiB
Plaintext
Raw Normal View History

/* eslint-disable
no-return-assign,
no-undef,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2018-11-21 11:08:53 +01:00
'use strict';
2016-03-23 18:39:41 +01:00
Application.Controllers.controller('PlansIndexController', ['$scope', '$rootScope', '$state', '$uibModal', 'Auth', 'dialogs', 'growl', 'plansPromise', 'groupsPromise', 'Subscription', 'Member', 'subscriptionExplicationsPromise', '_t', 'Wallet', 'helpers',
function ($scope, $rootScope, $state, $uibModal, Auth, dialogs, growl, plansPromise, groupsPromise, Subscription, Member, subscriptionExplicationsPromise, _t, Wallet, helpers) {
/* PUBLIC SCOPE */
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// list of groups
2018-11-21 11:08:53 +01:00
$scope.groups = groupsPromise.filter(function (g) { return (g.slug !== 'admins') & !g.disabled; });
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// default : do not show the group changing form
// group ID of the current/selected user
$scope.group = {
change: false,
id: null
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
// list of plans, classified by group
2018-11-21 11:08:53 +01:00
$scope.plansClassifiedByGroup = [];
for (var group of Array.from($scope.groups)) {
2018-11-21 11:08:53 +01:00
const groupObj = { id: group.id, name: group.name, plans: [] };
2018-11-20 12:26:06 +01:00
for (let plan of Array.from(plansPromise)) {
2018-11-21 11:08:53 +01:00
if (plan.group_id === group.id) { groupObj.plans.push(plan); }
}
2018-11-21 11:08:53 +01:00
$scope.plansClassifiedByGroup.push(groupObj);
}
2018-11-19 16:17:49 +01:00
// user to deal with
$scope.ctrl = {
member: null,
2016-03-23 18:39:41 +01:00
member_id: null
2018-11-21 11:08:53 +01:00
};
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// already subscribed plan of the current user
$scope.paid =
2018-11-21 11:08:53 +01:00
{ plan: null };
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// plan to subscribe (shopping cart)
2018-11-21 11:08:53 +01:00
$scope.selectedPlan = null;
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// Discount coupon to apply to the basket, if any
2016-08-11 18:17:28 +02:00
$scope.coupon =
2018-11-21 11:08:53 +01:00
{ applied: null };
2016-08-11 18:17:28 +02:00
2018-11-19 16:17:49 +01:00
// Storage for the total price (plan price + coupon, if any)
2016-08-11 18:17:28 +02:00
$scope.cart =
2018-11-21 11:08:53 +01:00
{ total: null };
2018-11-19 16:17:49 +01:00
// text that appears in the bottom-right box of the page (subscriptions rules details)
2018-11-21 11:08:53 +01:00
$scope.subscriptionExplicationsAlert = subscriptionExplicationsPromise.setting.value;
2018-11-19 16:17:49 +01:00
/**
* Callback to deal with the subscription of the user selected in the dropdown list instead of the current user's
* subscription. (admins only)
*/
$scope.updateMember = function () {
2018-11-21 11:08:53 +01:00
$scope.selectedPlan = null;
$scope.paid.plan = null;
$scope.group.change = false;
return Member.get({ id: $scope.ctrl.member.id }, function (member) {
2018-11-21 11:08:53 +01:00
$scope.ctrl.member = member;
return $scope.group.id = $scope.ctrl.member.group_id;
});
};
2018-11-19 16:17:49 +01:00
/**
* Add the provided plan to the shopping basket
* @param plan {Object} The plan to subscribe to
*/
$scope.selectPlan = function (plan) {
if ($scope.isAuthenticated()) {
if ($scope.selectedPlan !== plan) {
2018-11-21 11:08:53 +01:00
$scope.selectedPlan = plan;
updateCartPrice();
} else {
$scope.selectedPlan = null;
}
} else {
$scope.login();
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Callback to trigger the payment process of the subscription
*/
2018-11-20 12:26:06 +01:00
$scope.openSubscribePlanModal = function () {
Wallet.getWalletByUser({ user_id: $scope.ctrl.member.id }, function (wallet) {
2018-11-21 11:08:53 +01:00
const amountToPay = helpers.getAmountToPay($scope.cart.total, wallet.amount);
if (($scope.currentUser.role !== 'admin') && (amountToPay > 0)) {
2018-11-21 11:08:53 +01:00
return payByStripe();
} else {
if (($scope.currentUser.role === 'admin') || (amountToPay === 0)) {
2018-11-21 11:08:53 +01:00
return payOnSite();
}
}
2018-11-21 11:08:53 +01:00
});
};
2018-11-19 16:17:49 +01:00
/**
* Return the group object, identified by the ID set in $scope.group.id
*/
$scope.getUserGroup = function () {
for (group of Array.from($scope.groups)) {
if (group.id === $scope.group.id) {
2018-11-21 11:08:53 +01:00
return group;
}
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Change the group of the current/selected user to the one set in $scope.group.id
*/
2018-11-20 12:26:06 +01:00
$scope.selectGroup = function () {
Member.update({ id: $scope.ctrl.member.id }, { user: { group_id: $scope.group.id } }, function (user) {
2018-11-21 11:08:53 +01:00
$scope.ctrl.member = user;
$scope.group.change = false;
$scope.selectedPlan = null;
2018-11-21 10:59:07 +01:00
if ($scope.currentUser.role !== 'admin') {
2018-11-21 11:08:53 +01:00
$rootScope.currentUser = user;
Auth._currentUser.group_id = user.group_id;
growl.success(_t('your_group_was_successfully_changed'));
2018-11-21 10:59:07 +01:00
} else {
2018-11-21 11:08:53 +01:00
growl.success(_t('the_user_s_group_was_successfully_changed'));
}
2018-11-21 10:59:07 +01:00
}
, function (err) {
if ($scope.currentUser.role !== 'admin') {
2018-11-21 11:08:53 +01:00
growl.error(_t('an_error_prevented_your_group_from_being_changed'));
2018-11-21 10:59:07 +01:00
} else {
2018-11-21 11:08:53 +01:00
growl.error(_t('an_error_prevented_to_change_the_user_s_group'));
2018-11-21 10:59:07 +01:00
}
2018-11-21 11:08:53 +01:00
console.error(err);
});
};
2018-11-19 16:17:49 +01:00
/**
* Return an enumerable meaninful string for the gender of the provider user
* @param user {Object} Database user record
* @return {string} 'male' or 'female'
*/
$scope.getGender = function (user) {
if (user && user.statistic_profile) {
if (user.statistic_profile.gender === 'true') { return 'male'; } else { return 'female'; }
2018-11-21 11:08:53 +01:00
} else { return 'other'; }
};
2018-11-19 16:17:49 +01:00
/**
* Test if the provided date is in the future
* @param dateTime {Date}
* @return {boolean}
*/
$scope.isInFuture = function (dateTime) {
2018-11-21 11:08:53 +01:00
return (moment().diff(moment(dateTime)) < 0);
};
2018-11-19 16:17:49 +01:00
/**
* To use as callback in Array.prototype.filter to get only enabled plans
*/
2018-11-21 11:08:53 +01:00
$scope.filterDisabledPlans = function (plan) { return !plan.disabled; };
/* PRIVATE SCOPE */
2018-11-19 16:17:49 +01:00
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
const initialize = function () {
if ($scope.currentUser) {
if ($scope.currentUser.role !== 'admin') {
2018-11-21 11:08:53 +01:00
$scope.ctrl.member = $scope.currentUser;
$scope.paid.plan = $scope.currentUser.subscribed_plan;
$scope.group.id = $scope.currentUser.group_id;
}
}
2018-11-21 11:08:53 +01:00
$scope.$on('devise:new-session', function (event, user) { $scope.ctrl.member = user; });
// watch when a coupon is applied to re-compute the total price
return $scope.$watch('coupon.applied', function (newValue, oldValue) {
if ((newValue !== null) || (oldValue !== null)) {
2018-11-21 11:08:53 +01:00
return updateCartPrice();
}
2018-11-21 11:08:53 +01:00
});
};
2018-11-19 16:17:49 +01:00
/**
* Compute the total amount for the current reservation according to the previously set parameters
* and assign the result in $scope.reserve.amountTotal
*/
var updateCartPrice = function () {
// first we check that a user was selected
if (Object.keys($scope.ctrl.member).length > 0) {
2018-11-21 11:08:53 +01:00
$scope.cart.total = $scope.selectedPlan.amount;
// apply the coupon if any
if ($scope.coupon.applied) {
2018-11-21 11:08:53 +01:00
let discount;
if ($scope.coupon.applied.type === 'percent_off') {
2018-11-21 11:08:53 +01:00
discount = ($scope.cart.total * $scope.coupon.applied.percent_off) / 100;
} else if ($scope.coupon.applied.type === 'amount_off') {
2018-11-21 11:08:53 +01:00
discount = $scope.coupon.applied.amount_off;
}
2018-11-21 11:08:53 +01:00
return $scope.cart.total -= discount;
}
} else {
2018-11-21 11:08:53 +01:00
return $scope.reserve.amountTotal = null;
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Open a modal window which trigger the stripe payment process
*/
2018-11-20 12:26:06 +01:00
var payByStripe = function () {
$uibModal.open({
templateUrl: '<%= asset_path "stripe/payment_modal.html" %>',
size: 'md',
resolve: {
2018-11-21 11:08:53 +01:00
selectedPlan () { return $scope.selectedPlan; },
member () { return $scope.ctrl.member; },
price () { return $scope.cart.total; },
wallet () {
2018-11-21 11:08:53 +01:00
return Wallet.getWalletByUser({ user_id: $scope.ctrl.member.id }).$promise;
},
2018-11-21 11:08:53 +01:00
coupon () { return $scope.coupon.applied; }
},
2016-11-23 14:27:11 +01:00
controller: ['$scope', '$uibModalInstance', '$state', 'selectedPlan', 'member', 'price', 'Subscription', 'CustomAsset', 'wallet', 'helpers', '$filter', 'coupon',
function ($scope, $uibModalInstance, $state, selectedPlan, member, price, Subscription, CustomAsset, wallet, helpers, $filter, coupon) {
2018-11-20 12:26:06 +01:00
// User's wallet amount
2018-11-21 11:08:53 +01:00
$scope.walletAmount = wallet.amount;
// Final price to pay by the user
2018-11-21 11:08:53 +01:00
$scope.amount = helpers.getAmountToPay(price, wallet.amount);
// The plan that the user is about to subscribe
2018-11-21 11:08:53 +01:00
$scope.selectedPlan = selectedPlan;
// Used in wallet info template to interpolate some translations
2018-11-21 11:08:53 +01:00
$scope.numberFilter = $filter('number');
// retrieve the CGV
2018-11-21 11:08:53 +01:00
CustomAsset.get({ name: 'cgv-file' }, function (cgv) { $scope.cgv = cgv.custom_asset; });
2018-11-19 16:17:49 +01:00
/**
* Callback for click on the 'proceed' button.
* Handle the stripe's card tokenization process response and save the subscription to the API with the
* card token just created.
*/
2018-11-20 12:26:06 +01:00
$scope.payment = function (status, response) {
if (response.error) {
2018-11-21 11:08:53 +01:00
growl.error(response.error.message);
} else {
2018-11-21 11:08:53 +01:00
$scope.attempting = true;
2018-11-20 12:26:06 +01:00
Subscription.save({
2018-11-21 10:59:07 +01:00
coupon_code: ((coupon ? coupon.code : undefined)),
subscription: {
plan_id: selectedPlan.id,
user_id: member.id,
card_token: response.id
2018-11-20 12:26:06 +01:00
}
2018-11-21 10:59:07 +01:00
}
, function (data) { // success
2018-11-21 11:08:53 +01:00
$uibModalInstance.close(data);
2018-11-21 10:59:07 +01:00
}
, function (data, status) { // failed
2018-11-21 11:08:53 +01:00
$scope.alerts = [];
$scope.alerts.push({ msg: _t('an_error_occured_during_the_payment_process_please_try_again_later'), type: 'danger' });
$scope.attempting = false;
2018-11-21 10:59:07 +01:00
}
2018-11-21 11:08:53 +01:00
);
}
2018-11-21 11:08:53 +01:00
};
}
2018-11-20 12:26:06 +01:00
]
}).result['finally'](null).then(function (subscription) {
2018-11-21 11:08:53 +01:00
$scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedPlan);
Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan);
$scope.paid.plan = angular.copy($scope.selectedPlan);
$scope.selectedPlan = null;
$scope.coupon.applied = null;
});
};
2018-11-19 16:17:49 +01:00
/**
* Open a modal window which trigger the local payment process
*/
2018-11-20 12:26:06 +01:00
var payOnSite = function () {
$uibModal.open({
templateUrl: '<%= asset_path "plans/payment_modal.html" %>',
size: 'sm',
resolve: {
2018-11-21 11:08:53 +01:00
selectedPlan () { return $scope.selectedPlan; },
member () { return $scope.ctrl.member; },
price () { return $scope.cart.total; },
wallet () {
2018-11-21 11:08:53 +01:00
return Wallet.getWalletByUser({ user_id: $scope.ctrl.member.id }).$promise;
},
2018-11-21 11:08:53 +01:00
coupon () { return $scope.coupon.applied; }
},
2016-11-23 14:27:11 +01:00
controller: ['$scope', '$uibModalInstance', '$state', 'selectedPlan', 'member', 'price', 'Subscription', 'wallet', 'helpers', '$filter', 'coupon',
function ($scope, $uibModalInstance, $state, selectedPlan, member, price, Subscription, wallet, helpers, $filter, coupon) {
2018-11-20 12:26:06 +01:00
// user wallet amount
2018-11-21 11:08:53 +01:00
$scope.walletAmount = wallet.amount;
// subcription price, coupon subtracted if any
2018-11-21 11:08:53 +01:00
$scope.price = price;
// price to pay
2018-11-21 11:08:53 +01:00
$scope.amount = helpers.getAmountToPay($scope.price, wallet.amount);
// Used in wallet info template to interpolate some translations
2018-11-21 11:08:53 +01:00
$scope.numberFilter = $filter('number');
// The plan that the user is about to subscribe
2018-11-21 11:08:53 +01:00
$scope.plan = selectedPlan;
// The member who is subscribing a plan
2018-11-21 11:08:53 +01:00
$scope.member = member;
// Button label
if ($scope.amount > 0) {
2018-11-21 11:08:53 +01:00
$scope.validButtonName = _t('confirm_payment_of_html', { ROLE: $scope.currentUser.role, AMOUNT: $filter('currency')($scope.amount) }, 'messageformat');
} else {
if ((price.price > 0) && ($scope.walletAmount === 0)) {
2018-11-21 11:08:53 +01:00
$scope.validButtonName = _t('confirm_payment_of_html', { ROLE: $scope.currentUser.role, AMOUNT: $filter('currency')(price.price) }, 'messageformat');
} else {
2018-11-21 11:08:53 +01:00
$scope.validButtonName = _t('confirm');
}
}
2018-11-19 16:17:49 +01:00
/**
* Callback for the 'proceed' button.
* Save the subscription to the API
*/
$scope.ok = function () {
2018-11-21 11:08:53 +01:00
$scope.attempting = true;
2018-11-20 12:26:06 +01:00
Subscription.save({
2018-11-21 10:59:07 +01:00
coupon_code: ((coupon ? coupon.code : undefined)),
subscription: {
plan_id: selectedPlan.id,
user_id: member.id
2018-11-20 12:26:06 +01:00
}
2018-11-21 10:59:07 +01:00
}
, function (data) { // success
2018-11-21 11:08:53 +01:00
$uibModalInstance.close(data);
2018-11-21 10:59:07 +01:00
}
, function (data, status) { // failed
2018-11-21 11:08:53 +01:00
$scope.alerts = [];
$scope.alerts.push({ msg: _t('an_error_occured_during_the_payment_process_please_try_again_later'), type: 'danger' });
$scope.attempting = false;
2018-11-21 10:59:07 +01:00
}
2018-11-21 11:08:53 +01:00
);
};
2018-11-19 16:17:49 +01:00
/**
* Callback for the 'cancel' button.
* Close the modal box.
*/
2018-11-21 11:08:53 +01:00
$scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
}
2018-11-20 12:26:06 +01:00
]
}).result['finally'](null).then(function (reservation) {
2018-11-21 11:08:53 +01:00
$scope.ctrl.member.subscribed_plan = angular.copy($scope.selectedPlan);
Auth._currentUser.subscribed_plan = angular.copy($scope.selectedPlan);
$scope.ctrl.member = null;
$scope.paid.plan = angular.copy($scope.selectedPlan);
$scope.selectedPlan = null;
return $scope.coupon.applied = null;
});
};
2018-11-19 16:17:49 +01:00
// !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:08:53 +01:00
return initialize();
}
2018-11-21 11:08:53 +01:00
]);