erp-platform/api/modules/Kurs.Notifications/Kurs.Notifications.Application/NotificationAppService.cs
Sedat ÖZTÜRK e1a9562b22 init project
2025-05-06 09:45:49 +03:00

155 lines
5.9 KiB
C#
Raw 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 Kurs.Notifications.Domain;
using Kurs.Notifications.Entities;
using Kurs.Notifications.Enums;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
namespace Kurs.Notifications.Application;
[Authorize]
public class NotificationAppService : ReadOnlyAppService<
Notification,
NotificationDto,
Guid,
NotificationFilterRequestDto>
{
private readonly IRepository<Notification, Guid> repository;
private readonly IRepository<NotificationRule, Guid> repositoryRule;
private readonly INotificationManager notificationManager;
private readonly IRepository<IdentityUser, Guid> repositoryUser;
public NotificationAppService(
IRepository<Notification, Guid> repository,
IRepository<NotificationRule, Guid> repositoryRule,
INotificationManager notificationManager,
IRepository<IdentityUser, Guid> repositoryUser) : base(repository)
{
this.repository = repository;
this.repositoryRule = repositoryRule;
this.notificationManager = notificationManager;
this.repositoryUser = repositoryUser;
}
public async Task GetTestAsync()
{
await notificationManager.SendNotificationAsync(NotificationTypes.SiparisPasla, "Sayin kullanici, nasilsiniz?");
await notificationManager.SendNotificationAsync(NotificationTypes.YeniKullanici, "Sayin kullanici, nasilsiniz?");
await notificationManager.SendNotificationAsync(NotificationTypes.YeniSiparis, "Sayin kullanici, nasilsiniz?");
//smsgönder
// alıcılar: +1435345134, +1435345134
// mesaj: Sayin kullanici, nasilsiniz?
// await notificationManager.SendNotificationAsync(
// NotificationTypes.SiparisPasla,
// NotificationChannels.WhatsApp,
// "Sayin kullanici, nasilsiniz?",
// "+905449476346");
}
public async Task CreateNotificationByNotificationRuleIdAsync(NotificationRequestDto input)
{
var notificationRule = await repositoryRule.GetAsync(input.Id);
if (notificationRule != null)
{
await notificationManager.SendNotificationAsync(notificationRule.NotificationType, input.Message);
}
}
protected override async Task<IQueryable<Notification>> CreateFilteredQueryAsync(NotificationFilterRequestDto input)
{
var query = await base.CreateFilteredQueryAsync(input);
input.Channels ??= [];
var now = Clock.Now.AddDays(-1);
query = query
.AsNoTracking()
.Where(a => a.UserId == CurrentUser.Id)
.Where(a => input.Channels.Contains(a.NotificationChannel))
.WhereIf(input.IsRead.HasValue, a => a.IsRead == input.IsRead)
.WhereIf(!input.IsListRequest && !input.Channels.IsNullOrEmpty() && input.Channels!.Contains(NotificationChannels.UiToast), a => a.CreationTime >= now);
return query;
}
protected override async Task<List<NotificationDto>> MapToGetListOutputDtosAsync(List<Notification> entities)
{
var dtos = new List<NotificationDto>();
var userIds = entities.Select(a => a.CreatorId).Distinct().ToArray();
var query = await repositoryUser.GetQueryableAsync();
var users = await query.AsNoTracking().Where(a => userIds.Contains(a.Id)).Select(a => new { a.Id, Fullname = $"{a.Name} {a.Surname}".Trim(), a.TenantId }).ToDictionaryAsync(x => x.Id, x => x);
foreach (var entity in entities)
{
var dto = await MapToGetListOutputDtoAsync(entity);
if (entity.CreatorId.HasValue && users.ContainsKey(entity.CreatorId.Value))
{
dto.CreatorFullname = users[entity.CreatorId.Value].Fullname;
dto.TenantId = users[entity.CreatorId.Value].TenantId;
}
dtos.Add(dto);
}
return dtos;
}
public async Task<NotificationDto> UpdateReadAsync(Guid notificationId, bool isRead)
{
var query = await repository.GetQueryableAsync();
var item = await query.FirstOrDefaultAsync(a =>
a.UserId == CurrentUser.Id &&
a.Id == notificationId);
if (item == null)
{
throw new EntityNotFoundException(L["RecordNotFound"]);
}
item.IsRead = isRead;
await repository.UpdateAsync(item, autoSave: true);
return ObjectMapper.Map<Notification, NotificationDto>(item);
}
public async Task<int> UpdateReadAllAsync(string notificationChannel, bool isRead)
{
var query = await repository.GetQueryableAsync();
var affectedRows = await query.Where(a => a.UserId == CurrentUser.Id && a.NotificationChannel == notificationChannel && a.IsRead == !isRead)
.ExecuteUpdateAsync(b =>
b.SetProperty(u => u.IsRead, isRead)
);
return affectedRows;
}
public async Task<NotificationDto> UpdateSentAsync(Guid notificationId, bool isSent)
{
var query = await repository.GetQueryableAsync();
var item = await query.FirstOrDefaultAsync(a =>
a.UserId == CurrentUser.Id &&
a.Id == notificationId);
if (item == null)
{
throw new EntityNotFoundException(L["RecordNotFound"]);
}
item.IsSent = isSent;
await repository.UpdateAsync(item, autoSave: true);
return ObjectMapper.Map<Notification, NotificationDto>(item);
}
[RemoteService(false)]
public override Task<NotificationDto> GetAsync(Guid id) => throw new NotImplementedException();
[RemoteService(false)]
protected override Task<Notification> GetEntityByIdAsync(Guid id) => throw new NotImplementedException();
}