"use strict"; (function () { 'use strict'; angular.module('personModule') .controller('PersonController', ['$scope', 'personModel', 'userModel', '$state', '$filter', 'attachmentService', 'screenConfigurationModel', 'events', 'ticketActionService', '$q', 'chatModel', '$modal', 'openMailClientService', 'configurationModel', 'metadataModel', 'systemAlertService', '$timeout', '$window', '$location', function ($scope, personModel, userModel, $state, $filter, attachmentService, screenConfigurationModel, events, ticketActionService, $q, chatModel, $modal, openMailClientService, configurationModel, metadataModel, systemAlertService, $timeout, $window, $location) { var person_id, screenName = screenConfigurationModel.getScreenNameByTicketType(EntityVO.TYPE_PERSON); $scope.isServiceBrokerEnabled = configurationModel.isServerApplicationEnabled(EntityVO.TYPE_SBEREQUEST); $scope.$on('personDataUpdated', handlePersonDataUpdated); var queryMinimalView; $scope.restrictPersonProfileResultForNoOfDays = 0; /** * Public functions * */ $scope.follow = function () { var userLoginId = userModel.userFullData.loginId; return personModel.followPerson(userLoginId, $scope.person.loginId); }; $scope.unfollow = function () { var userLoginId = userModel.userFullData.loginId; return personModel.unfollowPerson(userLoginId, $scope.person.loginId); }; $scope.toggleFollowingFlag = function () { var toggleFollowingFunction = $scope.person.following ? $scope.unfollow : $scope.follow; toggleFollowingFunction($scope.person).then(function () { $scope.person.following = !$scope.person.following; }); }; $scope.share = function ($event) { ticketActionService.showShareDialog($scope.person).result.then(function () { $event.currentTarget.focus(); }, function () { $event.currentTarget.focus(); }); }; $scope.emailPerson = function () { var subject = userModel.userFullData.fullName + $filter('i18n')('shareBlade.emailSubject'); openMailClientService.openMailClient($scope.person.email, subject, ''); }; $scope.showPrintDialog = function ($event) { var ticketPrintDialog = $modal.open({ templateUrl: 'views/common/print-action-blade.html', controller: 'PrintController', windowClass: 'action-blade', size: 'extra-lg', resolve: { params: function () { return { entity: $scope.person, feed: $scope.person.feed }; } } }); ticketPrintDialog.result.finally(function () { $event.currentTarget.focus(); }); }; /** * Set active task section (Home / Workorders / View All) * @param {String} sectionId */ $scope.setTaskSection = function (sectionId) { $scope.activeTaskSection = sectionId; }; /** * Entry point */ $scope.init = function (personId) { person_id = personId; $scope.$on(events.TOGGLE_EDIT_MODE, handleToggleEditMode); $scope.$on(events.EDIT_COMPLETE, handleEditComplete); if (!personId) { return; } queryMinimalView = 1; $scope.editHeader = false; $scope.isCollapsed = true; $scope.activeTaskSection = 'assets'; $scope.state = { isPersonDataLoading: true }; $scope.chatModel = chatModel; $q.all([personModel.getPersonDetailsByID(personId), screenConfigurationModel.loadScreenConfigurationAndCustomFieldLabels(screenName, EntityVO.TYPE_PERSON)]).then(onPersonDetailsLoaded).finally(onDataLoadComplete); }; $scope.showProfileDetails = function (item, type, $event) { if ('href' in $event.target) { if ($scope.selectedChat && $event.target.href !== 'javascript:void(0)') { $event.target.target = '_blank'; } return; } switch (type) { case EntityVO.TYPE_TICKET: if ($scope.selectedChat) { var url = $state.href(item.type, { id: item.id }); $window.open(url, '_blank'); } else { $state.go(item.type, { id: item.id }); } break; case EntityVO.TYPE_ASSET: if ($scope.selectedChat) { var url = $state.href(type, { assetId: item.reconciliationId, assetClassId: item.classId }); $window.open(url, '_blank'); } else { $state.go(type, { assetId: item.reconciliationId, assetClassId: item.classId }); } break; case EntityVO.TYPE_KNOWLEDGE: if ($scope.selectedChat) { var url = $state.href(type, { id: item.uuid }); $window.open(url, '_blank'); } else { $state.go(type, { id: item.uuid }); } break; } }; $scope.handleFileChange = function (fileInput) { //TODO: create ng-upload directive, which will be handling file inputs, to get rid of input class hardcoding attachmentService.uploadProfileThumbnail($scope.person.loginId, EntityVO.TYPE_PERSON, fileInput, '') .then(function (response) { if (response.thumbnail) { $scope.person.thumbnail = response.thumbnail; if ($scope.person.loginId === userModel.userFullData.loginId) { userModel.userFullData.thumbnail = response.thumbnail; } } }, function (error) { systemAlertService.error({ text: $filter('i18n')('attachment.fileExtension.error') }); }); }; function setRelateToChatFlag() { var showRelateToChat, showUnRelateToChat; if (!$scope.selectedChat) { return; } if ($scope.selectedChat && $scope.selectedChat.status !== 'Assigned') { return; } $scope.setDeafultStyleForTickets = true; if ($scope.selectedChat && $scope.selectedChat.status === 'Assigned') { showRelateToChat = $scope.selectedChat.ticket && $scope.selectedChat.ticket.ticketId ? false : true; showUnRelateToChat = $scope.selectedChat.ticket && $scope.selectedChat.ticket.ticketId ? true : false; if ($scope.setDeafultStyleForTickets && (showRelateToChat || showUnRelateToChat)) { $scope.setDeafultStyleForTickets = false; } } } /** * Handler for toggling edit mode * * @param event * @param editableContentSectionId */ function handleToggleEditMode(event, editableContentSectionId) { $scope.activeEditableSectionId = editableContentSectionId; if (editableContentSectionId === 'ticket-header') { $scope.editHeader = true; } } function handleEditComplete(event, editableContentSectionId) { $scope.activeEditableSectionId = null; if (editableContentSectionId === 'ticket-header') { $scope.editHeader = false; } } function handlePersonDataUpdated(event, reloadPerson) { $timeout(function () { $scope.person = personModel.personDetails; if (reloadPerson && !_.isEmpty($scope.person)) { onPersonDetailsLoaded(); onDataLoadComplete(); } }, 0); } function isLiveChatConsolePath() { return $location.path().startsWith('/live-chat/console'); } $scope.editDisabledFor = function (sectionId) { return $scope.activeEditableSectionId && $scope.activeEditableSectionId !== sectionId; }; /** * Handle "Load More" event */ $scope.loadMoreRequestedByTickets = function () { if (!$scope.state.isPersonDataLoading && !$scope.state.isMoreRequestedByTicketsLoading && $scope.personOpenTicketsChunkAvailable) { $scope.state.isMoreRequestedByTicketsLoading = true; personModel.getOpenTickets(personModel.personDetails.id, personModel.personOpenTickets.ticketList.length, !!$scope.person.isSupportStaff, queryMinimalView, $scope.restrictPersonProfileResultForNoOfDays).finally(function () { $scope.state.isMoreRequestedByTicketsLoading = false; $scope.personOpenTickets = personModel.personOpenTickets.ticketList; $scope.personOpenTicketsTotalMatches = personModel.personOpenTickets.totalMatches; $scope.personOpenTicketsChunkAvailable = personModel.personOpenTickets.exceedsChunkSize; setRelateToChatFlag(); }); } }; $scope.requestedByCount = function () { if (window.isRtl) { var localeString = " (" + ($scope.personOpenTicketsTotalMatches || 0) + ") " + $scope.person.firstName + " " + ($filter('i18n')('person.details.tickets.requestedBy')); return localeString; } return ($filter('i18n')('person.details.tickets.requestedBy')) + " " + $scope.person.firstName + " (" + ($scope.personOpenTicketsTotalMatches || 0) + ")"; }; $scope.loadMoreAllTickets = function () { if (!$scope.state.isPersonDataLoading && !$scope.state.isMoreAllTicketsLoading && $scope.personAllTicketsChunkAvailable) { $scope.state.isMoreAllTicketsLoading = true; personModel.getAllTickets(personModel.personDetails.id, personModel.personAllTickets.ticketList.length, false, queryMinimalView, $scope.restrictPersonProfileResultForNoOfDays).finally(function () { $scope.state.isMoreAllTicketsLoading = false; $scope.personAllTickets = personModel.personAllTickets.ticketList; $scope.personAllTicketsTotalMatches = personModel.personAllTickets.totalMatches; $scope.personAllTicketsChunkAvailable = personModel.personAllTickets.exceedsChunkSize; setRelateToChatFlag(); }); } }; $scope.loadMoreAssignedTickets = function () { if (!$scope.state.isPersonDataLoading && !$scope.state.isPersonMoreTicketsLoading && personModel.personAssignedTickets.totalMatches > personModel.personAssignedTickets.ticketList.length) { $scope.state.isPersonMoreTicketsLoading = true; personModel.getAssignedTickets(personModel.personDetails.id, personModel.personAssignedTickets.ticketList.length, true, queryMinimalView, $scope.restrictPersonProfileResultForNoOfDays).finally(function () { $scope.state.isPersonMoreTicketsLoading = false; $scope.personAssignedTickets = personModel.personAssignedTickets.ticketList; $scope.totalMatches = personModel.personAssignedTickets.totalMatches; setRelateToChatFlag(); }); } }; $scope.getMoreOpenServiceBrokerTickets = function () { $scope.state.isPersonMoreOpenSBETicketsLoading = true; personModel.getOpenServiceBrokerTickets(personModel.personDetails.id, personModel.personOpenServiceBrokerTickets.ticketList.length, $scope.restrictPersonProfileResultForNoOfDays).finally(function () { $scope.state.isPersonMoreOpenSBETicketsLoading = false; $scope.personOpenSBETickets = personModel.personOpenServiceBrokerTickets.ticketList; $scope.personOpenSBETicketsTotalMatches = personModel.personOpenServiceBrokerTickets.totalMatches; setRelateToChatFlag(); }); }; $scope.isURL = function (str) { var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; return regexp.test(str); }; function onPersonDetailsLoaded() { $scope.restrictPersonProfileResultForNoOfDays = configurationModel.restrictPersonProfileResultForNoOfDays; $scope.person = personModel.personDetails; $scope.$emit(events.PERSON_DATA_LOADED, $scope.person); $scope.disableClick = (_.isUndefined($scope.person.email) || $scope.person.email === "") ? true : false; $scope.setTarget = $scope.selectedChat ? "_blank" : null; $scope.personSupportGroups = personModel.personDetails.supportGroups; $scope.personAvailability = $scope.person.availability; if (!$scope.isFullVersion && $scope.person.accessMappings) { $scope.person.accessMappings.detailsEditAllowed = false; } if ($scope.person.isSupportStaff) { personModel.getAssignedTickets($scope.person.loginId, 0, true, queryMinimalView, $scope.restrictPersonProfileResultForNoOfDays).then(function () { $scope.personAssignedTickets = personModel.personAssignedTickets.ticketList; $scope.personAssignedTicketsTotalMatches = personModel.personAssignedTickets.totalMatches; }); personModel.getKnowledgeArticles($scope.person.loginId).then(function () { $scope.personKnowledgeArticles = personModel.personKnowledgeArticles; $scope.personKnowledgeArticlesTotalMatches = personModel.personKnowledgeArticles.totalMatches; }); } else { personModel.getAllTickets($scope.person.loginId, 0, false, queryMinimalView, $scope.restrictPersonProfileResultForNoOfDays).then(function () { //in person-profile however in smart-recorder we are showing and the code for that is different. $scope.personAllTickets = personModel.personAllTickets.ticketList; $scope.personAllTicketsTotalMatches = personModel.personAllTickets.totalMatches; $scope.personAllTicketsChunkAvailable = personModel.personAllTickets.exceedsChunkSize; }); } if ($scope.isServiceBrokerEnabled) { personModel.getOpenServiceBrokerTickets($scope.person.loginId, 0, $scope.restrictPersonProfileResultForNoOfDays).then(function () { $scope.personOpenSBETickets = personModel.personOpenServiceBrokerTickets.ticketList; $scope.personOpenSBETicketsTotalMatches = personModel.personOpenServiceBrokerTickets.totalMatches; }); } } function onDataLoadComplete() { $scope.state.isPersonDataLoading = false; personModel.getServiceSummaryStats(person_id).then(function () { $scope.serviceSummary = personModel.serviceSummary; }); personModel.getPersonAssets(person_id, true).then(function () { $scope.personAssetList = personModel.personAssets; }); $q.all(metadataModel.getMetadataByType(EntityVO.TYPE_WORKORDER), metadataModel.getMetadataByType(EntityVO.TYPE_INCIDENT), metadataModel.getMetadataByType(EntityVO.TYPE_ASSET), metadataModel.getMetadataByType(EntityVO.TYPE_PROBLEM), metadataModel.getMetadataByType(EntityVO.TYPE_CHANGE), metadataModel.getMetadataByType(EntityVO.TYPE_TASK), metadataModel.getMetadataByType(EntityVO.TYPE_RELEASE), metadataModel.getMetadataByType(EntityVO.TYPE_KNOWLEDGE)).then(function () { $scope.restrictPersonProfileResultForNoOfDays = configurationModel.restrictPersonProfileResultForNoOfDays; personModel.getOpenTickets($scope.person.loginId, 0, !!$scope.person.isSupportStaff, queryMinimalView, $scope.restrictPersonProfileResultForNoOfDays).then(function () { $scope.personOpenTickets = personModel.personOpenTickets.ticketList; $scope.personOpenTicketsTotalMatches = personModel.personOpenTickets.totalMatches; $scope.personOpenTicketsChunkAvailable = personModel.personOpenTickets.exceedsChunkSize; setRelateToChatFlag(); }); }); } $scope.$on(events.EDIT_COMPLETE, handleEditComplete); }]); })();