using FrameworkDAL.DTO.EIPConversation; using FrameworkDAL.Query.EIPConversation.EIPRouting; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace FrameworkDAL.CustomCode.EIPConversation.EIPRouting { public class EIPRoutingDAL : IEIPRoutingDAL { private readonly IQueryExecutor _queryExecutor; private readonly ILogger _logger; public EIPRoutingDAL( IQueryExecutor queryExecutor, ILogger logger) { _queryExecutor = queryExecutor ?? throw new ArgumentNullException(nameof(queryExecutor)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } // ========================================================= // Validate Tenant // ========================================================= public async Task ValidateTenantAsync( int? tenantId, LoginDTO loginDTO, CancellationToken cancellationToken) { if (tenantId is null) return false; cancellationToken.ThrowIfCancellationRequested(); try { _logger.LogInformation( "DAL | ValidateTenant | TenantId={TenantId}", tenantId); var result = await _queryExecutor .QuerySingleAsync( loginDTO, EIPRoutingQB.VALIDATE_TENANT, new { TenantId = tenantId.Value }); return result > 0; } catch (OperationCanceledException) { _logger.LogWarning( "DAL | ValidateTenant | Cancelled | TenantId={TenantId}", tenantId); throw; } catch (Exception ex) { _logger.LogError(ex, "DAL | ValidateTenant | Error | TenantId={TenantId}", tenantId); throw; } } // ========================================================= // Get Active Routing Rules // ========================================================= public async Task> GetActiveRoutingRulesAsync( int? tenantId, EIPChannelType channelType, LoginDTO loginDTO, CancellationToken cancellationToken) { if (tenantId is null) return new List(); cancellationToken.ThrowIfCancellationRequested(); try { _logger.LogInformation( "DAL | GetRoutingRules | Tenant={Tenant} | Channel={Channel}", tenantId, channelType); var result = await _queryExecutor.QueryAsync( loginDTO, EIPRoutingQB.GET_ACTIVE_ROUTING_RULES, new { TenantId = tenantId.Value, ChannelType = (byte)channelType // numeric }); return result?.ToList() ?? new List(); } catch (OperationCanceledException) { _logger.LogWarning( "DAL | GetRoutingRules | Cancelled | Tenant={Tenant}", tenantId); throw; } catch (Exception ex) { _logger.LogError(ex, "DAL | GetRoutingRules | Error | Tenant={Tenant}", tenantId); throw; } } } }