Add product configurations
This commit is contained in:
@@ -42,10 +42,11 @@ public class SysConfigController(SysConfigService service, ICurrentUserService c
|
|||||||
[HttpPut("{key}")]
|
[HttpPut("{key}")]
|
||||||
public async Task<IActionResult> Set(string key, SetSysConfigRequest request, CancellationToken ct)
|
public async Task<IActionResult> Set(string key, SetSysConfigRequest request, CancellationToken ct)
|
||||||
{
|
{
|
||||||
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
var userId = currentUser.UserId ?? request.UserId;
|
||||||
|
if (userId is null) return BadRequest(new { error = "UserId is required while auth is unavailable" });
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = await service.SetAsync(key, request.Value, userId, ct);
|
var result = await service.SetAsync(key, request.Value, userId.Value, ct);
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
||||||
|
|||||||
@@ -4,6 +4,6 @@ namespace Distribution.Features.SysConfig;
|
|||||||
|
|
||||||
public record SysConfigResponse(string Key, string Value, DateTime UpdatedAt, Guid? UpdatedBy);
|
public record SysConfigResponse(string Key, string Value, DateTime UpdatedAt, Guid? UpdatedBy);
|
||||||
|
|
||||||
public record SetSysConfigRequest(string Value);
|
public record SetSysConfigRequest(string Value, Guid? UserId);
|
||||||
|
|
||||||
public record SetInvoiceCreationModeRequest(InvoiceCreationMode Mode);
|
public record SetInvoiceCreationModeRequest(InvoiceCreationMode Mode);
|
||||||
|
|||||||
@@ -7,6 +7,22 @@ public class SysConfigService(ISysConfigRepository repo, IUnitOfWork uow)
|
|||||||
{
|
{
|
||||||
public const string KeyInvoiceCreationMode = "InvoiceCreationMode";
|
public const string KeyInvoiceCreationMode = "InvoiceCreationMode";
|
||||||
|
|
||||||
|
public const string KeySerialNumberTrackingEnabled = "Feature.SerialNumberTracking";
|
||||||
|
public const string KeyBatchTrackingEnabled = "Feature.BatchTracking";
|
||||||
|
public const string KeyExpiryDateTrackingEnabled = "Feature.ExpiryDateTracking";
|
||||||
|
public const string KeyWarrantyTrackingEnabled = "Feature.WarrantyTracking";
|
||||||
|
public const string KeyCustomWarrantyOverrideEnabled = "Feature.CustomWarrantyOverride";
|
||||||
|
public const string KeyDefaultWarrantyPeriodDays = "Feature.DefaultWarrantyPeriodDays";
|
||||||
|
|
||||||
|
private static readonly string[] BooleanFeatureKeys =
|
||||||
|
[
|
||||||
|
KeySerialNumberTrackingEnabled,
|
||||||
|
KeyBatchTrackingEnabled,
|
||||||
|
KeyExpiryDateTrackingEnabled,
|
||||||
|
KeyWarrantyTrackingEnabled,
|
||||||
|
KeyCustomWarrantyOverrideEnabled,
|
||||||
|
];
|
||||||
|
|
||||||
public async Task<List<SysConfigResponse>> GetAllAsync()
|
public async Task<List<SysConfigResponse>> GetAllAsync()
|
||||||
{
|
{
|
||||||
var configs = await repo.GetAllAsync();
|
var configs = await repo.GetAllAsync();
|
||||||
@@ -55,11 +71,35 @@ public class SysConfigService(ISysConfigRepository repo, IUnitOfWork uow)
|
|||||||
return Enum.TryParse<InvoiceCreationMode>(config.Value, out var mode) ? mode : InvoiceCreationMode.ModeB;
|
return Enum.TryParse<InvoiceCreationMode>(config.Value, out var mode) ? mode : InvoiceCreationMode.ModeB;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> GetSerialNumberTrackingEnabledAsync() => await GetBoolAsync(KeySerialNumberTrackingEnabled);
|
||||||
|
public async Task<bool> GetBatchTrackingEnabledAsync() => await GetBoolAsync(KeyBatchTrackingEnabled);
|
||||||
|
public async Task<bool> GetExpiryDateTrackingEnabledAsync() => await GetBoolAsync(KeyExpiryDateTrackingEnabled);
|
||||||
|
public async Task<bool> GetWarrantyTrackingEnabledAsync() => await GetBoolAsync(KeyWarrantyTrackingEnabled);
|
||||||
|
public async Task<bool> GetCustomWarrantyOverrideEnabledAsync() => await GetBoolAsync(KeyCustomWarrantyOverrideEnabled);
|
||||||
|
|
||||||
|
public async Task<int> GetDefaultWarrantyPeriodDaysAsync()
|
||||||
|
{
|
||||||
|
var config = await repo.GetByKeyAsync(KeyDefaultWarrantyPeriodDays);
|
||||||
|
return config is not null && int.TryParse(config.Value, out var days) ? days : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<bool> GetBoolAsync(string key)
|
||||||
|
{
|
||||||
|
var config = await repo.GetByKeyAsync(key);
|
||||||
|
return config is not null && bool.TryParse(config.Value, out var enabled) && enabled;
|
||||||
|
}
|
||||||
|
|
||||||
private static void ValidateValue(string key, string value)
|
private static void ValidateValue(string key, string value)
|
||||||
{
|
{
|
||||||
if (key == KeyInvoiceCreationMode && !Enum.TryParse<InvoiceCreationMode>(value, out _))
|
if (key == KeyInvoiceCreationMode && !Enum.TryParse<InvoiceCreationMode>(value, out _))
|
||||||
throw new InvalidOperationException(
|
throw new InvalidOperationException(
|
||||||
$"Invalid value '{value}' for '{key}'. Allowed values: {string.Join(", ", Enum.GetNames<InvoiceCreationMode>())}.");
|
$"Invalid value '{value}' for '{key}'. Allowed values: {string.Join(", ", Enum.GetNames<InvoiceCreationMode>())}.");
|
||||||
|
|
||||||
|
if (BooleanFeatureKeys.Contains(key) && !bool.TryParse(value, out _))
|
||||||
|
throw new InvalidOperationException($"Invalid value '{value}' for '{key}'. Allowed values: true, false.");
|
||||||
|
|
||||||
|
if (key == KeyDefaultWarrantyPeriodDays && (!int.TryParse(value, out var days) || days < 0))
|
||||||
|
throw new InvalidOperationException($"Invalid value '{value}' for '{key}'. Must be a non-negative integer.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SysConfigResponse ToResponse(Models.SysConfig c) =>
|
private static SysConfigResponse ToResponse(Models.SysConfig c) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user