"use strict"; (function () { 'use strict'; angular.module('customWidgetsModule').directive('ticketTemplate', ['customFieldLinkFunction', 'events', '$modal', 'objectValueMapperService', 'systemAlertService', 'categoriesService', 'ticketModel', 'fieldValidationModel', 'createTicketModel', '$filter', 'permissionModel', 'roles', '$timeout', function (customFieldLinkFunction, events, $modal, objectValueMapperService, systemAlertService, categoriesService, ticketModel, fieldValidationModel, createTicketModel, $filter, permissionModel, roles, $timeout) { return { restrict: 'E', replace: true, scope: { data: '=', context: '=', metadata: '=', isEditable: '=' }, templateUrl: 'views/custom-widgets/ticket-template.html', link: function (scope) { var storedcontext; scope.state = {}; scope.templateType = scope.context.type + 'Template'; customFieldLinkFunction(scope); scope.$watch('context.customer', function (newValue) { if (newValue && _.isObject(newValue)) { scope.context.selectedCompany = scope.context.customer.company; if (scope.context.type !== EntityVO.TYPE_TASK) { scope.updatePriority(); } } else { scope.context.selectedCompany = ''; } }); scope.$watch('context.selectedTemplate', function (newValue) { if (newValue && _.isObject(newValue)) { var templateId = scope.context.selectedTemplate.id; scope.data.value.templateId = templateId; scope.data.value.templateName = scope.context.selectedTemplate.name; scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name }); storedcontext = createTicketModel.collectTicketChanges(); //if the categorizations are empty initially, the collectChanges method does not return it //this fails to reset the values to empty on clearing the template if (!_.find(storedcontext.categorizations, { name: 'operational' })) { storedcontext.categorizations.push({ name: 'operational', tiers: {} }); } if (!_.find(storedcontext.categorizations, { name: 'product' })) { storedcontext.categorizations.push({ name: 'product', tiers: {} }); } if (!_.find(storedcontext.categorizations, { name: 'resolution' })) { storedcontext.categorizations.push({ name: 'resolution', tiers: {} }); } if (!_.find(storedcontext.categorizations, { name: 'resolutionProduct' })) { storedcontext.categorizations.push({ name: 'resolutionProduct', tiers: {} }); } scope.context.selectedCompany = newValue.company && newValue.company.name ? newValue.company.name : scope.context.selectedCompany; ticketModel.getDraft(scope.context.type, templateId).then(function (data) { var template = objectValueMapperService.getFieldByName(scope.templateType); template.value.templateId = data.templateId; objectValueMapperService.addFields([scope.data]); if (data.templateId) { scope.context.templateId = data.templateId; } if (data.customFields) { scope.context.customFields = data.customFields; } if (data.id) { scope.context.id = data.id; } if (data.displayId) { scope.context.displayId = data.displayId; } if (data.summary) { scope.context.summary = data.summary; } if (data.desc.trim()) { var templateNotes = data.desc ? data.desc + ' ' : ''; scope.context.desc = (storedcontext.desc ? (templateNotes ? (storedcontext.desc + '\n' + templateNotes) : storedcontext.desc) : templateNotes ? templateNotes : ''); scope.context.desc.trim(); } else if (storedcontext.desc) { scope.context.desc = storedcontext.desc.trim(); } scope.context.selectedImpact = data.impact ? _.find(scope.metadata.impacts, { name: data.impact }) : _.last(scope.metadata.impacts); scope.context.selectedUrgency = data.urgency ? _.find(scope.metadata.urgencies, { name: data.urgency }) : _.last(scope.metadata.urgencies); scope.context.selectedPriority = data.priority ? _.find(scope.metadata.priorities, { name: data.priority }) : _.last(scope.metadata.priorities); if (data.status.value) { scope.context.selectedStatus = _.find(scope.metadata.statuses, { name: data.status.value }); if (data.status.reason) { scope.context.selectedStatusReason = _.find(scope.context.selectedStatus.statusReasons, { name: data.status.reason }); var statusReason = objectValueMapperService.getFieldByName(FieldVO.prototype.STATUS_REASON); statusReason.value = (scope.context.selectedStatusReason && scope.context.selectedStatusReason.name) ? scope.context.selectedStatusReason.name : ''; } else { scope.updateStatusReason(); } } else { scope.context.selectedStatus = _.find(scope.metadata.statuses, { name: scope.context.status.value }); } if (data.serviceType) { if (!angular.isObject(scope.context.selectedServiceType) || scope.context.selectedServiceType.name !== data.serviceType) { scope.context.selectedServiceType = _.find(scope.metadata.types, { name: data.serviceType }); updateCategories(); } } if (data.resolution) { scope.context.resolution = data.resolution; } if (data.supportGroup.name) { scope.context.selectedGroup = data.supportGroup; if (data.assignee.loginId) { scope.context.selectedAssignee = data.assignee; } } else { scope.context.selectedGroup = ''; } if (scope.context.type === EntityVO.TYPE_WORKORDER) { if (data.managerGroup.name) { scope.context.selectedManagerGroup = data.managerGroup; if (data.manager.loginId) { scope.context.selectedManager = data.manager; } } else { scope.context.selectedManagerGroup = ''; } } if (data.impactedService && data.impactedService.name && data.impactedService.reconciliationId) { scope.context.selectedService = data.impactedService; } if (data.causalCI && data.causalCI.name && data.causalCI.reconciliationId) { scope.context.selectedAsset = data.causalCI; } if (data.categorizations) { _.forEach(data.categorizations, function (category) { var match = _.find(scope.context.allCategories, { name: category.name }); if (match) { for (var i = 0; i < match.listOfTiers.length; i++) { var tier = match.listOfTiers[i]; tier.selectedValue = category.tiers[tier.name]; tier.disabled = false; } match.serializedValue = categoriesService.getDisplayValues(match); match.dirty = true; } }); } if (data.resCategorizations) { _.forEach(data.resCategorizations, function (category) { var match = _.find(scope.context.allCategories, { name: category.name }); var i; if (match) { for (i = 0; i < match.listOfTiers.length; i++) { var tier = match.listOfTiers[i]; tier.selectedValue = category.tiers[tier.name]; tier.disabled = false; } match.serializedValue = categoriesService.getDisplayValues(match); match.dirty = true; } var resMatch = _.find(scope.context.resCategories, { name: category.name }); if (resMatch) { for (var j = 0; j < resMatch.listOfTiers.length; j++) { var resCatTier = resMatch.listOfTiers[j]; resCatTier.selectedValue = category.tiers[resCatTier.name]; resCatTier.disabled = false; } resMatch.serializedValue = categoriesService.getDisplayValues(resMatch); resMatch.dirty = true; } }); } if (!_.isEmpty(data.locationCompany)) { scope.context.locationCompany = data.locationCompany.name; } else if (objectValueMapperService.getExactValueByFieldName('locationCompany')) { scope.context.locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } if (scope.context.type === EntityVO.TYPE_INCIDENT) { scope.updatePriority(); } if (scope.context.type === EntityVO.TYPE_TASK) { updatePriorityForTask(data.priority); } if (data.dynamicFields) { var dynamicFields = data.dynamicFields; scope.$emit(events.SHOW_DYNAMIC_FIELDS_FROM_TEMPLATE, dynamicFields); } setRelatedFieldValues(); }).finally(function () { scope.state.dataIsLoading = false; }); } else if (!newValue) { scope.data.value.templateId = ''; scope.data.value.templateName = ''; scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name }); } }); function updatePriorityForTask(priority) { var setPriority = { label: priority, name: priority }; var priorityWidget = objectValueMapperService.getFieldByName('priority'); priorityWidget.value.priority = setPriority; if (priorityWidget.members) { _.forEach(priorityWidget.members, function (member) { if (member.name === 'priority') { member.setValue(setPriority); } }); } } scope.browseTemplate = function () { var modalInstance = $modal.open({ templateUrl: scope.context.type === EntityVO.TYPE_TASK ? 'views/template/browse-task-template-action-blade.html' : 'views/template/browse-ticket-template-action-blade.html', windowClass: 'action-blade', controller: scope.context.type === EntityVO.TYPE_TASK ? 'BrowseTaskTemplateController' : 'BrowseTicketTemplateController', resolve: scope.context.type === EntityVO.TYPE_TASK ? { isFromBrowseTemplate: function () { return true; } } : { ticketType: function () { return scope.context.type; }, metadata: function () { return scope.metadata; }, company: function () { return scope.context.selectedCompany; } } }); modalInstance.result.then(function (data) { scope.context.selectedTemplate = data; }); }; scope.clearSelectedTemplate = function () { scope.context.selectedTemplate = ''; if (storedcontext) { scope.context.id = storedcontext.id; scope.context.summary = storedcontext.summary; scope.context.desc = storedcontext.desc; scope.context.resolution = storedcontext.resolution; scope.context.selectedGroup = storedcontext.selectedGroup ? storedcontext.selectedGroup.name : ''; scope.context.selectedAssignee = storedcontext.assignee; scope.context.supportGroup = storedcontext.supportGroup; if (scope.context.type === EntityVO.TYPE_WORKORDER) { scope.context.selectedManagerGroup = storedcontext.selectedManagerGroup ? storedcontext.selectedManagerGroup.name : ''; scope.context.selectedManager = storedcontext.manager; scope.context.managerGroup = storedcontext.managerGroup; } scope.context.selectedService = storedcontext.impactedService; scope.context.selectedAsset = storedcontext.causalCI; if (storedcontext.impact) { scope.context.selectedImpact = _.find(scope.metadata.impacts, { name: storedcontext.impact }); } if (storedcontext.urgency) { scope.context.selectedUrgency = _.find(scope.metadata.urgencies, { name: storedcontext.urgency }); } if (storedcontext.priority) { scope.context.selectedPriority = _.find(scope.metadata.priorities, { name: storedcontext.priority }); if (scope.context.type === EntityVO.TYPE_TASK) { updatePriorityForTask(storedcontext.priority); } } if (storedcontext.status.value) { scope.context.selectedStatus = _.find(scope.metadata.statuses, { name: storedcontext.status.value }); if (storedcontext.status.reason) { scope.context.selectedStatusReason = _.find(scope.context.selectedStatus.statusReasons, { name: storedcontext.status.reason }); } else { scope.updateStatusReason(); } } if (storedcontext.serviceType) { if (!angular.isObject(scope.context.selectedServiceType) || scope.context.selectedServiceType.name !== storedcontext.serviceType) { scope.context.selectedServiceType = _.find(scope.metadata.types, { name: storedcontext.serviceType }); } } if (storedcontext.categorizations.length) { _.forEach(storedcontext.categorizations, function (category) { var catName = category.name === 'resolutionOperational' ? 'resolution' : category.name; var match = _.find(scope.context.allCategories, { name: catName }); if (match) { for (var i = 0; i < match.listOfTiers.length; i++) { var tier = match.listOfTiers[i]; tier.selectedValue = category.tiers[tier.name]; tier.disabled = false; } match.serializedValue = categoriesService.getDisplayValues(match); match.dirty = true; } }); } else { _.forEach(scope.context.allCategories, function (category) { for (var i = 0; i < category.listOfTiers.length; i++) { var tier = category.listOfTiers[i]; tier.selectedValue = ''; tier.disabled = true; } category.serializedValue = ''; category.dirty = true; }); } scope.context.locationCompany = storedcontext.locationCompany && storedcontext.locationCompany.name; scope.context.customFields = storedcontext.customFields; if (scope.context.type === EntityVO.TYPE_INCIDENT) { scope.updatePriority(); } if (scope.context.dynamicFields) { var dynamicFields = storedcontext.dynamicFields ? storedcontext.dynamicFields : ''; scope.$emit(events.SHOW_DYNAMIC_FIELDS_FROM_TEMPLATE, dynamicFields); } setRelatedFieldValues(); } }; scope.updatePriority = function () { ticketModel.getPriority(scope.context.selectedCompany, scope.context.selectedImpact.name, scope.context.selectedUrgency.name, scope.context.type).then(function (data) { scope.context.calculatedPriority = _.find(scope.metadata.priorities, { name: data }); var priorityWidget = objectValueMapperService.getFieldByName('priority'); priorityWidget.value.impact = scope.context.selectedImpact; priorityWidget.value.urgency = scope.context.selectedUrgency; priorityWidget.value.priority = scope.context.calculatedPriority; if (priorityWidget.members) { _.forEach(priorityWidget.members, function (member) { if (member.name === 'impact') { member.setValue(scope.context.selectedImpact); } else if (member.name === 'urgency') { member.setValue(scope.context.selectedUrgency); } else if (member.name === 'priority') { member.setValue(scope.context.calculatedPriority); } }); } if (!scope.context.calculatedPriority) { systemAlertService.error({ text: $filter('i18n')('ticket.​calculationError.calculatedPriority'), clear: false }); } }); }; scope.updateStatusReason = function () { if (!_.isEmpty(scope.context.selectedStatus.statusReasons) && scope.isFieldRequired('statusReason')) { scope.context.selectedStatusReason = _.first(scope.context.selectedStatus.statusReasons); } else { scope.context.selectedStatusReason = {}; } var statusReason = objectValueMapperService.getFieldByName(FieldVO.prototype.STATUS_REASON); statusReason.value = scope.context.selectedStatusReason.name ? scope.context.selectedStatusReason.name : ''; }; scope.isFieldRequired = function (field) { return fieldValidationModel.isFieldRequired(scope.context.type, scope.context.selectedStatus.name, '', field); }; scope.getList = function (type, term) { return createTicketModel.getList(type, term).then(function (response) { scope.state.exceedsChunkSize = response.exceedsChunkSize; scope.state.isTooltipOpen = response.exceedsChunkSize; $timeout(function () { scope.state.isTooltipOpen = false; }, 10000); if (type === EntityVO.TYPE_PERSON && permissionModel.hasRole(roles.ITSM_PEOPLE_USER_ROLE)) { response.list.unshift({ isEmptyResult: true }); } return response.list; }); }; scope.onInputFocusBlur = function () { scope.state.isTooltipOpen = false; }; scope.$watch('data.setValueFlag', function (value) { if (value !== '#$#' && value) { //TODO: Set the values after provider action works scope.data.setValueFlag = '#$#'; } }); function updateCategories() { scope.$emit(events.CLEAR_ALL_CATEGORIES, scope.context.selectedServiceType && scope.context.selectedServiceType.name); } function setRelatedFieldValues() { objectValueMapperService.setValueByFieldName('summary', scope.context.summary); objectValueMapperService.setValueByFieldName('resolution', scope.context.resolution); var selectedSupportGroup = scope.context.selectedGroup ? scope.context.selectedGroup.name : scope.context.selectedGroup; if (selectedSupportGroup) { scope.context.selectedGroup.supportGroups = selectedSupportGroup; } else { scope.context.selectedGroup = { supportGroups: selectedSupportGroup }; } if (scope.context.type === EntityVO.TYPE_WORKORDER) { var selectedManagerSupportGroup = scope.context.selectedManagerGroup ? scope.context.selectedManagerGroup.name : scope.context.selectedManagerGroup; if (selectedManagerSupportGroup) { scope.context.selectedManagerGroup.supportGroups = selectedManagerSupportGroup; } else { scope.context.selectedManagerGroup = { supportGroups: selectedManagerSupportGroup }; } objectValueMapperService.setValueByFieldName('supportGroups', scope.context.selectedGroup); objectValueMapperService.setValueByFieldName('assignee', scope.context.selectedAssignee); objectValueMapperService.setValueByFieldName('requestManagerSupportGroups', scope.context.selectedManagerGroup); objectValueMapperService.setValueByFieldName('requestManager', scope.context.selectedManager); } else { objectValueMapperService.setValueByFieldName('assigneeSupportGroups', scope.context.selectedGroup); objectValueMapperService.setValueByFieldName('assignee', scope.context.selectedAssignee); } if (scope.context.type !== EntityVO.TYPE_TASK) { objectValueMapperService.setValueByFieldName('impactedService', { ci: scope.context.selectedService }); objectValueMapperService.setValueByFieldName('causalCI', { ci: scope.context.selectedAsset }); objectValueMapperService.setValueByFieldName('locationCompany', scope.context.locationCompany); } //set values for categorization widget var categoriesList = objectValueMapperService.getFieldsByType('category'); _.forEach(categoriesList, function (catField) { var catFieldName = catField.name === 'resolutionOperational' ? 'resolution' : catField.name, newCategoryValue = _.find(scope.context.allCategories, { name: catFieldName }); if (newCategoryValue) { var categoryValue = objectValueMapperService.getValueByFieldName(catField.name); _.forEach(categoryValue.listOfTiers, function (tier, index) { tier.selectedValue = newCategoryValue.listOfTiers && newCategoryValue.listOfTiers[index] && newCategoryValue.listOfTiers[index].selectedValue; }); categoryValue.serializedValue = newCategoryValue.serializedValue; } }); //set values for categorization field menus var catMenuFieldsList = objectValueMapperService.getFieldsByType('dynamicSelectionField'); _.forEach(catMenuFieldsList, function (catMenuField) { if (catMenuField.name !== 'company') { _.forEach(scope.context.allCategories, function (catField) { var catMenu = _.find(catField.listOfTiers, { name: catMenuField.name }); if (catMenu) { objectValueMapperService.setValueByFieldName(catMenuField.name, catMenu.selectedValue); return; } }); } }); for (var customFieldName in scope.context.customFields) { var customField = objectValueMapperService.getFieldByName(customFieldName); var customFieldValue = scope.context.customFields[customFieldName]; if (customField) { customField.value = customFieldValue; customField.setValueFlag = customFieldValue; } } if (scope.context.type !== EntityVO.TYPE_TASK) { scope.$emit(events.TICKET_TEMPLATE_UPDATED, scope.context); } } } }; }]); }());