using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using Microsoft.Extensions.Configuration; using System.Threading.Tasks; using TMTCronJob.Services; using TMTCronJob.Model; namespace TMTCronJob.Controllers { [ApiController] [Route("[controller]")] public class CronJobController : ControllerBase { private readonly ILogger _logger; MySqlDataReader _reader; string connectionString = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("ConnectionStrings")["DefaultConnection"]; public CronJobController(ILogger logger) { _logger = logger; _reader = null; } [HttpGet(Name = "RunCronJob")] public async Task Get() { string cronLog = $"DB::CronJob::{DateTime.Now.ToString("yyyy-MM-dd h:mm:ss tt")}"; //log when cron ran string statement = "DB::CronJob"; using (var connection = new MySqlConnection(connectionString)) { try { connection.Open(); //First try out the previous failed payments statement = "SELECT InfringementID,strInfringementNumber,transaction.strStatus, transaction.TransactionID, transaction.strAmount, transaction.dtPaid, strIssuingAuthority, strRecieptNumber, strProviderName, strInfringementSource, intSettlementTries FROM infringement INNER JOIN transaction ON infringement.refTrasactionID = transaction.TransactionID INNER JOIN paymentProvider ON transaction.refPaymentProviderID = paymentProvider.PaymentProviderID WHERE infringement.strStatus = 'Source Failed Payment'"; var cmd = new MySqlCommand(); cmd.Connection = connection; var paymentService = new PaymentService(connection); List eNatisInfridgments = new List(); List iForceInfridgments = new List(); List problemInfridgments = new List(); bool hasRows = paymentService.LoadInfringements(statement, eNatisInfridgments, iForceInfridgments); if (hasRows) { //Process previously failed payments based on source var iforcePaymentResult = await paymentService.ProcessIForcePayments(iForceInfridgments, problemInfridgments); var eNatisPaymentResult = await paymentService.ProcessENatisPayments(eNatisInfridgments, problemInfridgments); string adminLabel = "[" + (char)34 + "ADMIN" + (char)34 + "]"; if (problemInfridgments.Count > 0) { _reader = cmd.ExecuteReader(); statement = $"SELECT * FROM user WHERE strRole ='{adminLabel}'"; //Admin tag cmd.CommandText = statement; cmd.ExecuteNonQuery(); List adminUsers = new List { }; if (_reader.HasRows) { while (_reader.Read()) { adminUsers.Add(_reader.GetString(0)); } _reader.Close(); } var mailer = new MailService(); mailer.SendEmail(problemInfridgments, adminUsers); } iForceInfridgments.Clear(); eNatisInfridgments.Clear(); } else { statement = "NO Failed Payments Found"; //_logger.WriteLog("CRONJOB", statement, cronLog, "TMT"); } //Then try out the new payments statement = "SELECT TransactionID, InfringementID,strInfringementNumber,transaction.strStatus, transaction.strAmount, transaction.dtPaid, strIssuingAuthority, strRecieptNumber, strProviderName, strInfringementSource, intSettlementTries FROM infringement INNER JOIN transaction ON infringement.refTrasactionID = transaction.TransactionID INNER JOIN paymentProvider ON transaction.refPaymentProviderID = paymentProvider.PaymentProviderID WHERE transaction.intPaid = 'YES'"; hasRows = paymentService.LoadInfringements(statement, eNatisInfridgments, iForceInfridgments); if (hasRows) { //Process new payments. var iforcePaymentResult = await paymentService.ProcessIForcePayments(iForceInfridgments); var eNatisPaymentResult = await paymentService.ProcessENatisPayments(eNatisInfridgments); // _logger.WriteLog("CRONJOB", $"Processed iForce: {iForceInfridgments.Count} Processed eNatis: {eNatisInfridgments.Count} ", cronLog, "TMT"); } else { statement = "NO New Payments Found"; // _logger.WriteLog("CRONJOB", statement, cronLog, "TMT"); } } catch (Exception ex) { //_logger.WriteLog("CRONJOB", ex.Message, cronLog, "TMT"); } finally { if (_reader != null) _reader.Close(); } } return Ok("Cronjob completed"); } } }