Passed
Push — master ( 98975f...e9b166 )
by
unknown
09:13
created

myems-admin/app/controllers/settings/iotsimcard/iotsimcard.controller.js   A

Complexity

Total Complexity 27
Complexity/F 1.23

Size

Lines of Code 170
Function Count 22

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 27
eloc 117
dl 0
loc 170
rs 10
c 0
b 0
f 0
mnd 5
bc 5
fnc 22
bpm 0.2272
cpm 1.2272
noi 0
1
'use strict';
2
3
app.controller('IoTSIMCardController', function(
4
    $scope,
5
    $rootScope,
6
    $window,
7
    $translate,
8
    $uibModal,
9
    IoTSIMCardService,
10
    toaster,
11
    SweetAlert) {
12
13
	$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
14
	$scope.getAllIoTSIMCards = function() {
15
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
16
		IoTSIMCardService.getAllIoTSIMCards(headers, function (response) {
17
			if (angular.isDefined(response.status) && response.status === 200) {
18
				$scope.iotsimcards = response.data;
19
			} else {
20
				$scope.iotsimcards = [];
21
			}
22
		});
23
24
	};
25
26
	$scope.addIoTSIMCard = function() {
27
		var modalInstance = $uibModal.open({
28
			templateUrl: 'views/settings/iotsimcard/iotsimcard.model.html',
29
			controller: 'ModalAddIoTSIMCardCtrl',
30
			windowClass: "animated fadeIn",
31
			resolve: {
32
		        params:function(){
33
                    return {
34
                        iotsimcards:angular.copy($scope.iotsimcards)
35
                    };
36
                }
37
		    }
38
		});
39
		modalInstance.result.then(function(iotsimcard) {
40
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
41
			IoTSIMCardService.addIoTSIMCard(iotsimcard, headers, function(response) {
42
				if (angular.isDefined(response.status) && response.status === 201) {
43
					toaster.pop({
44
						type: "success",
45
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
46
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("SETTING.iotsimcard")}),
47
						showCloseButton: true,
48
					});
49
					$scope.getAllIoTSIMCards();
50
				} else {
51
					toaster.pop({
52
						type: "error",
53
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("SETTING.iotsimcard")}),
54
						body: $translate.instant(response.data.description),
55
						showCloseButton: true,
56
					});
57
				}
58
			});
59
		}, function() {
60
61
		});
62
		$rootScope.modalInstance = modalInstance;
63
	};
64
65
	$scope.editIoTSIMCard=function(iotsimcard){
66
		var modalInstance = $uibModal.open({
67
		    windowClass: "animated fadeIn",
68
		    templateUrl: 'views/settings/iotsimcard/iotsimcard.model.html',
69
		    controller: 'ModalEditIoTSIMCardCtrl',
70
		    resolve: {
71
		        params:function(){
72
                    return {
73
                        iotsimcard:angular.copy(iotsimcard),
74
                        iotsimcards:angular.copy($scope.iotsimcards)
75
                    };
76
                }
77
		    }
78
		});
79
80
		modalInstance.result.then(function (modifiedIoTSIMCard) {
81
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
82
	        IoTSIMCardService.editIoTSIMCard(modifiedIoTSIMCard, headers, function (response) {
83
	            if(angular.isDefined(response.status) && response.status === 200){
84
					toaster.pop({
85
						type: "success",
86
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
87
						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("SETTING.iotsimcard")}),
88
						showCloseButton: true,
89
					});
90
	            $scope.getAllIoTSIMCards();
91
	            }else{
92
					toaster.pop({
93
						type: "error",
94
						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("SETTING.iotsimcard")}),
95
						body: $translate.instant(response.data.description),
96
						showCloseButton: true,
97
					});
98
	            }
99
	        });
100
		}, function () {
101
	        //do nothing;
102
		});
103
		$rootScope.modalInstance = modalInstance;
104
	};
105
106
	$scope.deleteIoTSIMCard=function(iotsimcard){
107
		SweetAlert.swal({
108
		        title: $translate.instant("SWEET.TITLE"),
109
		        text: $translate.instant("SWEET.TEXT"),
110
		        type: "warning",
111
		        showCancelButton: true,
112
		        confirmButtonColor: "#DD6B55",
113
		        confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
114
		        cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
115
		        closeOnConfirm: true,
116
		        closeOnCancel: true },
117
		    function (isConfirm) {
118
		        if (isConfirm) {
119
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
120
		            IoTSIMCardService.deleteIoTSIMCard(iotsimcard, headers, function (response) {
121
		            	if (angular.isDefined(response.status) && response.status === 204) {
122
                            toaster.pop({
123
                                type: "success",
124
                                title: $translate.instant("TOASTER.SUCCESS_TITLE"),
125
                                body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("SETTING.iotsimcard")}),
126
                                showCloseButton: true,
127
                            });
128
		            		$scope.getAllIoTSIMCards();
129
		            	} else {
130
                            toaster.pop({
131
                                type: "error",
132
                                title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("SETTING.iotsimcard")}),
133
                                body: $translate.instant(response.data.description),
134
                                showCloseButton: true,
135
                            });
136
		            	}
137
		            });
138
		        }
139
		    });
140
	};
141
142
	$scope.getAllIoTSIMCards();
143
});
144
145
app.controller('ModalAddIoTSIMCardCtrl', function ($scope, $uibModalInstance,params) {
146
147
    $scope.operation="IOTSIMCARD.ADD_IOTSIMCARD";
148
    $scope.iotsimcards=params.iotsimcards;
149
    $scope.ok = function () {
150
        $uibModalInstance.close($scope.iotsimcard);
151
    };
152
153
    $scope.cancel = function () {
154
        $uibModalInstance.dismiss('cancel');
155
    };
156
});
157
158
app.controller('ModalEditIoTSIMCardCtrl', function ($scope, $uibModalInstance, params) {
159
    $scope.operation="IOTSIMCARD.EDIT_IOTSIMCARD";
160
    $scope.iotsimcard = params.iotsimcard;
161
    $scope.iotsimcards=params.iotsimcards;
162
163
    $scope.ok = function () {
164
        $uibModalInstance.close($scope.iotsimcard);
165
    };
166
167
    $scope.cancel = function () {
168
        $uibModalInstance.dismiss('cancel');
169
    };
170
});
171