"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .directive('assigneeChooser', ['ticketService', 'searchModel', 'userModel', '$q', 'i18nService', 'events', '$timeout', 'objectValueMapperService', function (ticketService, searchModel, userModel, $q, i18nService, events, $timeout, objectValueMapperService) { return { restrict: 'EA', scope: { ticket: '=', assignee: '=', role: '@', label: "=", assignToMe: '=?', isDraft: '=' }, templateUrl: 'views/common/assignee-chooser-directive.html', link: function (scope) { var role = '', state = { processing: true, tooManyCompanies: false, tooManyOrganizations: false, tooManySupportGroups: false, tooManySupportPeople: false }, allCompanies = { name: i18nService.getLocalizedString('assignBlade.company.all'), id: 'All' }, allOrganizations = { name: i18nService.getLocalizedString('assignBlade.organization.all'), id: 'All' }, allSupportGroups = { name: i18nService.getLocalizedString('assignBlade.supportGroup.all'), id: 'All' }, timerPromise, pauseInterval = 2000; scope.assigneeCompany = {}; scope.assigneeOrganization = {}; scope.companies = []; scope.assignment = { organizations: [], groups: [] }; scope.organizations = []; scope.groups = []; scope.search = { text: '', filterText: '' }; scope.selectedParent = {}; scope.conditions = { showAssignToGroup: true, showAssignToMe: true }; scope.searchUser = function (row) { return (row.fullName.indexOf(scope.search.filterText || '') !== -1 || row.loginId.indexOf(scope.search.filterText || '') !== -1 || row.email.indexOf(scope.search.filterText || '') !== -1 || row.id.indexOf(scope.search.filterText || '') !== -1); }; function init() { if (scope.role) { role = scope.role; } else if (scope.ticket.type === EntityVO.TYPE_WORKORDER) { role = 'workorderassignee'; } else if (scope.ticket.type === EntityVO.TYPE_RELEASE) { role = 'releasecoordinator'; } else if (scope.ticket.type === EntityVO.TYPE_KNOWLEDGE) { var ticketStatusValue = scope.ticket.status ? scope.ticket.status.value : scope.ticket.statusValue ? scope.ticket.statusValue.value : ''; role = 'knowledgeassignee'; if (ticketStatusValue !== EntityVO.STATUS_CONTENT_REVIEW && ticketStatusValue !== EntityVO.STATUS_SME_REVIEW) { scope.conditions.showAssignToGroup = false; } } if (scope.ticket.accessMappings) { if (scope.role === 'changecoordinator' || scope.role === 'problemcoordinator') { scope.conditions.showAssignToMe = scope.ticket.accessMappings.coordinatorSelfAssignmentAllowed; } else if (scope.role === 'workordermanager' || scope.role === 'changemanager') { scope.conditions.showAssignToMe = scope.ticket.accessMappings.managerSelfAssignmentAllowed; } else if (role === 'knowledgeassignee') { scope.conditions.showAssignToMe = scope.ticket.accessMappings['assignee-self-assignmentEditAllowed']; } else if (!scope.role) { scope.conditions.showAssignToMe = scope.ticket.accessMappings.assigneeSelfAssignmentAllowed; } } if (scope.ticket.type === EntityVO.TYPE_PROBLEM || scope.ticket.type === EntityVO.TYPE_KNOWNERROR || scope.ticket.type === EntityVO.TYPE_RELEASE) { scope.conditions.showAssignToMe = userModel.userFullData.availableForAssignment; } if (scope.assignee.person && scope.assignee.person.loginId === userModel.userFullData.loginId && scope.ticket.type !== EntityVO.TYPE_ACTIVITY) { scope.assignToMe = true; } if (scope.ticket.type === EntityVO.TYPE_CHANGE && scope.ticket.supportGroup && scope.ticket.supportGroup.id != scope.assignee.person.supportGroupId) { scope.assignee.person.supportGroupId = scope.ticket.supportGroup.id; scope.assignee.person.supportGroup = scope.ticket.supportGroup.name; } else if ((scope.ticket.type === EntityVO.TYPE_PROBLEM || scope.ticket.type === EntityVO.TYPE_KNOWNERROR) && scope.ticket.coordinatorGroup && scope.ticket.coordinatorGroup.id != scope.assignee.person.supportGroupId) { scope.assignee.person.supportGroupId = scope.ticket.coordinatorGroup.id; scope.assignee.person.supportGroup = scope.ticket.coordinatorGroup.name; } getCompanies().then(function () { var customerCompany = null; if (scope.assignee.group.id || scope.assignToMe) { var promises = [], locationCompany = null, ownerCompany = null, authorCompany = null, primaryCompany = null; if (scope.ticket.type !== EntityVO.TYPE_KNOWLEDGE) { customerCompany = getCustomerCompanyName(); if ((scope.ticket.type === EntityVO.TYPE_TASK || scope.ticket.type === EntityVO.TYPE_CHANGE) && scope.ticket.id && scope.ticket.company) { locationCompany = scope.ticket.company.name; } else if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } else { locationCompany = scope.ticket.locationCompany ? scope.ticket.locationCompany.name : null; } } else { primaryCompany = getPrimaryCompany().name; ownerCompany = scope.ticket.owner && scope.ticket.owner.company ? scope.ticket.owner.company.name : ''; authorCompany = scope.ticket.author && scope.ticket.author.company ? scope.ticket.author.company.name : ''; if (primaryCompany === "All") { primaryCompany = null; } } scope.assigneeCompany = scope.assignee.group.company || {}; scope.assigneeOrganization = scope.assignee.group.organization ? { name: scope.assignee.group.organization } : allOrganizations; if (scope.assignToMe) { scope.assigneeCompany = allCompanies; scope.assigneeOrganization = allOrganizations; scope.assignee.group = allSupportGroups; scope.assignee.isGroup = false; scope.search.text = userModel.userFullData.fullName; scope.assigneeLoginId = userModel.userFullData.loginId; scope.conditions.showAssignToGroup = false; } promises.push(getSupportOrganizationsForCompany(scope.assigneeCompany, role, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany)); promises.push(getSupportGroupsForCompanyAndOrg(scope.assigneeCompany, scope.assigneeOrganization, role, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany)); if (scope.ticket.type !== EntityVO.TYPE_KNOWLEDGE) { promises.push(ticketService.getAssigneeBySupportGroupId(scope.assignee.group.id === 'All' ? '' : scope.assignee.group.id, scope.search.text, scope.assigneeLoginId, '', role, null, true, null, null, null, true)); } else { promises.push(ticketService.getAssigneeBySupportGroupId(scope.assignee.group.id === 'All' ? '' : scope.assignee.group.id, scope.search.text, scope.assigneeLoginId, customerCompany, role, null, true, '', ownerCompany, authorCompany, true)); } $q.all(promises).then(function (results) { scope.assignment.organizations = results[0]; scope.assignment.groups = results[1]; state.tooManySupportPeople = results[2].exceedsChunkSize; if (results[2].results.length) { handleMembers(results[2].results); } }).finally(completeProcessing); } else { scope.assigneeCompany = {}; customerCompany = {}; if (scope.ticket.customer && scope.ticket.customer.company) { customerCompany = scope.ticket.customer.company; } else if (scope.ticket.selectedCompany) { customerCompany = { name: scope.ticket.selectedCompany }; } else if (scope.ticket.author && scope.ticket.author.company) { customerCompany = scope.ticket.author.company; } else if (scope.ticket.company) { customerCompany = scope.ticket.company; } if (customerCompany.name && scope.companies.length) { _.some(scope.companies, function (company) { if (company.name === customerCompany.name) { scope.assigneeCompany = company; return true; } }); } scope.selectCompany(scope.assigneeCompany); } }); } scope.selectAssignToMe = function () { if (scope.assignToMe) { scope.assigneeCompany = allCompanies; scope.assigneeOrganization = allOrganizations; scope.assignee.group = allSupportGroups; scope.assignee.isGroup = false; scope.search.text = userModel.userFullData.fullName; scope.assigneeLoginId = userModel.userFullData.loginId; scope.conditions.showAssignToGroup = false; scope.onSearchTextChanged(); } else { scope.search.text = ''; scope.assigneeLoginId = ''; scope.conditions.showAssignToGroup = true; if (scope.ticket.type === EntityVO.TYPE_KNOWLEDGE) { var ticketStatusValue = scope.ticket.status ? scope.ticket.status.value : scope.ticket.statusValue ? scope.ticket.statusValue.value : ''; if (ticketStatusValue !== EntityVO.STATUS_CONTENT_REVIEW && ticketStatusValue !== EntityVO.STATUS_SME_REVIEW) { scope.conditions.showAssignToGroup = false; } } scope.selectedParent = {}; scope.selectGroup(scope.assignee.group); } }; scope.selectCompany = function (company) { state.processing = true; scope.assigneeCompany = company || {}; scope.assignment.organizations = []; scope.assigneeOrganization = allOrganizations; scope.assignment.groups = []; scope.assignee.group = {}; scope.assignee.group.persons = []; scope.assignee.person = {}; scope.assignee.isGroup = false; var promises = [], customerCompany = null, locationCompany = null, ownerCompany = null, authorCompany = null, primaryCompany = null; if (scope.ticket.type === EntityVO.TYPE_KNOWLEDGE) { primaryCompany = getPrimaryCompany().name; ownerCompany = scope.ticket.owner.company ? scope.ticket.owner.company.name : ''; authorCompany = scope.ticket.author.company ? scope.ticket.author.company.name : ''; if (primaryCompany === "All") { primaryCompany = null; } } else { if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { customerCompany = objectValueMapperService.getExactValueByFieldName('customerCompany') || ''; } else { customerCompany = (scope.ticket.customer && scope.ticket.customer.company) ? scope.ticket.customer.company.name : (scope.ticket.company ? scope.ticket.company.name : ''); } if ((scope.ticket.type === EntityVO.TYPE_TASK || scope.ticket.type === EntityVO.TYPE_CHANGE) && scope.ticket.id && scope.ticket.company) { locationCompany = scope.ticket.company.name; } else if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } else { locationCompany = scope.ticket.locationCompany ? scope.ticket.locationCompany.name : null; } } if (scope.assigneeCompany.name) { promises.push(getSupportOrganizationsForCompany(scope.assigneeCompany, role, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany)); promises.push(getSupportGroupsForCompanyAndOrg(scope.assigneeCompany, scope.assigneeOrganization, role, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany)); promises.push(ticketService.getAssigneeSupportGroupPersons(scope.assigneeCompany.id === 'All' ? '' : scope.assigneeCompany.name, '', '', '', role, null, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany, true, true)); } $q.all(promises).then(function (results) { scope.assignment.organizations = results[0]; scope.assignment.groups = results[1]; if ((scope.assignment.groups && scope.assignment.groups.length) || state.tooManySupportGroups) { allSupportGroups.persons = []; scope.assignee.group = allSupportGroups; } state.tooManySupportPeople = results[2] && results[2].exceedsChunkSize; if (scope.assignToMe) { scope.onSearchTextChanged(); } else if (results[2].results.length) { handleMembers(results[2].results); } completeProcessing(); }).catch(completeProcessing); }; scope.selectOrganization = function (organization) { state.processing = true; scope.assigneeOrganization = organization; scope.assignee.group = {}; scope.assignee.group.persons = []; scope.assignee.person = {}; scope.assignee.isGroup = false; var promises = [], customerCompany = null, locationCompany = null, ownerCompany = null, authorCompany = null, primaryCompany = null; if (scope.ticket.type === EntityVO.TYPE_KNOWLEDGE) { primaryCompany = getPrimaryCompany().name; ownerCompany = scope.ticket.owner.company ? scope.ticket.owner.company.name : ''; authorCompany = scope.ticket.author.company ? scope.ticket.author.company.name : ''; if (primaryCompany === "All") { primaryCompany = null; } } else { if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { customerCompany = objectValueMapperService.getExactValueByFieldName('customerCompany') || ''; } else { customerCompany = (scope.ticket.customer && scope.ticket.customer.company) ? scope.ticket.customer.company.name : (scope.ticket.company ? scope.ticket.company.name : ''); } if ((scope.ticket.type === EntityVO.TYPE_TASK || scope.ticket.type === EntityVO.TYPE_CHANGE) && scope.ticket.id && scope.ticket.company) { locationCompany = scope.ticket.company.name; } else if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } else { locationCompany = scope.ticket.locationCompany ? scope.ticket.locationCompany.name : null; } } promises.push(getSupportGroupsForCompanyAndOrg(scope.assigneeCompany, scope.assigneeOrganization, role, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany)); promises.push(ticketService.getAssigneeSupportGroupPersons(scope.assigneeCompany.id === 'All' ? '' : scope.assigneeCompany.name, organization.id === 'All' ? '' : organization.name, '', '', role, null, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany, true, true)); $q.all(promises).then(function (results) { scope.assignment.groups = results[0]; if (scope.assignment.groups.length || state.tooManySupportGroups) { allSupportGroups.persons = []; scope.assignee.group = allSupportGroups; } state.tooManySupportPeople = results[1].exceedsChunkSize; if (scope.assignToMe) { scope.onSearchTextChanged(); } else if (results[1].results.length) { handleMembers(results[1].results); } completeProcessing(); }).catch(completeProcessing); }; scope.selectGroup = function (group) { state.processing = true; scope.assignee.group = group; scope.assignee.group.persons = []; scope.assignee.person = {}; var customerCompany = null, locationCompany = null, ownerCompany = null, authorCompany = null, primaryCompany = null; if (scope.ticket.type === EntityVO.TYPE_KNOWLEDGE) { primaryCompany = getPrimaryCompany().name; ownerCompany = scope.ticket.owner.company ? scope.ticket.owner.company.name : ''; authorCompany = scope.ticket.author.company ? scope.ticket.author.company.name : ''; if (primaryCompany === "All") { primaryCompany = null; } } else { if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { customerCompany = objectValueMapperService.getExactValueByFieldName('customerCompany') || ''; } else { customerCompany = (scope.ticket.customer && scope.ticket.customer.company) ? scope.ticket.customer.company.name : (scope.ticket.company ? scope.ticket.company.name : ''); } if ((scope.ticket.type === EntityVO.TYPE_TASK || scope.ticket.type === EntityVO.TYPE_CHANGE) && scope.ticket.id && scope.ticket.company) { locationCompany = scope.ticket.company.name; } else if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } else { locationCompany = scope.ticket.locationCompany ? scope.ticket.locationCompany.name : null; } } if (group.id === 'All') { scope.assignee.isGroup = false; ticketService.getAssigneeSupportGroupPersons(scope.assigneeCompany.id === 'All' ? '' : scope.assigneeCompany.name, scope.assigneeOrganization.id === 'All' ? '' : scope.assigneeOrganization.name, '', '', role, null, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany, true, true) .then(function (searchResult) { state.tooManySupportPeople = searchResult.exceedsChunkSize; if (scope.assignToMe) { scope.onSearchTextChanged(); } else if (searchResult.results.length) { handleMembers(searchResult.results); } completeProcessing(); }).catch(completeProcessing); } else { ticketService.getAssigneeBySupportGroupId(scope.assignee.group.id, '', '', customerCompany, role, searchModel.supportGroupChunkSize, true, locationCompany, ownerCompany, authorCompany, true) .then(function (searchResult) { state.tooManySupportPeople = searchResult.exceedsChunkSize; if (scope.assignToMe) { scope.onSearchTextChanged(); } else if (searchResult.results.length) { handleMembers(searchResult.results); } completeProcessing(); }).catch(completeProcessing); } }; scope.assignToGroup = function () { scope.assignee.isGroup = true; scope.assignee.person = {}; scope.$emit(events.MODAL_FORM_IS_DIRTY); }; scope.assignToPerson = function (person) { if (person.type === 'parent') { scope.selectedParent = scope.selectedParent.id === person.id ? {} : person; } else { scope.assignee.isGroup = false; scope.assignee.person = person; scope.$emit(events.MODAL_FORM_IS_DIRTY); } }; scope.clearSearchText = function () { scope.search.text = ''; if (scope.assignToMe) { scope.assignToMe = false; if (scope.ticket.type === EntityVO.TYPE_KNOWLEDGE) { var ticketStatusValue = scope.ticket.status ? scope.ticket.status.value : scope.ticket.statusValue ? scope.ticket.statusValue.value : ''; if (ticketStatusValue !== EntityVO.STATUS_CONTENT_REVIEW && ticketStatusValue !== EntityVO.STATUS_SME_REVIEW) { scope.conditions.showAssignToGroup = false; } else { scope.conditions.showAssignToGroup = true; } } else { scope.conditions.showAssignToGroup = true; } scope.selectGroup(scope.assignee.group); } else { scope.onSearchTextChanged(); } }; scope.onSearchTextChanged = function () { var ownerCompany = null, authorCompany = null; if (scope.search.text !== userModel.userFullData.fullName) { scope.assigneeLoginId = ''; } if (state.tooManySupportPeople || scope.assignToMe) { scope.search.filterText = ''; scope.assignee.person = {}; if (scope.search.text.length > 2 || scope.search.text.length === 0) { $timeout.cancel(timerPromise); timerPromise = $timeout(function () { // take a local copy of the search text we use for this particular query var searchText = scope.search.text; state.processing = true; var locationCompany = null, customerCompany, primaryCompany = getPrimaryCompany(); primaryCompany = primaryCompany ? primaryCompany.name : ''; if (scope.ticket.type !== EntityVO.TYPE_KNOWLEDGE) { if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { customerCompany = objectValueMapperService.getExactValueByFieldName('customerCompany') || ''; } else { customerCompany = (scope.ticket.customer && scope.ticket.customer.company) ? scope.ticket.customer.company.name : (scope.ticket.company ? scope.ticket.company.name : ''); } if ((scope.ticket.type === EntityVO.TYPE_TASK || scope.ticket.type === EntityVO.TYPE_CHANGE) && scope.ticket.id && scope.ticket.company) { locationCompany = scope.ticket.company.name; } else if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } else { locationCompany = scope.ticket.locationCompany ? scope.ticket.locationCompany.name : null; } } else { ownerCompany = scope.ticket.owner.company ? scope.ticket.owner.company.name : ''; authorCompany = scope.ticket.author.company ? scope.ticket.author.company.name : ''; } if (scope.assignee.group.id === 'All') { ticketService.getAssigneeSupportGroupPersons(scope.assigneeCompany.id === 'All' ? '' : scope.assigneeCompany.name, scope.assigneeOrganization.id === 'All' ? '' : scope.assigneeOrganization.name, scope.search.text, scope.assigneeLoginId, role, null, customerCompany, null, ownerCompany, primaryCompany, authorCompany, true, true) .then(function (searchResult) { // it might happen that several async queries were in progress if a user were typing fast // but we are interested only in responses that correspond to the current scope.search.text value if (scope.search.text === searchText) { handleMembers(searchResult.results); } }) .finally(function () { state.processing = false; }); } else { ticketService.getAssigneeBySupportGroupId(scope.assignee.group.id, scope.search.text, scope.assigneeLoginId, null, role, null, true, locationCompany, null, null, true) .then(function (searchResult) { if (scope.search.text === searchText) { handleMembers(searchResult.results); } }) .finally(function () { state.processing = false; }); } }, pauseInterval); } else { scope.assignee.group.persons = []; } } else { scope.search.filterText = scope.search.text; } }; scope.setGroupToAll = function () { allSupportGroups.persons = []; scope.selectGroup(allSupportGroups); }; scope.getSupportGroupsForCompanyAndOrgByName = function (name) { var customerCompany = null, locationCompany = null, ownerCompany = null, authorCompany = null, primaryCompany = null; if (scope.ticket.type === EntityVO.TYPE_KNOWLEDGE) { primaryCompany = getPrimaryCompany().name; ownerCompany = scope.ticket.owner.company ? scope.ticket.owner.company.name : ''; authorCompany = scope.ticket.author.company ? scope.ticket.author.company.name : ''; if (primaryCompany === "All") { primaryCompany = null; } } else { if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { customerCompany = objectValueMapperService.getExactValueByFieldName('customerCompany') || ''; } else { customerCompany = (scope.ticket.customer && scope.ticket.customer.company) ? scope.ticket.customer.company.name : (scope.ticket.company ? scope.ticket.company.name : ''); } if ((scope.ticket.type === EntityVO.TYPE_TASK || scope.ticket.type === EntityVO.TYPE_CHANGE) && scope.ticket.id && scope.ticket.company) { locationCompany = scope.ticket.company.name; } else if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } else { locationCompany = scope.ticket.locationCompany ? scope.ticket.locationCompany.name : null; } } return searchModel.getAssigneeSupportGroupsForCompanyAndOrgByName(scope.assigneeCompany.id === 'All' ? '' : scope.assigneeCompany.name, scope.assigneeOrganization.id === 'All' ? '' : scope.assigneeOrganization.name, name, null, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany).then(function (result) { return { list: result.supportGroups, exceedsChunkSize: result.exceedsChunkSize }; }); }; function getCustomerCompanyName() { var companyName = null; if (scope.ticket.type !== EntityVO.TYPE_KNOWLEDGE) { if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { companyName = objectValueMapperService.getExactValueByFieldName('customerCompany'); } else if (scope.ticket.customer) { companyName = scope.ticket.customer.company && scope.ticket.customer.company.name; } else { companyName = scope.ticket.company ? scope.ticket.company.name : scope.ticket.selectedCompany; } } return companyName; } function getSupportGroupsForCompanyAndOrg(assigneeCompany, organization, role, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany) { return searchModel.getAssigneeSupportGroupsForCompanyAndOrg(assigneeCompany.id === 'All' ? '' : assigneeCompany.name, organization.id === 'All' ? '' : organization.name, role, null, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany) .then(function (groups) { groups.supportGroups = _.unique(groups.supportGroups, function (group) { return group.id; }); groups.supportGroups = _.sortBy(groups.supportGroups, function (group) { return group.name; }); state.tooManySupportGroups = groups.exceedsChunkSize; if (groups.supportGroups.length && groups.supportGroups[0].id !== 'All') { allSupportGroups.persons = []; groups.supportGroups.unshift(allSupportGroups); } var groupArr = []; _.forEach(groups.supportGroups, function (group) { if (group.id !== 'All') { if (groupArr.indexOf(group.name) === -1) { groupArr.push(group.name); } else { var filteredGroups = _.filter(groups.supportGroups, { name: group.name }); _.forEach(filteredGroups, function (filteredGroup) { filteredGroup.duplicate = true; }); } } }); return groups.supportGroups; }); } scope.getSupportOrganizationsByTextAndCompany = function (searchText) { var customerCompany = null, locationCompany = null, ownerCompany = null, authorCompany = null, primaryCompany = null; if (scope.ticket.type === EntityVO.TYPE_KNOWLEDGE) { primaryCompany = getPrimaryCompany().name; ownerCompany = scope.ticket.owner.company ? scope.ticket.owner.company.name : ''; authorCompany = scope.ticket.author.company ? scope.ticket.author.company.name : ''; if (primaryCompany === "All") { primaryCompany = null; } } else { if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { customerCompany = objectValueMapperService.getExactValueByFieldName('customerCompany') || ''; } else { customerCompany = (scope.ticket.customer && scope.ticket.customer.company) ? scope.ticket.customer.company.name : (scope.ticket.company ? scope.ticket.company.name : ''); } if ((scope.ticket.type === EntityVO.TYPE_TASK || scope.ticket.type === EntityVO.TYPE_CHANGE) && scope.ticket.id && scope.ticket.company) { locationCompany = scope.ticket.company.name; } else if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } else { locationCompany = scope.ticket.locationCompany ? scope.ticket.locationCompany.name : null; } } return searchModel.getSupportOrganizationsByTextAndCompany(searchText, scope.assigneeCompany.id === 'All' ? '' : scope.assigneeCompany.name, null, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany).then(function (result) { return { list: result.organizations, exceedsChunkSize: result.exceedsChunkSize }; }); }; function getSupportOrganizationsForCompany(assigneeCompany, role, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany) { return searchModel.getAssigneeSupportOrgByCompany(assigneeCompany.id === 'All' ? '' : assigneeCompany.name, null, role, customerCompany, locationCompany, ownerCompany, primaryCompany, authorCompany) .then(function (result) { state.tooManyOrganizations = result.exceedsChunkSize; if (result.organizations && result.organizations.length && result.organizations[0].id !== 'All') { result.organizations.unshift(allOrganizations); } var orgArr = []; _.forEach(result.organizations, function (organization) { if (organization.id !== 'All') { if (orgArr.indexOf(organization.name) === -1) { orgArr.push(organization.name); } else { var filteredOrgs = _.filter(result.organizations, { name: organization.name }); _.forEach(filteredOrgs, function (filteredOrg) { filteredOrg.duplicate = true; }); } } }); return result.organizations; }); } function getCompanies() { var options = {}, customerCompany; if (scope.ticket.type === EntityVO.TYPE_KNOWLEDGE) { var primaryCompany = getPrimaryCompany(); options = { ownerCompany: scope.ticket.owner.company ? scope.ticket.owner.company.name : '', authorCompany: scope.ticket.author.company ? scope.ticket.author.company.name : '' }; if (primaryCompany.name !== "All") { options.primaryCompany = primaryCompany.name; } } else { if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { customerCompany = objectValueMapperService.getExactValueByFieldName('customerCompany') || ''; } else { customerCompany = (scope.ticket.customer && scope.ticket.customer.company) ? scope.ticket.customer.company.name : (scope.ticket.company ? scope.ticket.company.name : ''); } if (!customerCompany) { customerCompany = scope.ticket.selectedCompany; } options = { customerCompany: customerCompany }; if (scope.ticket.locationCompany && scope.ticket.locationCompany.name) { options.locationCompany = scope.ticket.locationCompany.name; } else if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { options.locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } else if ((scope.ticket.type === EntityVO.TYPE_TASK || scope.ticket.type === EntityVO.TYPE_CHANGE) && scope.ticket.id && scope.ticket.company) { options.locationCompany = scope.ticket.company.name; } } if (role) { options.role = role; } return searchModel.getAssigneeSupportCompanies(options).then(function (companies) { state.tooManyCompanies = companies.exceedsChunkSize; if (companies.objects.length && companies.objects[0].id !== 'All') { companies.objects.unshift(allCompanies); } scope.companies = _.cloneDeep(companies.objects); }); } function getPrimaryCompany() { if (scope.ticket.allCompanies) { return _.find(scope.ticket.allCompanies, function (company) { return company.primary; }); } else { return scope.ticket.company; } } scope.getCompaniesByName = function (name) { var options = {}, customerCompany; if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { customerCompany = objectValueMapperService.getExactValueByFieldName('customerCompany') || ''; } else { customerCompany = (scope.ticket.customer && scope.ticket.customer.company) ? scope.ticket.customer.company.name : (scope.ticket.company ? scope.ticket.company.name : ''); } if (!customerCompany) { customerCompany = scope.ticket.selectedCompany; } options.customerCompany = customerCompany; if (scope.ticket.locationCompany && scope.ticket.locationCompany.name) { options.locationCompany = scope.ticket.locationCompany.name; } else if (scope.ticket.type === EntityVO.TYPE_INCIDENT || scope.ticket.type === EntityVO.TYPE_WORKORDER) { options.locationCompany = objectValueMapperService.getExactValueByFieldName('locationCompany'); } else if ((scope.ticket.type === EntityVO.TYPE_TASK || scope.ticket.type === EntityVO.TYPE_CHANGE) && scope.ticket.id && scope.ticket.company) { options.locationCompany = scope.ticket.company.name; } return searchModel.getAssigneeSupportCompaniesByText(name, options).then(function (companies) { scope.companies = _.cloneDeep(companies.objects); return { list: companies.objects, exceedsChunkSize: companies.exceedsChunkSize }; }); }; function handleMembers(members) { if (scope.assignee.person && scope.assignee.person.loginId && !scope.assignToMe) { scope.assignee.group.persons = _.reject(members, function (member) { return member.loginId === scope.assignee.person.loginId; }); } else { scope.assignee.group.persons = members; } scope.assignee.group.persons = _.sortBy(scope.assignee.group.persons, function (person) { return person.fullName.toLowerCase(); }); if (scope.assignee.group.id === 'All') { var originalList = getUniqueUsersBySupportGroups(scope.assignee.group.persons); var newList = []; for (var count = 0; count < (originalList.length - 1); count++) { if (originalList[count].loginId === originalList[count + 1].loginId) { var parent = _.cloneDeep(originalList[count]); parent.type = 'parent'; newList.push(parent); originalList[count].type = 'child'; newList.push(originalList[count]); count++; var numOfChildren = 1; do { originalList[count].type = 'child'; newList.push(originalList[count]); numOfChildren++; count++; } while (count < originalList.length && originalList[count - 1].loginId === originalList[count].loginId); parent.numOfChildren = numOfChildren; count--; } else { newList.push(originalList[count]); } } if (count < originalList.length) { newList.push(originalList[count]); } scope.assignee.group.persons = _.sortBy(newList, function (person) { return person.fullName.toLowerCase(); }); if (scope.assignToMe && scope.assignee.group.persons.length > 1) { scope.assignToPerson(scope.assignee.group.persons[0]); } } completeProcessing(); } function getUniqueUsersBySupportGroups(users) { return (users || []).reduce(function (acc, person) { var hasUserFromThisGroup = acc.some(function (item) { return item.supportGroupId === person.supportGroupId && item.loginId === person.loginId; }); if (!hasUserFromThisGroup) { acc.push(person); } return acc; }, []); } function completeProcessing() { state.processing = false; } scope.onValueChange = function () { scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name }); }; scope.$watch('data.setValueFlag', function (value) { if (value !== '#$#' && value) { scope.data.value = value; scope.onValueChange(); scope.data.setValueFlag = '#$#'; } }); scope.state = state; init(); } }; }]); }());