"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .service('approvalService', ['$resource', '$q', '$modal', function ($resource, $q, $modal) { var resource = $resource('', {}, { useApprovalAPI: { url: '/smartit/rest/approval', method: 'POST', isArray: true }, getApproverByText: { url: '/smartit/rest/person/search', method: 'GET', isArray: true }, addApprover: { url: '/smartit/rest/approval', method: 'POST', isArray: true } }), approvalSerivce = this; /** * @ngdoc method * @name myitsmApp.ticketService#getListOfApprovers * @methodOf myitsmApp.ticketService * * @description * Retrieves information about approvers for the ticket * @param {Object} param - Object that has information abot the ticket required in the request API. * @param {String} param.id|parentId - UUID of the ticket * @param {String} param.type|parentType - Ticket type value */ this.getListOfApprovers = function (params) { var argsType = Object.prototype.toString.call(params).slice(8, -1), reqParams; if (argsType !== 'Object') { return $q.when({ error: 'Bad input parameter. Request cancelled' }); } else { if (params.id && params.type) { reqParams = { parentId: params.id, parentType: params.type }; } else if (!params.parentId || !params.parentType) { return $q.when({ error: 'Missing required parameter. Request cancelled' }); } else { reqParams = params; } reqParams.type = 'getListOfApprovers'; return resource.useApprovalAPI(null, [reqParams]).$promise .then(function (response) { return handleChangeApprovalData(response); }); } }; /** * @ngdoc method * @name myitsmApp.ticketService#sendReviewResult * @methodOf myitsmApp.ticketService * * @description * Retrieves information about approvers for the ticket * @param {object} approval - Approval Request parameters. * @param {string} approval.type - Type of review result(approve, reject or hold) * @param {string} approval.parentId - UUID of the ticket * @param {string} approval.parentType - Ticket type value * @param {string} approval.signatureId - Unique id of the pending user review * @param {string} approval.justification - User's review justification */ this.sendReviewResult = function (approval) { return resource.useApprovalAPI(null, approval).$promise; }; this.getApprovals = function (context) { if (context.approvalSummaries) { context.approvalList = { closed: [], open: [], rejected: [] }; _.forEach(context.approvalSummaries, function (approval) { var approvalValue = approval.status.value; if (_.contains(['Approved', 'Cancelled', 'Closed', 'Error'], approvalValue)) { context.approvalList.closed.push(approval); } else if (approvalValue === 'Rejected') { context.approvalList.rejected.push(approval); } else { context.approvalList.open.push(approval); } }); } }; this.showApproversDialog = function (approvalList, context, showClosedTabDefault) { return $modal.open({ templateUrl: 'views/approval/approvals-action-blade.html', windowClass: 'action-blade', controller: 'ApproversDialogController', resolve: { changeApprovalParams: function () { return { approvalList: approvalList, id: context && context.id, type: context && context.type, statusValue: context && context.status.value, addApproverAllowed: context && context.accessMappings && context.accessMappings.addApproverEditAllowed, showClosedTabDefault: showClosedTabDefault }; } } }); }; function handleChangeApprovalData(approvalResponse) { var approvalData; try { approvalData = approvalResponse[0].items[0][0].responseObject; } catch (e) { approvalData = { approvalSummaries: [] }; } if (approvalData.approvalSummaries && approvalData.approvalSummaries.length > 0) { approvalSerivce.getApprovals(approvalData); if (approvalData.approvalList.rejected.length) { approvalData.approvalList.closed = (approvalData.approvalList.closed || []).concat(approvalData.approvalList.rejected || []); } approvalData.groupedSummary = _.groupBy(approvalData.approvalSummaries, function (item) { item.status.name = item.status.value ? item.status.value.split(' ').join('_').toLowerCase() : ''; return item.status.value; }); _.each(approvalData.approvalSummaries, function (item) { if (item.type === 'person') { var person = item.approver; item.approver = new PersonVO().build(person); } }); } return approvalData || { approvalSummaries: [], isUserApprovalPending: false, groupedSummary: {} }; } this.getApproverByText = function (searchText) { var params = { role: 'approver', name: searchText }; return resource.getApproverByText(params).$promise.then(function (result) { return result[0].items; }); }; this.addApprover = function (type, approverId, ticketId, ticketType, TicketStatus) { var data = [{ parentId: ticketId, parentType: ticketType, type: type, status: TicketStatus, approverId: approverId }]; return resource.addApprover(null, data).$promise.then(function (result) { return result[0].items[0][0]; }); }; }]); }());