BCards/src/BCards.Web/Controllers/TestToolsController.cs
Ricardo Carneiro 5d70ba797a
All checks were successful
BCards Deployment Pipeline / Run Tests (push) Successful in 11s
BCards Deployment Pipeline / PR Validation (push) Has been skipped
BCards Deployment Pipeline / Build and Push Image (push) Successful in 11m22s
BCards Deployment Pipeline / Deploy to Production (ARM - OCI) (push) Has been skipped
BCards Deployment Pipeline / Deploy to Release Swarm (ARM) (push) Successful in 1m42s
BCards Deployment Pipeline / Cleanup Old Resources (push) Has been skipped
BCards Deployment Pipeline / Deployment Summary (push) Successful in 1s
feat: ajustes para downgrade
2025-10-05 21:57:50 -03:00

171 lines
5.8 KiB
C#

#if TESTING
using BCards.Web.Models;
using BCards.Web.Repositories;
using BCards.Web.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BCards.Web.Controllers;
[Authorize]
[ApiController]
[Route("testing/tools")]
public class TestToolsController : ControllerBase
{
private readonly IUserRepository _userRepository;
private readonly ISubscriptionRepository _subscriptionRepository;
private readonly IUserPageService _userPageService;
private readonly ILivePageService _livePageService;
private readonly IPlanConfigurationService _planConfigurationService;
private readonly IWebHostEnvironment _environment;
public TestToolsController(
IUserRepository userRepository,
ISubscriptionRepository subscriptionRepository,
IUserPageService userPageService,
ILivePageService livePageService,
IPlanConfigurationService planConfigurationService,
IWebHostEnvironment environment)
{
_userRepository = userRepository;
_subscriptionRepository = subscriptionRepository;
_userPageService = userPageService;
_livePageService = livePageService;
_planConfigurationService = planConfigurationService;
_environment = environment;
}
[HttpPost("plan")]
public async Task<IActionResult> SetPlan([FromBody] SetPlanRequest request)
{
if (!_environment.IsEnvironment("Testing"))
{
return NotFound();
}
if (!Enum.TryParse<PlanType>(request.Plan, true, out var planType))
{
return BadRequest(new { error = $"Plano desconhecido: {request.Plan}" });
}
var email = string.IsNullOrWhiteSpace(request.Email)
? TestUserDefaults.Email
: request.Email!.Trim();
var user = await _userRepository.GetByEmailAsync(email);
if (user == null)
{
return NotFound(new { error = $"Usuário de teste não encontrado para o e-mail {email}" });
}
var planLimits = _planConfigurationService.GetPlanLimitations(planType);
var normalizedPlan = planLimits.PlanType ?? planType.ToString().ToLowerInvariant();
user.CurrentPlan = normalizedPlan;
user.SubscriptionStatus = "active";
user.UpdatedAt = DateTime.UtcNow;
await _userRepository.UpdateAsync(user);
var subscription = await _subscriptionRepository.GetByUserIdAsync(user.Id);
if (subscription == null)
{
subscription = new Subscription
{
UserId = user.Id,
StripeSubscriptionId = $"test-{Guid.NewGuid():N}",
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
}
subscription.PlanType = normalizedPlan;
subscription.Status = "active";
subscription.MaxLinks = planLimits.MaxLinks;
subscription.AllowAnalytics = planLimits.AllowAnalytics;
subscription.AllowCustomThemes = planLimits.AllowCustomThemes;
subscription.AllowCustomDomain = planLimits.AllowCustomDomain;
subscription.AllowMultipleDomains = planLimits.AllowMultipleDomains;
subscription.PrioritySupport = planLimits.PrioritySupport;
subscription.CurrentPeriodStart = DateTime.UtcNow.Date;
subscription.CurrentPeriodEnd = DateTime.UtcNow.Date.AddMonths(1);
subscription.CancelAtPeriodEnd = false;
subscription.UpdatedAt = DateTime.UtcNow;
if (string.IsNullOrEmpty(subscription.Id))
{
await _subscriptionRepository.CreateAsync(subscription);
}
else
{
await _subscriptionRepository.UpdateAsync(subscription);
}
var pages = await _userPageService.GetUserPagesAsync(user.Id);
var originalPagesCount = pages.Count;
var removed = 0;
if (request.ResetPages)
{
foreach (var page in pages)
{
await _livePageService.DeleteByOriginalPageIdAsync(page.Id);
await _userPageService.DeletePageAsync(page.Id);
removed++;
}
pages = new List<UserPage>();
}
else
{
foreach (var page in pages)
{
page.PlanLimitations = ClonePlanLimitations(planLimits);
page.UpdatedAt = DateTime.UtcNow;
await _userPageService.UpdatePageAsync(page);
}
}
return Ok(new
{
email,
plan = normalizedPlan,
pagesAffected = request.ResetPages ? originalPagesCount : pages.Count,
pagesRemoved = removed
});
}
private static PlanLimitations ClonePlanLimitations(PlanLimitations source)
{
return new PlanLimitations
{
MaxLinks = source.MaxLinks,
AllowCustomThemes = source.AllowCustomThemes,
AllowAnalytics = source.AllowAnalytics,
AllowCustomDomain = source.AllowCustomDomain,
AllowMultipleDomains = source.AllowMultipleDomains,
PrioritySupport = source.PrioritySupport,
PlanType = source.PlanType,
MaxProductLinks = source.MaxProductLinks,
MaxOGExtractionsPerDay = source.MaxOGExtractionsPerDay,
AllowProductLinks = source.AllowProductLinks,
SpecialModeration = source.SpecialModeration,
OGExtractionsUsedToday = 0,
LastExtractionDate = null
};
}
private static class TestUserDefaults
{
public const string Email = "test.user@example.com";
}
public class SetPlanRequest
{
public string? Email { get; set; }
public string Plan { get; set; } = "trial";
public bool ResetPages { get; set; }
}
}
#endif