feat: Enhance network membership and withdrawal processing with user tracking and logging

This commit is contained in:
masoodafar-web
2025-12-01 20:52:18 +03:30
parent 4aaf2247ff
commit 25fc73ae28
47 changed files with 9545 additions and 284 deletions
@@ -25,6 +25,10 @@ public class ApplicationDbContext : DbContext, IApplicationDbContext
{
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
builder.HasDefaultSchema("CMS");
// Ignore MediatR notification types
builder.Ignore<CMSMicroservice.Domain.Common.BaseEvent>();
base.OnModelCreating(builder);
}
@@ -81,4 +85,5 @@ public class ApplicationDbContext : DbContext, IApplicationDbContext
public DbSet<WeeklyCommissionPool> WeeklyCommissionPools => Set<WeeklyCommissionPool>();
public DbSet<UserCommissionPayout> UserCommissionPayouts => Set<UserCommissionPayout>();
public DbSet<CommissionPayoutHistory> CommissionPayoutHistories => Set<CommissionPayoutHistory>();
public DbSet<WorkerExecutionLog> WorkerExecutionLogs => Set<WorkerExecutionLog>();
}
@@ -47,104 +47,95 @@ public class ApplicationDbContextInitialiser
}
public async Task TrySeedAsync()
{
// Seed default System Configurations for Network-Club-Commission System
if (!_context.SystemConfigurations.Any())
// Seed / upsert default System Configurations for Network-Club-Commission System
var desiredConfigurations = new List<SystemConfiguration>
{
var defaultConfigurations = new List<SystemConfiguration>
// Network Configuration
new SystemConfiguration
{
// Network Configuration
new SystemConfiguration
{
Key = "Network.MaxDepth",
Value = "10",
Description = "حداکثر عمق شبکه باینری",
Scope = ConfigurationScope.Network,
IsActive = true
},
new SystemConfiguration
{
Key = "Network.AllowOrphanNodes",
Value = "false",
Description = "اجازه حذف والدین که فرزند دارند",
Scope = ConfigurationScope.Network,
IsActive = true
},
// Club Configuration
new SystemConfiguration
{
Key = "Club.DefaultMembershipDurationMonths",
Value = "12",
Description = "مدت زمان پیش‌فرض عضویت باشگاه (ماه)",
Scope = ConfigurationScope.Club,
IsActive = true
},
new SystemConfiguration
{
Key = "Club.MinimumActivationAmount",
Value = "1000000",
Description = "حداقل مبلغ برای فعال‌سازی عضویت (ریال)",
Scope = ConfigurationScope.Club,
IsActive = true
},
// Commission Configuration
new SystemConfiguration
{
Key = "Commission.WeeklyPoolContributionPercent",
Value = "10",
Description = "درصد مشارکت در استخر هفتگی از تعادل کل",
Scope = ConfigurationScope.Commission,
IsActive = true
},
new SystemConfiguration
{
Key = "Commission.MinimumPayoutAmount",
Value = "100000",
Description = "حداقل مبلغ برای پرداخت کمیسیون (ریال)",
Scope = ConfigurationScope.Commission,
IsActive = true
},
new SystemConfiguration
{
Key = "Commission.CashWithdrawalEnabled",
Value = "true",
Description = "امکان برداشت نقدی فعال باشد",
Scope = ConfigurationScope.Commission,
IsActive = true
},
new SystemConfiguration
{
Key = "Commission.DiamondWithdrawalEnabled",
Value = "true",
Description = "امکان تبدیل به الماس فعال باشد",
Scope = ConfigurationScope.Commission,
IsActive = true
},
// System Configuration
new SystemConfiguration
{
Key = "System.MaintenanceMode",
Value = "false",
Description = "حالت تعمیر و نگهداری سیستم",
Scope = ConfigurationScope.System,
IsActive = true
},
new SystemConfiguration
{
Key = "System.EnableAuditLog",
Value = "true",
Description = "فعال‌سازی لاگ تغییرات",
Scope = ConfigurationScope.System,
IsActive = true
}
};
Key = "Network.MaxNetworkDepth",
Value = "15",
Description = "حداکثر عمق شبکه باینری",
Scope = ConfigurationScope.Network,
IsActive = true
},
new SystemConfiguration
{
Key = "Network.MaxChildrenPerLeg",
Value = "1",
Description = "حداکثر تعداد فرزند مستقیم در هر پا",
Scope = ConfigurationScope.Network,
IsActive = true
},
await _context.SystemConfigurations.AddRangeAsync(defaultConfigurations);
// Commission Configuration
new SystemConfiguration
{
Key = "Commission.MaxWeeklyBalancesPerUser",
Value = "300",
Description = "سقف امتیاز/تعادل هفتگی برای هر کاربر",
Scope = ConfigurationScope.Commission,
IsActive = true
},
new SystemConfiguration
{
Key = "Commission.MinWithdrawalAmount",
Value = "1000000",
Description = "حداقل مبلغ برداشت (ریال)",
Scope = ConfigurationScope.Commission,
IsActive = true
},
new SystemConfiguration
{
Key = "Commission.DefaultInitialContribution",
Value = "25000000",
Description = "مبلغ پیش‌فرض مشارکت/هزینه فعال‌سازی",
Scope = ConfigurationScope.Commission,
IsActive = true
},
new SystemConfiguration
{
Key = "Commission.WeeklyPoolContributionPercent",
Value = "20",
Description = "درصد مشارکت در استخر هفتگی از کل فعال‌سازی‌های جدید شبکه (20%)",
Scope = ConfigurationScope.Commission,
IsActive = true
},
// Club Configuration
new SystemConfiguration
{
Key = "Club.ActivationFee",
Value = "25000000",
Description = "هزینه فعال‌سازی عضویت باشگاه (ریال)",
Scope = ConfigurationScope.Club,
IsActive = true
},
// System Configuration
new SystemConfiguration
{
Key = "System.EnableAuditLog",
Value = "true",
Description = "فعال‌سازی لاگ تغییرات",
Scope = ConfigurationScope.System,
IsActive = true
}
};
var existingKeys = _context.SystemConfigurations
.Select(c => c.Key)
.ToHashSet(StringComparer.OrdinalIgnoreCase);
var newConfigs = desiredConfigurations
.Where(c => !existingKeys.Contains(c.Key))
.ToList();
if (newConfigs.Any())
{
await _context.SystemConfigurations.AddRangeAsync(newConfigs);
await _context.SaveChangesAsync();
_logger.LogInformation("Seeded {Count} default system configurations", defaultConfigurations.Count);
_logger.LogInformation("Seeded {Count} default system configurations", newConfigs.Count);
}
}
}
@@ -27,6 +27,9 @@ public class UserCommissionPayoutConfiguration : IEntityTypeConfiguration<UserCo
builder.Property(entity => entity.WithdrawalMethod).IsRequired(false);
builder.Property(entity => entity.IbanNumber).IsRequired(false).HasMaxLength(26);
builder.Property(entity => entity.WithdrawnAt).IsRequired(false);
builder.Property(entity => entity.ProcessedBy).IsRequired(false).HasMaxLength(200);
builder.Property(entity => entity.ProcessedAt).IsRequired(false);
builder.Property(entity => entity.RejectionReason).IsRequired(false).HasMaxLength(500);
// رابطه با User
builder.HasOne(entity => entity.User)
@@ -0,0 +1,43 @@
using CMSMicroservice.Domain.Entities.Commission;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace CMSMicroservice.Infrastructure.Persistence.Configurations;
public class WorkerExecutionLogConfiguration : IEntityTypeConfiguration<WorkerExecutionLog>
{
public void Configure(EntityTypeBuilder<WorkerExecutionLog> builder)
{
builder.ToTable("WorkerExecutionLogs", "CMS");
builder.HasKey(x => x.Id);
builder.Property(x => x.ExecutionId)
.IsRequired();
builder.Property(x => x.WeekNumber)
.HasMaxLength(10)
.IsRequired();
builder.Property(x => x.StartedAt)
.IsRequired();
builder.Property(x => x.Status)
.IsRequired();
builder.Property(x => x.ErrorMessage)
.HasMaxLength(2000);
builder.Property(x => x.Details)
.HasColumnType("nvarchar(max)");
// Index for querying by week
builder.HasIndex(x => x.WeekNumber);
// Index for querying by execution time
builder.HasIndex(x => x.StartedAt);
// Index for querying by status
builder.HasIndex(x => x.Status);
}
}
@@ -0,0 +1,122 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace CMSMicroservice.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class UpdateNetworkWeeklyBalanceWithCarryover : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "LeftLegCarryover",
schema: "CMS",
table: "NetworkWeeklyBalances",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "LeftLegNewMembers",
schema: "CMS",
table: "NetworkWeeklyBalances",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "LeftLegRemainder",
schema: "CMS",
table: "NetworkWeeklyBalances",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "LeftLegTotal",
schema: "CMS",
table: "NetworkWeeklyBalances",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "RightLegCarryover",
schema: "CMS",
table: "NetworkWeeklyBalances",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "RightLegNewMembers",
schema: "CMS",
table: "NetworkWeeklyBalances",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "RightLegRemainder",
schema: "CMS",
table: "NetworkWeeklyBalances",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "RightLegTotal",
schema: "CMS",
table: "NetworkWeeklyBalances",
type: "int",
nullable: false,
defaultValue: 0);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "LeftLegCarryover",
schema: "CMS",
table: "NetworkWeeklyBalances");
migrationBuilder.DropColumn(
name: "LeftLegNewMembers",
schema: "CMS",
table: "NetworkWeeklyBalances");
migrationBuilder.DropColumn(
name: "LeftLegRemainder",
schema: "CMS",
table: "NetworkWeeklyBalances");
migrationBuilder.DropColumn(
name: "LeftLegTotal",
schema: "CMS",
table: "NetworkWeeklyBalances");
migrationBuilder.DropColumn(
name: "RightLegCarryover",
schema: "CMS",
table: "NetworkWeeklyBalances");
migrationBuilder.DropColumn(
name: "RightLegNewMembers",
schema: "CMS",
table: "NetworkWeeklyBalances");
migrationBuilder.DropColumn(
name: "RightLegRemainder",
schema: "CMS",
table: "NetworkWeeklyBalances");
migrationBuilder.DropColumn(
name: "RightLegTotal",
schema: "CMS",
table: "NetworkWeeklyBalances");
}
}
}
@@ -0,0 +1,70 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace CMSMicroservice.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class AddWorkerExecutionLog : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "WorkerExecutionLogs",
schema: "CMS",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ExecutionId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
WeekNumber = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: false),
StartedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
CompletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
DurationMs = table.Column<long>(type: "bigint", nullable: true),
Status = table.Column<int>(type: "int", nullable: false),
ProcessedCount = table.Column<int>(type: "int", nullable: false),
ErrorCount = table.Column<int>(type: "int", nullable: false),
ErrorMessage = table.Column<string>(type: "nvarchar(2000)", maxLength: 2000, nullable: true),
ErrorStackTrace = table.Column<string>(type: "nvarchar(max)", nullable: true),
Details = table.Column<string>(type: "nvarchar(max)", nullable: true),
Created = table.Column<DateTime>(type: "datetime2", nullable: false),
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
LastModified = table.Column<DateTime>(type: "datetime2", nullable: true),
LastModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_WorkerExecutionLogs", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_WorkerExecutionLogs_StartedAt",
schema: "CMS",
table: "WorkerExecutionLogs",
column: "StartedAt");
migrationBuilder.CreateIndex(
name: "IX_WorkerExecutionLogs_Status",
schema: "CMS",
table: "WorkerExecutionLogs",
column: "Status");
migrationBuilder.CreateIndex(
name: "IX_WorkerExecutionLogs_WeekNumber",
schema: "CMS",
table: "WorkerExecutionLogs",
column: "WeekNumber");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "WorkerExecutionLogs",
schema: "CMS");
}
}
}
@@ -0,0 +1,57 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace CMSMicroservice.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class AddProcessedByToWithdrawal : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "ProcessedAt",
schema: "CMS",
table: "UserCommissionPayouts",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ProcessedBy",
schema: "CMS",
table: "UserCommissionPayouts",
type: "nvarchar(200)",
maxLength: 200,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "RejectionReason",
schema: "CMS",
table: "UserCommissionPayouts",
type: "nvarchar(500)",
maxLength: 500,
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ProcessedAt",
schema: "CMS",
table: "UserCommissionPayouts");
migrationBuilder.DropColumn(
name: "ProcessedBy",
schema: "CMS",
table: "UserCommissionPayouts");
migrationBuilder.DropColumn(
name: "RejectionReason",
schema: "CMS",
table: "UserCommissionPayouts");
}
}
}
@@ -261,6 +261,17 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
b.Property<DateTime?>("PaidAt")
.HasColumnType("datetime2");
b.Property<DateTime?>("ProcessedAt")
.HasColumnType("datetime2");
b.Property<string>("ProcessedBy")
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("RejectionReason")
.HasMaxLength(500)
.HasColumnType("nvarchar(500)");
b.Property<int>("Status")
.HasColumnType("int");
@@ -360,6 +371,76 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
b.ToTable("WeeklyCommissionPools", "CMS");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Commission.WorkerExecutionLog", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"));
b.Property<DateTime?>("CompletedAt")
.HasColumnType("datetime2");
b.Property<DateTime>("Created")
.HasColumnType("datetime2");
b.Property<string>("CreatedBy")
.HasColumnType("nvarchar(max)");
b.Property<string>("Details")
.HasColumnType("nvarchar(max)");
b.Property<long?>("DurationMs")
.HasColumnType("bigint");
b.Property<int>("ErrorCount")
.HasColumnType("int");
b.Property<string>("ErrorMessage")
.HasMaxLength(2000)
.HasColumnType("nvarchar(2000)");
b.Property<string>("ErrorStackTrace")
.HasColumnType("nvarchar(max)");
b.Property<Guid>("ExecutionId")
.HasColumnType("uniqueidentifier");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<DateTime?>("LastModified")
.HasColumnType("datetime2");
b.Property<string>("LastModifiedBy")
.HasColumnType("nvarchar(max)");
b.Property<int>("ProcessedCount")
.HasColumnType("int");
b.Property<DateTime>("StartedAt")
.HasColumnType("datetime2");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<string>("WeekNumber")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.HasKey("Id");
b.HasIndex("StartedAt");
b.HasIndex("Status");
b.HasIndex("WeekNumber");
b.ToTable("WorkerExecutionLogs", "CMS");
});
modelBuilder.Entity("CMSMicroservice.Domain.Entities.Configuration.SystemConfiguration", b =>
{
b.Property<long>("Id")
@@ -810,9 +891,33 @@ namespace CMSMicroservice.Infrastructure.Persistence.Migrations
b.Property<int>("LeftLegBalances")
.HasColumnType("int");
b.Property<int>("LeftLegCarryover")
.HasColumnType("int");
b.Property<int>("LeftLegNewMembers")
.HasColumnType("int");
b.Property<int>("LeftLegRemainder")
.HasColumnType("int");
b.Property<int>("LeftLegTotal")
.HasColumnType("int");
b.Property<int>("RightLegBalances")
.HasColumnType("int");
b.Property<int>("RightLegCarryover")
.HasColumnType("int");
b.Property<int>("RightLegNewMembers")
.HasColumnType("int");
b.Property<int>("RightLegRemainder")
.HasColumnType("int");
b.Property<int>("RightLegTotal")
.HasColumnType("int");
b.Property<int>("TotalBalances")
.HasColumnType("int");