Fix modalità invio email
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using GPW.CORE.Api.Data;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NLog;
|
||||
using System.Text;
|
||||
@@ -17,30 +18,87 @@ namespace GPW.CORE.Api.Controllers
|
||||
protected ApiDataService dataService { get; set; }
|
||||
|
||||
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
||||
private IConfiguration _configuration;
|
||||
|
||||
public ProjCheckController(ILogger<ProjCheckController> logger, ApiDataService DataService)
|
||||
/// <summary>
|
||||
/// Avvio controller x CheckProgetti
|
||||
/// </summary>
|
||||
/// <param name="logger"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="DataService"></param>
|
||||
public ProjCheckController(ILogger<ProjCheckController> logger, IConfiguration configuration, IEmailSender emailSender, ApiDataService DataService)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_emailSender = emailSender;
|
||||
dataService = DataService;
|
||||
Log.Info("Avviata classe CheckProj");
|
||||
}
|
||||
|
||||
protected enum logType
|
||||
{
|
||||
none,
|
||||
trace,
|
||||
info,
|
||||
warn
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Effettua log del tipo richiesto aggiungendo riga anche allo stringbuilder indicato
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="tipoLog"></param>
|
||||
/// <param name="currSB"></param>
|
||||
protected void doLog(string message, logType tipoLog, ref StringBuilder currSB)
|
||||
{
|
||||
switch (tipoLog)
|
||||
{
|
||||
case logType.info:
|
||||
Log.Info(message);
|
||||
break;
|
||||
case logType.trace:
|
||||
Log.Trace(message);
|
||||
break;
|
||||
case logType.warn:
|
||||
Log.Warn(message);
|
||||
break;
|
||||
default:
|
||||
case logType.none:
|
||||
break;
|
||||
}
|
||||
currSB.AppendLine(message);
|
||||
}
|
||||
|
||||
[HttpGet("ResAlloc")]
|
||||
public async Task<string> Get()
|
||||
{
|
||||
// oggetti base
|
||||
string answ = "ND";
|
||||
StringBuilder sbMain = new StringBuilder();
|
||||
StringBuilder sbActions = new StringBuilder();
|
||||
sbActions.AppendLine("------------------------------");
|
||||
StringBuilder sbAllProj = new StringBuilder();
|
||||
string sepaMsg = "-----------------------------------";
|
||||
int numFix = 0;
|
||||
int numProj = 0;
|
||||
int numFasi = 0;
|
||||
|
||||
// recupero progetti attivi...
|
||||
Log.Info("----------------------------------------------");
|
||||
doLog("Esito della verifica stato ore progetto (allocate/consumate)", logType.info, ref sbMain);
|
||||
doLog("<div style=\"font-size: 1.3em; color: #00AACC;\">", logType.none, ref sbMain);
|
||||
var currProj = await dataService.AnagProjActiv();
|
||||
Log.Info($"Trovati {currProj.Count} Progetti Attivi");
|
||||
numProj = currProj.Count;
|
||||
doLog($"Progetti Attivi: <b>{numProj}</b>", logType.info, ref sbMain);
|
||||
doLog("</div>", logType.none, ref sbMain);
|
||||
|
||||
// leggo fasi attive
|
||||
var currFasiAct = await dataService.AnagFasiActiv();
|
||||
Log.Info($"Trovate {currFasiAct.Count} Fasi Attive");
|
||||
Log.Info("----------------------------------------------");
|
||||
numFasi = currFasiAct.Count;
|
||||
doLog("<div style=\"font-size: 1.2em; color: #22BB22;\">", logType.none, ref sbMain);
|
||||
doLog($"Fasi Attive: <b>{numFasi}</b>", logType.info, ref sbMain);
|
||||
doLog("</div>", logType.none, ref sbMain);
|
||||
|
||||
// ciclo elenco fasi ancestor di ognuno...
|
||||
doLog(sepaMsg, logType.none, ref sbActions);
|
||||
doLog("<b>Elenco Progetti valutati</b>", logType.none, ref sbAllProj);
|
||||
foreach (var itemProj in currProj)
|
||||
{
|
||||
// cerco le sue fasi PARENT...
|
||||
@@ -48,7 +106,7 @@ namespace GPW.CORE.Api.Controllers
|
||||
.Where(x => x.IdxProgetto == itemProj.IdxProgetto && x.IdxFaseAncest == 0)
|
||||
.ToList();
|
||||
string projData = $"[P.{itemProj.IdxProgetto}] {itemProj.Gruppo} | {itemProj.ClienteNav.RagSociale} | {itemProj.NomeProj} | {subsetFasi.Count} Fasi";
|
||||
Log.Info(projData);
|
||||
doLog(projData, logType.trace, ref sbAllProj);
|
||||
|
||||
// ciclo x ogni fase Parent
|
||||
foreach (var parentFase in subsetFasi)
|
||||
@@ -59,19 +117,58 @@ namespace GPW.CORE.Api.Controllers
|
||||
// se ore usate > (ore budget x %) --> CHIUDO!
|
||||
if (statoFase != null && statoFase.totOre >= (statoFase.budgetTime * (decimal)statoFase.percOpen))
|
||||
{
|
||||
// segno che ho +1 fix
|
||||
numFix++;
|
||||
// x ora LOG... poi chiudo...
|
||||
string currAction = $"[F.{parentFase.IdxFase}] Budget esaurito in Fase {parentFase.NomeFase} {statoFase.totOre:N2}h consumate, budgt {statoFase.budgetTime:N0} al {statoFase.percOpen:P1}";
|
||||
Log.Warn(currAction);
|
||||
sbActions.AppendLine(projData);
|
||||
sbActions.AppendLine(currAction);
|
||||
sbActions.AppendLine("------------------------------");
|
||||
|
||||
doLog(projData, logType.none, ref sbActions);
|
||||
doLog(currAction, logType.warn, ref sbActions);
|
||||
doLog(sepaMsg, logType.none, ref sbActions);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (numFix > 0)
|
||||
{
|
||||
doLog("<div style=\"font-size: 1.1em;\">", logType.none, ref sbMain);
|
||||
doLog($"Fasi critiche: <b>{numFix}</b>", logType.info, ref sbMain);
|
||||
doLog("</div>", logType.none, ref sbMain);
|
||||
}
|
||||
|
||||
// predispongo email
|
||||
string msgTopic = "GPW: effettuata verifica progetti";
|
||||
|
||||
string msgBody = sbMain.ToString();
|
||||
// se ho errori loggo...
|
||||
if (numFix > 0)
|
||||
{
|
||||
msgBody += sbActions.ToString();
|
||||
}
|
||||
msgBody += "<br/>";
|
||||
msgBody += "<hr/>";
|
||||
msgBody += "<br/>";
|
||||
msgBody += sbAllProj.ToString();
|
||||
//{ HtmlEncoder.Default.Encode(callbackUrl)}
|
||||
|
||||
// invio email
|
||||
await sendEmail(msgTopic, msgBody.Replace($"{Environment.NewLine}","<br/>"));
|
||||
|
||||
// ritorno solo LOG azioni
|
||||
answ = sbActions.ToString();
|
||||
answ = numFix == 0 ? $"ALL {numProj} Proj OK" : $"Proj: {numProj} | Fasi: {numFasi} | Fixed/Closed: {numFix}";
|
||||
return answ;
|
||||
}
|
||||
|
||||
|
||||
private readonly IEmailSender _emailSender;
|
||||
|
||||
protected async Task sendEmail(string subject, string message)
|
||||
{
|
||||
string emailRaw = _configuration["MailDest:ProjCheck"];
|
||||
List<string> emailDestList = emailRaw.Split(",").ToList();
|
||||
foreach (var dest in emailDestList)
|
||||
{
|
||||
await _emailSender.SendEmailAsync(dest, subject, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user