5afbd33648
- update conf - aggiunta metodo test invio email
288 lines
10 KiB
C#
288 lines
10 KiB
C#
using Core;
|
|
using LiMan.APi.Data;
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LiMan.APi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller livello LICENZA
|
|
/// </summary>
|
|
[Route("api/licenza")]
|
|
[ApiController]
|
|
public class LicenzaController : ControllerBase
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Init Controller Licenze
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="emailSender"></param>
|
|
/// <param name="DataService"></param>
|
|
public LicenzaController(IConfiguration configuration, IEmailSender emailSender, ApiDataService DataService)
|
|
{
|
|
_configuration = configuration;
|
|
_emailSender = emailSender;
|
|
dataService = DataService;
|
|
Log.Info("Avviata classe LicenzaController");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Recupera dati Licenza Applicativa (id licenza + num utenze) dati cliente + programma + licenza ATTUALE
|
|
///
|
|
/// GET api/licenza/id?CodApp=xxx&Chiave=yyyy
|
|
/// </summary>
|
|
/// <param name="id">Codice cliente/Installazione</param>
|
|
/// <param name="CodApp">Codice Applicazione</param>
|
|
/// <param name="Chiave">Chiave licenza da validare</param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id}")]
|
|
public async Task<List<DB.DTO.ApplicativoDTO>> Get(string id, string CodApp, string Chiave)
|
|
{
|
|
var result = await dataService.LicenzeSearch(id, CodApp, Chiave, false);
|
|
await dataService.recordCall(id, CodApp, $"GET:api/licenza:{Chiave}");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua verifica scadenze licenze secondo 3 livelli dati da diversi orizzonti temporali
|
|
/// - urgente: entro 1 settimana
|
|
/// - importante: entro 1 mese
|
|
/// - info: entro 2 mesi
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("CheckScadenze")]
|
|
public async Task<string> Get()
|
|
{
|
|
// oggetti base
|
|
string answ = "ND";
|
|
string msgBody = "";
|
|
StringBuilder sbMain = new StringBuilder();
|
|
StringBuilder sbActions = new StringBuilder();
|
|
StringBuilder sbAllProj = new StringBuilder();
|
|
|
|
// orizzonti da valutare
|
|
DateTime oggi = DateTime.Today;
|
|
|
|
doLog("Esito della verifica Licenze (scadute/in scadenza)", logType.info, ref sbMain);
|
|
|
|
// verifico quelle GIA' SCADUTE (< oggi)
|
|
List<DB.DTO.ApplicativoDTO> elencoScadute = await dataService.LicenzeExpiring(DateTime.MinValue, oggi);
|
|
List<DB.DTO.ApplicativoDTO> elencoWeek = await dataService.LicenzeExpiring(oggi, oggi.AddDays(8));
|
|
List<DB.DTO.ApplicativoDTO> elencoMonth = await dataService.LicenzeExpiring(oggi.AddDays(8), oggi.AddDays(1).AddMonths(1));
|
|
List<DB.DTO.ApplicativoDTO> elencoTrim = await dataService.LicenzeExpiring(oggi.AddDays(1).AddMonths(1), oggi.AddDays(1).AddMonths(3));
|
|
|
|
int numScadute = elencoScadute.Count;
|
|
int numWeek = elencoWeek.Count;
|
|
int numMonth = elencoMonth.Count;
|
|
int numTrim = elencoTrim.Count;
|
|
|
|
// display scadute scadute...
|
|
msgBody += formatCheckPeriodo(elencoScadute, "Licenze <b>SCADUTE</b>", "font-size: 1.3em; color: #D62424;");
|
|
msgBody += formatCheckPeriodo(elencoWeek, "Licenze in scadenza nella <b>SETTIMANA</b>", "font-size: 1.2em; color: #DF701C;");
|
|
msgBody += formatCheckPeriodo(elencoMonth, "Licenze in scadenza nel <b>MESE</b>", "font-size: 1.15em; color: #A99C45;");
|
|
msgBody += formatCheckPeriodo(elencoTrim, "Licenze in scadenza nel <b>TRIMESTRE</b>", "font-size: 1.1em; color: #9Dc935;");
|
|
|
|
// predispongo email
|
|
string msgTopic = "LiMan: verifica Licenze in scadenza";
|
|
|
|
// invio email
|
|
string destList = _configuration["MailDest:ExpiryNotify"];
|
|
await sendEmailExpiry(destList, msgTopic, msgBody.Replace($"{Environment.NewLine}", "<br/>"));
|
|
|
|
// ritorno solo LOG azioni
|
|
answ = $"Scadute {numScadute} | Settimana: {numWeek} | Mese: {numMonth} | Trimestre: {numTrim}";
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua test invio email
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("test-email")]
|
|
public async Task<string> TestEmail()
|
|
{
|
|
// oggetti base
|
|
string answ = "ND";
|
|
string msgBody = "Prova invio email a destinatari <b>MailDest:ExpiryNotify</b>";
|
|
// predispongo email
|
|
string msgTopic = "LiMan: Test invio email!";
|
|
|
|
// invio email
|
|
string destList = _configuration["MailDest:ExpiryNotify"];
|
|
await sendEmailExpiry(destList, msgTopic, msgBody.Replace($"{Environment.NewLine}", "<br/>"));
|
|
|
|
// ritorno solo LOG azioni
|
|
answ = $"Test effettuato!";
|
|
return answ;
|
|
}
|
|
|
|
/// POST api/licenza
|
|
/// <summary>
|
|
/// Recupera dati Licenza Applicativa (id licenza + num utenze) dati cliente + programma +
|
|
/// licenza ATTUALE
|
|
/// </summary>
|
|
/// <param name="AppInfo">Info licenza in formato LicenseCoord</param>
|
|
/// <returns></returns>
|
|
[HttpPost()]
|
|
public async Task<List<DB.DTO.ApplicativoDTO>> Post([FromBody] LicenseCoord AppInfo)
|
|
{
|
|
var result = await dataService.LicenzeSearch(AppInfo.CodInst, AppInfo.CodApp, AppInfo.MasterKey, false);
|
|
await dataService.recordCall(AppInfo.CodInst, AppInfo.CodApp, $"POST:api/licenza:{AppInfo.MasterKey}");
|
|
return result;
|
|
}
|
|
|
|
/// POST api/licenza/refreshPayload
|
|
/// <summary>
|
|
/// Effettua refresh del payload calcolato ed aggiorna record ed infine lo restituisce
|
|
/// </summary>
|
|
/// <param name="AppInfo">Info licenza in formato LicenseCoord</param>
|
|
[HttpPost("refreshPayload")]
|
|
public async Task<DB.DTO.ApplicativoDTO> RefreshPayload([FromBody] LicenseCoord AppInfo)
|
|
{
|
|
var done = await dataService.LicenzaRefreshPayload(AppInfo);
|
|
await dataService.recordCall(AppInfo.CodInst, AppInfo.CodApp, $"POST:api/licenza/refreshPayload:{AppInfo.MasterKey}");
|
|
return done;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Enums
|
|
|
|
protected enum logType
|
|
{
|
|
none,
|
|
local,
|
|
trace,
|
|
info,
|
|
warn
|
|
}
|
|
|
|
#endregion Protected Enums
|
|
|
|
#region Protected Properties
|
|
|
|
/// <summary>
|
|
/// Dataservice x accesso DB
|
|
/// </summary>
|
|
protected ApiDataService dataService { get; set; }
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
/// <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)
|
|
{
|
|
bool doLine = true;
|
|
switch (tipoLog)
|
|
{
|
|
case logType.info:
|
|
Log.Info(message);
|
|
break;
|
|
|
|
case logType.trace:
|
|
Log.Trace(message);
|
|
break;
|
|
|
|
case logType.warn:
|
|
Log.Warn(message);
|
|
break;
|
|
|
|
case logType.local:
|
|
break;
|
|
|
|
default:
|
|
case logType.none:
|
|
doLine = false;
|
|
break;
|
|
}
|
|
if (doLine)
|
|
{
|
|
currSB.AppendLine(message);
|
|
}
|
|
else
|
|
{
|
|
currSB.Append(message);
|
|
}
|
|
}
|
|
|
|
protected string formatCheckPeriodo(List<DB.DTO.ApplicativoDTO> elencoLicenze, string tipoVerifica, string titleStyle)
|
|
{
|
|
StringBuilder sbMain = new StringBuilder();
|
|
doLog($"<div style=\"{titleStyle}\">", logType.none, ref sbMain);
|
|
int numScadute = elencoLicenze.Count;
|
|
doLog($"{tipoVerifica}: <b>{numScadute}</b>", logType.info, ref sbMain);
|
|
doLog("<div style=\"font-size: 1em; color: black;\">", logType.none, ref sbMain);
|
|
if (elencoLicenze.Count == 0)
|
|
{
|
|
doLog($" - nessuna", logType.info, ref sbMain);
|
|
}
|
|
else
|
|
{
|
|
foreach (var item in elencoLicenze)
|
|
{
|
|
doLog($" - {item.Scadenza:yyyy.MM.dd} | <b>{item.CodInst}</b> | {item.Descrizione}", logType.info, ref sbMain);
|
|
}
|
|
}
|
|
doLog("</div>", logType.none, ref sbMain);
|
|
doLog("</div>", logType.none, ref sbMain);
|
|
return sbMain.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invio email a lista ExpiryNotify
|
|
/// </summary>
|
|
/// <param name="destList">Elenco destinatari comma separated</param>
|
|
/// <param name="subject">Oggetto Email</param>
|
|
/// <param name="message">MEssaggio</param>
|
|
/// <returns></returns>
|
|
protected async Task sendEmailExpiry(string destList, string subject, string message)
|
|
{
|
|
List<string> emailDestList = destList.Split(",").ToList();
|
|
foreach (var dest in emailDestList)
|
|
{
|
|
try
|
|
{
|
|
await _emailSender.SendEmailAsync(dest, subject, message);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante invio email:{Environment.NewLine}dest: {dest} | subject {subject}{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// Classe per logging
|
|
/// </summary>
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private readonly IEmailSender _emailSender;
|
|
|
|
private IConfiguration _configuration;
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |