SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/approval/approval-model.js

89 lines
4.4 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('myitsmApp')
.factory('approvalModel', ['approvalService', '$q', 'systemAlertService', '$filter',
function (approvalService, $q, systemAlertService, $filter) {
var approvalModel = {
approvals: []
};
/**
* @ngdoc method
* @name myitsmApp.approvalModel#getListOfApprovers
*
* @description
* Retrieves information about approvers for the context
* @param {Object} context - Object that has information about the ticket/KA required in the request API.
*/
approvalModel.getListOfApprovers = function (context, ignoreCache) {
/**
* @namespace
* @property {object} context - Approval Request parameters.
* @property {string} context.id - UUID of the ticket/KA
* @property {string} context.type - Ticket/KA type value
*/
var params = { id: context.id, type: context.type };
if (approvalModel.approvals[context.id] && !ignoreCache) {
return $q.when(approvalModel.approvals[context.id]);
}
approvalModel.approvals[context.id] = approvalService.getListOfApprovers(params)
.finally(function () {
approvalModel.getListOfApproversPromise = null;
});
return $q.when(approvalModel.approvals[context.id]);
};
/**
* @ngdoc method
* @name myitsmApp.approvalModel#sendReviewResult
*
* @description
* Saves user review for a pending change request
* @param {Object} approval - Object that has information about the ticket/KA required in the request API.
*/
approvalModel.sendReviewResult = function (approval) {
/**
* @namespace
* @property {object} approval - Approval Request parameters.
* @property {string} approval.type - Type of review result(approve, reject or hold)
* @property {string} approval.parentId - UUID of the ticket/KA
* @property {string} approval.parentType - Ticket/KA type value
* @property {string} approval.signatureId - Unique id of the pending user review
* @property {string} approval.justification - User's review justification
*/
approvalModel.approvals[approval[0].parentId] = angular.noop();
return approvalService.sendReviewResult(approval)
.catch(function (error) {
if (error) {
var errorExpr = /ERROR\s[(](\d+)[)]/, errorText = error.data ? error.data.error : error.error, errorMatch = errorExpr.exec(errorText), errorId;
if (errorMatch) {
errorId = parseInt(errorMatch[1]);
//45490 error id for missing required user password for approval.
//UC does not support approval scenario with password, so error should be shown to the user with proper description
if (errorId === 45490) {
errorText = $filter('i18n')('approval.error.46490');
}
}
systemAlertService.error({
text: errorText,
clear: false
});
}
return $q.reject(error);
});
};
approvalModel.getApproverByText = function (searchText) {
return approvalService.getApproverByText(searchText);
};
approvalModel.addApprover = function (type, approverId, ticketId, ticketType, TicketStatus) {
return approvalService.addApprover(type, approverId, ticketId, ticketType, TicketStatus);
};
approvalModel.clearListOfApproversCache = function (contextId) {
if (approvalModel.approvals[contextId]) {
delete approvalModel.approvals[contextId];
}
};
return approvalModel;
}
]);
})();