feat: Add NetworkMembershipCQ - Phase 4 Application Layer

- Implemented 3 Commands with handlers and validators:
  * JoinNetworkCommand: Add user to binary network tree
    - Validates parent exists and is in network
    - Validates leg position is empty
    - Records history with NetworkMembershipAction.Join
  * MoveInNetworkCommand: Move user to different position
    - Validates new parent and leg availability
    - Prevents circular dependencies (IsDescendant check)
    - Records old/new parent and leg in history
  * RemoveFromNetworkCommand: Remove user from network
    - Validates no children exist (must move/remove first)
    - Soft delete (sets NetworkParentId to null)
    - Idempotent design

- Implemented 3 Queries with handlers, validators, and DTOs:
  * GetNetworkTreeQuery: Binary tree visualization
    - Recursive tree building with MaxDepth limit (1-10)
    - Returns nested structure with Left/Right children
  * GetUserNetworkPositionQuery: User position details
    - Parent info, leg position, children counts
    - Left/Right child counts for balance view
  * GetNetworkMembershipHistoryQuery: Complete audit trail
    - Filter by UserId, pagination support
    - Shows Join/Move/Remove actions with full details

- All operations include complete history tracking
- Binary tree validation (parent-child relationships)
- Circular dependency prevention in MoveInNetwork
- 21 new files, ~850 lines of code
- Build successful with 0 errors
This commit is contained in:
masoodafar-web
2025-11-29 04:19:40 +03:30
parent b21dda515e
commit db96a02f89
21 changed files with 813 additions and 0 deletions
@@ -0,0 +1,12 @@
namespace CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetUserNetworkPosition;
/// <summary>
/// Query برای دریافت موقعیت کاربر در شبکه
/// </summary>
public record GetUserNetworkPositionQuery : IRequest<UserNetworkPositionDto?>
{
/// <summary>
/// شناسه کاربر
/// </summary>
public long UserId { get; init; }
}
@@ -0,0 +1,70 @@
namespace CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetUserNetworkPosition;
public class GetUserNetworkPositionQueryHandler : IRequestHandler<GetUserNetworkPositionQuery, UserNetworkPositionDto?>
{
private readonly IApplicationDbContext _context;
public GetUserNetworkPositionQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<UserNetworkPositionDto?> Handle(GetUserNetworkPositionQuery request, CancellationToken cancellationToken)
{
var user = await _context.Users
.AsNoTracking()
.Where(x => x.Id == request.UserId)
.Select(x => new
{
x.Id,
x.Mobile,
x.FirstName,
x.LastName,
x.NetworkParentId,
x.LegPosition
})
.FirstOrDefaultAsync(cancellationToken);
if (user == null)
{
return null;
}
// شمارش فرزندان
var childrenCount = await _context.Users
.CountAsync(x => x.NetworkParentId == request.UserId, cancellationToken);
var leftChildCount = await _context.Users
.CountAsync(x => x.NetworkParentId == request.UserId && x.LegPosition == NetworkLeg.Left,
cancellationToken);
var rightChildCount = await _context.Users
.CountAsync(x => x.NetworkParentId == request.UserId && x.LegPosition == NetworkLeg.Right,
cancellationToken);
// اطلاعات والد
string? parentMobile = null;
if (user.NetworkParentId.HasValue)
{
parentMobile = await _context.Users
.Where(x => x.Id == user.NetworkParentId)
.Select(x => x.Mobile)
.FirstOrDefaultAsync(cancellationToken);
}
return new UserNetworkPositionDto
{
UserId = user.Id,
Mobile = user.Mobile,
FirstName = user.FirstName,
LastName = user.LastName,
NetworkParentId = user.NetworkParentId,
ParentMobile = parentMobile,
LegPosition = user.LegPosition,
TotalChildren = childrenCount,
LeftChildCount = leftChildCount,
RightChildCount = rightChildCount,
IsInNetwork = user.NetworkParentId.HasValue
};
}
}
@@ -0,0 +1,24 @@
namespace CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetUserNetworkPosition;
public class GetUserNetworkPositionQueryValidator : AbstractValidator<GetUserNetworkPositionQuery>
{
public GetUserNetworkPositionQueryValidator()
{
RuleFor(x => x.UserId)
.GreaterThan(0)
.WithMessage("شناسه کاربر معتبر نیست");
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(
ValidationContext<GetUserNetworkPositionQuery>.CreateWithOptions(
(GetUserNetworkPositionQuery)model,
x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,19 @@
namespace CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetUserNetworkPosition;
/// <summary>
/// DTO برای نمایش موقعیت کاربر در شبکه
/// </summary>
public class UserNetworkPositionDto
{
public long UserId { get; set; }
public string? Mobile { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public long? NetworkParentId { get; set; }
public string? ParentMobile { get; set; }
public NetworkLeg? LegPosition { get; set; }
public int TotalChildren { get; set; }
public int LeftChildCount { get; set; }
public int RightChildCount { get; set; }
public bool IsInNetwork { get; set; }
}