refactor: rename UserWalletChangeLog→UserWalletHistory, add History interceptor & migration
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 9m11s

- Rename UserWalletChangeLog to UserWalletHistory across 54+ files (entities, configs, DTOs, commands, queries, protos, services)
- Rename 34 files and 11 directories accordingly
- Rename proto file userwalletchangelog.proto → userwallethistory.proto
- Add IHasHistory<T> generic interface for history auto-tracking
- Implement IHasHistory<PackageHistory> on Package entity
- Add HistoryTrackingSaveChangesInterceptor (reflection-based, auto-fills Old* values from OriginalValues)
- Wire interceptor in DI and ApplicationDbContext
- Add EF migration Q27_HistoryTables_And_RenameWalletHistory:
  * RenameTable UserWalletChangeLogs → UserWalletHistories (preserves data)
  * Rename PK, FK constraints and indexes via sp_rename
  * CreateTable ClubMembershipCycleHistories + PackageHistories
This commit is contained in:
masoodafar-web
2026-02-27 06:22:15 +03:30
parent fdbb91d2e1
commit 10d2ca20d1
86 changed files with 6255 additions and 472 deletions
@@ -0,0 +1,19 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.CreateNewUserWalletHistory;
public record CreateNewUserWalletHistoryCommand : IRequest<CreateNewUserWalletHistoryResponseDto>
{
//شناسه کیف پول
public long WalletId { get; init; }
//موجودی
public long CurrentBalance { get; init; }
//تغییر موجودی
public long ChangeValue { get; init; }
//موجودی جاری شبکه
public long CurrentNetworkBalance { get; init; }
//تغییر موجودی شبکه
public long ChangeNerworkValue { get; init; }
//افزایشی؟
public bool IsIncrease { get; init; }
//شناسه ارجاع
public long? RefrenceId { get; init; }
}
@@ -0,0 +1,21 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.CreateNewUserWalletHistory;
public class CreateNewUserWalletHistoryCommandHandler : IRequestHandler<CreateNewUserWalletHistoryCommand, CreateNewUserWalletHistoryResponseDto>
{
private readonly IApplicationDbContext _context;
public CreateNewUserWalletHistoryCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<CreateNewUserWalletHistoryResponseDto> Handle(CreateNewUserWalletHistoryCommand request,
CancellationToken cancellationToken)
{
var entity = request.Adapt<UserWalletHistory>();
await _context.UserWalletHistories.AddAsync(entity, cancellationToken);
entity.AddDomainEvent(new CreateNewUserWalletHistoryEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return entity.Adapt<CreateNewUserWalletHistoryResponseDto>();
}
}
@@ -0,0 +1,26 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.CreateNewUserWalletHistory;
public class CreateNewUserWalletHistoryCommandValidator : AbstractValidator<CreateNewUserWalletHistoryCommand>
{
public CreateNewUserWalletHistoryCommandValidator()
{
RuleFor(model => model.WalletId)
.NotNull();
RuleFor(model => model.CurrentBalance)
.NotNull();
RuleFor(model => model.ChangeValue)
.NotNull();
RuleFor(model => model.CurrentNetworkBalance)
.NotNull();
RuleFor(model => model.ChangeNerworkValue)
.NotNull();
RuleFor(model => model.IsIncrease)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<CreateNewUserWalletHistoryCommand>.CreateWithOptions((CreateNewUserWalletHistoryCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,7 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.CreateNewUserWalletHistory;
public class CreateNewUserWalletHistoryResponseDto
{
//شناسه
public long Id { get; set; }
}
@@ -0,0 +1,7 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.DeleteUserWalletHistory;
public record DeleteUserWalletHistoryCommand : IRequest<Unit>
{
//شناسه
public long Id { get; init; }
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.DeleteUserWalletHistory;
public class DeleteUserWalletHistoryCommandHandler : IRequestHandler<DeleteUserWalletHistoryCommand, Unit>
{
private readonly IApplicationDbContext _context;
public DeleteUserWalletHistoryCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(DeleteUserWalletHistoryCommand request, CancellationToken cancellationToken)
{
var entity = await _context.UserWalletHistories
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserWalletHistory), request.Id);
entity.IsDeleted = true;
_context.UserWalletHistories.Update(entity);
entity.AddDomainEvent(new DeleteUserWalletHistoryEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
@@ -0,0 +1,16 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.DeleteUserWalletHistory;
public class DeleteUserWalletHistoryCommandValidator : AbstractValidator<DeleteUserWalletHistoryCommand>
{
public DeleteUserWalletHistoryCommandValidator()
{
RuleFor(model => model.Id)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<DeleteUserWalletHistoryCommand>.CreateWithOptions((DeleteUserWalletHistoryCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,21 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.UpdateUserWalletHistory;
public record UpdateUserWalletHistoryCommand : IRequest<Unit>
{
//شناسه
public long Id { get; init; }
//شناسه کیف پول
public long WalletId { get; init; }
//موجودی
public long CurrentBalance { get; init; }
//تغییر موجودی
public long ChangeValue { get; init; }
//موجودی جاری شبکه
public long CurrentNetworkBalance { get; init; }
//تغییر موجودی شبکه
public long ChangeNerworkValue { get; init; }
//افزایشی؟
public bool IsIncrease { get; init; }
//شناسه ارجاع
public long? RefrenceId { get; init; }
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.UpdateUserWalletHistory;
public class UpdateUserWalletHistoryCommandHandler : IRequestHandler<UpdateUserWalletHistoryCommand, Unit>
{
private readonly IApplicationDbContext _context;
public UpdateUserWalletHistoryCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(UpdateUserWalletHistoryCommand request, CancellationToken cancellationToken)
{
var entity = await _context.UserWalletHistories
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserWalletHistory), request.Id);
request.Adapt(entity);
_context.UserWalletHistories.Update(entity);
entity.AddDomainEvent(new UpdateUserWalletHistoryEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
@@ -0,0 +1,28 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Commands.UpdateUserWalletHistory;
public class UpdateUserWalletHistoryCommandValidator : AbstractValidator<UpdateUserWalletHistoryCommand>
{
public UpdateUserWalletHistoryCommandValidator()
{
RuleFor(model => model.Id)
.NotNull();
RuleFor(model => model.WalletId)
.NotNull();
RuleFor(model => model.CurrentBalance)
.NotNull();
RuleFor(model => model.ChangeValue)
.NotNull();
RuleFor(model => model.CurrentNetworkBalance)
.NotNull();
RuleFor(model => model.ChangeNerworkValue)
.NotNull();
RuleFor(model => model.IsIncrease)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<UpdateUserWalletHistoryCommand>.CreateWithOptions((UpdateUserWalletHistoryCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.UserWalletHistoryCQ.EventHandlers;
public class CreateNewUserWalletHistoryEventHandler : INotificationHandler<CreateNewUserWalletHistoryEvent>
{
private readonly ILogger<
CreateNewUserWalletHistoryEventHandler> _logger;
public CreateNewUserWalletHistoryEventHandler(ILogger<CreateNewUserWalletHistoryEventHandler> logger)
{
_logger = logger;
}
public Task Handle(CreateNewUserWalletHistoryEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.UserWalletHistoryCQ.EventHandlers;
public class DeleteUserWalletHistoryEventHandler : INotificationHandler<DeleteUserWalletHistoryEvent>
{
private readonly ILogger<
DeleteUserWalletHistoryEventHandler> _logger;
public DeleteUserWalletHistoryEventHandler(ILogger<DeleteUserWalletHistoryEventHandler> logger)
{
_logger = logger;
}
public Task Handle(DeleteUserWalletHistoryEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.UserWalletHistoryCQ.EventHandlers;
public class UpdateUserWalletHistoryEventHandler : INotificationHandler<UpdateUserWalletHistoryEvent>
{
private readonly ILogger<
UpdateUserWalletHistoryEventHandler> _logger;
public UpdateUserWalletHistoryEventHandler(ILogger<UpdateUserWalletHistoryEventHandler> logger)
{
_logger = logger;
}
public Task Handle(UpdateUserWalletHistoryEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}
@@ -0,0 +1,27 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Queries.GetAllUserWalletHistoryByFilter;
public record GetAllUserWalletHistoryByFilterQuery : IRequest<GetAllUserWalletHistoryByFilterResponseDto>
{
//موقعیت صفحه بندی
public PaginationState? PaginationState { get; init; }
//مرتب سازی بر اساس
public string? SortBy { get; init; }
//فیلتر
public GetAllUserWalletHistoryByFilterFilter? Filter { get; init; }
}public class GetAllUserWalletHistoryByFilterFilter
{
//شناسه
public long? Id { get; set; }
//شناسه کیف پول
public long? WalletId { get; set; }
//موجودی
public long? CurrentBalance { get; set; }
//تغییر موجودی
public long? ChangeValue { get; set; }
//افزایشی؟
public bool? IsIncrease { get; set; }
//شناسه ارجاع
public long? RefrenceId { get; set; }
//شناسه کاربر
public long? UserId { get; set; }
}
@@ -0,0 +1,37 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Queries.GetAllUserWalletHistoryByFilter;
public class GetAllUserWalletHistoryByFilterQueryHandler : IRequestHandler<GetAllUserWalletHistoryByFilterQuery, GetAllUserWalletHistoryByFilterResponseDto>
{
private readonly IApplicationDbContext _context;
public GetAllUserWalletHistoryByFilterQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetAllUserWalletHistoryByFilterResponseDto> Handle(GetAllUserWalletHistoryByFilterQuery request, CancellationToken cancellationToken)
{
var query = _context.UserWalletHistories
.Include(i=>i.Wallet)
.ApplyOrder(sortBy: request.SortBy)
.AsNoTracking()
.AsQueryable();
if (request.Filter is not null)
{
query = query
.Where(x => request.Filter.Id == null || x.Id == request.Filter.Id)
.Where(x => request.Filter.WalletId == null || x.WalletId == request.Filter.WalletId)
.Where(x => request.Filter.CurrentBalance == null || x.CurrentBalance == request.Filter.CurrentBalance)
.Where(x => request.Filter.ChangeValue == null || x.ChangeValue == request.Filter.ChangeValue)
.Where(x => request.Filter.IsIncrease == null || x.IsIncrease == request.Filter.IsIncrease)
.Where(x => request.Filter.RefrenceId == null || x.RefrenceId == request.Filter.RefrenceId)
.Where(x => request.Filter.UserId == null || x.Wallet.UserId == request.Filter.UserId)
;
}
return new GetAllUserWalletHistoryByFilterResponseDto
{
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
.ProjectToType<GetAllUserWalletHistoryByFilterResponseModel>().ToListAsync(cancellationToken)
};
}
}
@@ -0,0 +1,14 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Queries.GetAllUserWalletHistoryByFilter;
public class GetAllUserWalletHistoryByFilterQueryValidator : AbstractValidator<GetAllUserWalletHistoryByFilterQuery>
{
public GetAllUserWalletHistoryByFilterQueryValidator()
{
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GetAllUserWalletHistoryByFilterQuery>.CreateWithOptions((GetAllUserWalletHistoryByFilterQuery)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,29 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Queries.GetAllUserWalletHistoryByFilter;
public class GetAllUserWalletHistoryByFilterResponseDto
{
//متادیتا
public MetaData MetaData { get; set; }
//مدل خروجی
public List<GetAllUserWalletHistoryByFilterResponseModel>? Models { get; set; }
}public class GetAllUserWalletHistoryByFilterResponseModel
{
//شناسه
public long Id { get; set; }
//شناسه کیف پول
public long WalletId { get; set; }
//موجودی
public long CurrentBalance { get; set; }
//تغییر موجودی
public long ChangeValue { get; set; }
//موجودی جاری شبکه
public long CurrentNetworkBalance { get; set; }
//تغییر موجودی شبکه
public long ChangeNerworkValue { get; set; }
//افزایشی؟
public bool IsIncrease { get; set; }
//شناسه ارجاع
public long? RefrenceId { get; set; }
//تاریخ ایجاد
public DateTime CreatedAt { get; set; }
}
@@ -0,0 +1,7 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Queries.GetUserWalletHistory;
public record GetUserWalletHistoryQuery : IRequest<GetUserWalletHistoryResponseDto>
{
//شناسه
public long Id { get; init; }
}
@@ -0,0 +1,22 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Queries.GetUserWalletHistory;
public class GetUserWalletHistoryQueryHandler : IRequestHandler<GetUserWalletHistoryQuery, GetUserWalletHistoryResponseDto>
{
private readonly IApplicationDbContext _context;
public GetUserWalletHistoryQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetUserWalletHistoryResponseDto> Handle(GetUserWalletHistoryQuery request,
CancellationToken cancellationToken)
{
var response = await _context.UserWalletHistories
.AsNoTracking()
.Where(x => x.Id == request.Id)
.ProjectToType<GetUserWalletHistoryResponseDto>()
.FirstOrDefaultAsync(cancellationToken);
return response ?? throw new NotFoundException(nameof(UserWalletHistory), request.Id);
}
}
@@ -0,0 +1,16 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Queries.GetUserWalletHistory;
public class GetUserWalletHistoryQueryValidator : AbstractValidator<GetUserWalletHistoryQuery>
{
public GetUserWalletHistoryQueryValidator()
{
RuleFor(model => model.Id)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GetUserWalletHistoryQuery>.CreateWithOptions((GetUserWalletHistoryQuery)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,23 @@
namespace CMSMicroservice.Application.UserWalletHistoryCQ.Queries.GetUserWalletHistory;
public class GetUserWalletHistoryResponseDto
{
//شناسه
public long Id { get; set; }
//شناسه کیف پول
public long WalletId { get; set; }
//موجودی
public long CurrentBalance { get; set; }
//تغییر موجودی
public long ChangeValue { get; set; }
//موجودی جاری شبکه
public long CurrentNetworkBalance { get; set; }
//تغییر موجودی شبکه
public long ChangeNerworkValue { get; set; }
//افزایشی؟
public bool IsIncrease { get; set; }
//شناسه ارجاع
public long? RefrenceId { get; set; }
//تاریخ ایجاد
public DateTime CreatedAt { get; set; }
}