389 lines
20 KiB
JavaScript
389 lines
20 KiB
JavaScript
"use strict";
|
||
/**
|
||
* Created by ygowtham on 7/23/2015.
|
||
*/
|
||
(function () {
|
||
'use strict';
|
||
angular.module('createProblemModule')
|
||
.controller('CreateProblemController', ['$scope', '$modal', '$state', 'userModel', '$filter', 'searchModel', 'events', 'createTicketModel',
|
||
'screenConfigurationModel', 'ticketModel', 'i18nService', 'systemAlertService', 'categoriesService', '$q',
|
||
'metadataModel', 'ticketActionService', 'fieldValidationModel', 'configurationModel', '$timeout',
|
||
function ($scope, $modal, $state, userModel, $filter, searchModel, events, createTicketModel, screenConfigurationModel, ticketModel, i18nService, systemAlertService, categoriesService, $q, metadataModel, ticketActionService, fieldValidationModel, configurationModel, $timeout) {
|
||
if (!configurationModel.isServerApplicationEnabled(EntityVO.TYPE_PROBLEM)) {
|
||
$state.go('unauthorized');
|
||
return;
|
||
}
|
||
$scope.currentDate = new Date();
|
||
var problem = {
|
||
type: EntityVO.TYPE_PROBLEM,
|
||
autoAssign: true,
|
||
coordinatorAutoAssign: true,
|
||
customFields: {}
|
||
};
|
||
$scope.datePickerOptions = {
|
||
startingDay: 7,
|
||
'show-weeks': false
|
||
};
|
||
var problemMetadata = {};
|
||
function init() {
|
||
$scope.state = {
|
||
dataIsLoading: true,
|
||
tooManyCompanies: false,
|
||
assigneeLoading: true,
|
||
initialLoading: true
|
||
};
|
||
$scope.selections = {};
|
||
$scope.siteOptions = {
|
||
company: {
|
||
visible: false,
|
||
attribute: 'companyName'
|
||
},
|
||
region: {
|
||
attribute: 'region'
|
||
},
|
||
siteGroup: {
|
||
attribute: 'siteGroup'
|
||
},
|
||
site: {
|
||
attribute: 'name'
|
||
}
|
||
};
|
||
$scope.formContainsInvalidFields = createTicketModel.formContainsInvalidFields;
|
||
createTicketModel.currentTicket = problem;
|
||
$scope.problem = problem;
|
||
$scope.problem.autoAssign = true;
|
||
$scope.problem.coordinatorAutoAssign = true;
|
||
$scope.problemMetadata = problemMetadata;
|
||
$scope.loggedInUserId = userModel.decodedUserId || userModel.userId;
|
||
$scope.company = {};
|
||
if (userModel.userFullData.availableForAssignment) {
|
||
$scope.availableForAssignment = userModel.userFullData.availableForAssignment;
|
||
}
|
||
else {
|
||
userModel.getFullCurrentUserData().then(function () {
|
||
$scope.availableForAssignment = userModel.userFullData.availableForAssignment;
|
||
});
|
||
}
|
||
initAlertForDirtyForm();
|
||
var metadataPromise = metadataModel.getMetadataByType(EntityVO.TYPE_PROBLEM), basicDataPromise = ticketModel.getDraft(EntityVO.TYPE_PROBLEM);
|
||
$q.all([metadataPromise, basicDataPromise]).then(function (responses) {
|
||
var data = responses[1];
|
||
angular.extend(problemMetadata, responses[0]);
|
||
problemMetadata.statuses = _.reject(problemMetadata.statuses, { isValidForCreate: false });
|
||
problem.selectedStatus = _.first(problemMetadata.statuses);
|
||
$scope.updateStatusReason();
|
||
problem.selectedImpact = _.last(problemMetadata.impacts);
|
||
problem.selectedUrgency = _.last(problemMetadata.urgencies);
|
||
problem.investigationDriver = _.first(problemMetadata.investigationDrivers);
|
||
problem.id = data.id;
|
||
problem.displayId = data.displayId;
|
||
problem.categorizations = data.categorizations;
|
||
problem.accessMappings = data.accessMappings;
|
||
problem.company = data.company;
|
||
problem.supportGroup = data.supportGroup ? data.supportGroup : {};
|
||
problem.coordinatorGroup = data.coordinatorGroup ? data.coordinatorGroup : {};
|
||
problem.type = data.type;
|
||
problem.location = data.location ? data.location : {};
|
||
problem.location.companyName = data.company.name;
|
||
if (data.status && data.status.value) {
|
||
problem.selectedStatus = _.first(_.where(problemMetadata.statuses, { name: data.status.value }));
|
||
$scope.updateStatusReason();
|
||
}
|
||
if (data.coordinator) {
|
||
problem.coordinator = data.coordinator;
|
||
$scope.problem.coordinatorAutoAssign = false;
|
||
}
|
||
else {
|
||
problem.coordinator = {};
|
||
}
|
||
if (data.assignee) {
|
||
problem.assignee = data.assignee;
|
||
$scope.problem.autoAssign = false;
|
||
}
|
||
else {
|
||
problem.assignee = {};
|
||
}
|
||
$scope.state.assigneeLoading = false;
|
||
getRootCause(null, data.categorizations);
|
||
})
|
||
.finally(function () {
|
||
$scope.state.dataIsLoading = false;
|
||
$scope.state.initialLoading = false;
|
||
});
|
||
var customFieldLabelsPromise = screenConfigurationModel
|
||
.loadScreenConfigurationAndCustomFieldLabels(ScreenConfigurationVO.prototype.PROBLEM_DETAILS_SCREEN, EntityVO.TYPE_PROBLEM);
|
||
var userPromise = userModel.getFullCurrentUserData().then(function () {
|
||
$scope.setCompany(userModel.userFullData);
|
||
});
|
||
var searchCompanyPromise = searchModel.getOperatingCompanies(null, -1).then(function (response) {
|
||
$scope.selections.companies = _.cloneDeep(response.companies);
|
||
$scope.state.tooManyCompanies = response.exceedsChunkSize;
|
||
});
|
||
$q.all([metadataPromise, basicDataPromise, userPromise, customFieldLabelsPromise, searchCompanyPromise]).then(function () {
|
||
problem.allCategories = _.cloneDeep(categoriesService.populateCategories(problem.categorizations, problemMetadata));
|
||
var fields = screenConfigurationModel.getEditableFieldsForScreen(ScreenConfigurationVO.prototype.PROBLEM_DETAILS_SCREEN);
|
||
$scope.customFields = angular.copy(fields);
|
||
});
|
||
}
|
||
$scope.getList = function (type, term) {
|
||
if (type === 'service' || type === 'asset') {
|
||
return createTicketModel.getList(type, term).then(function (response) {
|
||
if (type === 'service') {
|
||
$scope.exceedsChunkSizeService = response.exceedsChunkSize;
|
||
$scope.isTooltipOpenService = true;
|
||
}
|
||
else if (type === 'asset') {
|
||
$scope.exceedsChunkSizeAsset = response.exceedsChunkSize;
|
||
$scope.isTooltipOpenAsset = true;
|
||
}
|
||
$timeout(function () {
|
||
$scope.isTooltipOpenService = false;
|
||
$scope.isTooltipOpenAsset = false;
|
||
}, 10000);
|
||
return response.objects;
|
||
});
|
||
}
|
||
else {
|
||
return createTicketModel.getList(type, term);
|
||
}
|
||
};
|
||
$scope.onInputFocusBlur = function () {
|
||
$scope.isTooltipOpenService = false;
|
||
$scope.isTooltipOpenAsset = false;
|
||
};
|
||
$scope.setCompany = function (userdata) {
|
||
$scope.problem.company = userdata.company;
|
||
$scope.problem.location = {};
|
||
$scope.problem.location.companyName = $scope.problem.company.name;
|
||
$scope.company = _.cloneDeep($scope.problem.company);
|
||
};
|
||
$scope.updateCompany = function (value) {
|
||
var newCompany = value;
|
||
showCompanyChangeDialog().then(function (result) {
|
||
if (result) {
|
||
$scope.problem.company = newCompany;
|
||
$scope.$broadcast(events.CLEAR_FOUNDATION_SERVICE, $scope.problem.company);
|
||
$scope.$broadcast(events.CLEAR_CATEGORY_DETAILS, $scope.problem.company);
|
||
$scope.problem.location.companyName = $scope.problem.company.name;
|
||
problem.selectedService = '';
|
||
problem.selectedAsset = '';
|
||
if (!$scope.problem.coordinatorAutoAssign) {
|
||
problem.coordinator = {};
|
||
}
|
||
if (!$scope.problem.autoAssign) {
|
||
problem.assignee = {};
|
||
}
|
||
}
|
||
else {
|
||
$scope.company = $scope.problem.company;
|
||
}
|
||
});
|
||
};
|
||
$scope.getCompaniesByName = function (name) {
|
||
return searchModel.getCompaniesByText(name).then(function (response) {
|
||
return { list: response.companies, exceedsChunkSize: response.exceedsChunkSize };
|
||
});
|
||
};
|
||
function showCompanyChangeDialog() {
|
||
var modal = systemAlertService.modal({
|
||
type: 'info',
|
||
title: $filter('i18n')('common.notification.dirty.title'),
|
||
text: $filter('i18n')('create.problem.companyChange.warning'),
|
||
buttons: [
|
||
{
|
||
text: $filter('i18n')('common.button.yes'),
|
||
data: true
|
||
},
|
||
{
|
||
text: $filter('i18n')('common.button.no'),
|
||
data: false
|
||
}
|
||
]
|
||
});
|
||
return modal.result;
|
||
}
|
||
$scope.$watch('problem.selectedService', function () {
|
||
if (problem && _.isObject(problem.selectedService)) {
|
||
$scope.$emit(events.AFFECTED_SERVICE_UPDATED, problem.selectedService);
|
||
}
|
||
});
|
||
$scope.$watch('problem.company', function () {
|
||
$scope.updatePriority();
|
||
});
|
||
$scope.updatePriority = function () {
|
||
if (problem.company && problem.selectedImpact && problem.selectedUrgency) {
|
||
ticketModel.getPriority(problem.company.name, problem.selectedImpact.name, problem.selectedUrgency.name, EntityVO.TYPE_PROBLEM).then(function (data) {
|
||
problem.calculatedPriority = _.find(problemMetadata.priorities, { name: data });
|
||
if (!problem.calculatedPriority) {
|
||
systemAlertService.error({
|
||
text: $filter('i18n')('ticket.calculationError.calculatedPriority'),
|
||
clear: false
|
||
});
|
||
}
|
||
});
|
||
}
|
||
};
|
||
$scope.clearSelectedService = function () {
|
||
problem.selectedService = '';
|
||
};
|
||
$scope.clearSelectedAsset = function () {
|
||
problem.selectedAsset = '';
|
||
};
|
||
$scope.assign = function ($event) {
|
||
handleAssignment($event);
|
||
};
|
||
$scope.assignToMe = function ($event, role) {
|
||
var assigneeRole = role;
|
||
var assigneeSupportGroup = problem.supportGroup;
|
||
if (role === 'ticketassignee') {
|
||
assigneeRole = '';
|
||
}
|
||
else if (role === 'problemcoordinator') {
|
||
assigneeSupportGroup = problem.coordinatorGroup;
|
||
}
|
||
ticketModel.getTicketAssigneePersons(problem.type, assigneeRole).then(function (persons) {
|
||
persons = _.unique(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) {
|
||
updateAssignment(person, assigneeRole);
|
||
}
|
||
else {
|
||
handleAssignment($event, role);
|
||
}
|
||
});
|
||
};
|
||
function handleAssignment($event, role) {
|
||
problem.ghostEntity = true;
|
||
ticketActionService.showAssignDialog(problem, true, role).result.then(function (result) {
|
||
completeAssignment(result);
|
||
$event.currentTarget.focus();
|
||
}, function () {
|
||
$event.currentTarget.focus();
|
||
});
|
||
}
|
||
function updateAssignment(person, role) {
|
||
var response = {};
|
||
if (role === 'problemcoordinator') {
|
||
response.manager = { id: person.id, loginId: person.loginId, fullName: person.fullName };
|
||
response.managerGroup = { id: person.supportGroupId, name: person.supportGroup, organization: person.organization, company: person.company };
|
||
}
|
||
else {
|
||
response.assignee = { id: person.id, loginId: person.loginId, fullName: person.fullName };
|
||
response.group = { id: person.supportGroupId, name: person.supportGroup, organization: person.organization, company: person.company };
|
||
}
|
||
completeAssignment(response);
|
||
}
|
||
function completeAssignment(result) {
|
||
if (result.autoAssign) {
|
||
clearAssignment();
|
||
}
|
||
else if (result.group) {
|
||
problem.autoAssign = false;
|
||
problem.assignee = result.assignee;
|
||
problem.supportGroup = result.group;
|
||
}
|
||
if (result.coordinatorAutoAssign) {
|
||
clearCoordinator();
|
||
}
|
||
else if (result.managerGroup) {
|
||
problem.coordinatorAutoAssign = false;
|
||
problem.coordinator = result.manager;
|
||
problem.coordinatorGroup = result.managerGroup;
|
||
}
|
||
}
|
||
$scope.createProblem = function () {
|
||
$scope.state.dataIsLoading = true;
|
||
problem.summary = problem.summary.replace(/\u00a0/g, ' ');
|
||
var initialCategorizations = _.cloneDeep(problem.categorizations);
|
||
problem.categorizations = categoriesService.collectValues(problem.allCategories, $scope.problem).categorizations;
|
||
createTicketModel.createProblem().then(function (error) {
|
||
if (error) {
|
||
$scope.state.dataIsLoading = false;
|
||
problem.categorizations = _.cloneDeep(initialCategorizations);
|
||
systemAlertService.error({
|
||
text: error,
|
||
clear: true
|
||
});
|
||
}
|
||
});
|
||
};
|
||
$scope.cancel = function () {
|
||
$state.go('dashboard');
|
||
};
|
||
$scope.needResolutionNote = function () {
|
||
return ~[EntityVO.STATUS_CLOSED, EntityVO.STATUS_RESOLVED].indexOf(problem.selectedStatus.name.toLowerCase());
|
||
};
|
||
$scope.isFieldRequired = function (field) {
|
||
var selectedStatus = $scope.problem.selectedStatus ? $scope.problem.selectedStatus.name : "";
|
||
return fieldValidationModel.isFieldRequired(EntityVO.TYPE_PROBLEM, selectedStatus, "", field);
|
||
};
|
||
function isFormDirty() {
|
||
return $scope.createProblemForm.$dirty || !_.isEmpty(problem.attachments);
|
||
}
|
||
function initAlertForDirtyForm() {
|
||
$scope.$on('$stateChangeStart', function (event, toState, toParams) {
|
||
if (isFormDirty() && toState.name !== EntityVO.TYPE_PROBLEM) {
|
||
event.preventDefault();
|
||
var modalInstance = systemAlertService.modal({
|
||
title: i18nService.getLocalizedString('common.notification.dirty.title'),
|
||
text: i18nService.getLocalizedString('common.notification.dirty.message'),
|
||
buttons: [
|
||
{
|
||
text: i18nService.getLocalizedString('common.labels.yes'),
|
||
data: {
|
||
stateName: toState.name,
|
||
stateParams: toParams
|
||
}
|
||
},
|
||
{
|
||
text: i18nService.getLocalizedString('common.labels.no')
|
||
}
|
||
]
|
||
});
|
||
modalInstance.result.then(function (data) {
|
||
if (!_.isEmpty(data)) {
|
||
createTicketModel.currentTicket = problem = {};
|
||
$scope.createProblemForm.$setPristine();
|
||
$state.transitionTo(data.stateName, data.stateParams);
|
||
}
|
||
});
|
||
}
|
||
});
|
||
}
|
||
function clearAssignment() {
|
||
problem.autoAssign = true;
|
||
problem.assignee = {};
|
||
problem.supportGroup = {};
|
||
}
|
||
function clearCoordinator() {
|
||
problem.coordinatorAutoAssign = true;
|
||
problem.coordinator = {};
|
||
problem.coordinatorGroup = {};
|
||
}
|
||
$scope.updateStatusReason = function () {
|
||
if (!_.isEmpty(problem.selectedStatus.statusReasons) && $scope.isFieldRequired('statusReason')) {
|
||
problem.selectedStatusReason = _.first(problem.selectedStatus.statusReasons);
|
||
}
|
||
else {
|
||
problem.selectedStatusReason = {};
|
||
}
|
||
};
|
||
function getRootCause(event, data) {
|
||
var params = {};
|
||
params.depends = categoriesService.buildCategoriesForRootCause(data, problem.allCategories);
|
||
params.depends.productManufacturer = null;
|
||
var company = $scope.company && $scope.company.name ? { name: $scope.company.name } : null;
|
||
ticketModel.getRootCause(params, company).then(function (response) {
|
||
if (response) {
|
||
$scope.selections.rootCause = response;
|
||
}
|
||
});
|
||
}
|
||
$scope.$on(events.TICKET_UPDATE_ROOTCAUSE, getRootCause);
|
||
init();
|
||
}
|
||
]);
|
||
})();
|