<commit_before>﻿(function (ng, app) {
    app.config([
        "$stateProvider", function ($stateProvider) {
            $stateProvider.state("organization.structure", {
                url: "/structure",
                templateUrl: "app/components/org/structure/org-structure.view.html",
                controller: "org.StructureCtrl",
                resolve: {
                    orgUnits: [
                        "$http", "user", function ($http: ng.IHttpService, user) {
                            return $http.get<Kitos.API.Models.IApiWrapper<any>>("api/organizationunit?organization=" + user.currentOrganizationId).then((result) => {
                                return result.data.response;
                            });
                        }
                    ],
                    localOrgUnitRoles: ['$http', function ($http) {
                        return $http.get("odata/LocalOrganizationUnitRoles?$filter=IsLocallyAvailable eq true or IsObligatory&$orderby=Priority desc")
                            .then(function (result) {
                                return result.data.value;
                            });
                    }],
                    orgUnitRoles: ['$http', function ($http) {
                        return $http.get("odata/OrganizationUnitRoles")
                            .then(function (result) {
                                return result.data.value;
                            });
                    }],
                    user: [
                        "userService", function (userService) {
                            return userService.getUser();
                        }
                    ],
                    hasWriteAccess: [
                        '$http', '$stateParams', 'user', function ($http, $stateParams, user) {
                            return $http.get('api/Organization/' + user.currentOrganizationId + "?hasWriteAccess=true&organizationId=" + user.currentOrganizationId)
                                .then(function (result) {
                                    return result.data.response;
                                });
                        }
                    ]
                }
            });
        }
    ]);

    app.controller("org.StructureCtrl", [
        "$scope", "$http", "$q", "$filter", "$uibModal", "$state", "notify", "orgUnits", "localOrgUnitRoles", "orgUnitRoles", "user", "hasWriteAccess",
        function ($scope, $http: ng.IHttpService, $q, $filter, $modal, $state, notify, orgUnits, localOrgUnitRoles, orgUnitRoles, user, hasWriteAccess) {
            $scope.orgId = user.currentOrganizationId;
            $scope.pagination = {
                skip: 0,
                take: 50
            };
            $scope.rightsPagination = {
                skip: 0,
                take: 15
            };

            //cache
            var orgs = [];

            //flattened map of all loaded orgUnits
            $scope.orgUnits = {};
            $scope.hasWriteAccess = hasWriteAccess;

            $scope.orgUnitRoles = orgUnitRoles;
            $scope.activeOrgRoles = localOrgUnitRoles;
            $scope.orgRoles = {};
            _.each(localOrgUnitRoles, function (orgRole: { Id }) {
                $scope.orgRoles[orgRole.Id] = orgRole;
            });


            function flattenAndSave(orgUnit, inheritWriteAccess, parentOrgunit) {
                orgUnit.parent = parentOrgunit;

                //restore previously saved settings
                if ($scope.orgUnits[orgUnit.id]) {
                    var old = $scope.orgUnits[orgUnit.id];
                    orgUnit.isOpen = old.isOpen;
                }

                checkForDefaultUnit(orgUnit);

                $scope.orgUnits[orgUnit.id] = orgUnit;

                if (!inheritWriteAccess) {
                    $http.get<Kitos.API.Models.IApiWrapper<any>>("api/organizationUnit/" + orgUnit.id + "?hasWriteAccess&organizationId=" + user.currentOrganizationId).then((result) => {
                        orgUnit.hasWriteAccess = result.data.response;

                        _.each(orgUnit.children, function (u) {
                            flattenAndSave(u, result.data.response, orgUnit);
                        });

                    });
                } else {
                    orgUnit.hasWriteAccess = true;

                    _.each(orgUnit.children, function (u) {
                        return flattenAndSave(u, true, orgUnit);
                    });
                }
            }

            function checkForDefaultUnit(unit) {
                if (!user.currentOrganizationUnitId) return;

                if (!unit || unit.id !== user.currentOrganizationUnitId) return;

                open(unit);
                $scope.chooseOrgUnit(unit);

                function open(u) {
                    u.isOpen = true;
                    if (u.parent) open(u.parent);
                }
            }

            function loadUnits() {
                var rootNode = orgUnits;
                $scope.nodes = [rootNode];

                flattenAndSave(rootNode, false, null);
            }

            $scope.showChildren = false;

            $scope.chosenOrgUnit = null;

            $scope.chooseOrgUnit = function (node, event) {
                if (event) {
                    var isDiv = angular.element(event.target)[0].tagName === "DIV";
                    if (!isDiv) {
                        return;
                    }
                }
                if ($scope.chosenOrgUnit === node) return;

                // reset state between selecting the current organization
                $scope.showChildren = false;

                if ($scope.chosenOrgUnit === node) return;

                //get organization related to the org unit
                if (!node.organization) {
                    //try get from cache
                    if (orgs[node.organizationId]) {
                        node.organization = orgs[node.organizationId];
                    } else {
                        //else get from server
                        $http.get<Kitos.API.Models.IApiWrapper<any>>("api/organization/" + node.organizationId).then((result) => {
                            node.organization = result.data.response;

                            //save to cache
                            orgs[node.organizationId] = result.data.response;
                        });
                    }
                }

                // reset pagination
                $scope.rightsPagination = {
                    skip: 0,
                    take: 15
                };

                loadRights(node);
                loadTasks();
            };

            function loadRights(node) {
                //get org rights on the org unit and subtree
                $http.get<Kitos.API.Models.IApiWrapper<any>>("api/organizationUnitRight/" + node.id + "?paged&take=" + $scope.rightsPagination.take + "&skip=" + $scope.rightsPagination.skip).then((result) => {
                    var paginationHeader = JSON.parse(result.headers("X-Pagination"));
                    $scope.totalRightsCount = paginationHeader.TotalCount;
                    node.orgRights = result.data.response;

                    _.each(node.orgRights, function (right: { userForSelect; roleForSelect; user; roleId; show; objectId; }) {
                        right.userForSelect = { id: right.user.id, text: right.user.fullName };
                        right.roleForSelect = right.roleId;
                        right.show = belongsToChosenNode(node, right);
                    });
                });

                $scope.chosenOrgUnit = node;
            }

            // don't show users in subunits if showChildren is false
            $scope.toggleChildren = function () {
                const node = $scope.chosenOrgUnit;
                _.each(node.orgRights,
                    function (right: { show; }) {
                        right.show = $scope.showChildren || belongsToChosenNode(node, right);
                    });
            };

            function belongsToChosenNode(node, right) {
                return node.id === right.objectId;
            }

            $scope.$watch("selectedUser", function () {
                $scope.submitRight();
            });

            /* the role of "medarbejder" */
            function getDefaultNewRole() {
                return 3;
            }

            $scope.newRole = getDefaultNewRole();

            $scope.submitRight = function () {
                if (!$scope.selectedUser || !$scope.newRole) return;

                var oId = $scope.chosenOrgUnit.id;
                var rId = parseInt($scope.newRole);
                var uId = $scope.selectedUser.id;

                if (!oId || !rId || !uId) return;<commit_msg>OS2KITOS-989 Fejl i visningen af organisationsroller under organisation<commit_after>﻿(function (ng, app) {
    app.config([
        "$stateProvider", function ($stateProvider) {
            $stateProvider.state("organization.structure", {
                url: "/structure",
                templateUrl: "app/components/org/structure/org-structure.view.html",
                controller: "org.StructureCtrl",
                resolve: {
                    orgUnits: [
                        "$http", "user", function ($http: ng.IHttpService, user) {
                            return $http.get<Kitos.API.Models.IApiWrapper<any>>("api/organizationunit?organization=" + user.currentOrganizationId).then((result) => {
                                return result.data.response;
                            });
                        }
                    ],
                    localOrgUnitRoles: ['$http', function ($http) {
                        return $http.get("odata/LocalOrganizationUnitRoles?$filter=IsLocallyAvailable eq true or IsObligatory&$orderby=Priority desc")
                            .then(function (result) {
                                return result.data.value;
                            });
                    }],
                    orgUnitRoles: ['$http', function ($http) {
                        return $http.get("odata/OrganizationUnitRoles")
                            .then(function (result) {
                                return result.data.value;
                            });
                    }],
                    user: [
                        "userService", function (userService) {
                            return userService.getUser();
                        }
                    ],
                    hasWriteAccess: [
                        '$http', '$stateParams', 'user', function ($http, $stateParams, user) {
                            return $http.get('api/Organization/' + user.currentOrganizationId + "?hasWriteAccess=true&organizationId=" + user.currentOrganizationId)
                                .then(function (result) {
                                    return result.data.response;
                                });
                        }
                    ]
                }
            });
        }
    ]);

    app.controller("org.StructureCtrl", [
        "$scope", "$http", "$q", "$filter", "$uibModal", "$state", "notify", "orgUnits", "localOrgUnitRoles", "orgUnitRoles", "user", "hasWriteAccess",
        function ($scope, $http: ng.IHttpService, $q, $filter, $modal, $state, notify, orgUnits, localOrgUnitRoles, orgUnitRoles, user, hasWriteAccess) {
            $scope.orgId = user.currentOrganizationId;
            $scope.pagination = {
                skip: 0,
                take: 50
            };
            $scope.rightsPagination = {
                skip: 0,
                take: 15
            };

            //cache
            var orgs = [];

            //flattened map of all loaded orgUnits
            $scope.orgUnits = {};
            $scope.hasWriteAccess = hasWriteAccess;

            $scope.orgUnitRoles = orgUnitRoles;
            $scope.activeOrgRoles = localOrgUnitRoles;
            $scope.orgRoles = {};
            _.each(localOrgUnitRoles, function (orgRole: { Id }) {
                $scope.orgRoles[orgRole.Id] = orgRole;
            });


            function flattenAndSave(orgUnit, inheritWriteAccess, parentOrgunit) {
                orgUnit.parent = parentOrgunit;

                //restore previously saved settings
                if ($scope.orgUnits[orgUnit.id]) {
                    var old = $scope.orgUnits[orgUnit.id];
                    orgUnit.isOpen = old.isOpen;
                }

                checkForDefaultUnit(orgUnit);

                $scope.orgUnits[orgUnit.id] = orgUnit;

                if (!inheritWriteAccess) {
                    $http.get<Kitos.API.Models.IApiWrapper<any>>("api/organizationUnit/" + orgUnit.id + "?hasWriteAccess&organizationId=" + user.currentOrganizationId).then((result) => {
                        orgUnit.hasWriteAccess = result.data.response;

                        _.each(orgUnit.children, function (u) {
                            flattenAndSave(u, result.data.response, orgUnit);
                        });

                    });
                } else {
                    orgUnit.hasWriteAccess = true;

                    _.each(orgUnit.children, function (u) {
                        return flattenAndSave(u, true, orgUnit);
                    });
                }
            }

            function checkForDefaultUnit(unit) {
                if (!user.currentOrganizationUnitId) return;

                if (!unit || unit.id !== user.currentOrganizationUnitId) return;

                open(unit);
                $scope.chooseOrgUnit(unit);

                function open(u) {
                    u.isOpen = true;
                    if (u.parent) open(u.parent);
                }
            }

            function loadUnits() {
                var rootNode = orgUnits;
                $scope.nodes = [rootNode];

                flattenAndSave(rootNode, false, null);
            }

            $scope.showChildren = false;

            $scope.chosenOrgUnit = null;

            $scope.chooseOrgUnit = function (node, event) {
                if (event) {
                    var isDiv = angular.element(event.target)[0].tagName === "DIV";
                    if (!isDiv) {
                        return;
                    }
                }
                if ($scope.chosenOrgUnit === node) return;

                // reset state between selecting the current organization
                $scope.showChildren = false;

                if ($scope.chosenOrgUnit === node) return;

                //get organization related to the org unit
                if (!node.organization) {
                    //try get from cache
                    if (orgs[node.organizationId]) {
                        node.organization = orgs[node.organizationId];
                    } else {
                        //else get from server
                        $http.get<Kitos.API.Models.IApiWrapper<any>>("api/organization/" + node.organizationId).then((result) => {
                            node.organization = result.data.response;

                            //save to cache
                            orgs[node.organizationId] = result.data.response;
                        });
                    }
                }

                // reset pagination
                $scope.rightsPagination = {
                    skip: 0,
                    take: 15
                };

                loadRights(node);
                loadTasks();
            };

            function loadRights(node) {
                //get org rights on the org unit and subtree
                $http.get<Kitos.API.Models.IApiWrapper<any>>("api/organizationUnitRight/" + node.id + "?paged&take=" + $scope.rightsPagination.take + "&skip=" + $scope.rightsPagination.skip).then((result) => {
                    var paginationHeader = JSON.parse(result.headers("X-Pagination"));
                    $scope.totalRightsCountCopy = paginationHeader.TotalCount;
                    node.orgRights = result.data.response;

                    var count = 0;
                    _.each(node.orgRights, function (right: { userForSelect; roleForSelect; user; roleId; show; objectId; }) {
                        right.userForSelect = { id: right.user.id, text: right.user.fullName };
                        right.roleForSelect = right.roleId;
                        right.show = $scope.showChildren || belongsToChosenNode(node, right);
                        if (right.show)
                            count++;
                    });
                    
                    $scope.totalRightsCount = ($scope.showChildren) ? $scope.totalRightsCountCopy : count;
                });

                $scope.chosenOrgUnit = node;
            }

            // don't show users in subunits if showChildren is false
            $scope.toggleChildren = function () {
                const node = $scope.chosenOrgUnit;
                var count = 0;
                _.each(node.orgRights,
                    function (right: { show; }) {
                        right.show = $scope.showChildren || belongsToChosenNode(node, right);
                        if (right.show)
                            count++;
                    });

                $scope.totalRightsCount = ($scope.showChildren) ? $scope.totalRightsCountCopy : count;

                if (!$scope.showChildren && $scope.totalRightsCount === 0 && $scope.rightsPagination.skip > 0) {
                    $scope.rightsPagination.skip = 0;
                    loadRights($scope.chosenOrgUnit);
                }
            };

            function belongsToChosenNode(node, right) {
                return node.id === right.objectId;
            }

            $scope.$watch("selectedUser", function () {
                $scope.submitRight();
            });

            /* the role of "medarbejder" */
            function getDefaultNewRole() {
                return 3;
            }

            $scope.newRole = getDefaultNewRole();

            $scope.submitRight = function () {
                if (!$scope.selectedUser || !$scope.newRole) return;

                var oId = $scope.chosenOrgUnit.id;
                var rId = parseInt($scope.newRole);
                var uId = $scope.selectedUser.id;

                if (!oId || !rId || !uId) return;
{'fraction_non_alphanumeric': 0.1085955695174132, 'fraction_numerical': 0.0016790337431619997, 'mean_word_length': 1.2283369538981415, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4046553', 'n_tokens_mistral': 4756, 'n_tokens_neox': 4357, 'n_words': 816}