Passed
Push — master ( 4d6f13...ccf8db )
by
unknown
11:59
created

myems-admin/app/controllers/settings/contact/contact.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('ContactController', function(
4
    $scope,
5
    $rootScope,
6
    $window,
7
    $translate,
8
    $uibModal,
9
    ContactService,
10
    toaster,
11
    SweetAlert) {
12
13
	$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
14
	$scope.searchKeyword = '';
15
	$scope.getAllContacts = function() {
16
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
17
		ContactService.getAllContacts(headers, function (response) {
18
			if (angular.isDefined(response.status) && response.status === 200) {
19
				$scope.contacts = response.data;
20
			} else {
21
				$scope.contacts = [];
22
			}
23
		});
24
25
	};
26
27
	$scope.addContact = function() {
28
		var modalInstance = $uibModal.open({
29
			templateUrl: 'views/settings/contact/contact.model.html',
30
			controller: 'ModalAddContactCtrl',
31
			windowClass: "animated fadeIn",
32
			resolve: {
33
		        params:function(){
34
                    return {
35
                        contacts:angular.copy($scope.contacts)
36
                    };
37
                }
38
		    }
39
		});
40
		modalInstance.result.then(function(contact) {
41
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
42
			ContactService.addContact(contact, headers, function(response) {
43
				if (angular.isDefined(response.status) && response.status === 201) {
44
					toaster.pop({
45
						type: "success",
46
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
47
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("SETTING.CONTACT")}),
48
						showCloseButton: true,
49
					});
50
					$scope.getAllContacts();
51
				} else {
52
					toaster.pop({
53
						type: "error",
54
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("SETTING.CONTACT")}),
55
						body: $translate.instant(response.data.description),
56
						showCloseButton: true,
57
					});
58
				}
59
			});
60
		}, function() {
61
62
		});
63
		$rootScope.modalInstance = modalInstance;
64
	};
65
66
	$scope.editContact=function(contact){
67
		var modalInstance = $uibModal.open({
68
		    windowClass: "animated fadeIn",
69
		    templateUrl: 'views/settings/contact/contact.model.html',
70
		    controller: 'ModalEditContactCtrl',
71
		    resolve: {
72
		        params:function(){
73
                    return {
74
                        contact:angular.copy(contact),
75
                        contacts:angular.copy($scope.contacts)
76
                    };
77
                }
78
		    }
79
		});
80
81
		modalInstance.result.then(function (modifiedContact) {
82
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
83
	        ContactService.editContact(modifiedContact, headers, function (response) {
84
	            if(angular.isDefined(response.status) && response.status === 200){
85
					toaster.pop({
86
						type: "success",
87
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
88
						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("SETTING.CONTACT")}),
89
						showCloseButton: true,
90
					});
91
	            $scope.getAllContacts();
92
	            }else{
93
					toaster.pop({
94
						type: "error",
95
						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("SETTING.CONTACT")}),
96
						body: $translate.instant(response.data.description),
97
						showCloseButton: true,
98
					});
99
	            }
100
	        });
101
		}, function () {
102
	        //do nothing;
103
		});
104
		$rootScope.modalInstance = modalInstance;
105
	};
106
107
	$scope.deleteContact=function(contact){
108
		SweetAlert.swal({
109
		        title: $translate.instant("SWEET.TITLE"),
110
		        text: $translate.instant("SWEET.TEXT"),
111
		        type: "warning",
112
		        showCancelButton: true,
113
		        confirmButtonColor: "#DD6B55",
114
		        confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
115
		        cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
116
		        closeOnConfirm: true,
117
		        closeOnCancel: true },
118
		    function (isConfirm) {
119
		        if (isConfirm) {
120
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
121
		            ContactService.deleteContact(contact, headers, function (response) {
122
		            	if (angular.isDefined(response.status) && response.status === 204) {
123
                            toaster.pop({
124
                                type: "success",
125
                                title: $translate.instant("TOASTER.SUCCESS_TITLE"),
126
                                body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("SETTING.CONTACT")}),
127
                                showCloseButton: true,
128
                            });
129
		            		$scope.getAllContacts();
130
		            	} else {
131
                            toaster.pop({
132
                                type: "error",
133
                                title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("SETTING.CONTACT")}),
134
                                body: $translate.instant(response.data.description),
135
                                showCloseButton: true,
136
                            });
137
		            	}
138
		            });
139
		        }
140
		    });
141
	};
142
143
	let searchDebounceTimer = null;
144
	function safeApply(scope) {
145
		if (!scope.$$phase && !scope.$root.$$phase) {
146
			scope.$apply();
147
		}
148
	}
149
	$scope.searchContact = function() {
150
		const headers = {
151
			"User-UUID": $scope.cur_user?.uuid,
152
			"Token": $scope.cur_user?.token
153
		};
154
155
		const rawKeyword = $scope.searchKeyword || "";
156
		const trimmedKeyword = rawKeyword.trim();
157
158
		if (searchDebounceTimer) {
159
			clearTimeout(searchDebounceTimer);
160
		}
161
162
		searchDebounceTimer = setTimeout(() => {
163
			if (!trimmedKeyword) {
164
				$scope.getAllContacts();
165
				safeApply($scope);
166
				return;
167
			}
168
169
			ContactService.searchContacts(trimmedKeyword, headers, (response) => {
170
				$scope.contacts = (response.status === 200) ? response.data : [];
171
				$scope.parentmeters = [...$scope.contacts];
172
			});
173
		}, 300);
174
	};
175
176
	$scope.getAllContacts();
177
});
178
179
app.controller('ModalAddContactCtrl', function ($scope, $uibModalInstance,params) {
180
181
    $scope.operation="SETTING.ADD_CONTACT";
182
    $scope.contacts=params.contacts;
183
    $scope.ok = function () {
184
        $uibModalInstance.close($scope.contact);
185
    };
186
187
    $scope.cancel = function () {
188
        $uibModalInstance.dismiss('cancel');
189
    };
190
});
191
192
app.controller('ModalEditContactCtrl', function ($scope, $uibModalInstance, params) {
193
    $scope.operation="SETTING.EDIT_CONTACT";
194
    $scope.contact = params.contact;
195
    $scope.contacts=params.contacts;
196
197
    $scope.ok = function () {
198
        $uibModalInstance.close($scope.contact);
199
    };
200
201
    $scope.cancel = function () {
202
        $uibModalInstance.dismiss('cancel');
203
    };
204
});
205