feat: Add ConfigurationCQ - Phase 2 Application Layer

Implemented complete CQRS pattern for System Configuration management:

Commands:
- SetConfigurationValueCommand: Create or update configurations with history tracking
- DeactivateConfigurationCommand: Deactivate configurations with audit trail

Queries:
- GetConfigurationByKeyQuery: Retrieve configuration by Scope and Key
- GetAllConfigurationsQuery: List all configurations with filters and pagination
- GetConfigurationHistoryQuery: View complete audit history for any configuration

Features:
- All commands include FluentValidation validators
- History recording to SystemConfigurationHistory table
- Pagination support for list queries
- DTOs for clean data transfer
- Null-safe implementations

Updated:
- IApplicationDbContext: Added 11 new DbSets for network-club entities
- GlobalUsings: Added new entity namespaces

Build Status:  Success (0 errors, 184 warnings in legacy code)
This commit is contained in:
masoodafar-web
2025-11-29 04:02:02 +03:30
parent 0d52515be4
commit f6fa070067
20 changed files with 612 additions and 0 deletions

View File

@@ -23,5 +23,16 @@ public interface IApplicationDbContext
DbSet<UserOrder> UserOrders { get; }
DbSet<UserWallet> UserWallets { get; }
DbSet<UserWalletChangeLog> UserWalletChangeLogs { get; }
DbSet<SystemConfiguration> SystemConfigurations { get; }
DbSet<SystemConfigurationHistory> SystemConfigurationHistories { get; }
DbSet<ClubMembership> ClubMemberships { get; }
DbSet<ClubMembershipHistory> ClubMembershipHistories { get; }
DbSet<ClubFeature> ClubFeatures { get; }
DbSet<UserClubFeature> UserClubFeatures { get; }
DbSet<NetworkWeeklyBalance> NetworkWeeklyBalances { get; }
DbSet<NetworkMembershipHistory> NetworkMembershipHistories { get; }
DbSet<WeeklyCommissionPool> WeeklyCommissionPools { get; }
DbSet<UserCommissionPayout> UserCommissionPayouts { get; }
DbSet<CommissionPayoutHistory> CommissionPayoutHistories { get; }
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}

View File

@@ -0,0 +1,17 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Commands.DeactivateConfiguration;
/// <summary>
/// Command برای غیرفعال کردن یک Configuration
/// </summary>
public record DeactivateConfigurationCommand : IRequest<Unit>
{
/// <summary>
/// شناسه Configuration
/// </summary>
public long ConfigurationId { get; init; }
/// <summary>
/// دلیل غیرفعال‌سازی
/// </summary>
public string? Reason { get; init; }
}

View File

@@ -0,0 +1,47 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Commands.DeactivateConfiguration;
public class DeactivateConfigurationCommandHandler : IRequestHandler<DeactivateConfigurationCommand, Unit>
{
private readonly IApplicationDbContext _context;
public DeactivateConfigurationCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(DeactivateConfigurationCommand request, CancellationToken cancellationToken)
{
var entity = await _context.SystemConfigurations
.FirstOrDefaultAsync(x => x.Id == request.ConfigurationId, cancellationToken)
?? throw new NotFoundException(nameof(SystemConfiguration), request.ConfigurationId);
// اگر از قبل غیرفعال است، خطا ندهیم
if (!entity.IsActive)
{
return Unit.Value;
}
var oldValue = entity.Value;
entity.IsActive = false;
_context.SystemConfigurations.Update(entity);
await _context.SaveChangesAsync(cancellationToken);
// ثبت تاریخچه
var history = new SystemConfigurationHistory
{
ConfigurationId = entity.Id,
Scope = entity.Scope,
Key = entity.Key,
OldValue = oldValue,
NewValue = entity.Value,
Reason = request.Reason ?? "Configuration deactivated",
PerformedBy = "System" // TODO: باید از Current User گرفته شود
};
await _context.SystemConfigurationHistories.AddAsync(history, cancellationToken);
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}

View File

@@ -0,0 +1,29 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Commands.DeactivateConfiguration;
public class DeactivateConfigurationCommandValidator : AbstractValidator<DeactivateConfigurationCommand>
{
public DeactivateConfigurationCommandValidator()
{
RuleFor(x => x.ConfigurationId)
.GreaterThan(0)
.WithMessage("شناسه Configuration معتبر نیست");
RuleFor(x => x.Reason)
.MaximumLength(500)
.WithMessage("دلیل غیرفعال‌سازی نمی‌تواند بیشتر از 500 کاراکتر باشد")
.When(x => !string.IsNullOrEmpty(x.Reason));
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(
ValidationContext<DeactivateConfigurationCommand>.CreateWithOptions(
(DeactivateConfigurationCommand)model,
x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}

View File

@@ -0,0 +1,32 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Commands.SetConfigurationValue;
/// <summary>
/// Command برای تنظیم یا به‌روزرسانی یک Configuration
/// </summary>
public record SetConfigurationValueCommand : IRequest<long>
{
/// <summary>
/// محدوده تنظیمات (System, Network, Club, Commission)
/// </summary>
public ConfigurationScope Scope { get; init; }
/// <summary>
/// کلید یکتا برای تنظیمات
/// </summary>
public string Key { get; init; }
/// <summary>
/// مقدار تنظیمات (JSON format)
/// </summary>
public string Value { get; init; }
/// <summary>
/// توضیحات تنظیمات
/// </summary>
public string? Description { get; init; }
/// <summary>
/// دلیل تغییر (برای History)
/// </summary>
public string? ChangeReason { get; init; }
}

View File

@@ -0,0 +1,74 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Commands.SetConfigurationValue;
public class SetConfigurationValueCommandHandler : IRequestHandler<SetConfigurationValueCommand, long>
{
private readonly IApplicationDbContext _context;
public SetConfigurationValueCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<long> Handle(SetConfigurationValueCommand request, CancellationToken cancellationToken)
{
// بررسی وجود Configuration با همین Scope و Key
var existingConfig = await _context.SystemConfigurations
.FirstOrDefaultAsync(x =>
x.Scope == request.Scope &&
x.Key == request.Key,
cancellationToken);
SystemConfiguration entity;
bool isNewRecord = existingConfig == null;
string oldValue = null;
if (isNewRecord)
{
// ایجاد Configuration جدید
entity = new SystemConfiguration
{
Scope = request.Scope,
Key = request.Key,
Value = request.Value,
Description = request.Description,
IsActive = true
};
await _context.SystemConfigurations.AddAsync(entity, cancellationToken);
}
else
{
// به‌روزرسانی Configuration موجود
entity = existingConfig;
oldValue = entity.Value;
entity.Value = request.Value;
if (!string.IsNullOrEmpty(request.Description))
{
entity.Description = request.Description;
}
_context.SystemConfigurations.Update(entity);
}
await _context.SaveChangesAsync(cancellationToken);
// ثبت تاریخچه
var history = new SystemConfigurationHistory
{
ConfigurationId = entity.Id,
Scope = entity.Scope,
Key = entity.Key,
OldValue = oldValue,
NewValue = entity.Value,
Reason = request.ChangeReason ?? (isNewRecord ? "Initial creation" : "Value updated"),
PerformedBy = "System" // TODO: باید از Current User گرفته شود
};
await _context.SystemConfigurationHistories.AddAsync(history, cancellationToken);
await _context.SaveChangesAsync(cancellationToken);
return entity.Id;
}
}

View File

@@ -0,0 +1,48 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Commands.SetConfigurationValue;
public class SetConfigurationValueCommandValidator : AbstractValidator<SetConfigurationValueCommand>
{
public SetConfigurationValueCommandValidator()
{
RuleFor(x => x.Scope)
.IsInEnum()
.WithMessage("محدوده تنظیمات معتبر نیست");
RuleFor(x => x.Key)
.NotEmpty()
.WithMessage("کلید تنظیمات الزامی است")
.MaximumLength(100)
.WithMessage("کلید تنظیمات نمی‌تواند بیشتر از 100 کاراکتر باشد")
.Matches(@"^[a-zA-Z0-9_\.]+$")
.WithMessage("کلید تنظیمات فقط می‌تواند شامل حروف انگلیسی، اعداد، نقطه و آندرلاین باشد");
RuleFor(x => x.Value)
.NotEmpty()
.WithMessage("مقدار تنظیمات الزامی است")
.MaximumLength(2000)
.WithMessage("مقدار تنظیمات نمی‌تواند بیشتر از 2000 کاراکتر باشد");
RuleFor(x => x.Description)
.MaximumLength(500)
.WithMessage("توضیحات نمی‌تواند بیشتر از 500 کاراکتر باشد")
.When(x => !string.IsNullOrEmpty(x.Description));
RuleFor(x => x.ChangeReason)
.MaximumLength(500)
.WithMessage("دلیل تغییر نمی‌تواند بیشتر از 500 کاراکتر باشد")
.When(x => !string.IsNullOrEmpty(x.ChangeReason));
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(
ValidationContext<SetConfigurationValueCommand>.CreateWithOptions(
(SetConfigurationValueCommand)model,
x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}

View File

@@ -0,0 +1,40 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetAllConfigurations;
/// <summary>
/// Query برای دریافت لیست تمام Configuration ها با فیلتر
/// </summary>
public record GetAllConfigurationsQuery : IRequest<GetAllConfigurationsResponseDto>
{
/// <summary>
/// موقعیت صفحه‌بندی
/// </summary>
public PaginationState? PaginationState { get; init; }
/// <summary>
/// مرتب‌سازی بر اساس
/// </summary>
public string? SortBy { get; init; }
/// <summary>
/// فیلتر
/// </summary>
public GetAllConfigurationsFilter? Filter { get; init; }
}
public class GetAllConfigurationsFilter
{
/// <summary>
/// فیلتر بر اساس محدوده
/// </summary>
public ConfigurationScope? Scope { get; set; }
/// <summary>
/// جستجو در کلید
/// </summary>
public string? KeyContains { get; set; }
/// <summary>
/// فقط Configuration های فعال
/// </summary>
public bool? IsActive { get; set; }
}

View File

@@ -0,0 +1,50 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetAllConfigurations;
public class GetAllConfigurationsQueryHandler : IRequestHandler<GetAllConfigurationsQuery, GetAllConfigurationsResponseDto>
{
private readonly IApplicationDbContext _context;
public GetAllConfigurationsQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetAllConfigurationsResponseDto> Handle(GetAllConfigurationsQuery request, CancellationToken cancellationToken)
{
var query = _context.SystemConfigurations
.ApplyOrder(sortBy: request.SortBy)
.AsNoTracking()
.AsQueryable();
if (request.Filter is not null)
{
query = query
.Where(x => request.Filter.Scope == null || x.Scope == request.Filter.Scope)
.Where(x => request.Filter.KeyContains == null || x.Key.Contains(request.Filter.KeyContains))
.Where(x => request.Filter.IsActive == null || x.IsActive == request.Filter.IsActive);
}
var meta = await query.GetMetaData(request.PaginationState, cancellationToken);
var models = await query
.PaginatedListAsync(paginationState: request.PaginationState)
.Select(x => new GetAllConfigurationsResponseModel
{
Id = x.Id,
Scope = x.Scope,
Key = x.Key,
Value = x.Value,
Description = x.Description,
IsActive = x.IsActive,
Created = x.Created,
LastModified = x.LastModified
})
.ToListAsync(cancellationToken);
return new GetAllConfigurationsResponseDto
{
MetaData = meta,
Models = models
};
}
}

View File

@@ -0,0 +1,25 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetAllConfigurations;
public class GetAllConfigurationsQueryValidator : AbstractValidator<GetAllConfigurationsQuery>
{
public GetAllConfigurationsQueryValidator()
{
RuleFor(x => x.Filter.Scope)
.IsInEnum()
.WithMessage("محدوده تنظیمات معتبر نیست")
.When(x => x.Filter?.Scope != null);
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(
ValidationContext<GetAllConfigurationsQuery>.CreateWithOptions(
(GetAllConfigurationsQuery)model,
x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}

View File

@@ -0,0 +1,19 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetAllConfigurations;
public class GetAllConfigurationsResponseDto
{
public MetaData MetaData { get; set; }
public List<GetAllConfigurationsResponseModel> Models { get; set; }
}
public class GetAllConfigurationsResponseModel
{
public long Id { get; set; }
public ConfigurationScope Scope { get; set; }
public string Key { get; set; }
public string Value { get; set; }
public string? Description { get; set; }
public bool IsActive { get; set; }
public DateTimeOffset Created { get; set; }
public DateTimeOffset? LastModified { get; set; }
}

View File

@@ -0,0 +1,16 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationByKey;
/// <summary>
/// DTO برای نمایش اطلاعات Configuration
/// </summary>
public class ConfigurationDto
{
public long Id { get; set; }
public ConfigurationScope Scope { get; set; }
public string Key { get; set; }
public string Value { get; set; }
public string? Description { get; set; }
public bool IsActive { get; set; }
public DateTimeOffset Created { get; set; }
public DateTimeOffset? LastModified { get; set; }
}

View File

@@ -0,0 +1,17 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationByKey;
/// <summary>
/// Query برای دریافت یک Configuration بر اساس Scope و Key
/// </summary>
public record GetConfigurationByKeyQuery : IRequest<ConfigurationDto?>
{
/// <summary>
/// محدوده تنظیمات
/// </summary>
public ConfigurationScope Scope { get; init; }
/// <summary>
/// کلید تنظیمات
/// </summary>
public string Key { get; init; }
}

View File

@@ -0,0 +1,34 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationByKey;
public class GetConfigurationByKeyQueryHandler : IRequestHandler<GetConfigurationByKeyQuery, ConfigurationDto?>
{
private readonly IApplicationDbContext _context;
public GetConfigurationByKeyQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<ConfigurationDto?> Handle(GetConfigurationByKeyQuery request, CancellationToken cancellationToken)
{
var config = await _context.SystemConfigurations
.AsNoTracking()
.Where(x => x.Scope == request.Scope && x.Key == request.Key)
.FirstOrDefaultAsync(cancellationToken);
if (config == null)
return null;
return new ConfigurationDto
{
Id = config.Id,
Scope = config.Scope,
Key = config.Key,
Value = config.Value,
Description = config.Description,
IsActive = config.IsActive,
Created = config.Created,
LastModified = config.LastModified
};
}
}

View File

@@ -0,0 +1,28 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationByKey;
public class GetConfigurationByKeyQueryValidator : AbstractValidator<GetConfigurationByKeyQuery>
{
public GetConfigurationByKeyQueryValidator()
{
RuleFor(x => x.Scope)
.IsInEnum()
.WithMessage("محدوده تنظیمات معتبر نیست");
RuleFor(x => x.Key)
.NotEmpty()
.WithMessage("کلید تنظیمات الزامی است");
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(
ValidationContext<GetConfigurationByKeyQuery>.CreateWithOptions(
(GetConfigurationByKeyQuery)model,
x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}

View File

@@ -0,0 +1,22 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationHistory;
/// <summary>
/// Query برای دریافت تاریخچه تغییرات یک Configuration
/// </summary>
public record GetConfigurationHistoryQuery : IRequest<GetConfigurationHistoryResponseDto>
{
/// <summary>
/// شناسه Configuration
/// </summary>
public long ConfigurationId { get; init; }
/// <summary>
/// موقعیت صفحه‌بندی
/// </summary>
public PaginationState? PaginationState { get; init; }
/// <summary>
/// مرتب‌سازی بر اساس
/// </summary>
public string? SortBy { get; init; }
}

View File

@@ -0,0 +1,53 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationHistory;
public class GetConfigurationHistoryQueryHandler : IRequestHandler<GetConfigurationHistoryQuery, GetConfigurationHistoryResponseDto>
{
private readonly IApplicationDbContext _context;
public GetConfigurationHistoryQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetConfigurationHistoryResponseDto> Handle(GetConfigurationHistoryQuery request, CancellationToken cancellationToken)
{
// بررسی وجود Configuration
var configExists = await _context.SystemConfigurations
.AnyAsync(x => x.Id == request.ConfigurationId, cancellationToken);
if (!configExists)
{
throw new NotFoundException(nameof(SystemConfiguration), request.ConfigurationId);
}
var query = _context.SystemConfigurationHistories
.Where(x => x.ConfigurationId == request.ConfigurationId)
.ApplyOrder(sortBy: request.SortBy ?? "-Created") // پیش‌فرض: جدیدترین اول
.AsNoTracking()
.AsQueryable();
var meta = await query.GetMetaData(request.PaginationState, cancellationToken);
var models = await query
.PaginatedListAsync(paginationState: request.PaginationState)
.Select(x => new GetConfigurationHistoryResponseModel
{
Id = x.Id,
ConfigurationId = x.ConfigurationId,
Scope = x.Scope,
Key = x.Key,
OldValue = x.OldValue,
NewValue = x.NewValue,
ChangeReason = x.Reason,
ChangedBy = x.PerformedBy,
Created = x.Created
})
.ToListAsync(cancellationToken);
return new GetConfigurationHistoryResponseDto
{
MetaData = meta,
Models = models
};
}
}

View File

@@ -0,0 +1,24 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationHistory;
public class GetConfigurationHistoryQueryValidator : AbstractValidator<GetConfigurationHistoryQuery>
{
public GetConfigurationHistoryQueryValidator()
{
RuleFor(x => x.ConfigurationId)
.GreaterThan(0)
.WithMessage("شناسه Configuration معتبر نیست");
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(
ValidationContext<GetConfigurationHistoryQuery>.CreateWithOptions(
(GetConfigurationHistoryQuery)model,
x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}

View File

@@ -0,0 +1,20 @@
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationHistory;
public class GetConfigurationHistoryResponseDto
{
public MetaData MetaData { get; set; }
public List<GetConfigurationHistoryResponseModel> Models { get; set; }
}
public class GetConfigurationHistoryResponseModel
{
public long Id { get; set; }
public long ConfigurationId { get; set; }
public ConfigurationScope Scope { get; set; }
public string Key { get; set; }
public string? OldValue { get; set; }
public string NewValue { get; set; }
public string ChangeReason { get; set; }
public string ChangedBy { get; set; }
public DateTimeOffset Created { get; set; }
}

View File

@@ -3,6 +3,12 @@ global using FluentValidation;
global using Mapster;
global using CMSMicroservice.Domain.Entities;
global using CMSMicroservice.Domain.Entities.Club;
global using CMSMicroservice.Domain.Entities.Network;
global using CMSMicroservice.Domain.Entities.Commission;
global using CMSMicroservice.Domain.Entities.Configuration;
global using CMSMicroservice.Domain.Entities.History;
global using CMSMicroservice.Domain.Enums;
global using CMSMicroservice.Application.Common.Interfaces;
global using System.Threading;
global using System.Threading.Tasks;