1 (function(angular
, $, _
) {
3 angular
.module('crmCxn').controller('CrmCxnManageCtrl', function CrmCxnManageCtrl($scope
, apiCalls
, crmApi
, crmUiAlert
, crmBlocker
, crmStatus
, $timeout
) {
4 var ts
= $scope
.ts
= CRM
.ts(null);
5 $scope
.appMetas
= apiCalls
.appMetas
.values
;
6 $scope
.cxns
= apiCalls
.cxns
.values
;
7 $scope
.alerts
= _
.where(apiCalls
.sysCheck
.values
, {name
: 'checkCxnOverrides'});
10 var block
= $scope
.block
= crmBlocker();
12 _
.each($scope
.alerts
, function(alert
){
13 crmUiAlert({text
: alert
.message
, title
: alert
.title
, type
: 'error'});
16 $scope
.findCxnByAppId = function(appId
) {
17 var result
= _
.where($scope
.cxns
, {
20 switch (result
.length
) {
26 throw "Error: Too many connections for appId: " + appId
;
30 $scope
.hasAvailApps = function() {
31 // This should usu return after the 1st or 2nd item, but in testing with small# apps, we may exhaust the list.
32 for (var i
= 0; i
< $scope
.appMetas
.length
; i
++) {
33 if (!$scope
.findCxnByAppId($scope
.appMetas
[i
].appId
)) {
40 $scope
.refreshCxns = function() {
41 crmApi('Cxn', 'get', {sequential
: 1}).then(function(result
) {
43 $scope
.cxns
= result
.values
;
48 $scope
.register = function(appMeta
) {
49 var reg
= crmApi('Cxn', 'register', {app_guid
: appMeta
.appId
}).then($scope
.refreshCxns
);
50 return block(crmStatus({start
: ts('Connecting...'), success
: ts('Connected')}, reg
));
53 $scope
.unregister = function(appMeta
) {
54 var reg
= crmApi('Cxn', 'unregister', {app_guid
: appMeta
.appId
, debug
: 1}).then($scope
.refreshCxns
);
55 return block(crmStatus({start
: ts('Disconnecting...'), success
: ts('Disconnected')}, reg
));
58 $scope
.toggleCxn
= function toggleCxn(cxn
) {
59 var reg
= crmApi('Cxn', 'create', {id
: cxn
.id
, is_active
: !cxn
.is_active
, debug
: 1}).then(function(){
60 cxn
.is_active
= !cxn
.is_active
;
62 return block(crmStatus({start
: ts('Saving...'), success
: ts('Saved')}, reg
));
66 })(angular
, CRM
.$, CRM
._
);