feat: add commerce refunds and reconciliation persistence

This commit is contained in:
xiong
2026-07-26 05:53:16 +08:00
parent 4b59b493e1
commit 3eb9f111ed
7 changed files with 15247 additions and 5 deletions

View File

@@ -77,7 +77,11 @@ internal sealed class OrderConfiguration : IEntityTypeConfiguration<Order>
builder.HasIndex(entity => new { entity.TenantId, entity.OrderNo }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.UserId, entity.CreatedAt });
builder.ToTable(table => table.HasCheckConstraint("ck_orders_amount", "amount_cents >= 0"));
builder.ToTable(table =>
{
table.HasCheckConstraint("ck_orders_amount", "amount_cents >= 0");
table.HasCheckConstraint("ck_orders_refunded_amount", "refunded_amount_cents >= 0");
});
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.UserId).OnDelete(DeleteBehavior.SetNull);
builder.HasOne<SvipPlan>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.PlanId })
@@ -132,7 +136,11 @@ internal sealed class PaymentConfiguration : IEntityTypeConfiguration<Payment>
builder.HasIndex(entity => new { entity.Provider, entity.ProviderTradeNo })
.IsUnique()
.HasFilter("provider_trade_no is not null");
builder.ToTable(table => table.HasCheckConstraint("ck_payments_amount", "amount_cents >= 0"));
builder.ToTable(table =>
{
table.HasCheckConstraint("ck_payments_amount", "amount_cents >= 0");
table.HasCheckConstraint("ck_payments_refunded_amount", "refunded_amount_cents >= 0");
});
builder.HasOne<Order>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.OrderId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
@@ -171,12 +179,14 @@ internal sealed class EntitlementConfiguration : IEntityTypeConfiguration<Entitl
builder.Property(entity => entity.SourceType).HasMaxLength(100);
builder.Property(entity => entity.LegacySourceId).HasMaxLength(64);
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.RevokedReason).HasMaxLength(1000);
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.Property(entity => entity.StartsAt).HasDefaultValueSql("now()");
builder.Property(entity => entity.CreatedAt).HasDefaultValueSql("now()");
builder.HasIndex(entity => new { entity.TenantId, entity.UserId, entity.Status, entity.ExpiresAt });
builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.UserId).OnDelete(DeleteBehavior.Cascade);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.RevokedBy).OnDelete(DeleteBehavior.SetNull);
}
}
@@ -331,3 +341,205 @@ internal sealed class TenantUsageRecordConfiguration : IEntityTypeConfiguration<
builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
}
}
internal sealed class CommerceRefundRequestConfiguration : IEntityTypeConfiguration<CommerceRefundRequest>
{
public void Configure(EntityTypeBuilder<CommerceRefundRequest> builder)
{
builder.ConfigureTenantEntity("commerce_refund_requests");
builder.ConfigureTimestamps();
builder.Property(entity => entity.RefundNo).HasMaxLength(100);
builder.Property(entity => entity.Provider).HasMaxLength(50);
builder.Property(entity => entity.ProviderRefundNo).HasMaxLength(200);
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.EntitlementAction).HasSnakeCaseEnum();
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.Property(entity => entity.RequestedAt).HasDefaultValueSql("now()");
builder.HasIndex(entity => new { entity.TenantId, entity.RefundNo }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.OrderId, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.CreatedAt });
builder.ToTable(table => table.HasCheckConstraint("ck_commerce_refund_requests_amount", "amount_cents > 0"));
builder.HasOne<Order>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.OrderId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<Payment>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.PaymentId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.RequestedBy).OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.ReviewedBy).OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.ProcessedBy).OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class CommerceRefundEventConfiguration : IEntityTypeConfiguration<CommerceRefundEvent>
{
public void Configure(EntityTypeBuilder<CommerceRefundEvent> builder)
{
builder.ConfigureEntity("commerce_refund_events");
builder.HasAlternateKey(entity => new { entity.TenantId, entity.Id });
builder.Property(entity => entity.FromStatus).HasNullableSnakeCaseEnum();
builder.Property(entity => entity.ToStatus).HasSnakeCaseEnum();
builder.Property(entity => entity.EventType).HasMaxLength(100);
builder.Property(entity => entity.Details).IsJson("{}");
builder.Property(entity => entity.CreatedAt).HasDefaultValueSql("now()");
builder.HasIndex(entity => new { entity.TenantId, entity.RefundRequestId, entity.CreatedAt });
builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
builder.HasOne<CommerceRefundRequest>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RefundRequestId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.ActorUserId).OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class CommerceReconciliationBatchConfiguration : IEntityTypeConfiguration<CommerceReconciliationBatch>
{
public void Configure(EntityTypeBuilder<CommerceReconciliationBatch> builder)
{
builder.ConfigureTenantEntity("commerce_reconciliation_batches");
builder.ConfigureTimestamps();
builder.Property(entity => entity.Provider).HasMaxLength(50);
builder.Property(entity => entity.BillType).HasSnakeCaseEnum();
builder.Property(entity => entity.Source).HasSnakeCaseEnum();
builder.Property(entity => entity.SourceName).HasMaxLength(300);
builder.Property(entity => entity.SourceHash).HasMaxLength(200);
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.Provider, entity.BillDate, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.CreatedAt });
builder.ToTable(table =>
{
table.HasCheckConstraint("ck_commerce_reconciliation_batches_counts", "total_count >= 0 and matched_count >= 0 and mismatch_count >= 0 and missing_local_count >= 0 and missing_provider_count >= 0 and duplicate_count >= 0 and ignored_count >= 0");
table.HasCheckConstraint("ck_commerce_reconciliation_batches_amounts", "amount_cents >= 0 and refund_amount_cents >= 0");
});
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.CreatedBy).OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class CommerceReconciliationItemConfiguration : IEntityTypeConfiguration<CommerceReconciliationItem>
{
public void Configure(EntityTypeBuilder<CommerceReconciliationItem> builder)
{
builder.ConfigureEntity("commerce_reconciliation_items");
builder.HasAlternateKey(entity => new { entity.TenantId, entity.Id });
builder.Property(entity => entity.Provider).HasMaxLength(50);
builder.Property(entity => entity.TransactionType).HasSnakeCaseEnum();
builder.Property(entity => entity.ProviderTradeNo).HasMaxLength(200);
builder.Property(entity => entity.ProviderRefundNo).HasMaxLength(200);
builder.Property(entity => entity.OrderNo).HasMaxLength(100);
builder.Property(entity => entity.RefundNo).HasMaxLength(100);
builder.Property(entity => entity.ProviderStatus).HasMaxLength(100);
builder.Property(entity => entity.LocalStatus).HasMaxLength(100);
builder.Property(entity => entity.MatchStatus).HasSnakeCaseEnum();
builder.Property(entity => entity.Severity).HasSnakeCaseEnum();
builder.Property(entity => entity.IssueCode).HasMaxLength(100);
builder.Property(entity => entity.Details).IsJson("{}");
builder.Property(entity => entity.CreatedAt).HasDefaultValueSql("now()");
builder.HasIndex(entity => new { entity.BatchId, entity.RowNo }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.BatchId, entity.MatchStatus, entity.RowNo });
builder.HasIndex(entity => new { entity.TenantId, entity.OrderNo, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.RefundNo, entity.ProviderRefundNo, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.Provider, entity.ProviderTradeNo, entity.CreatedAt });
builder.ToTable(table =>
{
table.HasCheckConstraint("ck_commerce_reconciliation_items_row", "row_no > 0");
table.HasCheckConstraint("ck_commerce_reconciliation_items_amounts", "amount_cents >= 0 and refund_amount_cents >= 0");
});
builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
builder.HasOne<CommerceReconciliationBatch>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.BatchId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<Order>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.OrderId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<Payment>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.PaymentId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<CommerceRefundRequest>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RefundRequestId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
}
}
internal sealed class CommerceReconciliationIssueConfiguration : IEntityTypeConfiguration<CommerceReconciliationIssue>
{
public void Configure(EntityTypeBuilder<CommerceReconciliationIssue> builder)
{
builder.ConfigureTenantEntity("commerce_reconciliation_issues");
builder.ConfigureTimestamps();
builder.Property(entity => entity.IssueNo).HasMaxLength(100);
builder.Property(entity => entity.Provider).HasMaxLength(50);
builder.Property(entity => entity.TransactionType).HasSnakeCaseEnum();
builder.Property(entity => entity.IssueCode).HasMaxLength(100);
builder.Property(entity => entity.MatchStatus).HasSnakeCaseEnum();
builder.Property(entity => entity.Severity).HasSnakeCaseEnum();
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.ResolutionType).HasSnakeCaseEnum();
builder.Property(entity => entity.OrderNo).HasMaxLength(100);
builder.Property(entity => entity.RefundNo).HasMaxLength(100);
builder.Property(entity => entity.ProviderTradeNo).HasMaxLength(200);
builder.Property(entity => entity.ProviderRefundNo).HasMaxLength(200);
builder.Property(entity => entity.Summary).HasMaxLength(1000);
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.IssueNo }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.ItemId })
.IsUnique()
.HasFilter("item_id is not null and status in ('open', 'investigating', 'escalated')");
builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.Severity, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.AssignedTo, entity.Status, entity.DueAt });
builder.HasIndex(entity => new { entity.TenantId, entity.BatchId, entity.Status, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.OrderNo, entity.CreatedAt });
builder.HasIndex(entity => new { entity.TenantId, entity.PaymentId, entity.CreatedAt });
builder.ToTable(table => table.HasCheckConstraint("ck_commerce_reconciliation_issues_amounts", "amount_cents >= 0 and refund_amount_cents >= 0"));
builder.HasOne<CommerceReconciliationBatch>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.BatchId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<CommerceReconciliationItem>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ItemId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<Order>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.OrderId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<Payment>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.PaymentId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<CommerceRefundRequest>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RefundRequestId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.AssignedTo).OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.CreatedBy).OnDelete(DeleteBehavior.SetNull);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.ResolvedBy).OnDelete(DeleteBehavior.SetNull);
}
}
internal sealed class CommerceReconciliationIssueEventConfiguration : IEntityTypeConfiguration<CommerceReconciliationIssueEvent>
{
public void Configure(EntityTypeBuilder<CommerceReconciliationIssueEvent> builder)
{
builder.ConfigureEntity("commerce_reconciliation_issue_events");
builder.HasAlternateKey(entity => new { entity.TenantId, entity.Id });
builder.Property(entity => entity.FromStatus).HasNullableSnakeCaseEnum();
builder.Property(entity => entity.ToStatus).HasNullableSnakeCaseEnum();
builder.Property(entity => entity.EventType).HasMaxLength(100);
builder.Property(entity => entity.Details).IsJson("{}");
builder.Property(entity => entity.CreatedAt).HasDefaultValueSql("now()");
builder.HasIndex(entity => new { entity.TenantId, entity.IssueId, entity.CreatedAt });
builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
builder.HasOne<CommerceReconciliationIssue>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.IssueId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.ActorUserId).OnDelete(DeleteBehavior.SetNull);
}
}

View File

@@ -0,0 +1,669 @@
using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Tiku.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class AddCommerceRefundAndReconciliationPersistence : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "refunded_amount_cents",
table: "payments",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "refunded_amount_cents",
table: "orders",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<DateTimeOffset>(
name: "revoked_at",
table: "entitlements",
type: "timestamp with time zone",
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "revoked_by",
table: "entitlements",
type: "uuid",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "revoked_reason",
table: "entitlements",
type: "character varying(1000)",
maxLength: 1000,
nullable: true);
migrationBuilder.CreateTable(
name: "commerce_reconciliation_batches",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
created_by = table.Column<Guid>(type: "uuid", nullable: true),
provider = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
bill_date = table.Column<DateOnly>(type: "date", nullable: false),
bill_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
source = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
source_name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
source_hash = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
total_count = table.Column<int>(type: "integer", nullable: false),
matched_count = table.Column<int>(type: "integer", nullable: false),
mismatch_count = table.Column<int>(type: "integer", nullable: false),
missing_local_count = table.Column<int>(type: "integer", nullable: false),
missing_provider_count = table.Column<int>(type: "integer", nullable: false),
duplicate_count = table.Column<int>(type: "integer", nullable: false),
ignored_count = table.Column<int>(type: "integer", nullable: false),
amount_cents = table.Column<int>(type: "integer", nullable: false),
refund_amount_cents = table.Column<int>(type: "integer", nullable: false),
fee_cents = table.Column<int>(type: "integer", nullable: false),
completed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
error = table.Column<string>(type: "text", nullable: true),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_commerce_reconciliation_batches", x => x.id);
table.UniqueConstraint("ak_commerce_reconciliation_batches_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_commerce_reconciliation_batches_amounts", "amount_cents >= 0 and refund_amount_cents >= 0");
table.CheckConstraint("ck_commerce_reconciliation_batches_counts", "total_count >= 0 and matched_count >= 0 and mismatch_count >= 0 and missing_local_count >= 0 and missing_provider_count >= 0 and duplicate_count >= 0 and ignored_count >= 0");
table.ForeignKey(
name: "fk_commerce_reconciliation_batches_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_commerce_reconciliation_batches_users_created_by",
column: x => x.created_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "commerce_refund_requests",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
order_id = table.Column<Guid>(type: "uuid", nullable: false),
payment_id = table.Column<Guid>(type: "uuid", nullable: true),
requested_by = table.Column<Guid>(type: "uuid", nullable: true),
reviewed_by = table.Column<Guid>(type: "uuid", nullable: true),
processed_by = table.Column<Guid>(type: "uuid", nullable: true),
refund_no = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
provider = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
provider_refund_no = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
amount_cents = table.Column<int>(type: "integer", nullable: false),
reason = table.Column<string>(type: "text", nullable: true),
entitlement_action = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
requested_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
reviewed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
processed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
succeeded_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
failed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
cancelled_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
failure_reason = table.Column<string>(type: "text", nullable: true),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_commerce_refund_requests", x => x.id);
table.UniqueConstraint("ak_commerce_refund_requests_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_commerce_refund_requests_amount", "amount_cents > 0");
table.ForeignKey(
name: "fk_commerce_refund_requests_orders_tenant_id_order_id",
columns: x => new { x.tenant_id, x.order_id },
principalTable: "orders",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_commerce_refund_requests_payments_tenant_id_payment_id",
columns: x => new { x.tenant_id, x.payment_id },
principalTable: "payments",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_commerce_refund_requests_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_commerce_refund_requests_users_processed_by",
column: x => x.processed_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_commerce_refund_requests_users_requested_by",
column: x => x.requested_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_commerce_refund_requests_users_reviewed_by",
column: x => x.reviewed_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "commerce_reconciliation_items",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
batch_id = table.Column<Guid>(type: "uuid", nullable: false),
order_id = table.Column<Guid>(type: "uuid", nullable: true),
payment_id = table.Column<Guid>(type: "uuid", nullable: true),
refund_request_id = table.Column<Guid>(type: "uuid", nullable: true),
row_no = table.Column<int>(type: "integer", nullable: false),
provider = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
transaction_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
provider_trade_no = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
provider_refund_no = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
order_no = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
refund_no = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
amount_cents = table.Column<int>(type: "integer", nullable: false),
refund_amount_cents = table.Column<int>(type: "integer", nullable: false),
fee_cents = table.Column<int>(type: "integer", nullable: false),
paid_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
refunded_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
provider_status = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
local_status = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
match_status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
severity = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
issue_code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
details = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_commerce_reconciliation_items", x => x.id);
table.UniqueConstraint("ak_commerce_reconciliation_items_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_commerce_reconciliation_items_amounts", "amount_cents >= 0 and refund_amount_cents >= 0");
table.CheckConstraint("ck_commerce_reconciliation_items_row", "row_no > 0");
table.ForeignKey(
name: "fk_commerce_reconciliation_items_commerce_reconciliation_batch~",
columns: x => new { x.tenant_id, x.batch_id },
principalTable: "commerce_reconciliation_batches",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_commerce_reconciliation_items_commerce_refund_requests_tena~",
columns: x => new { x.tenant_id, x.refund_request_id },
principalTable: "commerce_refund_requests",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_commerce_reconciliation_items_orders_tenant_id_order_id",
columns: x => new { x.tenant_id, x.order_id },
principalTable: "orders",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_commerce_reconciliation_items_payments_tenant_id_payment_id",
columns: x => new { x.tenant_id, x.payment_id },
principalTable: "payments",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_commerce_reconciliation_items_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "commerce_refund_events",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
refund_request_id = table.Column<Guid>(type: "uuid", nullable: false),
from_status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
to_status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
event_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
actor_user_id = table.Column<Guid>(type: "uuid", nullable: true),
details = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_commerce_refund_events", x => x.id);
table.UniqueConstraint("ak_commerce_refund_events_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_commerce_refund_events_commerce_refund_requests_tenant_id_r~",
columns: x => new { x.tenant_id, x.refund_request_id },
principalTable: "commerce_refund_requests",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_commerce_refund_events_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_commerce_refund_events_users_actor_user_id",
column: x => x.actor_user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "commerce_reconciliation_issues",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
batch_id = table.Column<Guid>(type: "uuid", nullable: true),
item_id = table.Column<Guid>(type: "uuid", nullable: true),
order_id = table.Column<Guid>(type: "uuid", nullable: true),
payment_id = table.Column<Guid>(type: "uuid", nullable: true),
refund_request_id = table.Column<Guid>(type: "uuid", nullable: true),
assigned_to = table.Column<Guid>(type: "uuid", nullable: true),
created_by = table.Column<Guid>(type: "uuid", nullable: true),
resolved_by = table.Column<Guid>(type: "uuid", nullable: true),
issue_no = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
provider = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
transaction_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
issue_code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
match_status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
severity = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
resolution_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
order_no = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
refund_no = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
provider_trade_no = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
provider_refund_no = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
amount_cents = table.Column<int>(type: "integer", nullable: false),
refund_amount_cents = table.Column<int>(type: "integer", nullable: false),
resolved_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
due_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
summary = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
resolution_note = table.Column<string>(type: "text", nullable: true),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_commerce_reconciliation_issues", x => x.id);
table.UniqueConstraint("ak_commerce_reconciliation_issues_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_commerce_reconciliation_issues_amounts", "amount_cents >= 0 and refund_amount_cents >= 0");
table.ForeignKey(
name: "fk_commerce_reconciliation_issues_commerce_reconciliation_batc~",
columns: x => new { x.tenant_id, x.batch_id },
principalTable: "commerce_reconciliation_batches",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_commerce_reconciliation_issues_commerce_reconciliation_item~",
columns: x => new { x.tenant_id, x.item_id },
principalTable: "commerce_reconciliation_items",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_commerce_reconciliation_issues_commerce_refund_requests_ten~",
columns: x => new { x.tenant_id, x.refund_request_id },
principalTable: "commerce_refund_requests",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_commerce_reconciliation_issues_orders_tenant_id_order_id",
columns: x => new { x.tenant_id, x.order_id },
principalTable: "orders",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_commerce_reconciliation_issues_payments_tenant_id_payment_id",
columns: x => new { x.tenant_id, x.payment_id },
principalTable: "payments",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_commerce_reconciliation_issues_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_commerce_reconciliation_issues_users_assigned_to",
column: x => x.assigned_to,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_commerce_reconciliation_issues_users_created_by",
column: x => x.created_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "fk_commerce_reconciliation_issues_users_resolved_by",
column: x => x.resolved_by,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "commerce_reconciliation_issue_events",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
issue_id = table.Column<Guid>(type: "uuid", nullable: false),
from_status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
to_status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
event_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
actor_user_id = table.Column<Guid>(type: "uuid", nullable: true),
note = table.Column<string>(type: "text", nullable: true),
details = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_commerce_reconciliation_issue_events", x => x.id);
table.UniqueConstraint("ak_commerce_reconciliation_issue_events_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_commerce_reconciliation_issue_events_commerce_reconciliatio~",
columns: x => new { x.tenant_id, x.issue_id },
principalTable: "commerce_reconciliation_issues",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_commerce_reconciliation_issue_events_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_commerce_reconciliation_issue_events_users_actor_user_id",
column: x => x.actor_user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.AddCheckConstraint(
name: "ck_payments_refunded_amount",
table: "payments",
sql: "refunded_amount_cents >= 0");
migrationBuilder.AddCheckConstraint(
name: "ck_orders_refunded_amount",
table: "orders",
sql: "refunded_amount_cents >= 0");
migrationBuilder.CreateIndex(
name: "ix_entitlements_revoked_by",
table: "entitlements",
column: "revoked_by");
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_batches_created_by",
table: "commerce_reconciliation_batches",
column: "created_by");
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_batches_tenant_id_provider_bill_dat~",
table: "commerce_reconciliation_batches",
columns: new[] { "tenant_id", "provider", "bill_date", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_batches_tenant_id_status_created_at",
table: "commerce_reconciliation_batches",
columns: new[] { "tenant_id", "status", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issue_events_actor_user_id",
table: "commerce_reconciliation_issue_events",
column: "actor_user_id");
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issue_events_tenant_id_issue_id_cre~",
table: "commerce_reconciliation_issue_events",
columns: new[] { "tenant_id", "issue_id", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_assigned_to",
table: "commerce_reconciliation_issues",
column: "assigned_to");
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_created_by",
table: "commerce_reconciliation_issues",
column: "created_by");
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_resolved_by",
table: "commerce_reconciliation_issues",
column: "resolved_by");
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_tenant_id_assigned_to_status~",
table: "commerce_reconciliation_issues",
columns: new[] { "tenant_id", "assigned_to", "status", "due_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_tenant_id_batch_id_status_cr~",
table: "commerce_reconciliation_issues",
columns: new[] { "tenant_id", "batch_id", "status", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_tenant_id_issue_no",
table: "commerce_reconciliation_issues",
columns: new[] { "tenant_id", "issue_no" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_tenant_id_item_id",
table: "commerce_reconciliation_issues",
columns: new[] { "tenant_id", "item_id" },
unique: true,
filter: "item_id is not null and status in ('open', 'investigating', 'escalated')");
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_tenant_id_order_id",
table: "commerce_reconciliation_issues",
columns: new[] { "tenant_id", "order_id" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_tenant_id_order_no_created_at",
table: "commerce_reconciliation_issues",
columns: new[] { "tenant_id", "order_no", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_tenant_id_payment_id_created~",
table: "commerce_reconciliation_issues",
columns: new[] { "tenant_id", "payment_id", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_tenant_id_refund_request_id",
table: "commerce_reconciliation_issues",
columns: new[] { "tenant_id", "refund_request_id" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_issues_tenant_id_status_severity_cr~",
table: "commerce_reconciliation_issues",
columns: new[] { "tenant_id", "status", "severity", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_items_batch_id_row_no",
table: "commerce_reconciliation_items",
columns: new[] { "batch_id", "row_no" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_items_tenant_id_batch_id_match_stat~",
table: "commerce_reconciliation_items",
columns: new[] { "tenant_id", "batch_id", "match_status", "row_no" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_items_tenant_id_order_id",
table: "commerce_reconciliation_items",
columns: new[] { "tenant_id", "order_id" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_items_tenant_id_order_no_created_at",
table: "commerce_reconciliation_items",
columns: new[] { "tenant_id", "order_no", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_items_tenant_id_payment_id",
table: "commerce_reconciliation_items",
columns: new[] { "tenant_id", "payment_id" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_items_tenant_id_provider_provider_t~",
table: "commerce_reconciliation_items",
columns: new[] { "tenant_id", "provider", "provider_trade_no", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_items_tenant_id_refund_no_provider_~",
table: "commerce_reconciliation_items",
columns: new[] { "tenant_id", "refund_no", "provider_refund_no", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_reconciliation_items_tenant_id_refund_request_id",
table: "commerce_reconciliation_items",
columns: new[] { "tenant_id", "refund_request_id" });
migrationBuilder.CreateIndex(
name: "ix_commerce_refund_events_actor_user_id",
table: "commerce_refund_events",
column: "actor_user_id");
migrationBuilder.CreateIndex(
name: "ix_commerce_refund_events_tenant_id_refund_request_id_created_~",
table: "commerce_refund_events",
columns: new[] { "tenant_id", "refund_request_id", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_refund_requests_processed_by",
table: "commerce_refund_requests",
column: "processed_by");
migrationBuilder.CreateIndex(
name: "ix_commerce_refund_requests_requested_by",
table: "commerce_refund_requests",
column: "requested_by");
migrationBuilder.CreateIndex(
name: "ix_commerce_refund_requests_reviewed_by",
table: "commerce_refund_requests",
column: "reviewed_by");
migrationBuilder.CreateIndex(
name: "ix_commerce_refund_requests_tenant_id_order_id_created_at",
table: "commerce_refund_requests",
columns: new[] { "tenant_id", "order_id", "created_at" });
migrationBuilder.CreateIndex(
name: "ix_commerce_refund_requests_tenant_id_payment_id",
table: "commerce_refund_requests",
columns: new[] { "tenant_id", "payment_id" });
migrationBuilder.CreateIndex(
name: "ix_commerce_refund_requests_tenant_id_refund_no",
table: "commerce_refund_requests",
columns: new[] { "tenant_id", "refund_no" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_commerce_refund_requests_tenant_id_status_created_at",
table: "commerce_refund_requests",
columns: new[] { "tenant_id", "status", "created_at" });
migrationBuilder.AddForeignKey(
name: "fk_entitlements_users_revoked_by",
table: "entitlements",
column: "revoked_by",
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.SetNull);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "fk_entitlements_users_revoked_by",
table: "entitlements");
migrationBuilder.DropTable(
name: "commerce_reconciliation_issue_events");
migrationBuilder.DropTable(
name: "commerce_refund_events");
migrationBuilder.DropTable(
name: "commerce_reconciliation_issues");
migrationBuilder.DropTable(
name: "commerce_reconciliation_items");
migrationBuilder.DropTable(
name: "commerce_reconciliation_batches");
migrationBuilder.DropTable(
name: "commerce_refund_requests");
migrationBuilder.DropCheckConstraint(
name: "ck_payments_refunded_amount",
table: "payments");
migrationBuilder.DropCheckConstraint(
name: "ck_orders_refunded_amount",
table: "orders");
migrationBuilder.DropIndex(
name: "ix_entitlements_revoked_by",
table: "entitlements");
migrationBuilder.DropColumn(
name: "refunded_amount_cents",
table: "payments");
migrationBuilder.DropColumn(
name: "refunded_amount_cents",
table: "orders");
migrationBuilder.DropColumn(
name: "revoked_at",
table: "entitlements");
migrationBuilder.DropColumn(
name: "revoked_by",
table: "entitlements");
migrationBuilder.DropColumn(
name: "revoked_reason",
table: "entitlements");
}
}
}

View File

@@ -93,6 +93,12 @@ public sealed class TikuDbContext(DbContextOptions<TikuDbContext> options) : DbC
public DbSet<TenantPaymentAccount> TenantPaymentAccounts => Set<TenantPaymentAccount>();
public DbSet<TenantSubscription> TenantSubscriptions => Set<TenantSubscription>();
public DbSet<TenantUsageRecord> TenantUsageRecords => Set<TenantUsageRecord>();
public DbSet<CommerceRefundRequest> CommerceRefundRequests => Set<CommerceRefundRequest>();
public DbSet<CommerceRefundEvent> CommerceRefundEvents => Set<CommerceRefundEvent>();
public DbSet<CommerceReconciliationBatch> CommerceReconciliationBatches => Set<CommerceReconciliationBatch>();
public DbSet<CommerceReconciliationItem> CommerceReconciliationItems => Set<CommerceReconciliationItem>();
public DbSet<CommerceReconciliationIssue> CommerceReconciliationIssues => Set<CommerceReconciliationIssue>();
public DbSet<CommerceReconciliationIssueEvent> CommerceReconciliationIssueEvents => Set<CommerceReconciliationIssueEvent>();
public DbSet<ReferralTrack> ReferralTracks => Set<ReferralTrack>();
public DbSet<ReferralCode> ReferralCodes => Set<ReferralCode>();
public DbSet<ReferralLead> ReferralLeads => Set<ReferralLead>();