SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/change/edit-dates-controller.js

209 lines
12 KiB
JavaScript

"use strict";
/**
* Created by ygowtham on 6/24/2015.
*/
(function () {
'use strict';
angular.module('changeModule')
.controller('EditDatesController', ['$scope', 'collisionModel', 'systemAlertService', 'events', 'ticketModel', '$q', '$state', '$stateParams', 'tabIds', '$timeout',
'screenConfigurationModel', 'layoutConfigurationModel', 'metadataModel', 'objectValueMapperService', 'relationModel', 'configurationModel',
function ($scope, collisionModel, systemAlertService, events, ticketModel, $q, $state, $stateParams, tabIds, $timeout, screenConfigurationModel, layoutConfigurationModel, metadataModel, objectValueMapperService, relationModel, configurationModel) {
objectValueMapperService.clearMap(EntityVO.TYPE_CHANGE);
$scope.editMode = true;
$scope.datesToBeRemovedFromPanel = [];
$scope.state = {
resolvingCollisions: false,
formInit: true,
editDatesFormInvalid: false,
noPendingTicketDates: true,
noPendingCollisionsStatus: true
};
metadataModel.getMetadataByType(EntityVO.TYPE_CHANGE).then(function (metadata) {
$scope.metadata = metadata;
});
$q.all([ticketModel.getTicket($stateParams.id, EntityVO.TYPE_CHANGE, true), relationModel.getRelations($stateParams.id, EntityVO.TYPE_CHANGE)]).then(function (response) {
$scope.context = response[0];
$scope.context.linkedCIs = response[1] && response[1].length && _.filter(response[1], { type: EntityVO.TYPE_ASSET });
$scope.context.scheduledStartDatePicker = { open: false };
$scope.context.scheduledEndDatePicker = { open: false };
$scope.context.actualStartDatePicker = { open: false };
$scope.context.actualEndDatePicker = { open: false };
$scope.context.targetDatePicker = { open: false };
$state.go('.calendar.book', {
id: $scope.context.id
});
var linkedCIs = [];
if ($scope.context.linkedCIs && $scope.context.linkedCIs.length) {
linkedCIs.push.apply(linkedCIs, $scope.context.linkedCIs);
}
if ($scope.context.impactedService && $scope.context.impactedService.reconciliationId) {
if (!_.find(linkedCIs, { id: $scope.context.impactedService.reconciliationId })) {
linkedCIs.push({ id: $scope.context.impactedService.reconciliationId });
}
}
if ($scope.context.scheduledStartDate && $scope.context.scheduledEndDate && linkedCIs && linkedCIs.length &&
(isNaN(configurationModel.autoTriggerChangeCollisionForCIsUpto) || _.isUndefined(configurationModel.autoTriggerChangeCollisionForCIsUpto) ||
linkedCIs.length <= configurationModel.autoTriggerChangeCollisionForCIsUpto)) {
collisionModel.getListOfCollisionsById($scope.context, true)
.then(function (collisionSummary) {
if (collisionSummary.count > 0) {
$scope.collisions = collisionSummary;
}
});
}
});
var screenName = screenConfigurationModel.getScreenNameByTicketType(EntityVO.TYPE_CHANGE);
layoutConfigurationModel.loadScreenLayout(screenName).then(function (screenLayout) {
$scope.screenLayout = screenLayout;
$scope.datesCustomFields = _.filter($scope.screenLayout.panels, { name: 'datesSection' });
});
$scope.cancel = function () {
_.forEach($scope.datesToBeRemovedFromPanel, function (panel) {
if (panel.screenPanelSection.fields) {
_.remove(panel.screenPanelSection.fields, { name: panel.datesField.name });
}
});
$state.go('change', {
id: $stateParams.id
});
};
$scope.resolveCollisions = function () {
var promises = [], collisionPayload;
if (!$scope.state.editDatesFormInvalid) {
$scope.state.resolvingCollisions = true;
promises.push(ticketModel.update($scope.context.id, $scope.context.type, collectChanges()));
collisionPayload = updateCollisionPayload();
if ($scope.collisions && collisionPayload && collisionPayload.length) {
promises.push(collisionModel.addCollisionStatuses(collisionPayload, $scope.context.id));
}
$q.all(promises).then(function (responses) {
if (responses[0].status !== 500) {
$scope.context = responses[0];
$scope.collisions = responses[1];
_.forEach($scope.datesToBeRemovedFromPanel, function (panel) {
if (panel.screenPanelSection.fields) {
_.remove(panel.screenPanelSection.fields, { name: panel.datesField.name });
}
});
$scope.cancel();
}
else {
systemAlertService.error({
text: responses[0].data.error,
clear: true
});
}
$scope.state.resolvingCollisions = false;
});
}
};
function updateCollisionPayload() {
var collisionArray = [], collisionPayload = [];
var isAdded;
if ($scope.collisions && $scope.collisions.changeList) {
$scope.collisions.changeList.forEach(function (change) {
for (var count = 0; count < change.configurationItems.length; count++) {
if (change.configurationItems[count].modified && change.configurationItems[count].status && change.configurationItems[count].status.name) {
collisionArray.push({ status: {
value: change.configurationItems[count].status.name,
reason: change.configurationItems[count].rationale
}, configurationItem: {
reconciliationId: change.configurationItems[count].reconciliationId,
refTicketId: change.id,
name: change.configurationItems[count].name
}
});
}
}
});
}
if (collisionArray.length > 0) {
for (var i = 0; i < collisionArray.length; i++) {
isAdded = false;
for (var j = 0; j < collisionPayload.length && collisionPayload.length > 0; j++) {
if (collisionPayload[j].status.value === collisionArray[i].status.value && collisionPayload[j].status.reason === collisionArray[i].status.reason) {
collisionPayload[j].configurationItems.push(collisionArray[i].configurationItem);
isAdded = true;
break;
}
}
if (!isAdded) {
collisionPayload.push({
status: collisionArray[i].status
});
collisionPayload[collisionPayload.length - 1].configurationItems = [];
collisionPayload[collisionPayload.length - 1].configurationItems.push(collisionArray[i].configurationItem);
}
}
}
return collisionPayload;
}
$scope.$on(events.CHANGE_WIZARD_FORM_STATE, function (event, form) {
if (form.name === tabIds.wizard.dates) {
$timeout(function () {
if ($scope.state.formInit) {
$scope.state.formInit = !$scope.state.formInit;
}
});
if (!$scope.state.formInit) {
$scope.state.noPendingTicketDates = form.invalid;
$scope.state.editDatesFormInvalid = form.invalid;
}
}
});
$scope.$on(events.UPDATE_CHANGE_COLLISION_STATUS, function (event, collisions) {
$scope.state.noPendingCollisionsStatus = false;
$scope.collisions = collisions;
});
$scope.$on(events.REMOVE_CHANGE_DATES_FROM_PANEL, function (event, data) {
if (data) {
$scope.datesToBeRemovedFromPanel.push(data);
}
});
function collectChanges() {
var allFieldsList = objectValueMapperService.getFieldList(), changesList = { customFields: {} };
_.forEach(allFieldsList, function (field, fieldName) {
if (!_.isEmpty(field.value) || _.isNumber(field.value) || _.isDate(field.value)) {
if (_.isObject(field.value) && Object.keys(field.value).length > 0) {
angular.extend(changesList, field.value);
}
else {
if (field.ootb) {
changesList[fieldName] = field.value;
}
else {
if (!field.ootb && (field.dataType === FieldVO.prototype.DATA_TYPE_DATE || field.dataType === FieldVO.prototype.DATA_TYPE_TIME || field.dataType === FieldVO.prototype.DATA_TYPE_DATE_TIME)) {
changesList.customFields[fieldName] = field.getValue();
}
else {
changesList.customFields[fieldName] = field.value;
}
}
}
}
else { //If field doesn't have any value then this code will execute
if (field.ootb) {
changesList[fieldName] = field.getValue();
}
else {
changesList.customFields[fieldName] = field.getValue();
}
}
});
return {
scheduledStartDate: getTimestamp(changesList.scheduledStartDate),
scheduledEndDate: getTimestamp(changesList.scheduledEndDate),
actualStartDate: getTimestamp(changesList.actualStartDate),
actualEndDate: getTimestamp(changesList.actualEndDate),
targetDate: getTimestamp(changesList.targetDate),
customFields: changesList.customFields
};
}
function getTimestamp(date, time) {
time = time ? moment(time) : moment(date);
return moment(date).set('hours', time.get('hours')).set('minutes', time.get('minutes')).format('X') * 1000;
}
}
]);
})();