2026-02-24 20:44:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-08-21 19:27:59 +00:00
|
|
|
|
using Sozsoft.Notifications.Domain;
|
|
|
|
|
|
using Sozsoft.Notifications.Entities;
|
2026-02-24 20:44:16 +00:00
|
|
|
|
using Volo.Abp.Application.Dtos;
|
|
|
|
|
|
using Volo.Abp.Application.Services;
|
2026-09-20 20:23:39 +00:00
|
|
|
|
using Volo.Abp.Domain.Entities;
|
2026-02-24 20:44:16 +00:00
|
|
|
|
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 IIdentityUserRepository repositoryUser;
|
|
|
|
|
|
|
|
|
|
|
|
public NotificationRuleAppService(
|
|
|
|
|
|
IRepository<NotificationRule, Guid> repository,
|
2026-08-21 19:27:59 +00:00
|
|
|
|
IIdentityUserRepository repositoryUser
|
2026-02-24 20:44:16 +00:00
|
|
|
|
) : base(repository)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.repositoryUser = repositoryUser;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-20 20:23:39 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Birimin kendi kodu ve tum ust birim kodlari. Ust birime tanimlanan kural alt birimlere de
|
|
|
|
|
|
/// isledigi icin (<c>NotificationManager</c> birimi cocuklariyla birlikte cozer) kullanicinin
|
|
|
|
|
|
/// gorecegi kurallar bu zincirin tamamindan gelir.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static void AddUnitCodeWithParents(string? code, List<string> codes)
|
2026-02-24 20:44:16 +00:00
|
|
|
|
{
|
2026-09-20 20:23:39 +00:00
|
|
|
|
while (!code.IsNullOrWhiteSpace())
|
2026-02-24 20:44:16 +00:00
|
|
|
|
{
|
2026-09-20 20:23:39 +00:00
|
|
|
|
codes.Add(code!);
|
|
|
|
|
|
code = OrganizationUnit.GetParentCode(code!);
|
2026-02-24 20:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 ouCodes = new List<string>();
|
2026-09-20 20:23:39 +00:00
|
|
|
|
foreach (var unit in organizationUnits)
|
2026-02-24 20:44:16 +00:00
|
|
|
|
{
|
2026-09-20 20:23:39 +00:00
|
|
|
|
AddUnitCodeWithParents(unit.Code, ouCodes); //01.02.03 -> 01.02.03, 01.02, 01
|
2026-02-24 20:44:16 +00:00
|
|
|
|
}
|
2026-09-20 20:23:39 +00:00
|
|
|
|
ouCodes = [.. ouCodes.Distinct()];
|
2026-02-24 20:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
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) ||
|
2026-09-20 20:23:39 +00:00
|
|
|
|
a.RecipientType == NotificationRecipientTypes.OrganizationUnit && a.RecipientId != null && ouCodes.Contains(a.RecipientId) ||
|
2026-02-24 20:44:16 +00:00
|
|
|
|
a.RecipientType == NotificationRecipientTypes.All);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-20 20:23:39 +00:00
|
|
|
|
public async Task<List<NotificationTypeDto>> GetMyNotificationTypesAsync()
|
2026-02-24 20:44:16 +00:00
|
|
|
|
{
|
|
|
|
|
|
var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto());
|
2026-05-11 09:37:52 +00:00
|
|
|
|
|
2026-09-20 20:23:39 +00:00
|
|
|
|
return await query
|
|
|
|
|
|
.Select(a => new NotificationTypeDto
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = a.NotificationTypeId,
|
|
|
|
|
|
Name = a.NotificationType.Name,
|
|
|
|
|
|
Description = a.NotificationType.Description
|
|
|
|
|
|
})
|
|
|
|
|
|
.Distinct()
|
|
|
|
|
|
.OrderBy(a => a.Name)
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<NotificationRuleSummaryDto> GetRuleSummaryAsync(Guid ruleId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var query = await Repository.GetQueryableAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return await query
|
|
|
|
|
|
.Where(rule => rule.Id == ruleId)
|
|
|
|
|
|
.Select(rule => new NotificationRuleSummaryDto
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = rule.Id,
|
|
|
|
|
|
NotificationTypeName = rule.NotificationType.Name,
|
|
|
|
|
|
Channel = rule.Channel,
|
|
|
|
|
|
RecipientType = rule.RecipientType,
|
|
|
|
|
|
RecipientId = rule.RecipientId,
|
|
|
|
|
|
IsActive = rule.IsActive
|
|
|
|
|
|
})
|
|
|
|
|
|
.FirstOrDefaultAsync()
|
|
|
|
|
|
?? throw new EntityNotFoundException(typeof(NotificationRule), ruleId);
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|