myems-admin/app/controllers/settings/energystoragecontainer/energystoragecontainer.controller.js

Summary

Maintainability
F
5 days
Test Coverage
'use strict';

app.controller('EnergyStorageContainerController', function(
    $scope,
    $rootScope,
    $window,
    $translate,
    $uibModal,
    CostCenterService,
    ContactService,
    SVGService,
    EnergyStorageContainerService,
    toaster,
    SweetAlert) {
    $scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
    $scope.getAllCostCenters = function() {
        let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
        CostCenterService.getAllCostCenters(headers, function (response) {
            if (angular.isDefined(response.status) && response.status === 200) {
                $scope.costcenters = response.data;
            } else {
                $scope.costcenters = [];
            }
        });
    };

    $scope.getAllContacts = function() {
        let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
        ContactService.getAllContacts(headers, function (response) {
            if (angular.isDefined(response.status) && response.status === 200) {
                $scope.contacts = response.data;
            } else {
                $scope.contacts = [];
            }
        });
    };

    $scope.getAllSVGs = function() {
        let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token, "Quickmode": 'true'  };
        SVGService.getAllSVGs(headers, function (response) {
            if (angular.isDefined(response.status) && response.status === 200) {
                $scope.svgs = response.data;
            } else {
                $scope.svgs = [];
            }
        });
    };

    $scope.getAllEnergyStorageContainers = function() {
        let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
        EnergyStorageContainerService.getAllEnergyStorageContainers(headers, function (response) {
            if (angular.isDefined(response.status) && response.status === 200) {
                $scope.energystoragecontainers = response.data;
            } else {
                $scope.energystoragecontainers = [];
            }
        });
    };

    $scope.addEnergyStorageContainer = function() {
        var modalInstance = $uibModal.open({
            templateUrl: 'views/settings/energystoragecontainer/energystoragecontainer.model.html',
            controller: 'ModalAddEnergyStorageContainerCtrl',
            windowClass: "animated fadeIn",
            resolve: {
                params: function() {
                    return {
                        costcenters: angular.copy($scope.costcenters),
                        contacts: angular.copy($scope.contacts),
                        svgs: angular.copy($scope.svgs),
                    };
                }
            }
        });
        modalInstance.result.then(function(energystoragecontainer) {
            energystoragecontainer.cost_center_id = energystoragecontainer.cost_center.id;
            energystoragecontainer.contact_id = energystoragecontainer.contact.id;
            if (angular.isDefined(energystoragecontainer.svg) && angular.isDefined(energystoragecontainer.svg.id)) {
                energystoragecontainer.svg_id = energystoragecontainer.svg.id;
            }
            let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
            EnergyStorageContainerService.addEnergyStorageContainer(energystoragecontainer, headers, function(response) {
                if (angular.isDefined(response.status) && response.status === 201) {
                    toaster.pop({
                        type: "success",
                        title: $translate.instant("TOASTER.SUCCESS_TITLE"),
                        body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: $translate.instant("COMMON.ENERGY_STORAGE_CONTAINER")}),
                        showCloseButton: true,
                    });
                    $scope.$emit('handleEmitEnergyStorageContainerChanged');
                } else {
                    toaster.pop({
                        type: "error",
                        title: $translate.instant("TOASTER.ERROR_ADD_BODY", { template: $translate.instant("COMMON.ENERGY_STORAGE_CONTAINER") }),
                        body: $translate.instant(response.data.description),
                        showCloseButton: true,
                    });
                }
            });
        }, function() {

        });
        $rootScope.modalInstance = modalInstance;
    };

    $scope.editEnergyStorageContainer = function(energystoragecontainer) {
        var modalInstance = $uibModal.open({
            windowClass: "animated fadeIn",
            templateUrl: 'views/settings/energystoragecontainer/energystoragecontainer.model.html',
            controller: 'ModalEditEnergyStorageContainerCtrl',
            resolve: {
                params: function() {
                    return {
                        energystoragecontainer: angular.copy(energystoragecontainer),
                        costcenters:angular.copy($scope.costcenters),
                        contacts:angular.copy($scope.contacts),
                        svgs: angular.copy($scope.svgs),
                    };
                }
            }
        });

        modalInstance.result.then(function(modifiedEnergyStorageContainer) {
            modifiedEnergyStorageContainer.cost_center_id=modifiedEnergyStorageContainer.cost_center.id;
            modifiedEnergyStorageContainer.contact_id=modifiedEnergyStorageContainer.contact.id;
            if (angular.isDefined(modifiedEnergyStorageContainer.svg) && angular.isDefined(modifiedEnergyStorageContainer.svg.id)) {
                modifiedEnergyStorageContainer.svg_id = modifiedEnergyStorageContainer.svg.id;
            }

            let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
            EnergyStorageContainerService.editEnergyStorageContainer(modifiedEnergyStorageContainer, headers, function(response) {
                if (angular.isDefined(response.status) && response.status === 200) {
                    toaster.pop({
                        type: "success",
                        title: $translate.instant("TOASTER.SUCCESS_TITLE"),
                        body: $translate.instant("TOASTER.SUCCESS_UPDATE_BODY", {template: $translate.instant("COMMON.ENERGY_STORAGE_CONTAINER")}),
                        showCloseButton: true,
                    });
                    $scope.$emit('handleEmitEnergyStorageContainerChanged');
                } else {
                    toaster.pop({
                        type: "error",
                        title: $translate.instant("TOASTER.ERROR_UPDATE_BODY", {template: $translate.instant("COMMON.ENERGY_STORAGE_CONTAINER")}),
                        body: $translate.instant(response.data.description),
                        showCloseButton: true,
                    });
                }
            });
        }, function() {
            //do nothing;
        });
        $rootScope.modalInstance = modalInstance;
    };

    $scope.deleteEnergyStorageContainer=function(energystoragecontainer){
        SweetAlert.swal({
                title: $translate.instant("SWEET.TITLE"),
                text: $translate.instant("SWEET.TEXT"),
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
                cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
                closeOnConfirm: true,
                closeOnCancel: true },
            function (isConfirm) {
                if (isConfirm) {
                    let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
                    EnergyStorageContainerService.deleteEnergyStorageContainer(energystoragecontainer, headers, function(response) {
                        if (angular.isDefined(response.status) && response.status === 204) {
                            toaster.pop({
                                type: "success",
                                title: $translate.instant("TOASTER.SUCCESS_TITLE"),
                                body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("COMMON.ENERGY_STORAGE_CONTAINER")}),
                                showCloseButton: true,
                            });
                            $scope.$emit('handleEmitEnergyStorageContainerChanged');
                        }else {
                            toaster.pop({
                                type: "error",
                                title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("COMMON.ENERGY_STORAGE_CONTAINER")}),
                                body: $translate.instant(response.data.description),
                                showCloseButton: true,
                            });
                        }
                    });
                }
            });
    };
    $scope.getAllEnergyStorageContainers();
    $scope.getAllCostCenters();
    $scope.getAllContacts();
    $scope.getAllSVGs();
    $scope.$on('handleBroadcastEnergyStorageContainerChanged', function(event) {
          $scope.getAllEnergyStorageContainers();
    });
});

app.controller('ModalAddEnergyStorageContainerCtrl', function($scope, $uibModalInstance,params) {

    $scope.operation = "SETTING.ADD_ENERGY_STORAGE_CONTAINER";
    $scope.costcenters=params.costcenters;
    $scope.contacts=params.contacts;
    $scope.svgs=params.svgs;
    $scope.ok = function() {
        $uibModalInstance.close($scope.energystoragecontainer);
    };

    $scope.cancel = function() {
        $uibModalInstance.dismiss('cancel');
    };
});

app.controller('ModalEditEnergyStorageContainerCtrl', function($scope, $uibModalInstance, params) {
    $scope.operation = "SETTING.EDIT_ENERGY_STORAGE_CONTAINER";
    $scope.energystoragecontainer = params.energystoragecontainer;
    $scope.costcenters=params.costcenters;
    $scope.contacts=params.contacts;
    $scope.svgs=params.svgs;
    $scope.ok = function() {
        $uibModalInstance.close($scope.energystoragecontainer);
    };

    $scope.cancel = function() {
        $uibModalInstance.dismiss('cancel');
    };
});