using Fengling.RiskControl.Domain.Aggregates.RiskAlerts; using Fengling.RiskControl.Domain.Events; using Fengling.RiskControl.Domain.Repositories; using MediatR; namespace Fengling.RiskControl.Application.Services; public class RiskAlertService : IRiskAlertService { private readonly IRiskAlertRepository _alertRepository; private readonly IMediator _mediator; public RiskAlertService(IRiskAlertRepository alertRepository, IMediator mediator) { _alertRepository = alertRepository; _mediator = mediator; } public async Task CreateAlertAsync(long memberId, string alertType, string description, RiskAlertPriority priority = RiskAlertPriority.Medium) { var alert = RiskAlert.Create(memberId, alertType, description, priority); await _alertRepository.AddAsync(alert); await _mediator.Publish(new RiskAlertTriggeredEvent( alert.Id, memberId, alertType, 0, description)); return alert; } public async Task ResolveAlertAsync(long alertId, string notes) { var alert = await _alertRepository.GetByIdAsync(alertId); if (alert == null) throw new KeyNotFoundException($"Alert not found: {alertId}"); alert.Resolve(notes); await _alertRepository.UpdateAsync(alert); return alert; } public async Task DismissAlertAsync(long alertId, string notes) { var alert = await _alertRepository.GetByIdAsync(alertId); if (alert == null) throw new KeyNotFoundException($"Alert not found: {alertId}"); alert.Dismiss(notes); await _alertRepository.UpdateAsync(alert); return alert; } public async Task EscalateAlertAsync(long alertId) { var alert = await _alertRepository.GetByIdAsync(alertId); if (alert == null) throw new KeyNotFoundException($"Alert not found: {alertId}"); alert.Escalate(); await _alertRepository.UpdateAsync(alert); return alert; } public Task> GetMemberAlertsAsync(long memberId) { return _alertRepository.GetByMemberIdAsync(memberId); } public Task> GetPendingAlertsAsync() { return _alertRepository.GetPendingAlertsAsync(); } }