Files

39 lines
1.5 KiB
C#

using General_Ledger_Service.Exceptions;
using General_Ledger_Service.Interfaces.Repositories;
namespace General_Ledger_Service.Services
{
// Shared "resulting depth must not exceed the configured max" check, used by every
// recursive-entity Service (Account, CostCenter, and any future one — see
// 01_Full_Development_Plan.md Section 4.2). Not a generic base class (CLAUDE.md Rule 2) —
// just the repeated system_config lookup + comparison + BadRequestException, extracted once.
internal static class DepthGuard
{
public static async Task EnsureWithinMaxDepthAsync(
ISystemConfigRepository systemConfigRepository,
string maxDepthConfigKey,
string? enforceConfigKey,
int resultingDepth,
string exceededMessage)
{
if (enforceConfigKey != null)
{
var enforceConfig = await systemConfigRepository.GetByKeyAsync(enforceConfigKey);
if (enforceConfig?.ConfigValue != "TRUE")
{
return;
}
}
var maxDepthConfig = await systemConfigRepository.GetByKeyAsync(maxDepthConfigKey)
?? throw new NotFoundException($"System config '{maxDepthConfigKey}' not found");
var maxDepth = int.Parse(maxDepthConfig.ConfigValue);
if (resultingDepth > maxDepth)
{
throw new BadRequestException(exceededMessage);
}
}
}
}