* Maintain FaKrosno api to handle Hangfire
* Added checking of missing EdiCo based on EdiCoTranslate and send email every 30 minutes * Added Admin Scheduler view
This commit is contained in:
30
FaKrosnoApi/Services/EmailService.cs
Normal file
30
FaKrosnoApi/Services/EmailService.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using FaKrosnoApi.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FaKrosnoApi.Services;
|
||||
|
||||
public class EmailService(IOptions<EmailSettingsModel> emailSettings) : IEmailService
|
||||
{
|
||||
private readonly EmailSettingsModel _emailSettings = emailSettings.Value;
|
||||
|
||||
public void SendEmail(string subject, string body)
|
||||
{
|
||||
using var smtpClient = new SmtpClient(_emailSettings.SmtpServer, _emailSettings.Port);
|
||||
smtpClient.EnableSsl = true;
|
||||
smtpClient.UseDefaultCredentials = false;
|
||||
smtpClient.Credentials = new NetworkCredential(_emailSettings.SenderEmail, _emailSettings.SenderPassword);
|
||||
|
||||
var mailMessage = new MailMessage
|
||||
{
|
||||
From = new MailAddress(_emailSettings.SenderEmail),
|
||||
Subject = subject,
|
||||
Body = body
|
||||
};
|
||||
|
||||
mailMessage.To.Add(_emailSettings.RecipientEmail);
|
||||
|
||||
smtpClient.Send(mailMessage);
|
||||
}
|
||||
}
|
||||
6
FaKrosnoApi/Services/IEmailService.cs
Normal file
6
FaKrosnoApi/Services/IEmailService.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace FaKrosnoApi.Services;
|
||||
|
||||
public interface IEmailService
|
||||
{
|
||||
void SendEmail(string subject, string body);
|
||||
}
|
||||
6
FaKrosnoApi/Services/IScheduleJobService.cs
Normal file
6
FaKrosnoApi/Services/IScheduleJobService.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace FaKrosnoApi.Services;
|
||||
|
||||
public interface IScheduleJobService
|
||||
{
|
||||
Task ExecuteAsync();
|
||||
}
|
||||
29
FaKrosnoApi/Services/ScheduleJobService.cs
Normal file
29
FaKrosnoApi/Services/ScheduleJobService.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Text;
|
||||
using SytelineSaAppEfDataModel.Dtos;
|
||||
using SytelineSaAppEfDataModel.Services;
|
||||
|
||||
namespace FaKrosnoApi.Services;
|
||||
|
||||
public class ScheduleJobService(IEmailService emailService, IServiceScopeFactory scopeFactory) : IScheduleJobService
|
||||
{
|
||||
public async Task ExecuteAsync()
|
||||
{
|
||||
using var scope = scopeFactory.CreateScope();
|
||||
IEdiCustomerOrderService ediCustomerOrderService = scope.ServiceProvider.GetRequiredService<IEdiCustomerOrderService>();
|
||||
IEnumerable<EdiCustomerOrderTranslateDto> missingOrders =
|
||||
(await ediCustomerOrderService.FindMissingOrders(new DateTime(2025, 2, 5))).ToList();
|
||||
|
||||
if (missingOrders.Any())
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
result.AppendLine("Znaleziono brakujące zamówienia w bazie 'edi_co':");
|
||||
|
||||
foreach (EdiCustomerOrderTranslateDto missingOrder in missingOrders)
|
||||
{
|
||||
result.AppendLine($"- {missingOrder.EdiCoCoNum}");
|
||||
}
|
||||
|
||||
emailService.SendEmail("Znaleziono brakujące zamówienia!", result.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
32
FaKrosnoApi/Services/TimedHostedService.cs
Normal file
32
FaKrosnoApi/Services/TimedHostedService.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using SytelineSaAppEfDataModel.Services;
|
||||
|
||||
namespace FaKrosnoApi.Services;
|
||||
|
||||
public class TimedHostedService(IServiceScopeFactory scopeFactory) : IHostedService, IDisposable
|
||||
{
|
||||
private Timer? _timer;
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(30));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void DoWork(object? state)
|
||||
{
|
||||
using var scope = scopeFactory.CreateScope();
|
||||
var scheduledJob = scope.ServiceProvider.GetRequiredService<IScheduleJobService>();
|
||||
scheduledJob.ExecuteAsync();
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_timer?.Change(Timeout.Infinite, 0);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user