76 lines
2.7 KiB
C#
76 lines
2.7 KiB
C#
using IntelligenceApi.Engines.Franchisee;
|
|
using IntelligenceApi.Engines.Franchisor;
|
|
using IntelligenceApi.Engines.General;
|
|
|
|
namespace IntelligenceApi.Engines;
|
|
|
|
/// <summary>
|
|
/// Routes incoming spend distribution requests to the correct engine
|
|
/// based on clientCategory.
|
|
///
|
|
/// ADDING A NEW ENGINE:
|
|
/// 1. Create a folder under Engines/ (e.g. Engines/FoodFranchisee/)
|
|
/// 2. Implement ISpendDistributionEngine
|
|
/// 3. Register in Program.cs
|
|
/// 4. Add the category string to the routing table below
|
|
///
|
|
/// The General engine is the fallback for any unrecognised category,
|
|
/// ensuring existing clients are never broken by new category additions.
|
|
/// </summary>
|
|
public sealed class EngineRouter
|
|
{
|
|
private readonly GeneralEngine _general;
|
|
private readonly FranchiseeEngine _franchisee;
|
|
private readonly FranchisorEngine _franchisor;
|
|
private readonly ILogger<EngineRouter> _logger;
|
|
|
|
public EngineRouter(
|
|
GeneralEngine general,
|
|
FranchiseeEngine franchisee,
|
|
FranchisorEngine franchisor,
|
|
ILogger<EngineRouter> logger)
|
|
{
|
|
_general = general;
|
|
_franchisee = franchisee;
|
|
_franchisor = franchisor;
|
|
_logger = logger;
|
|
}
|
|
|
|
public ISpendDistributionEngine Resolve(string? clientCategory)
|
|
{
|
|
var engine = (clientCategory ?? "General").Trim() switch
|
|
{
|
|
// ── Exact category matches ──────────────────────────────
|
|
"General" => (ISpendDistributionEngine)_general,
|
|
"Franchisee" => _franchisee,
|
|
"Franchisor" => _franchisor,
|
|
|
|
// ── Future sub-categories route to their parent stub
|
|
// until a dedicated engine is built.
|
|
// e.g. "FoodFranchisee" => _foodFranchisee (not yet registered)
|
|
// falls through to Franchisee as the nearest match.
|
|
var c when c.EndsWith("Franchisee", StringComparison.OrdinalIgnoreCase)
|
|
=> _franchisee,
|
|
var c when c.EndsWith("Franchisor", StringComparison.OrdinalIgnoreCase)
|
|
=> _franchisor,
|
|
|
|
// ── Unknown / unrecognised — safe fallback ──────────────
|
|
var c => LogAndFallback(c)
|
|
};
|
|
|
|
_logger.LogInformation(
|
|
"[EngineRouter] Category={Category} → Engine={Engine}",
|
|
clientCategory, engine.EngineName);
|
|
|
|
return engine;
|
|
}
|
|
|
|
private ISpendDistributionEngine LogAndFallback(string category)
|
|
{
|
|
_logger.LogWarning(
|
|
"[EngineRouter] Unrecognised category '{Category}' — falling back to GeneralEngine",
|
|
category);
|
|
return _general;
|
|
}
|
|
}
|