/**
* @ngdoc directive
* @name s4c.components.avisoOperacional.s4cAvisoOperacionalEncaminhar
* @module s4c.components.avisoOperacional
*
* @description
* `encaminharMensagemCtrl` Responsável por encaminhar as mensagens de aviso operacional.
*
*
*/
(function () {
'use strict';
function encaminharMensagemCtrl($scope, $q, TelegramService, Departamento, Perfil) {
$scope.res = $scope.$root.res;
$scope.list = [];
/**
* Fecha a diretiva
*
* @method desativar
*
*
*/
$scope.desativar = function () {
$scope.ativo = false;
};
/**
* Listener para verificar se há alteração no atributo ativo
*
* @method watch
*
* @param newVal {Boolean} Novo valor
* @param oldVal {Boolean} Valor antigo
*
*/
$scope.$watch('ativo', function (newVal, oldVal) {
if (oldVal === false && newVal === true) {
var lists = $q.all([
TelegramService.getGroups(),
TelegramService.getUsers()
]);
lists.then(function (list) {
var groups = filterLists(list[0], $scope.mensagem.Destinatarios);
var users = filterLists(list[1], $scope.mensagem.Destinatarios);
$scope.list = users.concat(groups);
});
}
});
$scope.tipos = [{
label: 'Todos',
value: ''
}, {
label: 'Grupo',
value: 'isGroup'
}, {
label: 'Usuário',
value: 'isUser'
}];
/**
* Obtem a lista de agências
*
* @method getAgencias
*
*
*/
Departamento.getAgencias().then(function (departamentos) {
$scope.departamentos = _.map(departamentos, function (departamento) {
return {
label: departamento.nome,
value: departamento.id
}
});
$scope.departamentos.unshift({
label: 'Todos',
value: 0
})
})
/**
* Obtem a lista de perfis
*
* @method getPerfis
*
*
*/
Perfil.getPerfis().then(function (perfis) {
perfis = perfis.filter(function (p) {
return p.DepartamentoId;
});
$scope.perfis = _.map(perfis, function (perfil) {
return {
label: perfil.nome,
value: perfil.id
}
});
$scope.perfis.unshift({
label: 'Todos',
value: 0
})
})
/**
* Envia as mensagens
*
* @method enviarEncaminhamento
*
*
*/
$scope.enviarEncaminhamento = function () {
var encaminhamento = {
message: $scope.mensagem.mensagem,
users: [],
groups: []
};
_.forEach($scope.list, function (value, key) {
if (value.isGroup && value.selected)
encaminhamento.groups.push(value);
if (value.isUser && value.selected)
encaminhamento.users.push(value);
});
TelegramService.sendMessageBulk(encaminhamento).then(function (mensagem) {
TelegramService.getMessages().then(function (messages) {
$scope.mensagens = messages;
$scope.ativo = false;
$scope.detalhamentoAtivo = false;
});
});
};
$scope.filters = {
tipo: '',
departamento: 0,
perfil: 0
}
/**
* Lista de filtros
*
* @method filterList
*
*
*/
$scope.filterList = function () {
return _.filter($scope.list, function (item) {
return (!$scope.filters.tipo || item[$scope.filters.tipo])
&& (!$scope.filters.departamento || item.DepartamentoId == $scope.filters.departamento)
&& (!$scope.filters.perfil || _.chain(item.Perfils)
.map('id')
.includes($scope.filters.perfil)
.value())
});
}
/**
* Filtra as listas
*
* @method filterLists
*
* @param lists {Array} Listas
* @param listBase {Array} Lista Base
*/
function filterLists(lists, listBase) {
_.forEach(lists, function (list, key) {
var foundList = _.find(listBase, function (o) {
return o.id == list.id;
});
list.selected = ((foundList !== undefined) ? true : false);
list.isGroup = ((list.hasOwnProperty('Usuarios')) ? true : false);
list.isUser = ((list.hasOwnProperty('GrupoMensageriaExternas')) ? true : false);
});
return lists;
}
}
encaminharMensagemCtrl.$inject = ['$scope', '$q', 'TelegramService', 'Departamento', 'Perfil'];
angular.module('s4c.components.avisoOperacional')
.directive('s4cAvisoOperacionalEncaminhar', function () {
return {
restrict: 'EA',
templateUrl: 'app/directives/aviso-operacional/encaminhar-mensagem/encaminhar-mensagem.html',
replace: true,
scope: {
'ativo': '=ativo',
'mensagem': '=mensagem',
'detalhamentoAtivo': '=detalhamentoAtivo',
'mensagens': '=mensagens'
},
controller: encaminharMensagemCtrl
};
});
}());