175 lines
6.5 KiB
C#
175 lines
6.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Sozsoft.Notifications.Domain;
|
||
using Sozsoft.Notifications.Entities;
|
||
using Volo.Abp.Application.Dtos;
|
||
using Volo.Abp.Application.Services;
|
||
using Volo.Abp.Domain.Entities;
|
||
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,
|
||
IIdentityUserRepository repositoryUser
|
||
) : base(repository)
|
||
{
|
||
this.repositoryUser = repositoryUser;
|
||
}
|
||
|
||
/// <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)
|
||
{
|
||
while (!code.IsNullOrWhiteSpace())
|
||
{
|
||
codes.Add(code!);
|
||
code = OrganizationUnit.GetParentCode(code!);
|
||
}
|
||
}
|
||
|
||
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>();
|
||
foreach (var unit in organizationUnits)
|
||
{
|
||
AddUnitCodeWithParents(unit.Code, ouCodes); //01.02.03 -> 01.02.03, 01.02, 01
|
||
}
|
||
ouCodes = [.. ouCodes.Distinct()];
|
||
|
||
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 && a.RecipientId != null && ouCodes.Contains(a.RecipientId) ||
|
||
a.RecipientType == NotificationRecipientTypes.All);
|
||
}
|
||
|
||
public async Task<List<NotificationTypeDto>> GetMyNotificationTypesAsync()
|
||
{
|
||
var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto());
|
||
|
||
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);
|
||
}
|
||
|
||
public async Task<List<NotificationRuleDto>> 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);
|
||
}
|
||
}
|