Esempio n. 1
0
        public IHttpActionResult Insert([FromBody] TicketMensagemRequest request)
        {
            UsuarioCliente          usuarioCliente   = null;
            AtendenteEmpresa        atendenteEmpresa = null;
            Ticket                  ticket           = null;
            List <AtendenteEmpresa> listaAtendentes  = null;

            try
            {
                //Valida objeto
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Dados inválidos."));
                }

                var entity            = Mapper.Map <TicketMensagemRequest, TicketMensagem>(request);
                var pathAnexosUsuario = request.PathAnexos;

                ticket = _ticketBusiness.GetById(request.IdTicket);

                ticket.DataHoraAlteracao      = DateTime.Now;
                ticket.DataHoraUltimaMensagem = DateTime.Now;

                //Se for uma mensagem interna enviada pelo suporte
                if (request.UserType == "S" && request.Interno)
                {
                    entity.IdAtendenteEmpresa = request.IdAutor;
                    ticket.IdStatusTicket     = request.IdStatusTicket > 0 ? request.IdStatusTicket : 5; //Em Análise
                }
                else
                {
                    //Se for uma mensagem enviada pelo suporte
                    if (request.UserType == "S" && !request.Interno)
                    {
                        entity.IdAtendenteEmpresa = request.IdAutor;
                        ticket.IdStatusTicket     = request.IdStatusTicket > 0 ? request.IdStatusTicket : 4; //Pendente com Cliente
                    }
                    else
                    {
                        //Se for uma mensagem enviada pelo usuário cliente
                        if (request.UserType == "C")
                        {
                            entity.IdUsuarioCliente = request.IdAutor;
                            ticket.IdStatusTicket   = 1; //Aguardando Atendimento
                        }
                    }
                }

                //Insere a nova mensagem
                _ticketMensagemBusiness.Insert(ref entity);

                if (entity.Id > 0)
                {
                    //Atualiza o status do ticket, para refletir o novo momento do atendimento
                    _ticketBusiness.UpdateStatusTicket(ticket);

                    //Monta response
                    _result = Ok(Retorno <TicketMensagemResponse> .Criar(true, "Inclusão Realizada Com Sucesso", Mapper.Map <TicketMensagem, TicketMensagemResponse>(entity)));

                    if (Directory.Exists(pathAnexosUsuario))
                    {
                        //Zipa todos os anexos
                        var zipName = Arquivo.Compress(ConfigurationManager.AppSettings["CaminhoFisicoAnexo"], pathAnexosUsuario, entity.Id);

                        //======================================
                        //Guarda anexo (zip) no banco de dados
                        //======================================
                        var anexo = new Anexo
                        {
                            IdTicketMensagem = entity.Id,
                            Nome             = zipName
                        };

                        _anexoBusiness.Insert(ref anexo);
                    }

                    //===========================================================================================
                    //Enviar email de confirmação de nova mensagem
                    //===========================================================================================

                    var ticketResponse = _ticketBusiness.GetByIdFilled(request.IdTicket, false);

                    if (request.UserType == "S")
                    {
                        usuarioCliente   = _usuarioClienteBusiness.GetById(ticketResponse.UsuarioCliente.Id);
                        atendenteEmpresa = _atendenteEmpresaBusiness.GetById(request.IdAutor);

                        if (atendenteEmpresa.Copia)
                        {
                            listaAtendentes = _atendenteEmpresaBusiness.GetList(x => x.IdEmpresa == atendenteEmpresa.IdEmpresa && x.Id != atendenteEmpresa.Id).ToList();
                        }
                    }
                    else
                    {
                        usuarioCliente  = _usuarioClienteBusiness.GetById(request.IdAutor);
                        listaAtendentes = _atendenteEmpresaBusiness.GetAll(ticketResponse.UsuarioCliente.Cliente.IdEmpresa).ToList();
                    }

                    try
                    {
                        _ticketMensagemBusiness.EnviarEmailConfirmacao(request, entity, ticketResponse, atendenteEmpresa, usuarioCliente, listaAtendentes);
                    }
                    catch (Exception)
                    {
                        //Monta response
                        _result = Ok(Retorno <TicketMensagemResponse> .Criar(true, "Inclusão Realizada Com Sucesso - Email de confirmação não enviado", Mapper.Map <TicketMensagem, TicketMensagemResponse>(entity)));
                    }

                    //===========================================================================================
                }

                //Retorna o response
                return(_result);
            }
            catch (Exception)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
Esempio n. 2
0
        //[AllowAnonymous]
        public IHttpActionResult Insert([FromBody] TicketRequest request)
        {
            UsuarioCliente          usuarioCliente   = null;
            AtendenteEmpresa        atendenteEmpresa = null;
            List <AtendenteEmpresa> listaAtendentes  = null;

            try
            {
                //Valida objeto
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Dados inválidos."));
                }

                var entity            = Mapper.Map <TicketRequest, Ticket>(request);
                var pathAnexosUsuario = request.PathAnexos;

                entity.DataHoraInicial = DateTime.Now;

                _ticketBusiness.Insert(ref entity);

                if (entity.Id > 0)
                {
                    //Monta response
                    _result = Ok(Retorno <TicketResponse> .Criar(true, "Inclusão Realizada Com Sucesso", Mapper.Map <Ticket, TicketResponse>(entity)));

                    //Trata dos anexos
                    if (Directory.Exists(pathAnexosUsuario))
                    {
                        //Zipa todos os anexos
                        var zipName = Arquivo.Compress(ConfigurationManager.AppSettings["CaminhoFisicoAnexo"], pathAnexosUsuario, entity.Id);

                        //======================================
                        //Guarda anexo (zip) no banco de dados
                        //======================================
                        var anexo = new Anexo
                        {
                            IdTicket = entity.Id,
                            Nome     = zipName
                        };

                        _anexoBusiness.Insert(ref anexo);
                    }

                    //===========================================================================================
                    //Enviar email de confirmação de criação do novo atendimento
                    //===========================================================================================

                    var ticketResponse = _ticketBusiness.GetByIdFilled(entity.Id, false);

                    if (request.UserTypeAgent == "S")
                    {
                        usuarioCliente   = _usuarioClienteBusiness.GetById(request.IdUsuarioCliente);
                        atendenteEmpresa = _atendenteEmpresaBusiness.GetById(request.IdAtendente);

                        if (atendenteEmpresa != null)
                        {
                            if (atendenteEmpresa.Copia)
                            {
                                listaAtendentes = _atendenteEmpresaBusiness.GetList(x => x.IdEmpresa == atendenteEmpresa.IdEmpresa && x.IdEmpresa != atendenteEmpresa.IdEmpresa).ToList();
                            }
                        }
                    }
                    else
                    {
                        usuarioCliente  = _usuarioClienteBusiness.GetById(request.IdUsuarioCliente);
                        listaAtendentes = _atendenteEmpresaBusiness.GetAll(ticketResponse.UsuarioCliente.Cliente.IdEmpresa).ToList();
                    }

                    var statusTicket = _statusTicketBusiness.GetById(request.IdStatusTicket);

                    try
                    {
                        _ticketBusiness.EnviarEmailConfirmacao(request.UserTypeAgent, statusTicket, null, entity, usuarioCliente, atendenteEmpresa, listaAtendentes, "insert");
                    }
                    catch (Exception)
                    {
                        //Monta response
                        _result = Ok(Retorno <TicketResponse> .Criar(true, "Inclusão Realizada Com Sucesso - Email de confirmação não enviado", Mapper.Map <Ticket, TicketResponse>(entity)));
                    }

                    //===========================================================================================
                }

                //Retorna o response
                return(_result);
            }
            catch (Exception)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }