using FastEndpoints;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.DTO.Framework.ResponseStandard;
using GB5Shared.FastEndPoint;
using TCMSBLL.Interfaces;
using static GB5Shared.GB5Constant.Constant;
namespace TCMSSL.EndPoints.TestEnv;
///
/// GET /TCMS/TestEnv/GetTestDbConnection?serverConfigId={int}
/// Returns decrypted connection details for a TEST instance (DBINSTANCETYPE=1).
/// Used by Cypress test suites to establish a direct DB connection for data assertions.
/// Returns a validation error — not 404 — when the configId is not a TEST instance,
/// to avoid leaking whether the config exists at all.
///
public class GetTestDbConnection : BaseEndpoint>
{
private readonly ITCMSTestEnvBLL _Bll;
public GetTestDbConnection(ITCMSTestEnvBLL bll) => _Bll = bll;
public override void Configure()
{
Get("/TCMS/TestEnv/GetTestDbConnection");
AllowAnonymous();
}
public record Params(
[property: FromHeader] string Login,
[property: QueryParam] int ServerConfigId
);
// Never cache — contains decrypted credentials
protected override string? GetCacheKey(Params req, LoginDTO loginDTO) => null;
protected override async Task> ExecuteAsync(
Params req, LoginDTO loginDTO, CancellationToken ct)
{
var result = await _Bll.GetTestDbConnectionAsync(req.ServerConfigId, loginDTO, ct);
if (result is null)
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
"ServerConfigId is not a registered TEST instance.",
CacheKeyLevel.NOT_REQUIRED, loginDTO);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
result, CacheKeyLevel.NOT_REQUIRED, loginDTO);
}
}