| .. | .. |
|---|
| 1 | 1 | (function() { |
|---|
| 2 | 2 | 'use strict'; |
|---|
| 3 | 3 | |
|---|
| 4 | | - var LIC_STATUS = { |
|---|
| 5 | | - CREATED: 1, |
|---|
| 6 | | - REQUESTED: 2, |
|---|
| 7 | | - PREACTIVE: 3, |
|---|
| 8 | | - ACTIVE: 4, |
|---|
| 9 | | - CANCELED: 5 |
|---|
| 10 | | - } |
|---|
| 11 | | - |
|---|
| 12 | | - var ACTIONS_BY_STATUS = { |
|---|
| 13 | | - activate: [LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE], |
|---|
| 14 | | - send: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE], |
|---|
| 15 | | - download: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE], |
|---|
| 16 | | - request: [LIC_STATUS.CREATED, LIC_STATUS.REQUESTED], |
|---|
| 17 | | - cancel: [LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE], |
|---|
| 18 | | - 'delete': [LIC_STATUS.CREATED, LIC_STATUS.CANCELED], |
|---|
| 19 | | - } |
|---|
| 4 | + |
|---|
| 20 | 5 | |
|---|
| 21 | 6 | var HTTP_ERRORS = { |
|---|
| 22 | 7 | 401: "Unathorized action", |
|---|
| .. | .. |
|---|
| 27 | 12 | } |
|---|
| 28 | 13 | |
|---|
| 29 | 14 | var app = angular.module('securis'); |
|---|
| 15 | + app.service('Packs', ['$L','$resource', 'toaster', function($L, $resource, toaster) { |
|---|
| 16 | + var PACK_STATUS = { |
|---|
| 17 | + CREATED: 'CR', |
|---|
| 18 | + ACTIVE: 'AC', |
|---|
| 19 | + ONHOLD: 'OH', |
|---|
| 20 | + EXPIRED: 'EX', |
|---|
| 21 | + CANCELLED: 'CA' |
|---|
| 22 | + } |
|---|
| 23 | + var PACK_STATUSES = { |
|---|
| 24 | + 'CR': $L.get('Created'), |
|---|
| 25 | + 'AC': $L.get('Active'), |
|---|
| 26 | + 'OH': $L.get('On Hold'), |
|---|
| 27 | + 'EX': $L.get('Expired'), |
|---|
| 28 | + 'CA': $L.get('Cancelled') |
|---|
| 29 | + }; |
|---|
| 30 | + /** |
|---|
| 31 | + * These transitions could be get from server, class Pack.Status, but we copy them for simplicity, this info won't change easily |
|---|
| 32 | + */ |
|---|
| 33 | + var PACK_ACTIONS_BY_STATUS = { |
|---|
| 34 | + activate: [PACK_STATUS.CREATED, PACK_STATUS.EXPIRED, PACK_STATUS.ONHOLD], |
|---|
| 35 | + putonhold: [PACK_STATUS.ACTIVE], |
|---|
| 36 | + cancel: [PACK_STATUS.EXPIRED, PACK_STATUS.ONHOLD, PACK_STATUS.ACTIVE], |
|---|
| 37 | + 'delete': [PACK_STATUS.CREATED, PACK_STATUS.CANCELLED] |
|---|
| 38 | + } |
|---|
| 39 | + |
|---|
| 40 | + var packResource = $resource('/pack/:packId/:action', |
|---|
| 41 | + { |
|---|
| 42 | + packId : '@id', |
|---|
| 43 | + action : '@action' |
|---|
| 44 | + }, |
|---|
| 45 | + { |
|---|
| 46 | + activate: { |
|---|
| 47 | + method: "POST", |
|---|
| 48 | + params: {action: "activate"} |
|---|
| 49 | + }, |
|---|
| 50 | + putonhold: { |
|---|
| 51 | + method: "POST", |
|---|
| 52 | + params: {action: "putonhold"} |
|---|
| 53 | + }, |
|---|
| 54 | + cancel: { |
|---|
| 55 | + method: "POST", |
|---|
| 56 | + params: {action: "cancel"} |
|---|
| 57 | + } |
|---|
| 58 | + } |
|---|
| 59 | + ); |
|---|
| 60 | + this.savePackData = function(pack, isNew, _onsuccess) { |
|---|
| 61 | + var _success = function() { |
|---|
| 62 | + _onsuccess(); |
|---|
| 63 | + toaster.pop('success', 'Packs', $L.get("Pack '{0}' {1} successfully", pack.code, isNew ? $L.get("created") : $L.get("updated"))); |
|---|
| 64 | + } |
|---|
| 65 | + var _error = function(error) { |
|---|
| 66 | + console.log(error); |
|---|
| 67 | + toaster.pop('error', 'Packs', $L.get("Error {0} pack '{1}'. Reason: {2}", isNew ? $L.get("creating") : $L.get("updating"), pack.code, $L.get(error.headers('X-SECURIS-ERROR')))); |
|---|
| 68 | + } |
|---|
| 69 | + packResource.save(pack, _success, _error); |
|---|
| 70 | + } |
|---|
| 71 | + |
|---|
| 72 | + this.isActionAvailable = function(action, pack) { |
|---|
| 73 | + var validStatuses = PACK_ACTIONS_BY_STATUS[action]; |
|---|
| 74 | + return pack && validStatuses && validStatuses.indexOf(pack.status) !== -1; |
|---|
| 75 | + } |
|---|
| 76 | + this.getStatusName = function(status) { |
|---|
| 77 | + return PACK_STATUSES[status]; |
|---|
| 78 | + } |
|---|
| 79 | + |
|---|
| 80 | + var _createSuccessCallback = function(actionName, message, _innerCallback) { |
|---|
| 81 | + return function() { |
|---|
| 82 | + _innerCallback && _innerCallback(); |
|---|
| 83 | + toaster.pop('success', actionName, message); |
|---|
| 84 | + } |
|---|
| 85 | + } |
|---|
| 86 | + var _createErrorCallback = function(pack, actionName, _innerCallback) { |
|---|
| 87 | + return function(error) { |
|---|
| 88 | + console.log(error); |
|---|
| 89 | + _innerCallback && _innerCallback(); |
|---|
| 90 | + toaster.pop('error', actionName, $L.get("Error on action '{0}', pack '{1}'. Reason: {2}", actionName, pack.code, $L.get(error.headers('X-SECURIS-ERROR')))); |
|---|
| 91 | + } |
|---|
| 92 | + } |
|---|
| 93 | + this.getPacksList = function(pack, _onsuccess, _onerror) { |
|---|
| 94 | + return packResource.query(_onsuccess, _onerror); |
|---|
| 95 | + } |
|---|
| 96 | + this.activate = function(pack, _onsuccess, _onerror) { |
|---|
| 97 | + console.log('Activation on pack: ' + pack.id); |
|---|
| 98 | + var _success = _createSuccessCallback($L.get('Activation'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("activated")), _onsuccess); |
|---|
| 99 | + var _error = _createErrorCallback(pack, $L.get('Activation'), _onerror); |
|---|
| 100 | + packResource.activate({id: pack.id}, _success, _error); |
|---|
| 101 | + } |
|---|
| 102 | + this.putonhold = function(pack, _onsuccess, _onerror) { |
|---|
| 103 | + console.log('Put on hold on pack: ' + pack.id); |
|---|
| 104 | + var _success = _createSuccessCallback($L.get('Put on hold'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("put on hold")), _onsuccess); |
|---|
| 105 | + var _error = _createErrorCallback(pack, $L.get('Put on hold'), _onerror); |
|---|
| 106 | + packResource.putonhold({id: pack.id}, _success, _error); |
|---|
| 107 | + } |
|---|
| 108 | + this.cancel = function(pack, _onsuccess, _onerror) { |
|---|
| 109 | + console.log('Cancellation on pack: ' + pack.id); |
|---|
| 110 | + var _success = _createSuccessCallback($L.get('Cancellation'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("cancelled")), _onsuccess); |
|---|
| 111 | + var _error = _createErrorCallback(pack, $L.get('Cancellation'), _onerror); |
|---|
| 112 | + packResource.cancel({id: pack.id}, _success, _error); |
|---|
| 113 | + } |
|---|
| 114 | + this.delete = function(pack, _onsuccess, _onerror) { |
|---|
| 115 | + console.log('Delete on pack: ' + pack.id); |
|---|
| 116 | + var _success = _createSuccessCallback($L.get('Deletion'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("deleted")), _onsuccess); |
|---|
| 117 | + var _error = _createErrorCallback(pack, $L.get('Deletion'), _onerror); |
|---|
| 118 | + packResource.delete({packId: pack.id}, _success, _error); |
|---|
| 119 | + } |
|---|
| 120 | + |
|---|
| 121 | + }]); |
|---|
| 122 | + |
|---|
| 123 | + app.service('Licenses', ['$L', '$resource', 'toaster', function($L, $resource, toaster) { |
|---|
| 124 | + var LIC_STATUS = { |
|---|
| 125 | + CREATED: 'CR', |
|---|
| 126 | + ACTIVE: 'AC', |
|---|
| 127 | + REQUESTED: 'RE', |
|---|
| 128 | + PREACTIVE: 'PA', |
|---|
| 129 | + EXPIRED: 'EX', |
|---|
| 130 | + CANCELLED: 'CA' |
|---|
| 131 | + } |
|---|
| 132 | + |
|---|
| 133 | + var LIC_STATUSES = { |
|---|
| 134 | + 'CR': $L.get('Created'), |
|---|
| 135 | + 'AC': $L.get('Active'), |
|---|
| 136 | + 'PA': $L.get('Pre-active'), |
|---|
| 137 | + 'RE': $L.get('Requested'), |
|---|
| 138 | + 'EX': $L.get('Expired'), |
|---|
| 139 | + 'CA': $L.get('Cancelled') |
|---|
| 140 | + }; |
|---|
| 141 | + |
|---|
| 142 | + /** |
|---|
| 143 | + * These transitions could be get from server, class License.Status, but we copy them for simplicity, this info won't change easily |
|---|
| 144 | + */ |
|---|
| 145 | + var LIC_ACTIONS_BY_STATUS = { |
|---|
| 146 | + activate: [LIC_STATUS.CREATED, LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE], |
|---|
| 147 | + send: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE], |
|---|
| 148 | + download: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE], |
|---|
| 149 | + cancel: [LIC_STATUS.REQUESTED, LIC_STATUS.EXPIRED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE], |
|---|
| 150 | + 'delete': [LIC_STATUS.CREATED, LIC_STATUS.CANCELLED] |
|---|
| 151 | + } |
|---|
| 152 | + |
|---|
| 153 | + this.isActionAvailable = function(action, lic) { |
|---|
| 154 | + var validStatuses = LIC_ACTIONS_BY_STATUS[action]; |
|---|
| 155 | + return lic && validStatuses && validStatuses.indexOf(lic.status) !== -1; |
|---|
| 156 | + } |
|---|
| 157 | + |
|---|
| 158 | + this.getStatusName = function(status) { |
|---|
| 159 | + return LIC_STATUSES[status]; |
|---|
| 160 | + } |
|---|
| 161 | + |
|---|
| 162 | + }]); |
|---|
| 30 | 163 | |
|---|
| 31 | 164 | app.directive('fileLoader', |
|---|
| 32 | 165 | function($timeout, $parse) { |
|---|
| .. | .. |
|---|
| 98 | 231 | '$resource', |
|---|
| 99 | 232 | 'toaster', |
|---|
| 100 | 233 | 'Catalogs', |
|---|
| 234 | + 'Packs', |
|---|
| 101 | 235 | '$store', |
|---|
| 102 | 236 | '$L', |
|---|
| 103 | | - function($scope, $http, $resource, toaster, Catalogs, $store, $L) { |
|---|
| 104 | | - var packResource = $resource('/pack/:packId/:action', |
|---|
| 105 | | - { |
|---|
| 106 | | - packId : '@id', |
|---|
| 107 | | - action : '@action' |
|---|
| 108 | | - }, |
|---|
| 109 | | - { |
|---|
| 110 | | - activate: { |
|---|
| 111 | | - method: "POST", |
|---|
| 112 | | - params: {action: "activate"} |
|---|
| 113 | | - } |
|---|
| 114 | | - } |
|---|
| 115 | | - ); |
|---|
| 116 | | - var PACK_STATUS = [ |
|---|
| 117 | | - {id: 'CR', label: $L.get('Created')}, |
|---|
| 118 | | - {id: 'AC', label: $L.get('Active')}, |
|---|
| 119 | | - {id: 'OH', label: $L.get('On Hold')}, |
|---|
| 120 | | - {id: 'EX', label: $L.get('Expired')}, |
|---|
| 121 | | - {id: 'CA', label: $L.get('Cancelled')} |
|---|
| 122 | | - ]; |
|---|
| 237 | + function($scope, $http, $resource, toaster, Catalogs, Packs, $store, $L) { |
|---|
| 238 | + $scope.Packs = Packs; |
|---|
| 239 | + |
|---|
| 240 | + |
|---|
| 123 | 241 | $scope.mandatory = { |
|---|
| 124 | 242 | code: true, |
|---|
| 125 | 243 | num_licenses: true, |
|---|
| .. | .. |
|---|
| 138 | 256 | var refFields = [{resource: 'organization', name: 'organization_id'},{resource: 'licensetype', name: 'license_type_id'}]; |
|---|
| 139 | 257 | Catalogs.loadRefs(function(refs) { |
|---|
| 140 | 258 | $scope.refs = refs; |
|---|
| 141 | | - $scope.refs['pack_status'] = PACK_STATUS; |
|---|
| 142 | 259 | }, refFields); |
|---|
| 143 | 260 | }); |
|---|
| 144 | 261 | |
|---|
| .. | .. |
|---|
| 149 | 266 | // pack is the edited pack, in creation contains the data for the new pack |
|---|
| 150 | 267 | $scope.pack = null; |
|---|
| 151 | 268 | |
|---|
| 152 | | - $scope.packs = packResource.query(); |
|---|
| 269 | + $scope.packs = Packs.getPacksList(); |
|---|
| 153 | 270 | |
|---|
| 154 | | - var _savePackData = function() { |
|---|
| 155 | | - var _success = function() { |
|---|
| 156 | | - if (!$scope.isNew) $scope.showForm = false; |
|---|
| 157 | | - $scope.packs = packResource.query(); |
|---|
| 158 | | - toaster.pop('success', Catalogs.getName(), $L.get("Pack '{0}' {1} successfully", $scope.pack.code, $scope.isNew ? $L.get("created") : $L.get("updated"))); |
|---|
| 159 | | - } |
|---|
| 160 | | - var _error = function(error) { |
|---|
| 161 | | - console.log(error); |
|---|
| 162 | | - toaster.pop('error', Catalogs.getName(), $L.get("Error {0} pack '{1}'. Reason: {2}", $scope.isNew ? $L.get("creating") : $L.get("updating"), $scope.pack.code, $L.get(error.headers('X-SECURIS-ERROR')))); |
|---|
| 163 | | - } |
|---|
| 164 | | - packResource.save($scope.pack, _success, _error); |
|---|
| 165 | | - } |
|---|
| 166 | 271 | $scope.save = function() { |
|---|
| 167 | 272 | if ($scope.pack.num_activations > 0) { |
|---|
| 168 | 273 | BootstrapDialog.confirm($L.get("The pack '{0}' has active licenses, Do you want to modify it ?", $scope.pack.code), function(answer){ |
|---|
| 169 | 274 | if (answer) { |
|---|
| 170 | | - _savePackData(); |
|---|
| 275 | + Packs.savePackData($scope.pack, $scope.isNew, function() { |
|---|
| 276 | + if (!$scope.isNew) $scope.showForm = false; |
|---|
| 277 | + $scope.packs = Packs.getPacksList(); |
|---|
| 278 | + }); |
|---|
| 171 | 279 | } |
|---|
| 172 | 280 | }); |
|---|
| 173 | 281 | } else { |
|---|
| 174 | | - _savePackData(); |
|---|
| 282 | + Packs.savePackData($scope.pack, $scope.isNew, function() { |
|---|
| 283 | + if (!$scope.isNew) { |
|---|
| 284 | + $scope.showForm = false; |
|---|
| 285 | + } else { |
|---|
| 286 | + $scope.newPack(); |
|---|
| 287 | + } |
|---|
| 288 | + $scope.packs = Packs.getPacksList(); |
|---|
| 289 | + }); |
|---|
| 175 | 290 | } |
|---|
| 176 | 291 | } |
|---|
| 177 | 292 | |
|---|
| 178 | 293 | /** |
|---|
| 179 | 294 | * Execute an action over the pack, activation, onhold, cancellation |
|---|
| 180 | 295 | */ |
|---|
| 181 | | - $scope.execute = function(action) { |
|---|
| 182 | | - console.log('Action: '+ action +' on pack: ' + $scope.pack.id); |
|---|
| 183 | | - var _success = function() { |
|---|
| 296 | + $scope.execute = function(action, pack) { |
|---|
| 297 | + |
|---|
| 298 | + Packs[action](pack || $scope.pack, function() { |
|---|
| 184 | 299 | if (!$scope.isNew) $scope.showForm = false; |
|---|
| 185 | | - $scope.packs = packResource.query(); |
|---|
| 186 | | - toaster.pop('success', Catalogs.getName(), $L.get("Pack '{0}' {1} successfully", $scope.pack.code, $L.get("activated"))); |
|---|
| 187 | | - } |
|---|
| 188 | | - var _error = function(error) { |
|---|
| 189 | | - console.log(error); |
|---|
| 190 | | - toaster.pop('error', Catalogs.getName(), $L.get("Error {0} pack '{1}'. Reason: {2}", $L.get("activating"), $scope.pack.code, $L.get(error.headers('X-SECURIS-ERROR')))); |
|---|
| 191 | | - } |
|---|
| 192 | | - packResource.activate({id: $scope.pack.id}, _success, _error); |
|---|
| 300 | + $scope.packs = Packs.getPacksList(); |
|---|
| 301 | + }); |
|---|
| 193 | 302 | } |
|---|
| 194 | 303 | |
|---|
| 195 | 304 | |
|---|
| .. | .. |
|---|
| 222 | 331 | |
|---|
| 223 | 332 | //$scope.pack.organization_name = $scope.getLabelFromId('organization_id', $scope.pack.organization_id); |
|---|
| 224 | 333 | $scope.pack.license_type_name = $scope.getLabelFromId('license_type_id', $scope.pack.license_type_id); |
|---|
| 225 | | - $scope.pack.status_name = $scope.getLabelFromId('pack_status', $scope.pack.status); |
|---|
| 334 | + $scope.pack.status_name = Packs.getStatusName($scope.pack.status); |
|---|
| 226 | 335 | |
|---|
| 227 | 336 | setTimeout(function() { |
|---|
| 228 | 337 | $('#code').focus(); |
|---|
| .. | .. |
|---|
| 302 | 411 | '$http', |
|---|
| 303 | 412 | '$resource', |
|---|
| 304 | 413 | 'toaster', |
|---|
| 414 | + 'Licenses', |
|---|
| 305 | 415 | '$store', |
|---|
| 306 | 416 | '$L', |
|---|
| 307 | | - function($scope, $http, $resource, toaster, $store, $L) { |
|---|
| 417 | + function($scope, $http, $resource, toaster, Licenses, $store, $L) { |
|---|
| 418 | + $scope.Licenses = Licenses; |
|---|
| 308 | 419 | $scope.$on('pack_changed', function(evt, message) { |
|---|
| 309 | 420 | $scope.licenses = licenseResource.query({packId: $scope.currentPack.id}); |
|---|
| 310 | 421 | $scope.creationAvailable = $scope.currentPack.status == 'AC'; |
|---|
| .. | .. |
|---|
| 430 | 541 | $scope.isNew = false; |
|---|
| 431 | 542 | $scope.showForm = true; |
|---|
| 432 | 543 | $scope.license = selectedlicense; |
|---|
| 544 | + $scope.license.status_name = Licenses.getStatusName($scope.license.status); |
|---|
| 545 | + |
|---|
| 433 | 546 | setTimeout(function() { |
|---|
| 434 | 547 | $('#licenseForm * #code').focus(); |
|---|
| 435 | 548 | }, 0); |
|---|
| .. | .. |
|---|
| 463 | 576 | } |
|---|
| 464 | 577 | $scope.showStatusLong = function(license) { |
|---|
| 465 | 578 | |
|---|
| 466 | | - } |
|---|
| 467 | | - $scope.isActionVisible = function(action, lic) { |
|---|
| 468 | | - var validStatuses = ACTIONS_BY_STATUS[action]; |
|---|
| 469 | | - return lic && validStatuses && validStatuses.indexOf(lic.status) !== -1; |
|---|
| 470 | 579 | } |
|---|
| 471 | 580 | |
|---|
| 472 | 581 | } ]); |
|---|