public async Task <IActionResult> Index(List <ActivarPersonalTalentoHumanoViewModel> model) { ListaActivarPersonalTalentoHumanoViewModel listaViewModel = new ListaActivarPersonalTalentoHumanoViewModel(); listaViewModel.listaAPTHVM = model; try { Response response = await apiServicio.InsertarAsync(listaViewModel, new Uri(WebApp.BaseAddress), "api/ActivacionesPersonalTalentoHumano/InsertarActivacionesPersonalTalentoHumano"); if (response.IsSuccess) { var mensajeResultado = response.Message; if (response.Resultado + "" != "") { mensajeResultado = response.Resultado + ""; } else { mensajeResultado = response.Message; } //return RedirectToAction("Index", "ActivacionesPersonalTalentoHumano", new { mensaje = mensajeResultado }); return(this.RedireccionarMensajeTime( "ActivacionesPersonalTalentoHumano", "Index", $"{Mensaje.Informacion}|{mensajeResultado}|{"7000"}" )); } //return RedirectToAction("Index", "ActivacionesPersonalTalentoHumano", new { mensaje = response.Message }); return(this.RedireccionarMensajeTime( "ActivacionesPersonalTalentoHumano", "Index", $"{Mensaje.Error}|{response.Message}|{"7000"}" )); } catch (Exception ex) { return(RedirectToAction("Index", "ActivacionesPersonalTalentoHumano", new { mensaje = Mensaje.Excepcion })); } }
public async Task <Response> InsertarActivacionesPersonalTalentoHumano([FromBody] ListaActivarPersonalTalentoHumanoViewModel listaRecibida) { using (var transaction = db.Database.BeginTransaction()) { List <Response> correoResponse = new List <Response>(); try { var resultadoMensaje = ""; var lista = listaRecibida.listaAPTHVM; var hoy = DateTime.Now.ToString("dd/MM/yyyy"); int estado = 0; if (!ModelState.IsValid) { return(new Response { IsSuccess = false, Message = "" }); } for (int i = 0; i < lista.Count; i++) { if (lista.ElementAt(i).Nombre != null) { var activarPersonalTalentoHumano = new ActivarPersonalTalentoHumano(); activarPersonalTalentoHumano.IdDependencia = lista.ElementAt(i).IdDependencia; activarPersonalTalentoHumano.Fecha = DateTime.Parse(hoy); if (lista.ElementAt(i).Estado == false) { estado = Convert.ToInt32(Constantes.ActivacionPersonalValorDesactivado); } else { estado = Convert.ToInt32(Constantes.ActivacionPersonalValorActivado); } activarPersonalTalentoHumano.Estado = estado; Response response = Existe(activarPersonalTalentoHumano); if (!response.IsSuccess && estado == Convert.ToInt32(Constantes.ActivacionPersonalValorActivado)) { var empleado = await GetJefePorDependencia(activarPersonalTalentoHumano.IdDependencia); if (empleado != null && empleado.IdEmpleado > 0) { var correo = empleado.Persona.CorreoPrivado; correoResponse.Add(await EnviarMailDesdeCorreoTalentohumano(correo, empleado.Dependencia.Nombre)); db.ActivarPersonalTalentoHumano.Add(activarPersonalTalentoHumano); await db.SaveChangesAsync(); } else { correoResponse.Add(new Response { IsSuccess = false, Message = Mensaje.DependenciaSinJefe + " " + lista.ElementAt(i).Nombre } ); } } else { var actualizar = await db.ActivarPersonalTalentoHumano .Where(apt => apt.IdActivarPersonalTalentoHumano == Convert.ToInt32(response.Resultado) ).FirstOrDefaultAsync(); if ( response.IsSuccess && actualizar.Estado == Convert.ToInt32(Constantes.ActivacionPersonalValorDesactivado) && estado == Convert.ToInt32(Constantes.ActivacionPersonalValorActivado) ) { actualizar.Estado = Convert.ToInt32(Constantes.ActivacionPersonalValorActivado); var empleado = await GetJefePorDependencia(activarPersonalTalentoHumano.IdDependencia); if (empleado != null && empleado.IdEmpleado > 0) { var correo = empleado.Persona.CorreoPrivado; correoResponse.Add(await EnviarMailDesdeCorreoTalentohumano(correo, empleado.Dependencia.Nombre)); db.ActivarPersonalTalentoHumano.Update(actualizar); await db.SaveChangesAsync(); } else { correoResponse.Add(new Response { IsSuccess = false, Message = Mensaje.DependenciaSinJefe + " " + lista.ElementAt(i).Nombre } ); } } } } } for (int k = 0; k < correoResponse.Count; k++) { if (correoResponse.ElementAt(k).IsSuccess == false) { if (resultadoMensaje.Length < 1) { resultadoMensaje = Mensaje.ErrorCorreo + ", " + correoResponse.ElementAt(k).Message; } else { resultadoMensaje = resultadoMensaje + ", " + correoResponse.ElementAt(k).Message + "\n"; } } } transaction.Commit(); return(new Response { IsSuccess = true, Message = Mensaje.CorreoSatisfactorio, Resultado = resultadoMensaje }); } catch (Exception ex) { transaction.Rollback(); return(new Response { IsSuccess = false, Message = correoResponse.Last().Message, }); } } }