141 lines
8.2 KiB
JavaScript
141 lines
8.2 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('createTicketModule')
|
|
.controller('CreateIncidentV2Controller', ['$scope', '$modal', '$state', '$filter', '$log', 'createTicketModel', 'screenConfigurationModel', 'ticketModel', 'i18nService', 'systemAlertService', 'categoriesService', '$q', 'metadataModel', 'ticketActionService', 'events', 'fieldValidationModel', 'userModel', 'permissionModel', 'roles', 'layoutConfigurationModel', 'objectValueMapperService',
|
|
function ($scope, $modal, $state, $filter, $log, createTicketModel, screenConfigurationModel, ticketModel, i18nService, systemAlertService, categoriesService, $q, metadataModel, ticketActionService, events, fieldValidationModel, userModel, permissionModel, roles, layoutConfigurationModel, objectValueMapperService) {
|
|
var incident = {
|
|
type: EntityVO.TYPE_INCIDENT,
|
|
autoAssign: true,
|
|
customFields: {}
|
|
};
|
|
var incidentMetadata = {};
|
|
function init() {
|
|
$scope.state = {
|
|
showSpinner: true,
|
|
dataIsLoading: true
|
|
};
|
|
$scope.editMode = true;
|
|
$scope.isNew = true;
|
|
$scope.formContainsInvalidFields = createTicketModel.formContainsInvalidFields;
|
|
createTicketModel.currentTicket = incident;
|
|
$scope.incident = incident;
|
|
$scope.incidentMetadata = incidentMetadata;
|
|
$scope.loggedInUserId = userModel.userId;
|
|
objectValueMapperService.clearMap(incident.type);
|
|
initAlertForDirtyForm();
|
|
var metadataPromise = metadataModel.getMetadataByType(EntityVO.TYPE_INCIDENT).then(function (metadata) {
|
|
angular.extend(incidentMetadata, metadata);
|
|
incident.serviceType = _.first(incidentMetadata.types).name;
|
|
var status = _.first(incidentMetadata.statuses);
|
|
incident.status = { value: status.name };
|
|
if (!_.isEmpty(status.statusReasons) && $scope.isFieldRequired('statusReason')) {
|
|
incident.status.statusReason = _.first(status.statusReasons).name;
|
|
}
|
|
//default to lowest
|
|
incident.impact = _.last(incidentMetadata.impacts).name;
|
|
incident.urgency = _.last(incidentMetadata.urgencies).name;
|
|
incident.priority = _.last(incidentMetadata.priorities).name;
|
|
});
|
|
var basicDataPromise = ticketModel.getDraft(EntityVO.TYPE_INCIDENT).then(function (data) {
|
|
incident.id = data.id;
|
|
incident.displayId = data.displayId;
|
|
incident.accessMappings = data.accessMappings;
|
|
incident.categorizations = data.categorizations;
|
|
incident.resCategorizations = data.resCategorizations;
|
|
incident.categorizations.push(_.find(incident.resCategorizations, { 'name': 'resolutionProduct' }));
|
|
incident.categorizations.push(_.find(incident.resCategorizations, { 'name': 'resolution' }));
|
|
});
|
|
var screen = ScreenConfigurationVO.prototype.CREATE_INCIDENT_SCREEN;
|
|
var layoutConfigurationPromise = layoutConfigurationModel.loadScreenLayout(screen);
|
|
var screenConfigurationPromise = screenConfigurationModel
|
|
.loadScreenConfigurationAndCustomFieldLabels(screen, EntityVO.TYPE_INCIDENT);
|
|
$q.all([metadataPromise, basicDataPromise, layoutConfigurationPromise, screenConfigurationPromise]).then(function (responses) {
|
|
$scope.state.dataIsLoading = false;
|
|
$scope.state.showSpinner = false;
|
|
incident.allCategories = _.cloneDeep(categoriesService.populateCategories(incident.categorizations, incidentMetadata));
|
|
incident.resCategories = _.cloneDeep(categoriesService.populateCategories(incident.resCategorizations, incidentMetadata));
|
|
$scope.screenLayout = responses[2];
|
|
var fields = screenConfigurationModel.getEditableFieldsForScreen(screen);
|
|
$scope.customFields = angular.copy(fields);
|
|
});
|
|
}
|
|
$scope.createIncident = function () {
|
|
$scope.state.showSpinner = true;
|
|
var incidentData = createTicketModel.collectTicketChanges(incidentMetadata);
|
|
incidentData.summary = incidentData.summary && incidentData.summary.replace(/\u00a0/g, ' ');
|
|
createTicketModel.createIncidentV2(incidentData).then(function (error) {
|
|
if (error) {
|
|
$scope.state.showSpinner = false;
|
|
systemAlertService.error({
|
|
text: error,
|
|
clear: false
|
|
});
|
|
}
|
|
});
|
|
};
|
|
$scope.cancel = function () {
|
|
$state.go('dashboard');
|
|
};
|
|
$scope.isFieldRequired = function (field) {
|
|
return fieldValidationModel.isFieldRequired(incident.type, incident.status.value, "", field);
|
|
};
|
|
function isFormDirty() {
|
|
return $scope.createIncidentForm.$dirty || !_.isEmpty(incident.attachments);
|
|
}
|
|
function initAlertForDirtyForm() {
|
|
$scope.$on('$stateChangeStart', function (event, toState, toParams) {
|
|
if (isFormDirty() && toState.name !== EntityVO.TYPE_INCIDENT) {
|
|
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 = incident = {};
|
|
$scope.createIncidentForm.$setPristine();
|
|
$state.transitionTo(data.stateName, data.stateParams);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
function handleShownAssignmentBlade($event, data) {
|
|
var isAssignToMeAction = false, originalEvent = data.originalEvent, doNotSaveSelection = !data.saveSelection;
|
|
if (data) {
|
|
var assigneeRole = 'ticketassignee';
|
|
isAssignToMeAction = !!data.assignToMe;
|
|
if (isAssignToMeAction) {
|
|
ticketActionService.assignToMe(originalEvent, assigneeRole, doNotSaveSelection, $scope.incident, $scope);
|
|
}
|
|
else {
|
|
ticketActionService.assign(originalEvent, doNotSaveSelection, $scope.incident, $scope, assigneeRole);
|
|
}
|
|
}
|
|
else {
|
|
$log.warn('Could not open assignment blade. Required Event data is missing. ');
|
|
return;
|
|
}
|
|
}
|
|
init();
|
|
$scope.$on(events.SHOW_ASSIGN_TICKET_BLADE, handleShownAssignmentBlade);
|
|
$scope.$on(events.CUSTOM_FIELD_VALUE_CHANGE, function () {
|
|
$scope.$broadcast(events.REFRESH_FIELD_VALUES);
|
|
});
|
|
}
|
|
]);
|
|
})();
|