From e646eb8cb583f5a1643d29db7a8cc9884a67daa0 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Fri, 8 Oct 2021 19:14:47 +0200 Subject: [PATCH] extend subscription UI --- .../api/subscriptions_controller.rb | 6 ++- .../javascript/controllers/admin/members.js | 44 ++++++++++++++++++- .../src/javascript/services/subscription.js | 4 ++ .../templates/admin/members/edit.html | 2 +- .../admin/subscriptions/expired_at_modal.html | 29 +++++++++--- app/policies/subscription_policy.rb | 4 ++ .../payment_details.json.jbuilder | 4 ++ config/locales/app.admin.en.yml | 18 +++++--- config/routes.rb | 4 +- 9 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 app/views/api/subscriptions/payment_details.json.jbuilder diff --git a/app/controllers/api/subscriptions_controller.rb b/app/controllers/api/subscriptions_controller.rb index bca08d5e5..a8217c6b6 100644 --- a/app/controllers/api/subscriptions_controller.rb +++ b/app/controllers/api/subscriptions_controller.rb @@ -2,7 +2,7 @@ # API Controller for resources of type Subscription class API::SubscriptionsController < API::ApiController - before_action :set_subscription, only: %i[show edit update destroy] + before_action :set_subscription, only: %i[show payment_details edit update destroy] before_action :authenticate_user! def show @@ -26,6 +26,10 @@ class API::SubscriptionsController < API::ApiController end end + def payment_details + authorize @subscription + end + private # Use callbacks to share common setup or constraints between actions. diff --git a/app/frontend/src/javascript/controllers/admin/members.js b/app/frontend/src/javascript/controllers/admin/members.js index 1ad5f24b4..7a37fb3c3 100644 --- a/app/frontend/src/javascript/controllers/admin/members.js +++ b/app/frontend/src/javascript/controllers/admin/members.js @@ -762,9 +762,19 @@ Application.Controllers.controller('EditMemberController', ['$scope', '$state', animation: true, templateUrl: '/admin/subscriptions/expired_at_modal.html', size: 'lg', - controller: ['$scope', '$uibModalInstance', 'Subscription', function ($scope, $uibModalInstance, Subscription) { - $scope.new_expired_at = angular.copy(subscription.expired_at); + resolve: { + paymentDetails () { + return Subscription.payment_details({ id: subscription.id }).$promise; + } + }, + controller: ['$scope', '$uibModalInstance', 'Subscription', 'paymentDetails', function ($scope, $uibModalInstance, Subscription, paymentDetails) { + /* PUBLIC SCOPE */ + + $scope.expire_at = subscription.expired_at; + $scope.new_expired_at = new Date(subscription.expired_at); $scope.free = free; + $scope.days = 0; + $scope.payment_details = paymentDetails; $scope.datePicker = { opened: false, format: Fablab.uibDateFormat, @@ -780,6 +790,11 @@ Application.Controllers.controller('EditMemberController', ['$scope', '$state', return $scope.datePicker.opened = true; }; + $scope.$watch(scope => scope.expire_at + , () => refreshDays()); + $scope.$watch(scope => scope.new_expired_at + , () => refreshDays()); + $scope.ok = function () { Subscription.update( { id: subscription.id }, @@ -796,6 +811,31 @@ Application.Controllers.controller('EditMemberController', ['$scope', '$state', }; $scope.cancel = function () { $uibModalInstance.dismiss('cancel'); }; + + /* PRIVATE SCOPE */ + + /** + * Kind of constructor: these actions will be realized first when the controller is loaded + */ + function initialize () { + if (!free) { + $scope.new_expired_at = moment($scope.expire_at).add(subscription.plan.interval_count, subscription.plan.interval).toDate(); + } + } + + /** + * Refresh the number of free days depending on the original subscription expiration date and on the new selected date + */ + function refreshDays () { + if (!$scope.new_expired_at || !$scope.expire_at) { + return $scope.days = 0; + } + // 86400000 = 1000 * 3600 * 24 = number of ms per day + $scope.days = Math.round((new Date($scope.new_expired_at).getTime() - new Date($scope.expire_at).getTime()) / 86400000); + } + + // !!! MUST BE CALLED AT THE END of the controller + initialize(); }] }); // once the form was validated successfully ... diff --git a/app/frontend/src/javascript/services/subscription.js b/app/frontend/src/javascript/services/subscription.js index 6056283ea..b98eb5f91 100644 --- a/app/frontend/src/javascript/services/subscription.js +++ b/app/frontend/src/javascript/services/subscription.js @@ -5,6 +5,10 @@ Application.Services.factory('Subscription', ['$resource', function ($resource) { id: '@id' }, { update: { method: 'PUT' + }, + payment_details: { + url: '/api/subscriptions/:id/payment_details', + method: 'GET' } } ); diff --git a/app/frontend/templates/admin/members/edit.html b/app/frontend/templates/admin/members/edit.html index 7a314e282..78e8ea5ce 100644 --- a/app/frontend/templates/admin/members/edit.html +++ b/app/frontend/templates/admin/members/edit.html @@ -78,7 +78,7 @@

- +

{{ 'app.admin.members_edit.cannot_extend_own_subscription' }} diff --git a/app/frontend/templates/admin/subscriptions/expired_at_modal.html b/app/frontend/templates/admin/subscriptions/expired_at_modal.html index 02e0b71ac..7e94d692e 100644 --- a/app/frontend/templates/admin/subscriptions/expired_at_modal.html +++ b/app/frontend/templates/admin/subscriptions/expired_at_modal.html @@ -4,30 +4,49 @@