This commit is contained in:
masoodafar-web
2025-12-02 03:32:26 +03:30
parent 6cd29e8b26
commit c9dab944fa
56 changed files with 1181 additions and 710 deletions
@@ -18,6 +18,10 @@
<ProjectReference Include="..\BackOffice.BFF.Domain\BackOffice.BFF.Domain.csproj" />
<ProjectReference Include="..\Protobufs\BackOffice.BFF.UserOrder.Protobuf\BackOffice.BFF.UserOrder.Protobuf.csproj" />
<ProjectReference Include="..\Protobufs\BackOffice.BFF.Commission.Protobuf\BackOffice.BFF.Commission.Protobuf.csproj" />
<ProjectReference Include="..\Protobufs\BackOffice.BFF.NetworkMembership.Protobuf\BackOffice.BFF.NetworkMembership.Protobuf.csproj" />
<ProjectReference Include="..\Protobufs\BackOffice.BFF.ClubMembership.Protobuf\BackOffice.BFF.ClubMembership.Protobuf.csproj" />
<ProjectReference Include="..\Protobufs\BackOffice.BFF.Configuration.Protobuf\BackOffice.BFF.Configuration.Protobuf.csproj" />
<ProjectReference Include="..\Protobufs\BackOffice.BFF.Health.Protobuf\BackOffice.BFF.Health.Protobuf.csproj" />
</ItemGroup>
</Project>
@@ -1,4 +1,4 @@
using CMSMicroservice.Protobuf.Protos.ClubMembership;
using BackOffice.BFF.ClubMembership.Protobuf;
namespace BackOffice.BFF.Application.ClubMembershipCQ.Commands.ActivateClub;
@@ -1,4 +1,4 @@
using CMSMicroservice.Protobuf.Protos.ClubMembership;
using BackOffice.BFF.ClubMembership.Protobuf;
namespace BackOffice.BFF.Application.ClubMembershipCQ.Queries.GetAllClubMembers;
@@ -0,0 +1,6 @@
namespace BackOffice.BFF.Application.ClubMembershipCQ.Queries.GetClubStatistics;
public class GetClubStatisticsQuery : IRequest<GetClubStatisticsResponseDto>
{
// No parameters - returns overall statistics
}
@@ -0,0 +1,22 @@
using BackOffice.BFF.ClubMembership.Protobuf;
namespace BackOffice.BFF.Application.ClubMembershipCQ.Queries.GetClubStatistics;
public class GetClubStatisticsQueryHandler : IRequestHandler<GetClubStatisticsQuery, GetClubStatisticsResponseDto>
{
private readonly IApplicationContractContext _context;
public GetClubStatisticsQueryHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<GetClubStatisticsResponseDto> Handle(GetClubStatisticsQuery request, CancellationToken cancellationToken)
{
var grpcRequest = new GetClubStatisticsRequest();
var response = await _context.ClubMemberships.GetClubStatisticsAsync(grpcRequest, cancellationToken: cancellationToken);
return response.Adapt<GetClubStatisticsResponseDto>();
}
}
@@ -0,0 +1,20 @@
namespace BackOffice.BFF.Application.ClubMembershipCQ.Queries.GetClubStatistics;
public class GetClubStatisticsResponseDto
{
public int TotalMemberships { get; set; }
public int ActiveMemberships { get; set; }
public int InactiveMemberships { get; set; }
public double ActivePercentage { get; set; }
public decimal TotalRevenue { get; set; }
public decimal AverageContribution { get; set; }
public double AverageDurationDays { get; set; }
public List<MonthlyMembershipTrendModel> MonthlyTrends { get; set; } = new();
}
public class MonthlyMembershipTrendModel
{
public string Month { get; set; } = string.Empty;
public int NewMemberships { get; set; }
public int ExpiredMemberships { get; set; }
}
@@ -15,16 +15,16 @@ public class ApproveWithdrawalCommandHandler : IRequestHandler<ApproveWithdrawal
{
var grpcRequest = new ApproveWithdrawalRequest
{
WithdrawalId = request.WithdrawalId,
AdminNote = request.AdminNote ?? string.Empty
PayoutId = request.WithdrawalId,
Notes = request.AdminNote ?? string.Empty
};
var response = await _context.Commissions.ApproveWithdrawalAsync(grpcRequest, cancellationToken: cancellationToken);
await _context.Commissions.ApproveWithdrawalAsync(grpcRequest, cancellationToken: cancellationToken);
return new ApproveWithdrawalResponseDto
{
Success = response.Success,
Message = response.Message
Success = true,
Message = "درخواست برداشت با موفقیت تایید شد"
};
}
}
@@ -13,6 +13,16 @@ public class ProcessWithdrawalCommandHandler : IRequestHandler<ProcessWithdrawal
public async Task<ProcessWithdrawalResponseDto> Handle(ProcessWithdrawalCommand request, CancellationToken cancellationToken)
{
// TODO: Implement when CMS ProcessWithdrawal endpoint is ready
await Task.CompletedTask;
return new ProcessWithdrawalResponseDto
{
Success = true,
Message = "Withdrawal processing pending CMS implementation"
};
/* Uncomment when CMS endpoint is ready:
var grpcRequest = new ProcessWithdrawalRequest
{
WithdrawalId = request.WithdrawalId,
@@ -27,5 +37,6 @@ public class ProcessWithdrawalCommandHandler : IRequestHandler<ProcessWithdrawal
Success = response.Success,
Message = response.Message
};
*/
}
}
@@ -15,16 +15,16 @@ public class RejectWithdrawalCommandHandler : IRequestHandler<RejectWithdrawalCo
{
var grpcRequest = new RejectWithdrawalRequest
{
WithdrawalId = request.WithdrawalId,
RejectReason = request.RejectReason
PayoutId = request.WithdrawalId,
Reason = request.RejectReason
};
var response = await _context.Commissions.RejectWithdrawalAsync(grpcRequest, cancellationToken: cancellationToken);
await _context.Commissions.RejectWithdrawalAsync(grpcRequest, cancellationToken: cancellationToken);
return new RejectWithdrawalResponseDto
{
Success = response.Success,
Message = response.Message
Success = true,
Message = "درخواست برداشت رد شد"
};
}
}
@@ -0,0 +1,10 @@
namespace BackOffice.BFF.Application.CommissionCQ.Commands.TriggerWeeklyCalculation;
public class TriggerWeeklyCalculationCommand : IRequest<TriggerWeeklyCalculationResponseDto>
{
public string WeekNumber { get; set; } = string.Empty;
public bool ForceRecalculate { get; set; }
public bool SkipBalances { get; set; }
public bool SkipPool { get; set; }
public bool SkipPayouts { get; set; }
}
@@ -0,0 +1,29 @@
using BackOffice.BFF.Commission.Protobuf;
namespace BackOffice.BFF.Application.CommissionCQ.Commands.TriggerWeeklyCalculation;
public class TriggerWeeklyCalculationCommandHandler : IRequestHandler<TriggerWeeklyCalculationCommand, TriggerWeeklyCalculationResponseDto>
{
private readonly IApplicationContractContext _context;
public TriggerWeeklyCalculationCommandHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<TriggerWeeklyCalculationResponseDto> Handle(TriggerWeeklyCalculationCommand request, CancellationToken cancellationToken)
{
var grpcRequest = new TriggerWeeklyCalculationRequest
{
WeekNumber = request.WeekNumber,
ForceRecalculate = request.ForceRecalculate,
SkipBalances = request.SkipBalances,
SkipPool = request.SkipPool,
SkipPayouts = request.SkipPayouts
};
var response = await _context.Commissions.TriggerWeeklyCalculationAsync(grpcRequest, cancellationToken: cancellationToken);
return response.Adapt<TriggerWeeklyCalculationResponseDto>();
}
}
@@ -0,0 +1,9 @@
namespace BackOffice.BFF.Application.CommissionCQ.Commands.TriggerWeeklyCalculation;
public class TriggerWeeklyCalculationResponseDto
{
public bool Success { get; set; }
public string Message { get; set; } = string.Empty;
public string ExecutionId { get; set; } = string.Empty;
public DateTime StartedAt { get; set; }
}
@@ -1,4 +1,4 @@
using CMSMicroservice.Protobuf.Protos.Commission;
using BackOffice.BFF.Commission.Protobuf;
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetAllWeeklyPools;
@@ -1,4 +1,4 @@
using CMSMicroservice.Protobuf.Protos.Commission;
using BackOffice.BFF.Commission.Protobuf;
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetUserPayouts;
@@ -0,0 +1,29 @@
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetUserWeeklyBalances;
public record GetUserWeeklyBalancesQuery : IRequest<GetUserWeeklyBalancesResponseDto>
{
/// <summary>
/// شناسه کاربر (فیلتر اختیاری)
/// </summary>
public long? UserId { get; init; }
/// <summary>
/// شماره هفته (فیلتر اختیاری)
/// </summary>
public string? WeekNumber { get; init; }
/// <summary>
/// فقط تعادل‌های فعال (منقضی نشده)
/// </summary>
public bool OnlyActive { get; init; }
/// <summary>
/// شماره صفحه
/// </summary>
public int PageIndex { get; init; } = 1;
/// <summary>
/// تعداد در صفحه
/// </summary>
public int PageSize { get; init; } = 10;
}
@@ -0,0 +1,37 @@
using BackOffice.BFF.Commission.Protobuf;
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetUserWeeklyBalances;
public class GetUserWeeklyBalancesQueryHandler : IRequestHandler<GetUserWeeklyBalancesQuery, GetUserWeeklyBalancesResponseDto>
{
private readonly IApplicationContractContext _context;
public GetUserWeeklyBalancesQueryHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<GetUserWeeklyBalancesResponseDto> Handle(GetUserWeeklyBalancesQuery request, CancellationToken cancellationToken)
{
var grpcRequest = new GetUserWeeklyBalancesRequest
{
OnlyActive = request.OnlyActive,
PageIndex = request.PageIndex,
PageSize = request.PageSize
};
if (request.UserId.HasValue)
{
grpcRequest.UserId = request.UserId.Value;
}
if (!string.IsNullOrWhiteSpace(request.WeekNumber))
{
grpcRequest.WeekNumber = request.WeekNumber;
}
var response = await _context.Commissions.GetUserWeeklyBalancesAsync(grpcRequest, cancellationToken: cancellationToken);
return response.Adapt<GetUserWeeklyBalancesResponseDto>();
}
}
@@ -0,0 +1,29 @@
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetUserWeeklyBalances;
public record GetUserWeeklyBalancesResponseDto
{
public MetaDataDto MetaData { get; init; } = new();
public List<UserWeeklyBalanceDto> Models { get; init; } = new();
}
public record UserWeeklyBalanceDto
{
public long Id { get; init; }
public long UserId { get; init; }
public string WeekNumber { get; init; } = string.Empty;
public int LeftLegBalances { get; init; }
public int RightLegBalances { get; init; }
public int TotalBalances { get; init; }
public long WeeklyPoolContribution { get; init; }
public DateTime? CalculatedAt { get; init; }
public bool IsExpired { get; init; }
public DateTime Created { get; init; }
}
public record MetaDataDto
{
public int TotalCount { get; init; }
public int PageSize { get; init; }
public int CurrentPage { get; init; }
public int TotalPages { get; init; }
}
@@ -1,4 +1,4 @@
using CMSMicroservice.Protobuf.Protos.Commission;
using BackOffice.BFF.Commission.Protobuf;
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetWeeklyPool;
@@ -30,7 +30,6 @@ public class GetWithdrawalRequestsQueryHandler : IRequestHandler<GetWithdrawalRe
}
var response = await _context.Commissions.GetWithdrawalRequestsAsync(grpcRequest, cancellationToken: cancellationToken);
return response.Adapt<GetWithdrawalRequestsResponseDto>();
}
}
@@ -0,0 +1,11 @@
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetWorkerExecutionLogs;
public class GetWorkerExecutionLogsQuery : IRequest<GetWorkerExecutionLogsResponseDto>
{
public string? WeekNumber { get; set; }
public string? ExecutionId { get; set; }
public bool? SuccessOnly { get; set; }
public bool? FailedOnly { get; set; }
public int PageIndex { get; set; }
public int PageSize { get; set; }
}
@@ -0,0 +1,46 @@
using BackOffice.BFF.Commission.Protobuf;
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetWorkerExecutionLogs;
public class GetWorkerExecutionLogsQueryHandler : IRequestHandler<GetWorkerExecutionLogsQuery, GetWorkerExecutionLogsResponseDto>
{
private readonly IApplicationContractContext _context;
public GetWorkerExecutionLogsQueryHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<GetWorkerExecutionLogsResponseDto> Handle(GetWorkerExecutionLogsQuery request, CancellationToken cancellationToken)
{
var grpcRequest = new GetWorkerExecutionLogsRequest
{
PageIndex = request.PageIndex,
PageSize = request.PageSize
};
if (!string.IsNullOrWhiteSpace(request.WeekNumber))
{
grpcRequest.WeekNumber = request.WeekNumber;
}
if (!string.IsNullOrWhiteSpace(request.ExecutionId))
{
grpcRequest.ExecutionId = request.ExecutionId;
}
if (request.SuccessOnly.HasValue)
{
grpcRequest.SuccessOnly = request.SuccessOnly.Value;
}
if (request.FailedOnly.HasValue)
{
grpcRequest.FailedOnly = request.FailedOnly.Value;
}
var response = await _context.Commissions.GetWorkerExecutionLogsAsync(grpcRequest, cancellationToken: cancellationToken);
return response.Adapt<GetWorkerExecutionLogsResponseDto>();
}
}
@@ -0,0 +1,23 @@
using BackOffice.BFF.Application.Common.Models;
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetWorkerExecutionLogs;
public class GetWorkerExecutionLogsResponseDto
{
public MetaData MetaData { get; set; } = new();
public List<WorkerExecutionLogModel> Models { get; set; } = new();
}
public class WorkerExecutionLogModel
{
public string ExecutionId { get; set; } = string.Empty;
public string WeekNumber { get; set; } = string.Empty;
public string Step { get; set; } = string.Empty;
public bool Success { get; set; }
public string? ErrorMessage { get; set; }
public DateTime StartedAt { get; set; }
public DateTime CompletedAt { get; set; }
public long DurationMs { get; set; }
public int RecordsProcessed { get; set; }
public string? Details { get; set; }
}
@@ -0,0 +1,6 @@
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetWorkerStatus;
public class GetWorkerStatusQuery : IRequest<GetWorkerStatusResponseDto>
{
// No parameters needed - returns current worker status
}
@@ -0,0 +1,22 @@
using BackOffice.BFF.Commission.Protobuf;
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetWorkerStatus;
public class GetWorkerStatusQueryHandler : IRequestHandler<GetWorkerStatusQuery, GetWorkerStatusResponseDto>
{
private readonly IApplicationContractContext _context;
public GetWorkerStatusQueryHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<GetWorkerStatusResponseDto> Handle(GetWorkerStatusQuery request, CancellationToken cancellationToken)
{
var grpcRequest = new GetWorkerStatusRequest();
var response = await _context.Commissions.GetWorkerStatusAsync(grpcRequest, cancellationToken: cancellationToken);
return response.Adapt<GetWorkerStatusResponseDto>();
}
}
@@ -0,0 +1,15 @@
namespace BackOffice.BFF.Application.CommissionCQ.Queries.GetWorkerStatus;
public class GetWorkerStatusResponseDto
{
public bool IsRunning { get; set; }
public bool IsEnabled { get; set; }
public string? CurrentExecutionId { get; set; }
public string? CurrentWeekNumber { get; set; }
public string? CurrentStep { get; set; }
public DateTime? LastRunAt { get; set; }
public DateTime? NextScheduledRun { get; set; }
public int TotalExecutions { get; set; }
public int SuccessfulExecutions { get; set; }
public int FailedExecutions { get; set; }
}
@@ -9,9 +9,10 @@ using CMSMicroservice.Protobuf.Protos.ProductImages;
using CMSMicroservice.Protobuf.Protos.ProductGallerys;
using CMSMicroservice.Protobuf.Protos.Category;
using CMSMicroservice.Protobuf.Protos.PruductCategory;
using CMSMicroservice.Protobuf.Protos.Commission;
using CMSMicroservice.Protobuf.Protos.NetworkMembership;
using CMSMicroservice.Protobuf.Protos.ClubMembership;
using BackOffice.BFF.Commission.Protobuf;
using BackOffice.BFF.NetworkMembership.Protobuf;
using BackOffice.BFF.ClubMembership.Protobuf;
using BackOffice.BFF.Configuration.Protobuf;
using FMSMicroservice.Protobuf.Protos.FileInfo;
namespace BackOffice.BFF.Application.Common.Interfaces;
@@ -35,9 +36,10 @@ public interface IApplicationContractContext
UserRoleContract.UserRoleContractClient UserRoles { get; }
// Network & Commission System
CommissionContract.CommissionContractClient Commissions { get; }
BackOffice.BFF.Commission.Protobuf.CommissionContract.CommissionContractClient Commissions { get; }
NetworkMembershipContract.NetworkMembershipContractClient NetworkMemberships { get; }
ClubMembershipContract.ClubMembershipContractClient ClubMemberships { get; }
ConfigurationContract.ConfigurationContractClient Configurations { get; }
#endregion
}
@@ -0,0 +1,9 @@
namespace BackOffice.BFF.Application.ConfigurationCQ.Commands.CreateOrUpdateConfiguration;
public record CreateOrUpdateConfigurationCommand : IRequest<Unit>
{
public string Key { get; init; } = string.Empty;
public string Value { get; init; } = string.Empty;
public string? Description { get; init; }
public int Scope { get; init; }
}
@@ -0,0 +1,33 @@
using BackOffice.BFF.Configuration.Protobuf;
using Google.Protobuf.WellKnownTypes;
namespace BackOffice.BFF.Application.ConfigurationCQ.Commands.CreateOrUpdateConfiguration;
public class CreateOrUpdateConfigurationCommandHandler : IRequestHandler<CreateOrUpdateConfigurationCommand, Unit>
{
private readonly IApplicationContractContext _context;
public CreateOrUpdateConfigurationCommandHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<Unit> Handle(CreateOrUpdateConfigurationCommand request, CancellationToken cancellationToken)
{
var grpcRequest = new CreateOrUpdateConfigurationRequest
{
Key = request.Key,
Value = request.Value,
Scope = request.Scope
};
if (!string.IsNullOrWhiteSpace(request.Description))
{
grpcRequest.Description = request.Description;
}
await _context.Configurations.CreateOrUpdateConfigurationAsync(grpcRequest, cancellationToken: cancellationToken);
return Unit.Value;
}
}
@@ -0,0 +1,7 @@
namespace BackOffice.BFF.Application.ConfigurationCQ.Commands.DeactivateConfiguration;
public record DeactivateConfigurationCommand : IRequest<Unit>
{
public string Key { get; init; } = string.Empty;
public string? Reason { get; init; }
}
@@ -0,0 +1,30 @@
using BackOffice.BFF.Configuration.Protobuf;
namespace BackOffice.BFF.Application.ConfigurationCQ.Commands.DeactivateConfiguration;
public class DeactivateConfigurationCommandHandler : IRequestHandler<DeactivateConfigurationCommand, Unit>
{
private readonly IApplicationContractContext _context;
public DeactivateConfigurationCommandHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<Unit> Handle(DeactivateConfigurationCommand request, CancellationToken cancellationToken)
{
var grpcRequest = new DeactivateConfigurationRequest
{
Key = request.Key
};
if (!string.IsNullOrWhiteSpace(request.Reason))
{
grpcRequest.Reason = request.Reason;
}
await _context.Configurations.DeactivateConfigurationAsync(grpcRequest, cancellationToken: cancellationToken);
return Unit.Value;
}
}
@@ -0,0 +1,24 @@
namespace BackOffice.BFF.Application.ConfigurationCQ.Queries.GetAllConfigurations;
public record GetAllConfigurationsQuery : IRequest<GetAllConfigurationsResponseDto>
{
/// <summary>
/// فیلتر بر اساس محدوده (System, Network, Club, Commission)
/// </summary>
public int? Scope { get; init; }
/// <summary>
/// فقط تنظیمات فعال
/// </summary>
public bool? IsActive { get; init; }
/// <summary>
/// شماره صفحه
/// </summary>
public int PageIndex { get; init; } = 1;
/// <summary>
/// تعداد در صفحه
/// </summary>
public int PageSize { get; init; } = 20;
}
@@ -0,0 +1,70 @@
using BackOffice.BFF.Configuration.Protobuf;
namespace BackOffice.BFF.Application.ConfigurationCQ.Queries.GetAllConfigurations;
public class GetAllConfigurationsQueryHandler : IRequestHandler<GetAllConfigurationsQuery, GetAllConfigurationsResponseDto>
{
private readonly IApplicationContractContext _context;
public GetAllConfigurationsQueryHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<GetAllConfigurationsResponseDto> Handle(GetAllConfigurationsQuery request, CancellationToken cancellationToken)
{
var grpcRequest = new GetAllConfigurationsRequest
{
PageIndex = request.PageIndex,
PageSize = request.PageSize
};
if (request.Scope.HasValue)
{
grpcRequest.Scope = request.Scope.Value;
}
if (request.IsActive.HasValue)
{
grpcRequest.IsActive = request.IsActive.Value;
}
var response = await _context.Configurations.GetAllConfigurationsAsync(grpcRequest, cancellationToken: cancellationToken);
var result = new GetAllConfigurationsResponseDto
{
MetaData = new MetaDataDto
{
TotalCount = (int)(response.MetaData?.TotalCount ?? 0),
PageSize = (int)(response.MetaData?.PageSize ?? request.PageSize),
CurrentPage = (int)(response.MetaData?.CurrentPage ?? request.PageIndex),
TotalPages = (int)(response.MetaData?.TotalPage ?? 0)
},
Models = response.Models.Select(m => new ConfigurationDto
{
Id = m.Id,
Key = m.Key,
Value = m.Value,
Description = m.Description,
Scope = m.Scope,
ScopeDisplay = GetScopeDisplay(m.Scope),
IsActive = m.IsActive,
Created = m.Created?.ToDateTime() ?? DateTime.UtcNow
}).ToList()
};
return result;
}
private string GetScopeDisplay(int scope)
{
return scope switch
{
0 => "سیستم",
1 => "شبکه",
2 => "باشگاه",
3 => "کمیسیون",
_ => "نامشخص"
};
}
}
@@ -0,0 +1,27 @@
namespace BackOffice.BFF.Application.ConfigurationCQ.Queries.GetAllConfigurations;
public record GetAllConfigurationsResponseDto
{
public MetaDataDto MetaData { get; init; } = new();
public List<ConfigurationDto> Models { get; init; } = new();
}
public record ConfigurationDto
{
public long Id { get; init; }
public string Key { get; init; } = string.Empty;
public string Value { get; init; } = string.Empty;
public string Description { get; init; } = string.Empty;
public int Scope { get; init; }
public string ScopeDisplay { get; init; } = string.Empty;
public bool IsActive { get; init; }
public DateTime Created { get; init; }
}
public record MetaDataDto
{
public int TotalCount { get; init; }
public int PageSize { get; init; }
public int CurrentPage { get; init; }
public int TotalPages { get; init; }
}
@@ -0,0 +1,8 @@
using BackOffice.BFF.Health.Protobuf;
namespace BackOffice.BFF.Application.HealthCQ.Queries.GetSystemHealth;
public class GetSystemHealthQuery : IRequest<GetSystemHealthResponse>
{
// Empty query - returns all services health status
}
@@ -0,0 +1,147 @@
using BackOffice.BFF.Health.Protobuf;
using Google.Protobuf.WellKnownTypes;
namespace BackOffice.BFF.Application.HealthCQ.Queries.GetSystemHealth;
public class GetSystemHealthQueryHandler : IRequestHandler<GetSystemHealthQuery, GetSystemHealthResponse>
{
private readonly IApplicationContractContext _context;
public GetSystemHealthQueryHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<GetSystemHealthResponse> Handle(GetSystemHealthQuery request, CancellationToken cancellationToken)
{
var services = new List<ServiceHealthModel>();
var overallHealthy = true;
// Check CMS Commission Service
var commissionHealthy = false;
long commissionResponseTime = 0;
try
{
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
await _context.Commissions.GetAllWeeklyPoolsAsync(
new BackOffice.BFF.Commission.Protobuf.GetAllWeeklyPoolsRequest
{
PageIndex = 1,
PageSize = 1
},
cancellationToken: cancellationToken);
stopwatch.Stop();
commissionHealthy = true;
commissionResponseTime = stopwatch.ElapsedMilliseconds;
}
catch
{
// Service is down
}
services.Add(new ServiceHealthModel
{
ServiceName = "CMS Commission Service",
Status = commissionHealthy ? "Healthy" : "Unhealthy",
Description = commissionHealthy ? "Connected" : "Connection failed",
ResponseTimeMs = commissionResponseTime,
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
});
if (!commissionHealthy) overallHealthy = false;
// Check CMS Configuration Service
var configHealthy = false;
long configResponseTime = 0;
try
{
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
await _context.Configurations.GetAllConfigurationsAsync(
new BackOffice.BFF.Configuration.Protobuf.GetAllConfigurationsRequest
{
PageIndex = 1,
PageSize = 1
},
cancellationToken: cancellationToken);
stopwatch.Stop();
configHealthy = true;
configResponseTime = stopwatch.ElapsedMilliseconds;
}
catch
{
// Service is down
}
services.Add(new ServiceHealthModel
{
ServiceName = "CMS Configuration Service",
Status = configHealthy ? "Healthy" : "Unhealthy",
Description = configHealthy ? "Connected" : "Connection failed",
ResponseTimeMs = configResponseTime,
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
});
if (!configHealthy) overallHealthy = false;
// Check Network Membership Service
var networkHealthy = false;
long networkResponseTime = 0;
try
{
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
await _context.NetworkMemberships.GetNetworkStatisticsAsync(
new BackOffice.BFF.NetworkMembership.Protobuf.GetNetworkStatisticsRequest(),
cancellationToken: cancellationToken);
stopwatch.Stop();
networkHealthy = true;
networkResponseTime = stopwatch.ElapsedMilliseconds;
}
catch
{
// Service is down
}
services.Add(new ServiceHealthModel
{
ServiceName = "Network Membership Service",
Status = networkHealthy ? "Healthy" : "Unhealthy",
Description = networkHealthy ? "Connected" : "Connection failed",
ResponseTimeMs = networkResponseTime,
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
});
if (!networkHealthy) overallHealthy = false;
// Check Club Membership Service
var clubHealthy = false;
long clubResponseTime = 0;
try
{
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
await _context.ClubMemberships.GetClubStatisticsAsync(
new BackOffice.BFF.ClubMembership.Protobuf.GetClubStatisticsRequest(),
cancellationToken: cancellationToken);
stopwatch.Stop();
clubHealthy = true;
clubResponseTime = stopwatch.ElapsedMilliseconds;
}
catch
{
// Service is down
}
services.Add(new ServiceHealthModel
{
ServiceName = "Club Membership Service",
Status = clubHealthy ? "Healthy" : "Unhealthy",
Description = clubHealthy ? "Connected" : "Connection failed",
ResponseTimeMs = clubResponseTime,
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
});
if (!clubHealthy) overallHealthy = false;
return new GetSystemHealthResponse
{
OverallHealthy = overallHealthy,
Services = { services },
CheckedAt = Timestamp.FromDateTime(DateTime.UtcNow)
};
}
}
@@ -1,4 +1,4 @@
using CMSMicroservice.Protobuf.Protos.NetworkMembership;
using BackOffice.BFF.NetworkMembership.Protobuf;
namespace BackOffice.BFF.Application.NetworkMembershipCQ.Queries.GetNetworkHistory;
@@ -0,0 +1,6 @@
namespace BackOffice.BFF.Application.NetworkMembershipCQ.Queries.GetNetworkStatistics;
public class GetNetworkStatisticsQuery : IRequest<GetNetworkStatisticsResponseDto>
{
// No parameters - returns overall statistics
}
@@ -0,0 +1,22 @@
using BackOffice.BFF.NetworkMembership.Protobuf;
namespace BackOffice.BFF.Application.NetworkMembershipCQ.Queries.GetNetworkStatistics;
public class GetNetworkStatisticsQueryHandler : IRequestHandler<GetNetworkStatisticsQuery, GetNetworkStatisticsResponseDto>
{
private readonly IApplicationContractContext _context;
public GetNetworkStatisticsQueryHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<GetNetworkStatisticsResponseDto> Handle(GetNetworkStatisticsQuery request, CancellationToken cancellationToken)
{
var grpcRequest = new GetNetworkStatisticsRequest();
var response = await _context.NetworkMemberships.GetNetworkStatisticsAsync(grpcRequest, cancellationToken: cancellationToken);
return response.Adapt<GetNetworkStatisticsResponseDto>();
}
}
@@ -0,0 +1,38 @@
namespace BackOffice.BFF.Application.NetworkMembershipCQ.Queries.GetNetworkStatistics;
public class GetNetworkStatisticsResponseDto
{
public int TotalMembers { get; set; }
public int ActiveMembers { get; set; }
public int LeftLegCount { get; set; }
public int RightLegCount { get; set; }
public double LeftPercentage { get; set; }
public double RightPercentage { get; set; }
public double AverageDepth { get; set; }
public int MaxDepth { get; set; }
public List<LevelDistributionModel> LevelDistribution { get; set; } = new();
public List<MonthlyGrowthModel> MonthlyGrowth { get; set; } = new();
public List<TopNetworkUserModel> TopUsers { get; set; } = new();
}
public class LevelDistributionModel
{
public int Level { get; set; }
public int Count { get; set; }
}
public class MonthlyGrowthModel
{
public string Month { get; set; } = string.Empty;
public int NewMembers { get; set; }
}
public class TopNetworkUserModel
{
public int Rank { get; set; }
public long UserId { get; set; }
public string UserName { get; set; } = string.Empty;
public int TotalChildren { get; set; }
public int LeftCount { get; set; }
public int RightCount { get; set; }
}
@@ -1,4 +1,4 @@
using CMSMicroservice.Protobuf.Protos.NetworkMembership;
using BackOffice.BFF.NetworkMembership.Protobuf;
namespace BackOffice.BFF.Application.NetworkMembershipCQ.Queries.GetNetworkTree;
@@ -1,4 +1,4 @@
using CMSMicroservice.Protobuf.Protos.NetworkMembership;
using BackOffice.BFF.NetworkMembership.Protobuf;
namespace BackOffice.BFF.Application.NetworkMembershipCQ.Queries.GetUserNetworkInfo;