erp-platform/api/modules/Erp.Notifications/Erp.Notifications.Application/NotificationRuleAppService.cs
2025-11-11 22:49:52 +03:00

194 lines
7.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Erp.Notifications.Domain;
using Erp.Notifications.Entities;
using Erp.Notifications.Enums;
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 Erp.Notifications.NotificationRules;
[Authorize]
public class NotificationRuleAppService : CrudAppService<
NotificationRule,
NotificationRuleDto,
Guid,
PagedAndSortedResultRequestDto,
CreateUpdateNotificationRuleDto>,
INotificationRuleAppService
{
private readonly IdentityUserManager userManager;
private readonly IIdentityUserRepository repositoryUser;
private readonly IOrganizationUnitRepository repositoryOrganizationUnit;
public NotificationRuleAppService(
IRepository<NotificationRule, Guid> repository,
IdentityUserManager userManager,
IIdentityUserRepository repositoryUser,
IOrganizationUnitRepository repositoryOrganizationUnit
) : base(repository)
{
this.userManager = userManager;
this.repositoryUser = repositoryUser;
this.repositoryOrganizationUnit = repositoryOrganizationUnit;
}
public string[] GetNotificationTypes()
{
return NotificationTypes.GetAll();
}
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());
return await query.Select(a => a.NotificationType).Distinct().ToArrayAsync();
}
public async Task<List<NotificationRuleDto>> GetMyNotificationRules()
{
var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto());
var all = await query.ToListAsync();
var list = all
.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 new NotificationRuleDto
{
Id = item.Id,
NotificationType = a.Key.NotificationType,
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.NotificationType == Input.NotificationType);
if (item is null)
{
// insert
item = new NotificationRule
{
NotificationType = Input.NotificationType,
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(string NotificationType)
{
var query = await CreateFilteredQueryAsync(new PagedAndSortedResultRequestDto());
var items = await query.Where(a =>
a.RecipientType == NotificationRecipientTypes.User &&
a.RecipientId == CurrentUser.UserName &&
a.IsCustomized &&
a.NotificationType == NotificationType)
.ToListAsync();
await Repository.DeleteManyAsync(items);
}
}
/*
var defaultRules = await query.Where(a => !a.IsCustomized && a.IsActive)
.GroupBy(a => new { a.NotificationType, a.Channel })
.Select(a => new NotificationRuleDto
{
Id = a.FirstOrDefault(a => a.UserId != null)?.Id ?? a.First().Id,
NotificationType = a.Key.NotificationType,
Channel = a.Key.Channel,
IsActive = a.First().IsActive,
IsFixed = a.First().IsFixed,
IsCustomized = a.First().IsCustomized,
})
.ToListAsync();
var customizedRules = await query.Where(a => a.IsCustomized)
.Select(a => new NotificationRuleDto
{
Id = a.Id,
NotificationType = a.NotificationType,
Channel = a.Channel,
IsActive = a.IsActive,
IsFixed = a.IsFixed,
IsCustomized = a.IsCustomized
})
.ToListAsync();
//defaultRules.IsFixed
//customizedRules - defaultRules.IsFixed
//!defaultRules.IsFixed - customizedRules
var list = new List<NotificationRuleDto>();
list.AddRange(defaultRules.Where(a => a.IsFixed).ToList());
list.AddRange(customizedRules.Where(a => !list.Any(b => b.NotificationType == a.NotificationType && b.Channel == a.Channel)).ToList());
list.AddRange(defaultRules.Where(a => !a.IsFixed && !list.Any(b => b.NotificationType == a.NotificationType && b.Channel == a.Channel)).ToList());
*/