feat: add IsActive field to UserClubFeatures for admin management
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using CMSMicroservice.Application.CommissionCQ.Commands.CalculateWeeklyBalances;
|
||||
using CMSMicroservice.Application.CommissionCQ.Commands.CalculateWeeklyCommissionPool;
|
||||
using CMSMicroservice.Application.CommissionCQ.Commands.ProcessUserPayouts;
|
||||
using CMSMicroservice.Application.CommissionCQ.Commands.TriggerWeeklyCalculation;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using CMSMicroservice.Domain.Entities;
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
@@ -54,16 +55,30 @@ public class WeeklyCommissionJob
|
||||
|
||||
/// <summary>
|
||||
/// Execute weekly commission calculation with retry logic
|
||||
/// Called by Hangfire scheduler
|
||||
/// Called by Hangfire scheduler or manually triggered
|
||||
/// </summary>
|
||||
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
|
||||
/// <param name="weekNumber">Week number in YYYY-Www format (e.g., 2025-W48). If null, uses previous week.</param>
|
||||
/// <param name="cancellationToken">Cancellation token</param>
|
||||
public async Task ExecuteAsync(string? weekNumber = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var executionId = Guid.NewGuid();
|
||||
var startTime = DateTime.UtcNow;
|
||||
var startTime = DateTime.Now;
|
||||
|
||||
// Calculate for PREVIOUS week (completed week)
|
||||
var previousWeek = DateTime.UtcNow.AddDays(-7);
|
||||
var previousWeekNumber = GetWeekNumber(previousWeek);
|
||||
// Use provided week number or calculate for PREVIOUS week (completed week)
|
||||
string targetWeekNumber;
|
||||
if (!string.IsNullOrWhiteSpace(weekNumber))
|
||||
{
|
||||
targetWeekNumber = weekNumber;
|
||||
_logger.LogInformation("📅 Using manually specified week: {WeekNumber}", targetWeekNumber);
|
||||
}
|
||||
else
|
||||
{
|
||||
var previousWeek = DateTime.Now.AddDays(-7);
|
||||
targetWeekNumber = GetWeekNumber(previousWeek);
|
||||
_logger.LogInformation("📅 Using previous week (auto-calculated): {WeekNumber}", targetWeekNumber);
|
||||
}
|
||||
|
||||
var previousWeekNumber = targetWeekNumber;
|
||||
|
||||
_logger.LogInformation(
|
||||
"🚀 [{ExecutionId}] Starting weekly commission calculation for {WeekNumber}",
|
||||
@@ -89,7 +104,7 @@ public class WeeklyCommissionJob
|
||||
}, cancellationToken);
|
||||
|
||||
// Update log on success
|
||||
var completedAt = DateTime.UtcNow;
|
||||
var completedAt = DateTime.Now;
|
||||
var duration = completedAt - startTime;
|
||||
|
||||
log.Status = WorkerExecutionStatus.Success;
|
||||
@@ -113,7 +128,7 @@ public class WeeklyCommissionJob
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Update log on failure
|
||||
var completedAt = DateTime.UtcNow;
|
||||
var completedAt = DateTime.Now;
|
||||
var duration = completedAt - startTime;
|
||||
|
||||
log.Status = WorkerExecutionStatus.Failed;
|
||||
@@ -165,38 +180,43 @@ public class WeeklyCommissionJob
|
||||
"📊 [{ExecutionId}] Step 1/3: Calculating weekly balances...",
|
||||
executionId);
|
||||
|
||||
await _mediator.Send(new CalculateWeeklyBalancesCommand
|
||||
await _mediator.Send(new TriggerWeeklyCalculationCommand
|
||||
{
|
||||
WeekNumber = weekNumber,
|
||||
ForceRecalculate = false
|
||||
}, cancellationToken);
|
||||
|
||||
// Step 2: Calculate global commission pool
|
||||
_logger.LogInformation(
|
||||
"💰 [{ExecutionId}] Step 2/3: Calculating commission pool...",
|
||||
executionId);
|
||||
|
||||
await _mediator.Send(new CalculateWeeklyCommissionPoolCommand
|
||||
{
|
||||
WeekNumber = weekNumber,
|
||||
ForceRecalculate = false
|
||||
}, cancellationToken);
|
||||
|
||||
// Step 3: Distribute commissions to users
|
||||
_logger.LogInformation(
|
||||
"💸 [{ExecutionId}] Step 3/3: Processing user payouts...",
|
||||
executionId);
|
||||
|
||||
await _mediator.Send(new ProcessUserPayoutsCommand
|
||||
{
|
||||
WeekNumber = weekNumber,
|
||||
ForceReprocess = false
|
||||
}, cancellationToken);
|
||||
// await _mediator.Send(new CalculateWeeklyBalancesCommand
|
||||
// {
|
||||
// WeekNumber = weekNumber,
|
||||
// ForceRecalculate = false
|
||||
// }, cancellationToken);
|
||||
//
|
||||
// // Step 2: Calculate global commission pool
|
||||
// _logger.LogInformation(
|
||||
// "💰 [{ExecutionId}] Step 2/3: Calculating commission pool...",
|
||||
// executionId);
|
||||
//
|
||||
// await _mediator.Send(new CalculateWeeklyCommissionPoolCommand
|
||||
// {
|
||||
// WeekNumber = weekNumber,
|
||||
// ForceRecalculate = false
|
||||
// }, cancellationToken);
|
||||
//
|
||||
// // Step 3: Distribute commissions to users
|
||||
// _logger.LogInformation(
|
||||
// "💸 [{ExecutionId}] Step 3/3: Processing user payouts...",
|
||||
// executionId);
|
||||
//
|
||||
// await _mediator.Send(new ProcessUserPayoutsCommand
|
||||
// {
|
||||
// WeekNumber = weekNumber,
|
||||
// ForceReprocess = false
|
||||
// }, cancellationToken);
|
||||
|
||||
transaction.Complete();
|
||||
|
||||
_logger.LogInformation(
|
||||
"✅ [{ExecutionId}] All 3 steps completed successfully",
|
||||
"✅ [{ExecutionId}] All 2 steps completed successfully",
|
||||
executionId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
+4
-4
@@ -109,7 +109,7 @@ public class WeeklyNetworkCommissionWorker : BackgroundService
|
||||
private async Task ExecuteWeeklyCalculationAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var executionId = Guid.NewGuid();
|
||||
var startTime = DateTime.UtcNow;
|
||||
var startTime = DateTime.Now;
|
||||
_logger.LogInformation("=== Starting Weekly Commission Calculation [{ExecutionId}] at {Time} (UTC) ===",
|
||||
executionId, startTime);
|
||||
|
||||
@@ -154,7 +154,7 @@ public class WeeklyNetworkCommissionWorker : BackgroundService
|
||||
|
||||
// Update log
|
||||
log.Status = WorkerExecutionStatus.SuccessWithWarnings;
|
||||
log.CompletedAt = DateTime.UtcNow;
|
||||
log.CompletedAt = DateTime.Now;
|
||||
log.DurationMs = (long)(log.CompletedAt.Value - log.StartedAt).TotalMilliseconds;
|
||||
log.Details = "Week already calculated - skipped";
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
@@ -222,7 +222,7 @@ public class WeeklyNetworkCommissionWorker : BackgroundService
|
||||
// Commit Transaction
|
||||
transaction.Complete();
|
||||
|
||||
var completedAt = DateTime.UtcNow;
|
||||
var completedAt = DateTime.Now;
|
||||
var duration = completedAt - startTime;
|
||||
|
||||
// Update log - Success
|
||||
@@ -297,7 +297,7 @@ public class WeeklyNetworkCommissionWorker : BackgroundService
|
||||
var context = errorScope.ServiceProvider.GetRequiredService<IApplicationDbContext>();
|
||||
|
||||
log.Status = WorkerExecutionStatus.Failed;
|
||||
log.CompletedAt = DateTime.UtcNow;
|
||||
log.CompletedAt = DateTime.Now;
|
||||
log.DurationMs = (long)(log.CompletedAt.Value - log.StartedAt).TotalMilliseconds;
|
||||
log.ErrorCount = 1;
|
||||
log.ErrorMessage = ex.Message;
|
||||
|
||||
Reference in New Issue
Block a user