"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .service('ticketActionService', ['$modal', '$filter', '$q', '$timeout', '$state', 'events', 'emailModel', 'followService', 'systemAlertService', 'ticketModel', 'metadataModel', 'screenConfigurationModel', 'relationModel', 'srdModel', '$log', 'objectValueMapperService', 'slaCalculatorService', function ($modal, $filter, $q, $timeout, $state, events, emailModel, followService, systemAlertService, ticketModel, metadataModel, screenConfigurationModel, relationModel, srdModel, $log, objectValueMapperService, slaCalculatorService) { /** * Shows action blade with "Assign" dialog * * @param ticket * @param isDraft * @returns {*} */ this.showAssignDialog = function (ticket, isDraft, role, activeRole) { if (!ticket.assignee) { ticket.assignee = { customFields: {} }; } return $modal.open({ templateUrl: 'views/common/assign-action-blade.html', controller: 'AssignController', windowClass: 'action-blade', keyboard: 'custom', backdrop: 'custom', resolve: { assignParams: function () { return { ticket: ticket, isDraft: isDraft, role: role, activeRole: activeRole }; } } }); }; /** * Shows action blade with "Assign" dialog * * @param selectedItems * @returns {*} */ this.showBulkAssignDialog = function (selectedItems) { return $modal.open({ templateUrl: 'views/common/bulk-assign-action-blade.html', controller: 'BulkAssignController', windowClass: 'action-blade', backdrop: 'custom', resolve: { assignParams: function () { return { selectedItems: selectedItems }; } } }); }; this.showShareDialog = function (tickets) { return $modal.open({ templateUrl: 'views/common/share-action-blade.html', controller: 'ShareController', windowClass: 'action-blade', resolve: { shareParams: function () { return { actionItems: _.isArray(tickets) ? tickets : [tickets] }; } } }); }; this.showEditStatusDialog = function (tickets, isDraft, isReopen) { return $modal.open({ templateUrl: 'views/ticket/edit-status-action-blade.html', windowClass: 'action-blade', controller: 'EditStatusController', keyboard: 'custom', backdrop: 'custom', resolve: { editStatusParams: function () { return { actionItems: _.isArray(tickets) ? tickets : [tickets], isDraft: isDraft, isReopen: isReopen }; } } }); }; this.showLinkDialog = function (tickets, isDraft) { return $modal.open({ templateUrl: 'views/ticket/link-item-action-blade.html', windowClass: 'action-blade', controller: 'LinkController', resolve: { linkParams: function () { return { selectedItems: tickets, isDraft: isDraft, linkFromTicketConsole: true }; } } }); }; this.showPlansEditDialog = function (metadata, ticket) { return $modal.open({ templateUrl: 'views/change/documents-action-blade.html', windowClass: 'action-blade', controller: 'EditChangeRequestPlansController', resolve: { planEditParams: function () { return { metadata: metadata, ticket: ticket }; } } }); }; this.share = function (ticket) { if (ticket.type === EntityVO.TYPE_SERVICEREQUEST || ticket.type === EntityVO.TYPE_SBEREQUEST) { this.showShareDialog(ticket).result.then(function () { $('.profile-action-bar__item-link').focus(); }, function () { $('.profile-action-bar__item-link').focus(); }); } else { emailModel.createEmail([ticket]); } }; this.follow = function (ticket) { return followService.follow(ticket); }; this.unfollow = function (ticket) { return followService.unfollow(ticket); }; this.showPrintDialog = function ($event, ticket, relationCounters, feed, metadata, screenLayout) { var ticketPrintDialog = $modal.open({ templateUrl: 'views/common/print-action-blade.html', controller: 'PrintController', windowClass: 'action-blade', size: 'extra-lg', resolve: { params: function () { return { entity: ticket, relationCounters: relationCounters, feed: feed, metadata: metadata, screenLayout: screenLayout }; } } }); ticketPrintDialog.result.finally(function () { $event.currentTarget.focus(); }); }; this.confirmAction = function (ticket, action, scope) { var confirmActionPromise = $q.defer(); if (ticket.type === EntityVO.TYPE_INCIDENT && action === EntityVO.ACTION_REOPEN) { this.showEditStatusDialog(ticket, false, true).result.then(function (statusData) { return ticketModel.refreshStatus(ticket.id, ticket.type, statusData); }).then(function () { confirmActionPromise.resolve(); }); } else if (ticket.type === EntityVO.TYPE_SERVICEREQUEST && action === EntityVO.ACTION_CANCEL) { var modalInstance = systemAlertService.modal({ title: $filter('i18n')('common.notification.delete.title'), text: $filter('i18n')('action.' + action + '.confirm', [$filter('i18n')('common.labels.' + ticket.type)]), buttons: [ { text: $filter('i18n')('common.notification.delete.button1'), data: true }, { text: $filter('i18n')('common.notification.delete.button2'), data: false } ] }); var ticketActionServiceContext = this; modalInstance.result.then(function (data) { if (data) { ticketActionServiceContext.applyAction(ticket, action, scope); } }); } return confirmActionPromise.promise; }; this.applyAction = function (ticket, action, scope) { var data = {}; scope.state.processing = true; scope.state.isDataLoading = true; ticketModel.applyAction(ticket.id, ticket.type, action, data).then(function (response) { if (!_.isEmpty(response)) { data = response[0].items[0]; if (ticket.type === EntityVO.TYPE_INCIDENT) { if (action === EntityVO.ACTION_REOPEN) { $state.go(EntityVO.TYPE_INCIDENT, { id: data.id }); $timeout(function () { systemAlertService.success({ text: $filter('i18n')('ticket.notification.reopen.message', data.displayId + ': ' + data.summary), hide: 10000 }); }, 100); } } else if (ticket.type === EntityVO.TYPE_SERVICEREQUEST) { if (action === EntityVO.ACTION_REOPEN || action === EntityVO.ACTION_CANCEL) { ticket.status = angular.copy(data.status); ticketModel.refreshSLA(ticket); scope.$emit(events.TICKET_PROFILE_RESEND_EVENT, { eventName: events.REFRESH_ACTIVITY_FEED }); } else if (action === EntityVO.ACTION_REQUESTAGAIN) { if (data.isCrossLaunchSubmitted) { srdModel.launchAIF(true, // createNew false, // historyBackOnCancel scope, data.requestTemplateTitle, data.crossLaunchURLCreate, ticket.customer.loginId, ticket.contact ? ticket.contact.loginId : null); } else { ticketModel.cache.draft = new ServiceRequestVO().build(data); $state.go('draftServicerequest'); } } } else if (ticket.type === EntityVO.TYPE_CHANGE) { if (action === "restart") { //theres a seperate call to fetch plans (documents) //,So storing localCopy of plans before assigning ticket data //, as restart API call doesnt return plans. //and assign it to ticket.plans after ticket assignment from ticket data //fetched from API. var localPlans = angular.copy(ticket.plans); ticket = new ChangeVO().build(response[0].items[0]); ticket.plans = localPlans; } else { ticket = new ChangeVO().build(response[0].items[0]); ticket.plans = angular.copy(ticket.plans); } scope.$emit(events.AFTER_SAVED_CHANGES, ticket); } else if (ticket.type === EntityVO.TYPE_RELEASE) { ticket = new ReleaseVO().build(response[0].items[0]); ticket.plans = angular.copy(ticket.plans); if (ticketModel.cache && ticketModel.cache[ticket.id]) { _.extend(ticketModel.cache[ticket.id], ticket); //basicdata should be loaded from cache only else it will change the object reference and any change in data will not be reflected. scope.basicData = ticketModel.cache[ticket.id]; } } var statusField = objectValueMapperService.getFieldByName(ticket.type === EntityVO.TYPE_TASK ? 'taskStatus' : 'status'), memberName = ticket.type === EntityVO.TYPE_TASK ? 'taskStatus' : 'status'; scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: statusField && statusField.name, memberName: memberName }); if (ticket.status && ticket.serviceTargets && ticket.serviceTargets.length > 0) { slaCalculatorService.processServiceTarget(ticket, ticket.serviceTargets); } } }).finally(function () { scope.state.processing = false; scope.state.isDataLoading = false; }); }; this.assign = function ($event, singleEditMode, ticket, scope, activeRole) { //SW00523909 added check for release type only if (ticket.type === EntityVO.TYPE_RELEASE) { ticket.assignee = ticket.coordinator; ticket.supportGroup = ticket.coordinatorGroup; } if (singleEditMode) { handleAssignmentWithoutSaving(ticket, null, this, scope, activeRole); } else { handleAssignment($event, ticket, null, this, scope, activeRole); } }; this.assignToMe = function ($event, role, singleEditMode, ticket, scope) { var assigneeRole = role; var assigneeSupportGroup = ticket.supportGroup || {}; var ticketActionServiceContext = this; if (role === 'ticketassignee') { if (ticket.type === EntityVO.TYPE_WORKORDER) { assigneeRole = 'workorderassignee'; } else { assigneeRole = ''; } } else if (role === 'workordermanager' || role === 'changemanager') { assigneeSupportGroup = ticket.managerGroup || {}; } else if (role === 'problemcoordinator' || role === 'releasecoordinator') { assigneeSupportGroup = ticket.coordinatorGroup; } if (ticket.type === EntityVO.TYPE_ACTIVITY) { assigneeRole = ""; } var locationCompany = null, customerCompany, primaryCompany = getPrimaryCompany(ticket), ownerCompany = null, authorCompany = null; primaryCompany = primaryCompany ? primaryCompany.name : ''; if (ticket.type !== EntityVO.TYPE_KNOWLEDGE) { if (!(ticket instanceof TicketConsoleItemVO) && (ticket.type === EntityVO.TYPE_INCIDENT || ticket.type === EntityVO.TYPE_WORKORDER)) { customerCompany = objectValueMapperService.getExactValueByFieldName('customerCompany') || ''; } else { customerCompany = (ticket.customer && ticket.customer.company) ? ticket.customer.company.name : (ticket.company ? ticket.company.name : ''); } if ((ticket.type === EntityVO.TYPE_TASK || ticket.type === EntityVO.TYPE_CHANGE) && ticket.id && ticket.company) { locationCompany = ticket.company.name; } else if (!(ticket instanceof TicketConsoleItemVO) && (ticket.type === EntityVO.TYPE_INCIDENT || ticket.type === EntityVO.TYPE_WORKORDER)) { locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } else { locationCompany = ticket.locationCompany ? ticket.locationCompany.name : null; } } else { ownerCompany = ticket.owner.company ? ticket.owner.company.name : ''; authorCompany = ticket.author.company ? ticket.author.company.name : ''; } scope.state.AssignToMeIsLoading = true; ticketModel.getTicketAssigneePersonsBySearchText(assigneeRole, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany).then(function (persons) { persons = _.uniqBy(persons.results, function (person) { return person.loginId && person.supportGroupId; }); var person = persons.length === 1 ? persons[0] : _.find(persons, { supportGroupId: assigneeSupportGroup.id }); if (person && person.loginId) { if (singleEditMode) { if (['workordermanager', 'changemanager'].indexOf(role) !== -1) { _.assign(ticket, { manager: { id: person.id, loginId: person.loginId, fullName: person.fullName, supportGroup: person.supportGroup, groupId: person.supportGroupId, company: person.company, organization: person.organization, thumbnail: person.thumbnail, thumbnailMime: person.thumbnailMime }, managerGroup: { id: person.supportGroupId, name: person.supportGroup, organization: person.organization, company: person.company } }); } else if (['problemcoordinator', 'releasecoordinator'].indexOf(role) !== -1) { _.assign(ticket, { coordinator: { id: person.id, loginId: person.loginId, fullName: person.fullName, supportGroup: person.supportGroup, groupId: person.supportGroupId, company: person.company, organization: person.organization, thumbnail: person.thumbnail, thumbnailMime: person.thumbnailMime }, coordinatorGroup: { id: person.supportGroupId, name: person.supportGroup, organization: person.organization, company: person.company } }); } else { _.assign(ticket, { assignee: { id: person.id, loginId: person.loginId, fullName: person.fullName, supportGroup: person.supportGroup, groupId: person.supportGroupId, company: person.company, organization: person.organization, thumbnail: person.thumbnail, thumbnailMime: person.thumbnailMime }, group: { id: person.supportGroupId, name: person.supportGroup, organization: person.organization, company: person.company } }); } ticket.autoAssign = false; notifyAboutAssignmentUpdate(ticket, ticket, scope, role, true); } else { updateAssignment(person, assigneeRole, ticket, scope); } } else { singleEditMode ? handleAssignmentWithoutSaving(ticket, role, ticketActionServiceContext, scope, role) : handleAssignment($event, ticket, role, ticketActionServiceContext, scope); } }).finally(function () { scope.state.AssignToMeIsLoading = false; }); }; this.toggleNeedAttentionFlag = function (items, data) { var ids = _.map(items, function (item) { return { uuid: item.id, type: item.type }; }); return ticketModel.updateStatus(ids, data); }; function getPrimaryCompany(ticket) { if (ticket.allCompanies) { return _.find(ticket.allCompanies, function (company) { return company.primary; }); } else { return ticket.company; } } function updateAssignment(person, role, ticket, scope) { scope.state.processing = true; var attributesMap = {}; if (role === 'problemcoordinator') { attributesMap = { coordinatorLoginId: person.loginId, coordinatorFullName: person.fullName, coordinatorGroup: person.supportGroup, coordinatorCompany: person.company.name, coordinatorOrganization: person.organization, coordinatorGroupId: person.supportGroupId, coordinatorAutoAssign: false }; } else if (role === 'workordermanager' || role === 'changemanager') { attributesMap = { managerLoginId: person.loginId, managerFullName: person.fullName, managerGroup: person.supportGroup, managerCompany: person.company.name, managerOrganization: person.organization, managerGroupId: person.supportGroupId, managerAutoAssign: false }; } else { attributesMap = { loginId: person.loginId, fullName: person.fullName, group: person.supportGroup, groupId: person.supportGroupId, company: person.company.name, organization: person.organization, autoAssign: false }; } ticketModel.update(ticket.id, ticket.type, attributesMap).then(function (results) { if (_.isUndefined(results.data)) { var response = {}; if (ticket.type === EntityVO.TYPE_RELEASE) { response = { assignee: results.coordinator, group: results.supportGroup || results.coordinatorGroup, manager: results.manager || results.coordinator, managerGroup: results.managerGroup || results.coordinatorGroup, status: results.status, accessMappings: results.accessMappings }; } else if (ticket.type === EntityVO.TYPE_PROBLEM || ticket.type === EntityVO.TYPE_KNOWNERROR) { response = { assignee: results.assignee, group: results.supportGroup, manager: results.coordinator, managerGroup: results.coordinatorGroup, status: results.status, accessMappings: results.accessMappings }; } else { response = { assignee: results.assignee, group: results.supportGroup, manager: results.manager || results.coordinator, managerGroup: results.managerGroup || results.coordinatorGroup, status: results.status, accessMappings: results.accessMappings }; } completeAssignment(response, ticket, scope); } else { systemAlertService.error({ text: results.data.error, clear: true }); } }).finally(function () { scope.state.processing = false; }); } function handleAssignment($event, ticket, role, ticketActionServiceContext, scope, activeRole) { ticketActionServiceContext.showAssignDialog(_.cloneDeep(ticket), false, role, activeRole).result.then(function (result) { completeAssignment(result, ticket, scope); $event.currentTarget.focus(); }, function () { $event.currentTarget.focus(); }); } function handleAssignmentWithoutSaving(ticket, role, ticketActionServiceContext, scope, activeRole) { ticketActionServiceContext.showAssignDialog(_.cloneDeep(ticket), true, role, activeRole).result.then(function (result) { notifyAboutAssignmentUpdate(result, ticket, scope, activeRole); }, function (e) { $log.error('Could not assign ticket properly. ', e, ticket); }); } function completeAssignment(result, ticket, scope) { if (ticket.type === EntityVO.TYPE_ACTIVITY) { result.originalObject.parentGuid = ticket.parentGuid; // This assignment should be at the top i.e. before overriding ticket with api response } if (result.originalObject && result.originalObject.constructor.name === ticket.constructor.name) { _.extend(ticket, result.originalObject); //Do not replace the existing ticket object } ticket.assignee = result.assignee; ticket.supportGroup = result.group; if (ticket.supportGroup) { delete ticket.supportGroup.assignedToGroup; } if (!ticket.coordinatorGroup) { ticket.coordinatorGroup = {}; } if (!ticket.coordinator) { ticket.coordinator = {}; } ticket.manager = result.manager; ticket.managerGroup = result.managerGroup; if (ticket.managerGroup) { delete ticket.managerGroup.assignedToGroup; } ticket.accessMappings = result.accessMappings; ticket.status = result.status; if (ticket.type === EntityVO.TYPE_RELEASE) { ticket.coordinator = result.assignee; } if (ticket.type === EntityVO.TYPE_PROBLEM || ticket.type === EntityVO.TYPE_KNOWNERROR) { ticket.coordinator = result.coordinator || result.manager; ticket.coordinatorGroup = result.coordinatorGroup || result.managerGroup; } else { ticket.coordinator.fullName = result.assignee ? result.assignee.fullName : ''; ticket.coordinatorGroup.name = result.group ? result.group.name : ''; ticket.coordinatorGroup.id = result.group ? result.group.id : ''; } screenConfigurationModel.updateTicketData(ticket, result.changes); if (ticketModel.cache && ticketModel.cache[ticket.id]) { _.extend(ticketModel.cache[ticket.id], ticket); } var targetPanelId = screenConfigurationModel.composePanelId(ticket.type, 'Assignment'); console.log('target panel id : ' + targetPanelId); scope.$broadcast(events.REFRESH_FIELD_VALUES, { panelId: targetPanelId }); scope.$broadcast(events.AFTER_SAVED_CHANGES, ticket); scope.$broadcast(events.TICKET_ASSIGNEES_UPDATED, ticket, true); //Fix for SW00537558 scope.$broadcast(events.TICKET_ASSIGNEES_UPDATED_FROM_BLADE, ticket, true); } /** * This method is handling assignment updates when single edit mode is on. * @param {Object} changes */ function notifyAboutAssignmentUpdate(changes, ticket, scope, assigneeRole, fromAssignToMe) { ticket.assignee = changes.assignee; ticket.supportGroup = changes.group || changes.supportGroup; ticket.manager = changes.manager; ticket.managerGroup = changes.managerGroup; ticket.coordinator = ticket.coordinator || {}; ticket.coordinator.fullName = changes.assignee ? changes.assignee.fullName : ''; ticket.coordinatorGroup = ticket.coordinatorGroup || {}; ticket.coordinatorGroup.name = changes.group ? changes.group.name : ''; if (ticket.type === EntityVO.TYPE_RELEASE) { ticket.coordinator.id = changes.assignee.id; } if (ticket.type === EntityVO.TYPE_PROBLEM || ticket.type === EntityVO.TYPE_KNOWNERROR) { ticket.coordinator = changes.manager; ticket.coordinatorGroup = changes.managerGroup; } if (changes.autoAssign) { ticket.assignee = { autoAssign: true }; ticket.supportGroup = {}; } if (changes.managerAutoAssign) { ticket.manager = { managerAutoAssign: true }; ticket.managerGroup = {}; } if (changes.coordinatorAutoAssign) { ticket.coordinator = { coordinatorAutoAssign: true }; ticket.coordinatorGroup = {}; } scope.$broadcast(events.TICKET_ASSIGNEES_UPDATED, _.cloneDeep(ticket), false, assigneeRole, fromAssignToMe); //Fix for SW00537558 scope.$broadcast(events.TICKET_ASSIGNEES_UPDATED_FROM_BLADE, ticket, true); } }]); })();