QrRapido/Services/AdDisplayService.cs
Ricardo Carneiro b54aa295ac
All checks were successful
Deploy QR Rapido / test (push) Successful in 44s
Deploy QR Rapido / build-and-push (push) Successful in 12m59s
Deploy QR Rapido / deploy-staging (push) Has been skipped
Deploy QR Rapido / deploy-production (push) Successful in 1m24s
fix: add Node.js to Docker build stage for frontend compilation
- Install Node.js 18.x in Docker build stage
- Add MongoDB DataProtection for Swarm compatibility
- Enables shared authentication keys across multiple replicas

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-22 16:25:54 -03:00

118 lines
3.9 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
{
if (string.IsNullOrEmpty(userId))
return true;
var user = await _userService.GetUserAsync(userId);
if (user == null) return true;
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
}
}
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}");
}
}
public void SetViewBagAds(dynamic viewBag)
{
viewBag.AdSenseTag = _config["AdSense:ClientId"];
viewBag.AdSenseEnabled = _config["AdSense:Enabled"]=="True";
}
}
}