202 lines
7.7 KiB
C#
202 lines
7.7 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Kurs.Notifications.Entities;
|
|||
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using Volo.Abp.DependencyInjection;
|
|||
|
|
using Volo.Abp.Domain.Repositories;
|
|||
|
|
using Volo.Abp.Identity;
|
|||
|
|
using Volo.Abp.Users;
|
|||
|
|
|
|||
|
|
namespace Kurs.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<NotificationRule> repository;
|
|||
|
|
private readonly IReadOnlyRepository<IdentityUser> repositoryUser;
|
|||
|
|
private readonly IReadOnlyRepository<IdentityRole> repositoryRole;
|
|||
|
|
private readonly IRepository<Notification, Guid> repositoryNotification;
|
|||
|
|
private readonly INotificationIdentifierProvider notificationIdentifierProvider;
|
|||
|
|
private readonly IOrganizationUnitRepository repositoryOrganizationUnit;
|
|||
|
|
private readonly ICurrentUser currentUser;
|
|||
|
|
|
|||
|
|
public NotificationManager(
|
|||
|
|
IReadOnlyRepository<NotificationRule> repository,
|
|||
|
|
IReadOnlyRepository<IdentityUser> repositoryUser,
|
|||
|
|
IReadOnlyRepository<IdentityRole> repositoryRole,
|
|||
|
|
IRepository<Notification, Guid> 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<Guid> 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<Notification>();
|
|||
|
|
|
|||
|
|
//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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Belirtilen Ident
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="NotificationType"></param>
|
|||
|
|
/// <param name="NotificationMessage"></param>
|
|||
|
|
/// <param name="Identifier"></param>
|
|||
|
|
/// <param name="UserId"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
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
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|