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);
}
}