sozsoft-platform/api/src/Sozsoft.Platform.Domain/BackgroundWorkers/NotificationWorker.cs
2026-08-21 22:27:59 +03:00

147 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sozsoft.Notifications.Entities;
using Sozsoft.Notifications.Enums;
using Sozsoft.Platform.Data.Seeds;
using Sozsoft.Sender.Mail;
using Sozsoft.Sender.Rocket;
using Sozsoft.Sender.Sms;
using Sozsoft.Sender.WhatsApp;
using Volo.Abp.BackgroundWorkers;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.Settings;
using Volo.Abp.Uow;
namespace Sozsoft.Platform.BackgroundWorkers;
public class NotificationWorker : BackgroundWorkerBase
{
protected IRepository<Notification> Repository { get; }
protected IIdentityUserRepository UserRepository { get; }
protected IdentityUserManager IdentityUserManager { get; }
protected ISozsoftRocketSender ErpRocketSender { get; }
protected ISozsoftEmailSender ErpEmailSender { get; }
protected ISozsoftSmsSender ErpSmsSender { get; }
protected ISozsoftWhatsAppSender ErpWhatsAppSender { get; }
protected ISettingProvider SettingProvider { get; }
public NotificationWorker(
IRepository<Notification> repository,
IIdentityUserRepository userRepository,
IdentityUserManager identityUserManager,
ISozsoftSmsSender erpSmsSender,
ISozsoftRocketSender erpRocketSender,
ISozsoftEmailSender erpEmailSender,
ISozsoftWhatsAppSender erpWhatsAppSender,
ISettingProvider settingProvider)
{
Repository = repository;
ErpSmsSender = erpSmsSender;
UserRepository = userRepository;
IdentityUserManager = identityUserManager;
ErpRocketSender = erpRocketSender;
ErpEmailSender = erpEmailSender;
ErpWhatsAppSender = erpWhatsAppSender;
SettingProvider = settingProvider;
}
public override async Task StartAsync(CancellationToken cancellationToken = default)
{
using var scope = ServiceProvider.CreateScope();
var uowManager = LazyServiceProvider.LazyGetRequiredService<IUnitOfWorkManager>();
using var uow = uowManager.Begin();
var sms = await Repository.GetListAsync(a => a.NotificationChannel == NotificationChannels.Sms && !a.IsSent, cancellationToken: cancellationToken);
foreach (var notification in sms)
{
try
{
var result = await ErpSmsSender.SendAsync(notification.Identifier, notification.Message);
if (result.Success)
{
notification.IsSent = true;
notification.IsRead = true;
await Repository.UpdateAsync(notification, autoSave: true, cancellationToken: cancellationToken);
}
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
var rocket = await Repository.GetListAsync(a => a.NotificationChannel == NotificationChannels.Rocket && !a.IsSent, cancellationToken: cancellationToken);
foreach (var notification in rocket)
{
try
{
var result = await ErpRocketSender.SendAsync(notification.Identifier, notification.Message);
if (result.Success)
{
notification.IsSent = true;
notification.IsRead = true;
await Repository.UpdateAsync(notification, autoSave: true, cancellationToken: cancellationToken);
}
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
var mail = await Repository.GetListAsync(a => a.NotificationChannel == NotificationChannels.Mail && !a.IsSent, cancellationToken: cancellationToken);
var senderName = await SettingProvider.GetOrNullAsync(SeedConsts.AbpSettings.Mailing.Default.DefaultFromDisplayName);
var senderEmail = await SettingProvider.GetOrNullAsync(SeedConsts.AbpSettings.Mailing.Default.DefaultFromAddress);
foreach (var notification in mail)
{
try
{
var result = await ErpEmailSender.SendEmailAsync(
notification.Identifier,
new KeyValuePair<string, string>(senderName, senderEmail),
new { },
notification.Message,
$"Platform Bildirim: {notification.NotificationType}");
if (result.Success)
{
notification.IsSent = true;
notification.IsRead = true;
await Repository.UpdateAsync(notification, autoSave: true, cancellationToken: cancellationToken);
}
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
var whatsapp = await Repository.GetListAsync(a => a.NotificationChannel == NotificationChannels.WhatsApp && !a.IsSent, cancellationToken: cancellationToken);
foreach (var notification in whatsapp)
{
try
{
var result = await ErpWhatsAppSender.SendAsync(notification.Identifier, notification.Message);
if (result.Success)
{
notification.IsSent = true;
notification.IsRead = true;
await Repository.UpdateAsync(notification, autoSave: true, cancellationToken: cancellationToken);
}
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
await uow.SaveChangesAsync(cancellationToken);
}
}