QrRapido/Services/AdDisplayService.cs
Ricardo Carneiro 2ccd35bb7d
Some checks failed
Deploy QR Rapido / test (push) Successful in 4m58s
Deploy QR Rapido / build-and-push (push) Failing after 1m39s
Deploy QR Rapido / deploy-staging (push) Has been skipped
Deploy QR Rapido / deploy-production (push) Has been skipped
first commit
2025-07-28 11:46:48 -03:00

117 lines
4.0 KiB
C#

using MongoDB.Driver;
using QRRapidoApp.Data;
using QRRapidoApp.Models;
namespace QRRapidoApp.Services
{
public class AdDisplayService
{
private readonly IUserService _userService;
private readonly MongoDbContext _context;
private readonly IConfiguration _config;
private readonly ILogger<AdDisplayService> _logger;
public AdDisplayService(IUserService userService, MongoDbContext context, IConfiguration config, ILogger<AdDisplayService> logger)
{
_userService = userService;
_context = context;
_config = config;
_logger = logger;
}
public async Task<bool> ShouldShowAds(string? userId = null)
{
try
{
// Usuários não logados: sempre mostrar anúncios
if (string.IsNullOrEmpty(userId))
return true;
var user = await _userService.GetUserAsync(userId);
if (user == null) return true;
// APENAS Premium users não veem anúncios
return !(user.IsPremium && user.PremiumExpiresAt > DateTime.UtcNow);
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error checking ad display status for user {userId}: {ex.Message}");
return true; // Default to showing ads on error
}
}
// MÉTODO REMOVIDO: GetAdFreeTimeRemaining - não é mais necessário
// MÉTODO REMOVIDO: GetActiveAdFreeSessionAsync - não é mais necessário
public async Task<bool> HasValidPremiumSubscription(string userId)
{
try
{
var user = await _userService.GetUserAsync(userId);
return user?.IsPremium == true && user.PremiumExpiresAt > DateTime.UtcNow;
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error checking premium subscription for user {userId}: {ex.Message}");
return false;
}
}
public async Task<string> GetAdFreeStatusAsync(string userId)
{
try
{
if (await HasValidPremiumSubscription(userId))
return "Premium";
return "None";
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error getting ad-free status for user {userId}: {ex.Message}");
return "None";
}
}
public async Task<DateTime?> GetAdFreeExpiryDate(string userId)
{
try
{
var user = await _userService.GetUserAsync(userId);
if (user?.IsPremium == true && user.PremiumExpiresAt > DateTime.UtcNow)
return user.PremiumExpiresAt;
return null; // Sem sessões ad-free temporárias
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error getting ad-free expiry date for user {userId}: {ex.Message}");
return null;
}
}
public async Task DeactivateExpiredSessionsAsync()
{
try
{
var filter = Builders<AdFreeSession>.Filter.And(
Builders<AdFreeSession>.Filter.Eq(s => s.IsActive, true),
Builders<AdFreeSession>.Filter.Lt(s => s.ExpiresAt, DateTime.UtcNow)
);
var update = Builders<AdFreeSession>.Update.Set(s => s.IsActive, false);
var result = await _context.AdFreeSessions.UpdateManyAsync(filter, update);
if (result.ModifiedCount > 0)
{
_logger.LogInformation($"Deactivated {result.ModifiedCount} expired ad-free sessions");
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error deactivating expired sessions: {ex.Message}");
}
}
}
}