using EntitlementBLL.Options;
using GB5Shared.Deployment;
using GB5Shared.DTO.Framework.Login;
using Microsoft.Extensions.Options;
namespace EntitlementBLL.Common;
///
/// Builds the LoginDTO used for every IQueryExecutor call inside the Entitlement platform
/// services (IEntitlementService, IFeatureFlagService, IEntitlementBundleService,
/// IProvisioningService, ISubscriptionService) — whose interface signatures (per spec) take
/// clientId/userId as plain ints rather than a caller-supplied LoginDTO, since these services
/// must also be callable from Dapr subscribers and Quartz jobs with no inbound HTTP session.
/// Mirrors the `new LoginDTO { ClientId = tenantId, UserId = -1 }` pattern already used by
/// FLSBLL.Handlers.FlsReminderJobHandler and ComplianceSL.Subscriptions.AlertCheckSubscriber,
/// extended with the fixed platform DatabaseName from EntitlementDbOptions (see that class's
/// doc comment for why Entitlement lives in one shared database, not a per-tenant one).
///
public interface IEntitlementLoginFactory
{
LoginDTO Create(int clientId, int userId = -1);
}
public class EntitlementLoginFactory : IEntitlementLoginFactory
{
private readonly EntitlementDbOptions _Options;
private readonly IGB5Environment _Env;
public EntitlementLoginFactory(IOptions options, IGB5Environment env)
{
_Options = options.Value;
_Env = env;
}
// DatabaseName resolves through IGB5Environment so Dev/QC/Live each hit a separate
// physical EntitlementDb-{Dev,QC} / EntitlementDb (Live, unsuffixed) database — see
// Docs/SqlWorkbench-DB-Settings-Guide.md and the environment-tiering plan.
public LoginDTO Create(int clientId, int userId = -1) => new()
{
ClientId = clientId,
UserId = userId,
DatabaseName = _Env.Resolve(_Options.DatabaseName),
ServerId = _Options.ServerId,
ServerConfigId = _Options.ServerConfigId
};
}