sozsoft-platform/api/modules/Sozsoft.Notifications/Sozsoft.Notifications.Application/NotificationRuleAppService.cs

165 lines
6 KiB
C#
Raw Normal View History

2026-02-24 20:44:16 +00:00
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
{
2026-05-11 09:37:52 +00:00
private readonly IRepository<NotificationType, Guid> repositoryType;
private readonly IOrganizationUnitRepository repositoryOrganizationUnit;
2026-02-24 20:44:16 +00:00
private readonly IdentityUserManager userManager;
private readonly IIdentityUserRepository repositoryUser;
public NotificationRuleAppService(
2026-05-11 09:37:52 +00:00
IRepository<NotificationType, Guid> repositoryType,
2026-02-24 20:44:16 +00:00
IRepository<NotificationRule, Guid> repository,
IdentityUserManager userManager,
IIdentityUserRepository repositoryUser,
IOrganizationUnitRepository repositoryOrganizationUnit
) : base(repository)
{
2026-05-11 09:37:52 +00:00
this.repositoryType = repositoryType;
2026-02-24 20:44:16 +00:00
this.userManager = userManager;
this.repositoryUser = repositoryUser;
this.repositoryOrganizationUnit = repositoryOrganizationUnit;
}
2026-05-11 09:37:52 +00:00
public async Task<string[]> GetNotificationTypes()
2026-02-24 20:44:16 +00:00
{
2026-05-11 09:37:52 +00:00
var types = await repositoryType.GetListAsync();
return types.Select(a => a.Name).ToArray();
2026-02-24 20:44:16 +00:00
}
private void GetAllParentCodes(string code, List<string>? codes)
{
codes ??= [];
var parentCode = OrganizationUnit.GetParentCode(code);
codes.Add(parentCode);
if (parentCode is null)
{
return;
}
else
{
GetAllParentCodes(parentCode, codes);
}
}
protected override async Task<IQueryable<NotificationRule>> 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<string>();
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<string[]> GetMyNotificationTypesAsync()
{
var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto());
2026-05-11 09:37:52 +00:00
return await query.Select(a => a.NotificationType.Name).Distinct().ToArrayAsync();
2026-02-24 20:44:16 +00:00
}
public async Task<List<NotificationRuleDto>> GetMyNotificationRules()
{
var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto());
var all = await query.ToListAsync();
var list = all
2026-05-11 09:37:52 +00:00
.GroupBy(a => new { a.NotificationTypeId, a.Channel })
2026-02-24 20:44:16 +00:00
.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,
2026-05-11 09:37:52 +00:00
NotificationTypeId = a.Key.NotificationTypeId,
2026-02-24 20:44:16 +00:00
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 &&
2026-05-11 09:37:52 +00:00
a.NotificationTypeId == Input.NotificationTypeId);
2026-02-24 20:44:16 +00:00
if (item is null)
{
// insert
item = new NotificationRule
{
2026-05-11 09:37:52 +00:00
NotificationTypeId = Input.NotificationTypeId,
2026-02-24 20:44:16 +00:00
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);
}
}
2026-05-11 09:37:52 +00:00
public async Task DeleteMyNotificationRules(Guid NotificationTypeId)
2026-02-24 20:44:16 +00:00
{
var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto());
var items = await query.Where(a =>
a.RecipientType == NotificationRecipientTypes.User &&
a.RecipientId == CurrentUser.UserName &&
a.IsCustomized &&
2026-05-11 09:37:52 +00:00
a.NotificationTypeId == NotificationTypeId)
2026-02-24 20:44:16 +00:00
.ToListAsync();
2026-05-11 09:37:52 +00:00
2026-02-24 20:44:16 +00:00
await Repository.DeleteManyAsync(items);
}
}