using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sozsoft.Notifications.Domain; using Sozsoft.Notifications.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.EntityFrameworkCore; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; using Volo.Abp.Identity; namespace Sozsoft.Notifications.NotificationRules; [Authorize] public class NotificationRuleAppService : CrudAppService< NotificationRule, NotificationRuleDto, Guid, PagedAndSortedResultRequestDto, CreateUpdateNotificationRuleDto>, INotificationRuleAppService { private readonly IRepository repositoryType; private readonly IOrganizationUnitRepository repositoryOrganizationUnit; private readonly IdentityUserManager userManager; private readonly IIdentityUserRepository repositoryUser; public NotificationRuleAppService( IRepository repositoryType, IRepository repository, IdentityUserManager userManager, IIdentityUserRepository repositoryUser, IOrganizationUnitRepository repositoryOrganizationUnit ) : base(repository) { this.repositoryType = repositoryType; this.userManager = userManager; this.repositoryUser = repositoryUser; this.repositoryOrganizationUnit = repositoryOrganizationUnit; } public async Task GetNotificationTypes() { var types = await repositoryType.GetListAsync(); return types.Select(a => a.Name).ToArray(); } private void GetAllParentCodes(string code, List? codes) { codes ??= []; var parentCode = OrganizationUnit.GetParentCode(code); codes.Add(parentCode); if (parentCode is null) { return; } else { GetAllParentCodes(parentCode, codes); } } protected override async Task> CreateFilteredQueryAsync(PagedAndSortedResultRequestDto input) { // Bir kullanıcının ait olduğu organization unit'lerin her bir parentının kodu lazım // Böylece bir kullanıcının üst birimleri de dahil alabileceği tüm bildirim tiplerini listeleyebileceğiz var organizationUnits = await repositoryUser.GetOrganizationUnitsAsync(CurrentUser.Id!.Value); var organizationUnitCodes = organizationUnits.Select(a => a.Code).ToList(); //01.02.03, 43.44.45 var ouCodes = new List(); foreach (var code in organizationUnitCodes) { GetAllParentCodes(code, ouCodes); } ouCodes = ouCodes.Distinct().ToList(); var query = await base.CreateFilteredQueryAsync(input); return query.Where(a => a.RecipientType == NotificationRecipientTypes.User && a.RecipientId == CurrentUser.UserName || a.RecipientType == NotificationRecipientTypes.Role && CurrentUser.Roles.Contains(a.RecipientId) || a.RecipientType == NotificationRecipientTypes.OrganizationUnit && ouCodes.Contains(a.RecipientId) || a.RecipientType == NotificationRecipientTypes.All); } public async Task GetMyNotificationTypesAsync() { var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto()); return await query.Select(a => a.NotificationType.Name).Distinct().ToArrayAsync(); } public async Task> GetMyNotificationRules() { var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto()); var all = await query.ToListAsync(); var list = all .GroupBy(a => new { a.NotificationTypeId, a.Channel }) .Select(a => { var item = a.FirstOrDefault(a => a.IsFixed) ?? a.FirstOrDefault(a => a.IsCustomized) ?? a.FirstOrDefault(a => a.IsActive) ?? a.First(); return new NotificationRuleDto { Id = item.Id, NotificationTypeId = a.Key.NotificationTypeId, Channel = a.Key.Channel, IsActive = item.IsActive, IsFixed = item.IsFixed, IsCustomized = item.IsCustomized, }; }) .ToList(); return list; } public async Task PostMyNotificationRule(CreateUpdateNotificationRuleDto Input) { var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto()); var item = await query.FirstOrDefaultAsync(a => a.RecipientType == NotificationRecipientTypes.User && a.RecipientId == CurrentUser.UserName && a.IsCustomized && a.Channel == Input.Channel && a.NotificationTypeId == Input.NotificationTypeId); if (item is null) { // insert item = new NotificationRule { NotificationTypeId = Input.NotificationTypeId, RecipientType = NotificationRecipientTypes.User, RecipientId = CurrentUser.UserName, Channel = Input.Channel, IsActive = Input.IsActive, IsFixed = false, IsCustomized = true }; await Repository.InsertAsync(item); } else { item.IsActive = Input.IsActive; await Repository.UpdateAsync(item); } } public async Task DeleteMyNotificationRules(Guid NotificationTypeId) { var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto()); var items = await query.Where(a => a.RecipientType == NotificationRecipientTypes.User && a.RecipientId == CurrentUser.UserName && a.IsCustomized && a.NotificationTypeId == NotificationTypeId) .ToListAsync(); await Repository.DeleteManyAsync(items); } }