"use strict"; (function () { 'use strict'; angular.module('ticketModule').controller('LinkCIController', ['$scope', '$modalInstance', 'linkParams', 'ticketModel', 'configurationModel', 'events', '$q', 'i18nService', '$state', 'systemAlertService', 'relationModel', '$rootScope', function ($scope, $modalInstance, linkParams, ticketModel, configurationModel, events, $q, i18nService, $state, systemAlertService, relationModel, $rootScope) { $scope.company = linkParams.selectedItem.company; $scope.data = { searches: linkParams.searches || [] }; $scope.linkedCount = 0; $scope.state = { processing: false }; var parent = ticketModel.linkCIsParent; if (parent) { $scope.parentName = parent.type; $scope.parentId = parent.id; $scope.parentDisplayId = parent.displayId; } else { $state.go('dashboard'); } initAlertForDirtyForm(); $scope.$on(events.LINKED_COUNT, function (e, searches) { $scope.linkedCount = 0; var totalRelatedItems = []; _.forEach(searches, function (search) { if (search.results && search.results.length > 0) { var itemsList = search.results; for (var i = 0; i < itemsList.length; i++) { if (itemsList[i].relations && itemsList[i].relations.length > 0) { if (!(search.allQueryItemsRelation && search.allQueryItemsRelation.length > 0)) { $scope.linkedCount++; } if (totalRelatedItems.indexOf(itemsList[i].instanceId) < 0) { totalRelatedItems.push(itemsList[i].instanceId); } } } if (search.allQueryItemsRelation && search.allQueryItemsRelation.length > 0) { $scope.linkedCount += search.totalMatchCount; } } }); }); function getLinkedCIs() { var linkedCIs = [], bulkCIsLinked = []; _.forEach($scope.data.searches, function (search) { if (search.allQueryItemsRelation && search.allQueryItemsRelation.length > 0) { bulkCIsLinked = bulkCIsLinked.concat(search.allQueryItemsRelation); } _.forEach(search.results, function (item) { _.forEach(item.relations, function (relation) { if (search.allQueryItemsRelation && search.allQueryItemsRelation.length > 0) { if (_.find(search.allQueryItemsRelation, { relationshipType: relation })) { return; } } linkedCIs.push({ relationshipType: relation, tag: EntityVO.TYPE_LINKEDITEM, id: item.reconciliationId, type: EntityVO.TYPE_ASSET, desc: item.name }); }); }); }); return { linkedCIs: linkedCIs, bulkCIsLinked: bulkCIsLinked }; } $scope.hasNotRelatedCIs = function () { if ($scope.linkedCount > 0) { return ($scope.data.searches || []).some(function (search) { return search.selectedCount > 0 && search.selectedCount > search.linkedCount; }); } return true; }; $scope.startLinkProcess = function () { var allLinkedCIs = getLinkedCIs(), linkedCIs = allLinkedCIs.linkedCIs, bulkCIsLinked = allLinkedCIs.bulkCIsLinked; if (linkedCIs.length >= 80 || bulkCIsLinked && bulkCIsLinked.length > 0) { return systemAlertService.modal({ type: 'info', title: i18nService.getLocalizedString('create.change.wizard.ci.confirmCIRelation.title'), text: i18nService.getLocalizedString('create.change.wizard.ci.confirmCIRelation.text'), buttons: [ { text: i18nService.getLocalizedString('common.button.continue'), data: true }, { text: i18nService.getLocalizedString('common.button.returnToScreen'), data: false } ] }).result.then(function (data) { if (data) { return $scope.link(); } }); } else { return $scope.link(); } }; $scope.link = function () { var allLinkedCIs = getLinkedCIs(), linkedCIs = allLinkedCIs.linkedCIs, bulkCIsLinked = allLinkedCIs.bulkCIsLinked, promisesList = [], catchErr = false; if (linkedCIs.length > 0) { catchErr = true; var linkedCisPromise = relationModel.addRelation({ uuid: linkParams.selectedItem.id, type: EntityVO.TYPE_CHANGE }, linkedCIs, catchErr); promisesList.push(linkedCisPromise); } if (bulkCIsLinked.length > 0) { _.forEach(bulkCIsLinked, function (relationInfo) { var ticket = { id: linkParams.selectedItem.id, type: EntityVO.TYPE_CHANGE }; if (relationInfo.matchesCount > 100) { var searchCriteria = relationInfo.searchCriteria; searchCriteria.chunkInfo = { startIndex: 0, chunkSize: 50 }; var bulkRelatePromise = relationModel.relateBulkCI(ticket, relationInfo, searchCriteria) .then(function () { return onBulkRequestComplete(ticket, relationInfo); }); promisesList.push(bulkRelatePromise); } }); } if (promisesList.length > 0) { $scope.state.processing = true; var relationsPromise = $q.all(promisesList), relationstatus; if (bulkCIsLinked.length > 0 || $scope.linkedCount >= 80) { $modalInstance.close({ linkedItems: allLinkedCIs, relationPromise: relationsPromise, linkedCount: $scope.linkedCount }); } else { relationsPromise.then(function (data) { var responseStatus = data[0] && data[0][linkParams.selectedItem && linkParams.selectedItem.id] && data[0][linkParams.selectedItem && linkParams.selectedItem.id].status; if (catchErr && responseStatus.type && responseStatus.message) { relationstatus = responseStatus; } }).catch(function (error) { systemAlertService.error({ text: (error.data && error.data.error) ? error.data.error : (error.data.results.error ? error.data.results.error : error.defaultMessage), clear: false }); return $q.reject(error); }).finally(function () { $rootScope.$broadcast(events.LINKED_CI_SAVE_COMPLETE); if (configurationModel.showARMessageOnGetEntry) { ticketModel.getARMessageInformation(linkParams.selectedItem.id, EntityVO.TYPE_CHANGE); } $modalInstance.close({ linkedItems: allLinkedCIs, linkedCount: $scope.linkedCount, status: relationstatus }); $scope.state.processing = false; }); } } }; function onBulkRequestComplete(ticket, relationInfo) { var searchCriteria = relationInfo.searchCriteria; searchCriteria.chunkInfo.startIndex += searchCriteria.chunkInfo.chunkSize; if (relationInfo.matchesCount >= searchCriteria.chunkInfo.startIndex) { return relationModel.relateBulkCI(ticket, relationInfo, searchCriteria).then(function () { return onBulkRequestComplete(ticket, relationInfo); }); } else { return $q.when(1); } } function initAlertForDirtyForm() { $scope.$on('$stateChangeStart', function (event, toState, toParams) { if ($scope.linkedCount > 0 && toState.name !== parent.type) { 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)) { $scope.linkedCount = 0; $scope.$dismiss(); $state.transitionTo(data.stateName, data.stateParams); } }); } else { $scope.$dismiss(); } }); } } ]); })();