SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/create/create-incident-v2-controll...

207 lines
12 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('createTicketModule')
.controller('CreateIncidentV2Controller', ['$scope', '$element', '$modal', '$state', '$filter', '$log', 'createTicketModel', 'screenConfigurationModel', 'ticketModel', 'i18nService', 'systemAlertService', 'categoriesService', '$q', 'metadataModel', 'ticketActionService', 'events', 'fieldValidationModel', 'userModel', 'permissionModel', 'roles', 'layoutConfigurationModel', 'objectValueMapperService', '$timeout',
function ($scope, $element, $modal, $state, $filter, $log, createTicketModel, screenConfigurationModel, ticketModel, i18nService, systemAlertService, categoriesService, $q, metadataModel, ticketActionService, events, fieldValidationModel, userModel, permissionModel, roles, layoutConfigurationModel, objectValueMapperService, $timeout) {
var incident = {
type: EntityVO.TYPE_INCIDENT,
autoAssign: true,
customFields: {}
};
var incidentMetadata = {};
function init() {
$scope.state = {
showSpinner: true,
dataIsLoading: true
};
$scope.statusValue = "";
$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 = _.head(incidentMetadata.types).name;
var status = _.head(incidentMetadata.statuses);
incident.status = { value: status.name };
if (!_.isEmpty(status.statusReasons) && $scope.isFieldRequired('statusReason')) {
incident.status.statusReason = _.head(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);
$timeout(function () {
$scope.createIncidentForm.$setPristine();
$scope.$watch('createIncidentForm.$error.required.length', function (newVal) {
console.log("Create Incident Form Required Fields Pending: " + newVal || '0');
if (newVal) {
$scope.createIncidentForm.$error.required.forEach(function (obj) {
console.log("Field Name: " + obj.$name);
});
}
//TODO: select the form else below code shows the ng-invalid of form also
var arr = $element.find('[ng-form=createIncidentForm] .ng-invalid').toArray();
if (arr && arr.length) {
console.log("Create Incident Form Invalid Fields: ");
arr.forEach(function (field) {
console.log("Name: " + field.name + " ux-id: " + field.getAttribute('ux-id'));
});
}
});
}, 0);
});
}
$scope.createIncident = function () {
$scope.state.showSpinner = true;
var incidentData = createTicketModel.collectTicketChanges(incidentMetadata);
incidentData.summary = incidentData.summary && incidentData.summary.replace(/\u00a0/g, ' ');
createTicketModel.createIncidentV2(incidentData).catch(function (error) {
if (error) {
$timeout(function () {
systemAlertService.error({
text: (error.data && error.data.error) || error,
clear: false,
displayOnStateChange: true
});
});
}
}).finally(function () {
$scope.state.showSpinner = 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 && !$scope.createIncidentForm.$pristine) || !_.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;
}
}
function handleIncidentRules(obj) {
if ($scope.incident.CIRequiredOnResolved && $scope.statusValue == "Resolved") {
$scope.$broadcast(events.SERVICECI_REQUIRED, { isRequired: true, name: "causalCI" });
}
else {
$scope.$broadcast(events.SERVICECI_REQUIRED, { isRequired: false, name: "causalCI" });
}
if ($scope.incident.applyCognitiveForSupportGroup) {
$scope.$broadcast(events.APPLY_COGNITIVE_SG, { applyCognitive: $scope.incident.applyCognitiveForSupportGroup });
}
}
metadataModel.getMetadataByType('global').then(function (metadata) {
var isPwaEnabled = (metadata.configurationParameters['Enable-Progressive-Views'] === 'T' || metadata.configurationParameters['Enable-Progressive-Views'] === 'true');
isPwaEnabled = localStorage.getItem('overridePV') === 'T' ? false : isPwaEnabled;
if (isPwaEnabled && localStorage.getItem('midtierUrl')) {
$state.go($state.current.name + 'PV');
}
else {
init();
}
}).catch(function (error) {
if (error) {
systemAlertService.error({
text: error.data.error,
clear: false
});
}
});
$scope.$on(events.WIDGET_VALUE_CHANGE, function (event, obj) {
if (obj.fieldName === EntityVO.TYPE_STATUS) {
$scope.statusValue = obj.fieldValue;
handleIncidentRules(obj);
}
});
$scope.$on(events.COMPANY_VALUE_CHANGED, function (event, obj) {
handleIncidentRules();
if ($scope.incident.serviceCIRequired) {
$scope.$broadcast(events.SERVICECI_REQUIRED, { isRequired: true, name: "impactedService" });
}
else {
$scope.$broadcast(events.SERVICECI_REQUIRED, { isRequired: false, name: "impactedService" });
}
});
$scope.$on(events.SHOW_ASSIGN_TICKET_BLADE, handleShownAssignmentBlade);
$scope.$on(events.CUSTOM_FIELD_VALUE_CHANGE, function () {
$scope.$broadcast(events.REFRESH_FIELD_VALUES);
});
}
]);
})();