myems-admin/app/controllers/settings/tenant/tenant.controller.js   F
last analyzed

Complexity

Total Complexity 64
Complexity/F 1.49

Size

Lines of Code 389
Function Count 43

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 64
eloc 277
dl 0
loc 389
rs 3.28
c 0
b 0
f 0
mnd 21
bc 21
fnc 43
bpm 0.4883
cpm 1.4883
noi 0

How to fix   Complexity   

Complexity

Complex classes like myems-admin/app/controllers/settings/tenant/tenant.controller.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'use strict';
2
3
app.controller('TenantController', function (
4
	$scope,
5
	$rootScope,
6
	$window,
7
	$translate,
8
	$uibModal,
9
	CostCenterService,
10
	ContactService,
11
	TenantService,
12
	TenantTypeService,
13
	toaster,
14
	SweetAlert) {
15
	$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
16
	$scope.exportdata = '';
17
	$scope.importdata = '';
18
19
	$scope.getAllCostCenters = function () {
20
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
21
		CostCenterService.getAllCostCenters(headers, function (response) {
22
			if (angular.isDefined(response.status) && response.status === 200) {
23
				$scope.costcenters = response.data;
24
			} else {
25
				$scope.costcenters = [];
26
			}
27
		});
28
	};
29
30
	$scope.getAllContacts = function () {
31
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
32
		ContactService.getAllContacts(headers, function (response) {
33
			if (angular.isDefined(response.status) && response.status === 200) {
34
				$scope.contacts = response.data;
35
			} else {
36
				$scope.contacts = [];
37
			}
38
		});
39
	};
40
41
	$scope.getAllTenants = function () {
42
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
43
		TenantService.getAllTenants(headers, function (response) {
44
			if (angular.isDefined(response.status) && response.status === 200) {
45
				$scope.tenants = response.data;
46
			} else {
47
				$scope.tenants = [];
48
			}
49
		});
50
	};
51
52
	$scope.getAllTenantTypes = function () {
53
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
54
		TenantTypeService.getAllTenantTypes(headers, function (response) {
55
			if (angular.isDefined(response.status) && response.status === 200) {
56
				$scope.tenanttypes = response.data;
57
			} else {
58
				$scope.tenanttypes = [];
59
			}
60
		});
61
	};
62
	$scope.addTenant = function () {
63
		var modalInstance = $uibModal.open({
64
			templateUrl: 'views/settings/tenant/tenant.model.html',
65
			controller: 'ModalAddTenantCtrl',
66
			windowClass: "animated fadeIn",
67
			resolve: {
68
				params: function () {
69
					return {
70
						tenants: angular.copy($scope.tenants),
71
						tenanttypes: angular.copy($scope.tenanttypes),
72
						costcenters: angular.copy($scope.costcenters),
73
						contacts: angular.copy($scope.contacts),
74
					};
75
				}
76
			}
77
		});
78
		modalInstance.result.then(function (tenant) {
79
			tenant.tenant_type_id = tenant.tenant_type.id;
80
			tenant.cost_center_id = tenant.cost_center.id;
81
			tenant.contact_id = tenant.contact.id;
82
			if (angular.isDefined(tenant.is_input_counted) == false) {
83
				tenant.is_input_counted = false;
84
			}
85
			if (angular.isDefined(tenant.is_key_tenant) == false) {
86
				tenant.is_key_tenant = false;
87
			}
88
			if (angular.isDefined(tenant.is_in_lease) == false) {
89
				tenant.is_in_lease = false;
90
			}
91
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
92
			TenantService.addTenant(tenant, headers, function (response) {
93
				if (angular.isDefined(response.status) && response.status === 201) {
94
					toaster.pop({
95
						type: "success",
96
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
97
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", { template: $translate.instant("COMMON.TENANT") }),
98
						showCloseButton: true,
99
					});
100
					$scope.$emit('handleEmitTenantChanged');
101
				} else {
102
					toaster.pop({
103
						type: "error",
104
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.TENANT") }),
105
						body: $translate.instant(response.data.description),
106
						showCloseButton: true,
107
					});
108
				}
109
			});
110
		}, function () {
111
112
		});
113
		$rootScope.modalInstance = modalInstance;
114
	};
115
116
	$scope.editTenant = function (tenant) {
117
		var modalInstance = $uibModal.open({
118
			windowClass: "animated fadeIn",
119
			templateUrl: 'views/settings/tenant/tenant.model.html',
120
			controller: 'ModalEditTenantCtrl',
121
			resolve: {
122
				params: function () {
123
					return {
124
						tenant: angular.copy(tenant),
125
						tenanttypes: angular.copy($scope.tenanttypes),
126
						costcenters: angular.copy($scope.costcenters),
127
						contacts: angular.copy($scope.contacts)
128
					};
129
				}
130
			}
131
		});
132
133
		modalInstance.result.then(function (modifiedTenant) {
134
			modifiedTenant.tenant_type_id = modifiedTenant.tenant_type.id;
135
			modifiedTenant.cost_center_id = modifiedTenant.cost_center.id;
136
			modifiedTenant.contact_id = modifiedTenant.contact.id;
137
			if (angular.isDefined(tenant.is_input_counted) == false) {
138
				tenant.is_input_counted = false;
139
			}
140
			if (angular.isDefined(tenant.is_key_tenant) == false) {
141
				tenant.is_key_tenant = false;
142
			}
143
			if (angular.isDefined(tenant.is_in_lease) == false) {
144
				tenant.is_in_lease = false;
145
			}
146
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
147
			TenantService.editTenant(modifiedTenant, headers, function (response) {
148
				if (angular.isDefined(response.status) && response.status === 200) {
149
					toaster.pop({
150
						type: "success",
151
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
152
						body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", { template: $translate.instant("COMMON.TENANT") }),
153
						showCloseButton: true,
154
					});
155
					$scope.$emit('handleEmitTenantChanged');
156
				} else {
157
					toaster.pop({
158
						type: "error",
159
						title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", { template: $translate.instant("COMMON.TENANT") }),
160
						body: $translate.instant(response.data.description),
161
						showCloseButton: true,
162
					});
163
				}
164
			});
165
		}, function () {
166
			//do nothing;
167
		});
168
		$rootScope.modalInstance = modalInstance;
169
	};
170
171
	$scope.deleteTenant = function (tenant) {
172
		SweetAlert.swal({
173
			title: $translate.instant("SWEET.TITLE"),
174
			text: $translate.instant("SWEET.TEXT"),
175
			type: "warning",
176
			showCancelButton: true,
177
			confirmButtonColor: "#DD6B55",
178
			confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
179
			cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
180
			closeOnConfirm: true,
181
			closeOnCancel: true
182
		},
183
			function (isConfirm) {
184
				if (isConfirm) {
185
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
186
					TenantService.deleteTenant(tenant, headers, function (response) {
187
						if (angular.isDefined(response.status) && response.status === 204) {
188
							toaster.pop({
189
								type: "success",
190
								title: $translate.instant("TOASTER.SUCCESS_TITLE"),
191
								body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", { template: $translate.instant("COMMON.TENANT") }),
192
								showCloseButton: true,
193
							});
194
							$scope.$emit('handleEmitTenantChanged');
195
						} else {
196
							toaster.pop({
197
								type: "error",
198
								title: $translate.instant("TOASTER.ERROR_DELETE_BODY", { template: $translate.instant("COMMON.TENANT") }),
199
								body: $translate.instant(response.data.description),
200
								showCloseButton: true,
201
							});
202
						}
203
					});
204
				}
205
			});
206
	};
207
208
	$scope.exportTenant = function(tenant) {
209
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
210
		TenantService.exportTenant(tenant, headers, function(response) {
211
			if (angular.isDefined(response.status) && response.status === 200) {
212
				$scope.exportdata = JSON.stringify(response.data);
213
				var modalInstance = $uibModal.open({
214
					windowClass: "animated fadeIn",
215
					templateUrl: 'views/common/export.html',
216
					controller: 'ModalExportCtrl',
217
					resolve: {
218
						params: function() {
219
							return {
220
								exportdata: angular.copy($scope.exportdata)
221
							};
222
						}
223
					}
224
				});
225
				modalInstance.result.then(function() {
226
					//do nothing;
227
				}, function() {
228
					//do nothing;
229
				});
230
				$rootScope.modalInstance = modalInstance;
231
			} else {
232
				$scope.exportdata = null;
233
			}
234
		});
235
	};
236
237
	$scope.cloneTenant = function(tenant){
238
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
239
		TenantService.cloneTenant(tenant, headers, function(response) {
240
			if (angular.isDefined(response.status) && response.status === 201) {
241
				toaster.pop({
242
					type: "success",
243
					title: $translate.instant("TOASTER.SUCCESS_TITLE"),
244
					body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.TENANT")}),
245
					showCloseButton: true,
246
				});
247
				$scope.$emit('handleEmitTenantChanged');
248
			}else {
249
				toaster.pop({
250
					type: "error",
251
					title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: $translate.instant("COMMON.TENANT")}),
252
					body: $translate.instant(response.data.description),
253
					showCloseButton: true,
254
				});
255
			}
256
		});
257
	};
258
259
	$scope.importTenant = function() {
260
		var modalInstance = $uibModal.open({
261
			templateUrl: 'views/common/import.html',
262
			controller: 'ModalImportCtrl',
263
			windowClass: "animated fadeIn",
264
			resolve: {
265
				params: function() {
266
					return {
267
					};
268
				}
269
			}
270
		});
271
		modalInstance.result.then(function(importdata) {
272
			let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
273
			TenantService.importTenant(importdata, headers, function(response) {
274
				if (angular.isDefined(response.status) && response.status === 201) {
275
					toaster.pop({
276
						type: "success",
277
						title: $translate.instant("TOASTER.SUCCESS_TITLE"),
278
						body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.TENANT")}),
279
						showCloseButton: true,
280
					});
281
					$scope.$emit('handleEmitTenantChanged');
282
				} else {
283
					toaster.pop({
284
						type: "error",
285
						title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.TENANT") }),
286
						body: $translate.instant(response.data.description),
287
						showCloseButton: true,
288
					});
289
				}
290
			});
291
		}, function() {
292
293
		});
294
		$rootScope.modalInstance = modalInstance;
295
	};
296
297
	$scope.getAllTenants();
298
	$scope.getAllTenantTypes();
299
	$scope.getAllCostCenters();
300
	$scope.getAllContacts();
301
	$scope.$on('handleBroadcastTenantChanged', function (event) {
302
		$scope.getAllTenants();
303
	});
304
});
305
306
app.controller('ModalAddTenantCtrl', function ($scope, $uibModalInstance, params) {
307
308
	$scope.operation = "SETTING.ADD_TENANT";
309
	$scope.tenanttypes = params.tenanttypes;
310
	$scope.costcenters = params.costcenters;
311
	$scope.contacts = params.contacts;
312
$scope.tenant = {
313
	lease_start_datetime: moment(),
314
	lease_end_datetime: null,
315
};
316
	$scope.dtOptions = {
317
		locale: {
318
			format: 'YYYY-MM-DD HH:mm:ss',
319
			applyLabel: "OK",
320
			cancelLabel: "Cancel",
321
		},
322
		timePicker: true,
323
		timePicker24Hour: true,
324
		timePickerIncrement: 15,
325
		singleDatePicker: true,
326
	};
327
	
328
	$scope.isLeaseDateInvalid = function () {
329
		if (!$scope.tenant.lease_start_datetime || !$scope.tenant.lease_end_datetime) {
330
			return false;
331
		}
332
		const start = moment($scope.tenant.lease_start_datetime);
333
		const end = moment($scope.tenant.lease_end_datetime);
334
		if (!start.isValid() || !end.isValid()) {
335
			return false;
336
		}
337
		return end.isSameOrBefore(start);
338
	};
339
340
	$scope.ok = function () {
341
		$scope.tenant.lease_start_datetime = moment($scope.tenant.lease_start_datetime).format().slice(0, 19);
342
		$scope.tenant.lease_end_datetime = moment($scope.tenant.lease_end_datetime).format().slice(0, 19);
343
		$uibModalInstance.close($scope.tenant);
344
	};
345
346
	$scope.cancel = function () {
347
		$uibModalInstance.dismiss('cancel');
348
	};
349
});
350
351
app.controller('ModalEditTenantCtrl', function ($scope, $uibModalInstance, params) {
352
	$scope.operation = "SETTING.EDIT_TENANT";
353
	$scope.tenant = params.tenant;
354
	$scope.tenanttypes = params.tenanttypes;
355
	$scope.costcenters = params.costcenters;
356
	$scope.contacts = params.contacts;
357
	$scope.dtOptions = {
358
		locale: {
359
			format: 'YYYY-MM-DD HH:mm:ss',
360
			applyLabel: "OK",
361
			cancelLabel: "Cancel",
362
		},
363
		timePicker: true,
364
		timePicker24Hour: true,
365
		timePickerIncrement: 15,
366
		singleDatePicker: true,
367
	};
368
	$scope.isLeaseDateInvalid = function () {
369
		if (!$scope.tenant.lease_start_datetime || !$scope.tenant.lease_end_datetime) {
370
			return false;
371
		}
372
		const start = moment($scope.tenant.lease_start_datetime);
373
		const end = moment($scope.tenant.lease_end_datetime);
374
		if (!start.isValid() || !end.isValid()) {
375
			return false;
376
		}
377
		return end.isSameOrBefore(start);
378
	};
379
	
380
	$scope.ok = function () {
381
		$scope.tenant.lease_start_datetime = moment($scope.tenant.lease_start_datetime).format().slice(0, 19);
382
		$scope.tenant.lease_end_datetime = moment($scope.tenant.lease_end_datetime).format().slice(0, 19);
383
		$uibModalInstance.close($scope.tenant);
384
	};
385
386
	$scope.cancel = function () {
387
		$uibModalInstance.dismiss('cancel');
388
	};
389
});
390