using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Erp.Notifications.Entities; using Microsoft.EntityFrameworkCore; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories; using Volo.Abp.Identity; using Volo.Abp.Users; namespace Erp.Notifications.Domain; public interface INotificationManager : ITransientDependency { Task SendNotificationAsync(string NotificationType, string NotificationMessage); Task SendNotificationAsync(string NotificationType, string NotificationChannel, string NotificationMessage, string Identifier, Guid? UserId = null); } public class NotificationManager : INotificationManager { private readonly IReadOnlyRepository repository; private readonly IReadOnlyRepository repositoryUser; private readonly IReadOnlyRepository repositoryRole; private readonly IRepository repositoryNotification; private readonly INotificationIdentifierProvider notificationIdentifierProvider; private readonly IOrganizationUnitRepository repositoryOrganizationUnit; private readonly ICurrentUser currentUser; public NotificationManager( IReadOnlyRepository repository, IReadOnlyRepository repositoryUser, IReadOnlyRepository repositoryRole, IRepository repositoryNotification, INotificationIdentifierProvider notificationIdentifierProvider, IOrganizationUnitRepository repositoryOrganizationUnit, ICurrentUser currentUser ) { this.repository = repository; this.repositoryUser = repositoryUser; this.repositoryRole = repositoryRole; this.repositoryNotification = repositoryNotification; this.notificationIdentifierProvider = notificationIdentifierProvider; this.repositoryOrganizationUnit = repositoryOrganizationUnit; this.currentUser = currentUser; } public async Task SendNotificationAsync(string NotificationType, string NotificationMessage) { var list = await repository.GetListAsync(a => a.NotificationType == NotificationType); var notificationRules = list .GroupBy(a => new { a.NotificationType, a.Channel }) .Select(a => { var item = a.FirstOrDefault(a => a.IsFixed) ?? a.FirstOrDefault(a => a.IsCustomized) ?? a.FirstOrDefault(a => a.IsActive) ?? a.First(); return item; }) .Where(a => a.IsActive) .ToList(); var queryUsers = await repositoryUser.GetQueryableAsync(); foreach (var notificationRule in notificationRules) { List userIds = []; // RecipientType bakarak ilgilileri bul if (notificationRule.RecipientType == NotificationRecipientTypes.All) { // Tum userlar var allUserIds = await queryUsers .Where(u => u.IsActive) .Select(a => a.Id) .ToListAsync(); userIds.AddRange(allUserIds); } else if (notificationRule.RecipientType == NotificationRecipientTypes.User) { var user = await queryUsers.FirstOrDefaultAsync(a => a.UserName == notificationRule.RecipientId); if (user != null) { userIds.Add(user.Id); } } else if (notificationRule.RecipientType == NotificationRecipientTypes.Role) { var role = await repositoryRole.FirstOrDefaultAsync(a => a.Name == notificationRule.RecipientId); if (role != null) { var roleUserIds = await queryUsers .Where(u => u.Roles.Any(r => r.RoleId == role.Id) && u.IsActive) .Select(a => a.Id) .ToListAsync(); userIds.AddRange(roleUserIds); } } else if (notificationRule.RecipientType == NotificationRecipientTypes.OrganizationUnit) { var units = await repositoryOrganizationUnit.GetAllChildrenWithParentCodeAsync(notificationRule.RecipientId, null); foreach (var unit in units) { var memberIds = await repositoryOrganizationUnit.GetMemberIdsAsync(unit.Id); userIds.AddRange(memberIds); } } var notifications = new List(); //Eğer "Custom" seçildiyse girilen RecipientId dinamik olabilir. Telefon Numarası, Email vs. if (notificationRule.RecipientType == NotificationRecipientTypes.Custom) { var notification = new Notification { NotificationRuleId = notificationRule.Id, NotificationType = NotificationType, NotificationChannel = notificationRule.Channel, Identifier = notificationRule.RecipientId!, UserId = currentUser.Id, Message = NotificationMessage, IsSent = false, IsRead = false }; notifications.Add(notification); } else { if (userIds.IsNullOrEmpty()) { continue; } userIds = [.. userIds.Distinct()]; foreach (var userId in userIds) { var identifier = await notificationIdentifierProvider.GetIdentifier(userId, notificationRule.Channel); if (identifier.IsNullOrEmpty()) { continue; } var notification = new Notification { NotificationRuleId = notificationRule.Id, NotificationType = NotificationType, NotificationChannel = notificationRule.Channel, Identifier = identifier, UserId = userId, Message = NotificationMessage, IsSent = false, IsRead = false }; notifications.Add(notification); } } await repositoryNotification.InsertManyAsync(notifications); } } /// /// Belirtilen Ident /// /// /// /// /// /// public async Task SendNotificationAsync( string NotificationType, string NotificationChannel, string NotificationMessage, string Identifier, Guid? UserId = null) { if (NotificationChannel == Enums.NotificationChannels.WhatsApp) { if (Identifier.StartsWith('5')) { Identifier = "+90" + Identifier; } else if (Identifier.StartsWith("05")) { Identifier = "+9" + Identifier; } } await repositoryNotification.InsertAsync(new Notification { NotificationType = NotificationType, NotificationChannel = NotificationChannel, Identifier = Identifier, UserId = UserId, Message = NotificationMessage, IsSent = false, IsRead = false }); } }