forked from xiongyuxing/tiku-backend.net
feat: add platform billing dunning and audit persistence
This commit is contained in:
190
Tiku.Domain/Platform/PlatformOperationsEntities.cs
Normal file
190
Tiku.Domain/Platform/PlatformOperationsEntities.cs
Normal file
@@ -0,0 +1,190 @@
|
||||
using System.Text.Json;
|
||||
using Tiku.Domain.Common;
|
||||
|
||||
namespace Tiku.Domain.Platform;
|
||||
|
||||
public sealed class PlatformSaasPlan : AuditableEntity
|
||||
{
|
||||
public string Code { get; set; } = string.Empty;
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public PlatformBillingCycle BillingCycle { get; set; } = PlatformBillingCycle.Yearly;
|
||||
public int BaseAmountCents { get; set; }
|
||||
public string Currency { get; set; } = "CNY";
|
||||
public JsonElement IncludedQuotas { get; set; } = JsonDefaults.Object();
|
||||
public JsonElement OveragePrices { get; set; } = JsonDefaults.Object();
|
||||
public JsonElement FeatureFlags { get; set; } = JsonDefaults.Object();
|
||||
public PlatformSaasPlanStatus Status { get; set; } = PlatformSaasPlanStatus.Active;
|
||||
public int SortOrder { get; set; }
|
||||
}
|
||||
|
||||
public sealed class TenantBillingProfile : IHasTimestamps
|
||||
{
|
||||
public Guid TenantId { get; set; }
|
||||
public string? BillingName { get; set; }
|
||||
public string? TaxId { get; set; }
|
||||
public string? ContactName { get; set; }
|
||||
public string? ContactPhone { get; set; }
|
||||
public string? ContactEmail { get; set; }
|
||||
public string? BillingAddress { get; set; }
|
||||
public string? InvoiceTitle { get; set; }
|
||||
public TenantInvoiceTitleType? InvoiceType { get; set; }
|
||||
public string? BankName { get; set; }
|
||||
public string? BankAccountMasked { get; set; }
|
||||
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
|
||||
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
|
||||
public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public sealed class TenantInvoice : AuditableTenantEntity
|
||||
{
|
||||
public Guid? CreatedBy { get; set; }
|
||||
public string InvoiceNo { get; set; } = string.Empty;
|
||||
public TenantInvoiceType InvoiceType { get; set; } = TenantInvoiceType.Subscription;
|
||||
public TenantInvoiceStatus Status { get; set; } = TenantInvoiceStatus.Draft;
|
||||
public string Currency { get; set; } = "CNY";
|
||||
public int SubtotalCents { get; set; }
|
||||
public int DiscountCents { get; set; }
|
||||
public int TaxCents { get; set; }
|
||||
public int TotalCents { get; set; }
|
||||
public int PaidCents { get; set; }
|
||||
public int BalanceCents { get; set; }
|
||||
public DateOnly? BillingPeriodStart { get; set; }
|
||||
public DateOnly? BillingPeriodEnd { get; set; }
|
||||
public DateOnly? DueDate { get; set; }
|
||||
public DateTimeOffset? IssuedAt { get; set; }
|
||||
public DateTimeOffset? PaidAt { get; set; }
|
||||
public string? Note { get; set; }
|
||||
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
|
||||
}
|
||||
|
||||
public sealed class TenantInvoiceItem : Entity
|
||||
{
|
||||
public Guid TenantId { get; set; }
|
||||
public Guid InvoiceId { get; set; }
|
||||
public string ItemType { get; set; } = "subscription";
|
||||
public Guid? ItemRefId { get; set; }
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public decimal Quantity { get; set; } = 1;
|
||||
public int UnitAmountCents { get; set; }
|
||||
public int AmountCents { get; set; }
|
||||
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
|
||||
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public sealed class TenantInvoicePayment : AuditableTenantEntity
|
||||
{
|
||||
public Guid InvoiceId { get; set; }
|
||||
public Guid? ReceivedBy { get; set; }
|
||||
public string PaymentNo { get; set; } = string.Empty;
|
||||
public string Provider { get; set; } = "manual";
|
||||
public string? Method { get; set; }
|
||||
public TenantInvoicePaymentStatus Status { get; set; } = TenantInvoicePaymentStatus.Paid;
|
||||
public int AmountCents { get; set; }
|
||||
public DateTimeOffset? PaidAt { get; set; }
|
||||
public string? ProviderTradeNo { get; set; }
|
||||
public JsonElement RawPayload { get; set; } = JsonDefaults.Object();
|
||||
}
|
||||
|
||||
public sealed class TenantInvoiceReminder : AuditableTenantEntity
|
||||
{
|
||||
public Guid InvoiceId { get; set; }
|
||||
public Guid? CreatedBy { get; set; }
|
||||
public TenantInvoiceReminderType ReminderType { get; set; } = TenantInvoiceReminderType.Overdue;
|
||||
public TenantInvoiceReminderChannel Channel { get; set; } = TenantInvoiceReminderChannel.Manual;
|
||||
public TenantInvoiceReminderStatus Status { get; set; } = TenantInvoiceReminderStatus.Pending;
|
||||
public DateOnly ReminderDate { get; set; }
|
||||
public int ReminderLevel { get; set; } = 1;
|
||||
public DateOnly? DueDate { get; set; }
|
||||
public int BalanceCentsSnapshot { get; set; }
|
||||
public string? Message { get; set; }
|
||||
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
|
||||
public DateTimeOffset? SentAt { get; set; }
|
||||
public DateTimeOffset? AcknowledgedAt { get; set; }
|
||||
}
|
||||
|
||||
public sealed class PlatformAuditAlertRule : AuditableEntity
|
||||
{
|
||||
public Guid? TenantId { get; set; }
|
||||
public string Code { get; set; } = string.Empty;
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public bool Enabled { get; set; } = true;
|
||||
public PlatformAlertSeverity Severity { get; set; } = PlatformAlertSeverity.Medium;
|
||||
public string[] ActionPatterns { get; set; } = [];
|
||||
public string[] TargetTypes { get; set; } = [];
|
||||
public JsonElement Conditions { get; set; } = JsonDefaults.Object();
|
||||
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
|
||||
}
|
||||
|
||||
public sealed class PlatformAuditAlert : AuditableEntity
|
||||
{
|
||||
public Guid RuleId { get; set; }
|
||||
public Guid AuditLogId { get; set; }
|
||||
public Guid? TenantId { get; set; }
|
||||
public Guid? AcknowledgedBy { get; set; }
|
||||
public Guid? ResolvedBy { get; set; }
|
||||
public PlatformAlertSeverity Severity { get; set; } = PlatformAlertSeverity.Medium;
|
||||
public PlatformAuditAlertStatus Status { get; set; } = PlatformAuditAlertStatus.Open;
|
||||
public string Action { get; set; } = string.Empty;
|
||||
public string? TargetType { get; set; }
|
||||
public string? TargetId { get; set; }
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string? Summary { get; set; }
|
||||
public JsonElement Details { get; set; } = JsonDefaults.Object();
|
||||
public DateTimeOffset FirstSeenAt { get; set; } = DateTimeOffset.UtcNow;
|
||||
public DateTimeOffset LastSeenAt { get; set; } = DateTimeOffset.UtcNow;
|
||||
public DateTimeOffset? AcknowledgedAt { get; set; }
|
||||
public DateTimeOffset? ResolvedAt { get; set; }
|
||||
public string? ResolutionNote { get; set; }
|
||||
}
|
||||
|
||||
public sealed class PlatformDunningNotificationChannel : AuditableEntity
|
||||
{
|
||||
public string ChannelCode { get; set; } = string.Empty;
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public bool Enabled { get; set; } = true;
|
||||
public PlatformDunningProvider Provider { get; set; } = PlatformDunningProvider.Generic;
|
||||
public string WebhookUrl { get; set; } = string.Empty;
|
||||
public string? SecretRef { get; set; }
|
||||
public string[] ReminderTypes { get; set; } = ["overdue", "final_notice"];
|
||||
public string[] ReminderChannels { get; set; } = ["internal"];
|
||||
public int MinReminderLevel { get; set; } = 1;
|
||||
public Guid[] TenantIds { get; set; } = [];
|
||||
public int TimeoutSeconds { get; set; } = 10;
|
||||
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
|
||||
}
|
||||
|
||||
public sealed class PlatformDunningNotificationEvent : AuditableTenantEntity
|
||||
{
|
||||
public Guid ChannelId { get; set; }
|
||||
public Guid ReminderId { get; set; }
|
||||
public Guid InvoiceId { get; set; }
|
||||
public PlatformDunningProvider Provider { get; set; } = PlatformDunningProvider.Generic;
|
||||
public PlatformDunningNotificationStatus Status { get; set; } = PlatformDunningNotificationStatus.Pending;
|
||||
public int Attempts { get; set; }
|
||||
public DateTimeOffset ScheduledAt { get; set; } = DateTimeOffset.UtcNow;
|
||||
public DateTimeOffset? NextAttemptAt { get; set; }
|
||||
public DateTimeOffset? LastAttemptAt { get; set; }
|
||||
public DateTimeOffset? SentAt { get; set; }
|
||||
public string? LastError { get; set; }
|
||||
public int? LastHttpCode { get; set; }
|
||||
public string? LastResponseSummary { get; set; }
|
||||
public JsonElement RequestPayload { get; set; } = JsonDefaults.Object();
|
||||
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
|
||||
}
|
||||
|
||||
public enum PlatformBillingCycle { Monthly, Quarterly, Yearly, OneTime }
|
||||
public enum PlatformSaasPlanStatus { Active, Archived }
|
||||
public enum TenantInvoiceTitleType { None, NormalVat, SpecialVat }
|
||||
public enum TenantInvoiceType { Subscription, ServiceFee, UsageOverage, ManualAdjustment }
|
||||
public enum TenantInvoiceStatus { Draft, Issued, Paid, Void, Overdue }
|
||||
public enum TenantInvoicePaymentStatus { Pending, Paid, Failed, Refunded }
|
||||
public enum TenantInvoiceReminderType { DueSoon, Overdue, FinalNotice, Manual }
|
||||
public enum TenantInvoiceReminderChannel { Manual, Internal, Sms, Email, Wechat, Crm }
|
||||
public enum TenantInvoiceReminderStatus { Pending, Sent, Acknowledged, Dismissed, Failed }
|
||||
public enum PlatformAlertSeverity { Low, Medium, High, Critical }
|
||||
public enum PlatformAuditAlertStatus { Open, Acknowledged, Resolved, Ignored }
|
||||
public enum PlatformDunningProvider { Generic, Dingtalk, Feishu, Wecom }
|
||||
public enum PlatformDunningNotificationStatus { Pending, Processing, Sent, Retrying, Failed, Discarded }
|
||||
@@ -0,0 +1,248 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Tiku.Domain.Identity;
|
||||
using Tiku.Domain.Operations;
|
||||
using Tiku.Domain.Platform;
|
||||
using Tiku.Domain.Tenancy;
|
||||
|
||||
namespace Tiku.Infrastructure.Persistence.Configurations;
|
||||
|
||||
internal sealed class PlatformSaasPlanConfiguration : IEntityTypeConfiguration<PlatformSaasPlan>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PlatformSaasPlan> builder)
|
||||
{
|
||||
builder.ConfigureEntity("platform_saas_plans");
|
||||
builder.ConfigureTimestamps();
|
||||
builder.Property(entity => entity.Code).HasMaxLength(100);
|
||||
builder.Property(entity => entity.Name).HasMaxLength(200);
|
||||
builder.Property(entity => entity.BillingCycle).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.Currency).HasMaxLength(10);
|
||||
builder.Property(entity => entity.IncludedQuotas).IsJson("{}");
|
||||
builder.Property(entity => entity.OveragePrices).IsJson("{}");
|
||||
builder.Property(entity => entity.FeatureFlags).IsJson("{}");
|
||||
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
|
||||
builder.HasIndex(entity => entity.Code).IsUnique();
|
||||
builder.HasIndex(entity => new { entity.Status, entity.SortOrder });
|
||||
builder.ToTable(table => table.HasCheckConstraint("ck_platform_saas_plans_amount", "base_amount_cents >= 0"));
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class TenantBillingProfileConfiguration : IEntityTypeConfiguration<TenantBillingProfile>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<TenantBillingProfile> builder)
|
||||
{
|
||||
builder.ToTable("tenant_billing_profiles");
|
||||
builder.HasKey(entity => entity.TenantId);
|
||||
builder.ConfigureTimestamps();
|
||||
builder.Property(entity => entity.BillingName).HasMaxLength(300);
|
||||
builder.Property(entity => entity.TaxId).HasMaxLength(100);
|
||||
builder.Property(entity => entity.ContactName).HasMaxLength(100);
|
||||
builder.Property(entity => entity.ContactPhone).HasMaxLength(50);
|
||||
builder.Property(entity => entity.ContactEmail).HasColumnType("citext").HasMaxLength(300);
|
||||
builder.Property(entity => entity.InvoiceTitle).HasMaxLength(300);
|
||||
builder.Property(entity => entity.InvoiceType).HasNullableSnakeCaseEnum();
|
||||
builder.Property(entity => entity.BankName).HasMaxLength(200);
|
||||
builder.Property(entity => entity.BankAccountMasked).HasMaxLength(100);
|
||||
builder.Property(entity => entity.Metadata).IsJson("{}");
|
||||
builder.HasOne<Tenant>().WithOne().HasForeignKey<TenantBillingProfile>(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class TenantInvoiceConfiguration : IEntityTypeConfiguration<TenantInvoice>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<TenantInvoice> builder)
|
||||
{
|
||||
builder.ConfigureTenantEntity("tenant_invoices");
|
||||
builder.ConfigureTimestamps();
|
||||
builder.Property(entity => entity.InvoiceNo).HasMaxLength(100);
|
||||
builder.Property(entity => entity.InvoiceType).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.Currency).HasMaxLength(10);
|
||||
builder.Property(entity => entity.Metadata).IsJson("{}");
|
||||
builder.HasIndex(entity => entity.InvoiceNo).IsUnique();
|
||||
builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.DueDate });
|
||||
builder.HasIndex(entity => new { entity.TenantId, entity.BillingPeriodStart, entity.BillingPeriodEnd })
|
||||
.IsUnique()
|
||||
.HasFilter("invoice_type = 'usage_overage' and status <> 'void' and metadata->>'source' = 'usage_overage_auto'");
|
||||
builder.ToTable(table =>
|
||||
{
|
||||
table.HasCheckConstraint("ck_tenant_invoices_amounts", "subtotal_cents >= 0 and discount_cents >= 0 and tax_cents >= 0 and total_cents >= 0 and paid_cents >= 0 and balance_cents >= 0");
|
||||
table.HasCheckConstraint("ck_tenant_invoices_period", "billing_period_end is null or billing_period_start is null or billing_period_end >= billing_period_start");
|
||||
});
|
||||
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.CreatedBy).OnDelete(DeleteBehavior.SetNull);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class TenantInvoiceItemConfiguration : IEntityTypeConfiguration<TenantInvoiceItem>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<TenantInvoiceItem> builder)
|
||||
{
|
||||
builder.ConfigureEntity("tenant_invoice_items");
|
||||
builder.HasAlternateKey(entity => new { entity.TenantId, entity.Id });
|
||||
builder.Property(entity => entity.ItemType).HasMaxLength(100);
|
||||
builder.Property(entity => entity.Description).HasMaxLength(500);
|
||||
builder.Property(entity => entity.Quantity).HasPrecision(12, 2);
|
||||
builder.Property(entity => entity.Metadata).IsJson("{}");
|
||||
builder.Property(entity => entity.CreatedAt).HasDefaultValueSql("now()");
|
||||
builder.HasIndex(entity => entity.InvoiceId);
|
||||
builder.ToTable(table =>
|
||||
{
|
||||
table.HasCheckConstraint("ck_tenant_invoice_items_quantity", "quantity > 0");
|
||||
table.HasCheckConstraint("ck_tenant_invoice_items_amounts", "unit_amount_cents >= 0 and amount_cents >= 0");
|
||||
});
|
||||
builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
|
||||
builder.HasOne<TenantInvoice>().WithMany()
|
||||
.HasForeignKey(entity => new { entity.TenantId, entity.InvoiceId })
|
||||
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class TenantInvoicePaymentConfiguration : IEntityTypeConfiguration<TenantInvoicePayment>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<TenantInvoicePayment> builder)
|
||||
{
|
||||
builder.ConfigureTenantEntity("tenant_invoice_payments");
|
||||
builder.ConfigureTimestamps();
|
||||
builder.Property(entity => entity.PaymentNo).HasMaxLength(100);
|
||||
builder.Property(entity => entity.Provider).HasMaxLength(50);
|
||||
builder.Property(entity => entity.Method).HasMaxLength(50);
|
||||
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.ProviderTradeNo).HasMaxLength(200);
|
||||
builder.Property(entity => entity.RawPayload).IsJson("{}");
|
||||
builder.HasIndex(entity => entity.PaymentNo).IsUnique();
|
||||
builder.HasIndex(entity => new { entity.InvoiceId, entity.Status });
|
||||
builder.ToTable(table => table.HasCheckConstraint("ck_tenant_invoice_payments_amount", "amount_cents >= 0"));
|
||||
builder.HasOne<TenantInvoice>().WithMany()
|
||||
.HasForeignKey(entity => new { entity.TenantId, entity.InvoiceId })
|
||||
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.ReceivedBy).OnDelete(DeleteBehavior.SetNull);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class TenantInvoiceReminderConfiguration : IEntityTypeConfiguration<TenantInvoiceReminder>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<TenantInvoiceReminder> builder)
|
||||
{
|
||||
builder.ConfigureTenantEntity("tenant_invoice_reminders");
|
||||
builder.ConfigureTimestamps();
|
||||
builder.Property(entity => entity.ReminderType).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.Channel).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.Metadata).IsJson("{}");
|
||||
builder.HasIndex(entity => new { entity.TenantId, entity.InvoiceId, entity.ReminderType, entity.Channel, entity.ReminderDate }).IsUnique();
|
||||
builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.ReminderDate });
|
||||
builder.HasIndex(entity => new { entity.InvoiceId, entity.ReminderDate });
|
||||
builder.ToTable(table =>
|
||||
{
|
||||
table.HasCheckConstraint("ck_tenant_invoice_reminders_level", "reminder_level between 1 and 20");
|
||||
table.HasCheckConstraint("ck_tenant_invoice_reminders_balance", "balance_cents_snapshot >= 0");
|
||||
});
|
||||
builder.HasOne<TenantInvoice>().WithMany()
|
||||
.HasForeignKey(entity => new { entity.TenantId, entity.InvoiceId })
|
||||
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.CreatedBy).OnDelete(DeleteBehavior.SetNull);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class PlatformAuditAlertRuleConfiguration : IEntityTypeConfiguration<PlatformAuditAlertRule>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PlatformAuditAlertRule> builder)
|
||||
{
|
||||
builder.ConfigureEntity("platform_audit_alert_rules");
|
||||
builder.ConfigureTimestamps();
|
||||
builder.Property(entity => entity.Code).HasMaxLength(100);
|
||||
builder.Property(entity => entity.Name).HasMaxLength(200);
|
||||
builder.Property(entity => entity.Severity).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.ActionPatterns).HasColumnType("text[]").HasDefaultValueSql("'{}'::text[]");
|
||||
builder.Property(entity => entity.TargetTypes).HasColumnType("text[]").HasDefaultValueSql("'{}'::text[]");
|
||||
builder.Property(entity => entity.Conditions).IsJson("{}");
|
||||
builder.Property(entity => entity.Metadata).IsJson("{}");
|
||||
builder.HasIndex(entity => entity.Code).IsUnique();
|
||||
builder.HasIndex(entity => new { entity.Enabled, entity.Severity, entity.Code });
|
||||
builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class PlatformAuditAlertConfiguration : IEntityTypeConfiguration<PlatformAuditAlert>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PlatformAuditAlert> builder)
|
||||
{
|
||||
builder.ConfigureEntity("platform_audit_alerts");
|
||||
builder.ConfigureTimestamps();
|
||||
builder.Property(entity => entity.Severity).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.Action).HasMaxLength(200);
|
||||
builder.Property(entity => entity.TargetType).HasMaxLength(200);
|
||||
builder.Property(entity => entity.TargetId).HasMaxLength(200);
|
||||
builder.Property(entity => entity.Title).HasMaxLength(300);
|
||||
builder.Property(entity => entity.Details).IsJson("{}");
|
||||
builder.Property(entity => entity.FirstSeenAt).HasDefaultValueSql("now()");
|
||||
builder.Property(entity => entity.LastSeenAt).HasDefaultValueSql("now()");
|
||||
builder.HasIndex(entity => new { entity.RuleId, entity.AuditLogId }).IsUnique();
|
||||
builder.HasIndex(entity => new { entity.Status, entity.Severity, entity.CreatedAt });
|
||||
builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.CreatedAt });
|
||||
builder.HasIndex(entity => entity.AuditLogId);
|
||||
builder.HasOne<PlatformAuditAlertRule>().WithMany().HasForeignKey(entity => entity.RuleId).OnDelete(DeleteBehavior.Cascade);
|
||||
builder.HasOne<AuditLog>().WithMany().HasForeignKey(entity => entity.AuditLogId).OnDelete(DeleteBehavior.Cascade);
|
||||
builder.HasOne<Tenant>().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade);
|
||||
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.AcknowledgedBy).OnDelete(DeleteBehavior.SetNull);
|
||||
builder.HasOne<User>().WithMany().HasForeignKey(entity => entity.ResolvedBy).OnDelete(DeleteBehavior.SetNull);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class PlatformDunningNotificationChannelConfiguration : IEntityTypeConfiguration<PlatformDunningNotificationChannel>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PlatformDunningNotificationChannel> builder)
|
||||
{
|
||||
builder.ConfigureEntity("platform_dunning_notification_channels");
|
||||
builder.ConfigureTimestamps();
|
||||
builder.Property(entity => entity.ChannelCode).HasMaxLength(100);
|
||||
builder.Property(entity => entity.Name).HasMaxLength(200);
|
||||
builder.Property(entity => entity.Provider).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.WebhookUrl).HasMaxLength(2048);
|
||||
builder.Property(entity => entity.SecretRef).HasMaxLength(300);
|
||||
builder.Property(entity => entity.ReminderTypes).HasColumnType("text[]").HasDefaultValueSql("array['overdue', 'final_notice']::text[]");
|
||||
builder.Property(entity => entity.ReminderChannels).HasColumnType("text[]").HasDefaultValueSql("array['internal']::text[]");
|
||||
builder.Property(entity => entity.TenantIds).HasColumnType("uuid[]").HasDefaultValueSql("'{}'::uuid[]");
|
||||
builder.Property(entity => entity.Metadata).IsJson("{}");
|
||||
builder.HasIndex(entity => entity.ChannelCode).IsUnique();
|
||||
builder.HasIndex(entity => new { entity.Enabled, entity.MinReminderLevel, entity.ChannelCode });
|
||||
builder.ToTable(table =>
|
||||
{
|
||||
table.HasCheckConstraint("ck_platform_dunning_channels_level", "min_reminder_level between 1 and 20");
|
||||
table.HasCheckConstraint("ck_platform_dunning_channels_timeout", "timeout_seconds between 1 and 60");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class PlatformDunningNotificationEventConfiguration : IEntityTypeConfiguration<PlatformDunningNotificationEvent>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PlatformDunningNotificationEvent> builder)
|
||||
{
|
||||
builder.ConfigureTenantEntity("platform_dunning_notification_events");
|
||||
builder.ConfigureTimestamps();
|
||||
builder.Property(entity => entity.Provider).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
|
||||
builder.Property(entity => entity.RequestPayload).IsJson("{}");
|
||||
builder.Property(entity => entity.Metadata).IsJson("{}");
|
||||
builder.Property(entity => entity.ScheduledAt).HasDefaultValueSql("now()");
|
||||
builder.HasIndex(entity => new { entity.ChannelId, entity.ReminderId }).IsUnique();
|
||||
builder.HasIndex(entity => new { entity.Status, entity.NextAttemptAt, entity.ScheduledAt, entity.CreatedAt });
|
||||
builder.HasIndex(entity => new { entity.ReminderId, entity.Status, entity.CreatedAt });
|
||||
builder.HasIndex(entity => new { entity.InvoiceId, entity.Status, entity.CreatedAt });
|
||||
builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.CreatedAt });
|
||||
builder.ToTable(table => table.HasCheckConstraint("ck_platform_dunning_events_attempts", "attempts >= 0"));
|
||||
builder.HasOne<PlatformDunningNotificationChannel>().WithMany().HasForeignKey(entity => entity.ChannelId).OnDelete(DeleteBehavior.Cascade);
|
||||
builder.HasOne<TenantInvoiceReminder>().WithMany()
|
||||
.HasForeignKey(entity => new { entity.TenantId, entity.ReminderId })
|
||||
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
builder.HasOne<TenantInvoice>().WithMany()
|
||||
.HasForeignKey(entity => new { entity.TenantId, entity.InvoiceId })
|
||||
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,633 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Tiku.Infrastructure.Persistence.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddPlatformBillingDunningAndAuditPersistence : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "platform_audit_alert_rules",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
|
||||
tenant_id = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
description = table.Column<string>(type: "text", nullable: true),
|
||||
enabled = table.Column<bool>(type: "boolean", nullable: false),
|
||||
severity = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
action_patterns = table.Column<string[]>(type: "text[]", nullable: false, defaultValueSql: "'{}'::text[]"),
|
||||
target_types = table.Column<string[]>(type: "text[]", nullable: false, defaultValueSql: "'{}'::text[]"),
|
||||
conditions = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
|
||||
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
|
||||
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_platform_audit_alert_rules", x => x.id);
|
||||
table.ForeignKey(
|
||||
name: "fk_platform_audit_alert_rules_tenants_tenant_id",
|
||||
column: x => x.tenant_id,
|
||||
principalTable: "tenants",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "platform_dunning_notification_channels",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
|
||||
channel_code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
description = table.Column<string>(type: "text", nullable: true),
|
||||
enabled = table.Column<bool>(type: "boolean", nullable: false),
|
||||
provider = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
webhook_url = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: false),
|
||||
secret_ref = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
|
||||
reminder_types = table.Column<string[]>(type: "text[]", nullable: false, defaultValueSql: "array['overdue', 'final_notice']::text[]"),
|
||||
reminder_channels = table.Column<string[]>(type: "text[]", nullable: false, defaultValueSql: "array['internal']::text[]"),
|
||||
min_reminder_level = table.Column<int>(type: "integer", nullable: false),
|
||||
tenant_ids = table.Column<Guid[]>(type: "uuid[]", nullable: false, defaultValueSql: "'{}'::uuid[]"),
|
||||
timeout_seconds = table.Column<int>(type: "integer", nullable: false),
|
||||
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
|
||||
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_platform_dunning_notification_channels", x => x.id);
|
||||
table.CheckConstraint("ck_platform_dunning_channels_level", "min_reminder_level between 1 and 20");
|
||||
table.CheckConstraint("ck_platform_dunning_channels_timeout", "timeout_seconds between 1 and 60");
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "platform_saas_plans",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
|
||||
code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
description = table.Column<string>(type: "text", nullable: true),
|
||||
billing_cycle = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
base_amount_cents = table.Column<int>(type: "integer", nullable: false),
|
||||
currency = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
|
||||
included_quotas = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
|
||||
overage_prices = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
|
||||
feature_flags = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
|
||||
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
sort_order = table.Column<int>(type: "integer", 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_platform_saas_plans", x => x.id);
|
||||
table.CheckConstraint("ck_platform_saas_plans_amount", "base_amount_cents >= 0");
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "tenant_billing_profiles",
|
||||
columns: table => new
|
||||
{
|
||||
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
billing_name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
|
||||
tax_id = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
|
||||
contact_name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
|
||||
contact_phone = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
|
||||
contact_email = table.Column<string>(type: "citext", maxLength: 300, nullable: true),
|
||||
billing_address = table.Column<string>(type: "text", nullable: true),
|
||||
invoice_title = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
|
||||
invoice_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
|
||||
bank_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
|
||||
bank_account_masked = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
|
||||
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
|
||||
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_tenant_billing_profiles", x => x.tenant_id);
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_billing_profiles_tenants_tenant_id",
|
||||
column: x => x.tenant_id,
|
||||
principalTable: "tenants",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "tenant_invoices",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
|
||||
created_by = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
invoice_no = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
invoice_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
currency = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
|
||||
subtotal_cents = table.Column<int>(type: "integer", nullable: false),
|
||||
discount_cents = table.Column<int>(type: "integer", nullable: false),
|
||||
tax_cents = table.Column<int>(type: "integer", nullable: false),
|
||||
total_cents = table.Column<int>(type: "integer", nullable: false),
|
||||
paid_cents = table.Column<int>(type: "integer", nullable: false),
|
||||
balance_cents = table.Column<int>(type: "integer", nullable: false),
|
||||
billing_period_start = table.Column<DateOnly>(type: "date", nullable: true),
|
||||
billing_period_end = table.Column<DateOnly>(type: "date", nullable: true),
|
||||
due_date = table.Column<DateOnly>(type: "date", nullable: true),
|
||||
issued_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
paid_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
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_tenant_invoices", x => x.id);
|
||||
table.UniqueConstraint("ak_tenant_invoices_tenant_id_id", x => new { x.tenant_id, x.id });
|
||||
table.CheckConstraint("ck_tenant_invoices_amounts", "subtotal_cents >= 0 and discount_cents >= 0 and tax_cents >= 0 and total_cents >= 0 and paid_cents >= 0 and balance_cents >= 0");
|
||||
table.CheckConstraint("ck_tenant_invoices_period", "billing_period_end is null or billing_period_start is null or billing_period_end >= billing_period_start");
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_invoices_tenants_tenant_id",
|
||||
column: x => x.tenant_id,
|
||||
principalTable: "tenants",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_invoices_users_created_by",
|
||||
column: x => x.created_by,
|
||||
principalTable: "users",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.SetNull);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "platform_audit_alerts",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
|
||||
rule_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
audit_log_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
tenant_id = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
acknowledged_by = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
resolved_by = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
severity = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
action = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
||||
target_type = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
|
||||
target_id = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
|
||||
title = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
|
||||
summary = table.Column<string>(type: "text", nullable: true),
|
||||
details = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
|
||||
first_seen_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
|
||||
last_seen_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
|
||||
acknowledged_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
resolved_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
resolution_note = table.Column<string>(type: "text", nullable: true),
|
||||
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_platform_audit_alerts", x => x.id);
|
||||
table.ForeignKey(
|
||||
name: "fk_platform_audit_alerts_audit_logs_audit_log_id",
|
||||
column: x => x.audit_log_id,
|
||||
principalTable: "audit_logs",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_platform_audit_alerts_platform_audit_alert_rules_rule_id",
|
||||
column: x => x.rule_id,
|
||||
principalTable: "platform_audit_alert_rules",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_platform_audit_alerts_tenants_tenant_id",
|
||||
column: x => x.tenant_id,
|
||||
principalTable: "tenants",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_platform_audit_alerts_users_acknowledged_by",
|
||||
column: x => x.acknowledged_by,
|
||||
principalTable: "users",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.SetNull);
|
||||
table.ForeignKey(
|
||||
name: "fk_platform_audit_alerts_users_resolved_by",
|
||||
column: x => x.resolved_by,
|
||||
principalTable: "users",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.SetNull);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "tenant_invoice_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),
|
||||
invoice_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
item_type = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
item_ref_id = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
description = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
|
||||
quantity = table.Column<decimal>(type: "numeric(12,2)", precision: 12, scale: 2, nullable: false),
|
||||
unit_amount_cents = table.Column<int>(type: "integer", nullable: false),
|
||||
amount_cents = table.Column<int>(type: "integer", nullable: false),
|
||||
metadata = 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_tenant_invoice_items", x => x.id);
|
||||
table.UniqueConstraint("ak_tenant_invoice_items_tenant_id_id", x => new { x.tenant_id, x.id });
|
||||
table.CheckConstraint("ck_tenant_invoice_items_amounts", "unit_amount_cents >= 0 and amount_cents >= 0");
|
||||
table.CheckConstraint("ck_tenant_invoice_items_quantity", "quantity > 0");
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_invoice_items_tenant_invoices_tenant_id_invoice_id",
|
||||
columns: x => new { x.tenant_id, x.invoice_id },
|
||||
principalTable: "tenant_invoices",
|
||||
principalColumns: new[] { "tenant_id", "id" },
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_invoice_items_tenants_tenant_id",
|
||||
column: x => x.tenant_id,
|
||||
principalTable: "tenants",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "tenant_invoice_payments",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
|
||||
invoice_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
received_by = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
payment_no = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
|
||||
provider = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
|
||||
method = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
|
||||
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
amount_cents = table.Column<int>(type: "integer", nullable: false),
|
||||
paid_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
provider_trade_no = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
|
||||
raw_payload = 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_tenant_invoice_payments", x => x.id);
|
||||
table.UniqueConstraint("ak_tenant_invoice_payments_tenant_id_id", x => new { x.tenant_id, x.id });
|
||||
table.CheckConstraint("ck_tenant_invoice_payments_amount", "amount_cents >= 0");
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_invoice_payments_tenant_invoices_tenant_id_invoice_id",
|
||||
columns: x => new { x.tenant_id, x.invoice_id },
|
||||
principalTable: "tenant_invoices",
|
||||
principalColumns: new[] { "tenant_id", "id" },
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_invoice_payments_tenants_tenant_id",
|
||||
column: x => x.tenant_id,
|
||||
principalTable: "tenants",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_invoice_payments_users_received_by",
|
||||
column: x => x.received_by,
|
||||
principalTable: "users",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.SetNull);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "tenant_invoice_reminders",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
|
||||
invoice_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
created_by = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
reminder_type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
channel = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
reminder_date = table.Column<DateOnly>(type: "date", nullable: false),
|
||||
reminder_level = table.Column<int>(type: "integer", nullable: false),
|
||||
due_date = table.Column<DateOnly>(type: "date", nullable: true),
|
||||
balance_cents_snapshot = table.Column<int>(type: "integer", nullable: false),
|
||||
message = table.Column<string>(type: "text", nullable: true),
|
||||
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
|
||||
sent_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
acknowledged_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
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_tenant_invoice_reminders", x => x.id);
|
||||
table.UniqueConstraint("ak_tenant_invoice_reminders_tenant_id_id", x => new { x.tenant_id, x.id });
|
||||
table.CheckConstraint("ck_tenant_invoice_reminders_balance", "balance_cents_snapshot >= 0");
|
||||
table.CheckConstraint("ck_tenant_invoice_reminders_level", "reminder_level between 1 and 20");
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_invoice_reminders_tenant_invoices_tenant_id_invoice_~",
|
||||
columns: x => new { x.tenant_id, x.invoice_id },
|
||||
principalTable: "tenant_invoices",
|
||||
principalColumns: new[] { "tenant_id", "id" },
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_invoice_reminders_tenants_tenant_id",
|
||||
column: x => x.tenant_id,
|
||||
principalTable: "tenants",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_tenant_invoice_reminders_users_created_by",
|
||||
column: x => x.created_by,
|
||||
principalTable: "users",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.SetNull);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "platform_dunning_notification_events",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
|
||||
channel_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
reminder_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
invoice_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
provider = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
|
||||
attempts = table.Column<int>(type: "integer", nullable: false),
|
||||
scheduled_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
|
||||
next_attempt_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
last_attempt_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
sent_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||
last_error = table.Column<string>(type: "text", nullable: true),
|
||||
last_http_code = table.Column<int>(type: "integer", nullable: true),
|
||||
last_response_summary = table.Column<string>(type: "text", nullable: true),
|
||||
request_payload = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
|
||||
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_platform_dunning_notification_events", x => x.id);
|
||||
table.UniqueConstraint("ak_platform_dunning_notification_events_tenant_id_id", x => new { x.tenant_id, x.id });
|
||||
table.CheckConstraint("ck_platform_dunning_events_attempts", "attempts >= 0");
|
||||
table.ForeignKey(
|
||||
name: "fk_platform_dunning_notification_events_platform_dunning_notif~",
|
||||
column: x => x.channel_id,
|
||||
principalTable: "platform_dunning_notification_channels",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_platform_dunning_notification_events_tenant_invoice_reminde~",
|
||||
columns: x => new { x.tenant_id, x.reminder_id },
|
||||
principalTable: "tenant_invoice_reminders",
|
||||
principalColumns: new[] { "tenant_id", "id" },
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_platform_dunning_notification_events_tenant_invoices_tenant~",
|
||||
columns: x => new { x.tenant_id, x.invoice_id },
|
||||
principalTable: "tenant_invoices",
|
||||
principalColumns: new[] { "tenant_id", "id" },
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "fk_platform_dunning_notification_events_tenants_tenant_id",
|
||||
column: x => x.tenant_id,
|
||||
principalTable: "tenants",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_audit_alert_rules_code",
|
||||
table: "platform_audit_alert_rules",
|
||||
column: "code",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_audit_alert_rules_enabled_severity_code",
|
||||
table: "platform_audit_alert_rules",
|
||||
columns: new[] { "enabled", "severity", "code" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_audit_alert_rules_tenant_id",
|
||||
table: "platform_audit_alert_rules",
|
||||
column: "tenant_id");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_audit_alerts_acknowledged_by",
|
||||
table: "platform_audit_alerts",
|
||||
column: "acknowledged_by");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_audit_alerts_audit_log_id",
|
||||
table: "platform_audit_alerts",
|
||||
column: "audit_log_id");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_audit_alerts_resolved_by",
|
||||
table: "platform_audit_alerts",
|
||||
column: "resolved_by");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_audit_alerts_rule_id_audit_log_id",
|
||||
table: "platform_audit_alerts",
|
||||
columns: new[] { "rule_id", "audit_log_id" },
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_audit_alerts_status_severity_created_at",
|
||||
table: "platform_audit_alerts",
|
||||
columns: new[] { "status", "severity", "created_at" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_audit_alerts_tenant_id_status_created_at",
|
||||
table: "platform_audit_alerts",
|
||||
columns: new[] { "tenant_id", "status", "created_at" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_dunning_notification_channels_channel_code",
|
||||
table: "platform_dunning_notification_channels",
|
||||
column: "channel_code",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_dunning_notification_channels_enabled_min_reminder~",
|
||||
table: "platform_dunning_notification_channels",
|
||||
columns: new[] { "enabled", "min_reminder_level", "channel_code" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_dunning_notification_events_channel_id_reminder_id",
|
||||
table: "platform_dunning_notification_events",
|
||||
columns: new[] { "channel_id", "reminder_id" },
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_dunning_notification_events_invoice_id_status_crea~",
|
||||
table: "platform_dunning_notification_events",
|
||||
columns: new[] { "invoice_id", "status", "created_at" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_dunning_notification_events_reminder_id_status_cre~",
|
||||
table: "platform_dunning_notification_events",
|
||||
columns: new[] { "reminder_id", "status", "created_at" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_dunning_notification_events_status_next_attempt_at~",
|
||||
table: "platform_dunning_notification_events",
|
||||
columns: new[] { "status", "next_attempt_at", "scheduled_at", "created_at" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_dunning_notification_events_tenant_id_invoice_id",
|
||||
table: "platform_dunning_notification_events",
|
||||
columns: new[] { "tenant_id", "invoice_id" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_dunning_notification_events_tenant_id_reminder_id",
|
||||
table: "platform_dunning_notification_events",
|
||||
columns: new[] { "tenant_id", "reminder_id" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_dunning_notification_events_tenant_id_status_creat~",
|
||||
table: "platform_dunning_notification_events",
|
||||
columns: new[] { "tenant_id", "status", "created_at" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_saas_plans_code",
|
||||
table: "platform_saas_plans",
|
||||
column: "code",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_platform_saas_plans_status_sort_order",
|
||||
table: "platform_saas_plans",
|
||||
columns: new[] { "status", "sort_order" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoice_items_invoice_id",
|
||||
table: "tenant_invoice_items",
|
||||
column: "invoice_id");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoice_items_tenant_id_invoice_id",
|
||||
table: "tenant_invoice_items",
|
||||
columns: new[] { "tenant_id", "invoice_id" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoice_payments_invoice_id_status",
|
||||
table: "tenant_invoice_payments",
|
||||
columns: new[] { "invoice_id", "status" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoice_payments_payment_no",
|
||||
table: "tenant_invoice_payments",
|
||||
column: "payment_no",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoice_payments_received_by",
|
||||
table: "tenant_invoice_payments",
|
||||
column: "received_by");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoice_payments_tenant_id_invoice_id",
|
||||
table: "tenant_invoice_payments",
|
||||
columns: new[] { "tenant_id", "invoice_id" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoice_reminders_created_by",
|
||||
table: "tenant_invoice_reminders",
|
||||
column: "created_by");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoice_reminders_invoice_id_reminder_date",
|
||||
table: "tenant_invoice_reminders",
|
||||
columns: new[] { "invoice_id", "reminder_date" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoice_reminders_tenant_id_invoice_id_reminder_type~",
|
||||
table: "tenant_invoice_reminders",
|
||||
columns: new[] { "tenant_id", "invoice_id", "reminder_type", "channel", "reminder_date" },
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoice_reminders_tenant_id_status_reminder_date",
|
||||
table: "tenant_invoice_reminders",
|
||||
columns: new[] { "tenant_id", "status", "reminder_date" });
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoices_created_by",
|
||||
table: "tenant_invoices",
|
||||
column: "created_by");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoices_invoice_no",
|
||||
table: "tenant_invoices",
|
||||
column: "invoice_no",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoices_tenant_id_billing_period_start_billing_peri~",
|
||||
table: "tenant_invoices",
|
||||
columns: new[] { "tenant_id", "billing_period_start", "billing_period_end" },
|
||||
unique: true,
|
||||
filter: "invoice_type = 'usage_overage' and status <> 'void' and metadata->>'source' = 'usage_overage_auto'");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_tenant_invoices_tenant_id_status_due_date",
|
||||
table: "tenant_invoices",
|
||||
columns: new[] { "tenant_id", "status", "due_date" });
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "platform_audit_alerts");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "platform_dunning_notification_events");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "platform_saas_plans");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "tenant_billing_profiles");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "tenant_invoice_items");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "tenant_invoice_payments");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "platform_audit_alert_rules");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "platform_dunning_notification_channels");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "tenant_invoice_reminders");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "tenant_invoices");
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,7 @@ using Tiku.Domain.Growth;
|
||||
using Tiku.Domain.Identity;
|
||||
using Tiku.Domain.Learning;
|
||||
using Tiku.Domain.Operations;
|
||||
using Tiku.Domain.Platform;
|
||||
using Tiku.Domain.QuestionBanks;
|
||||
using Tiku.Domain.Tenancy;
|
||||
|
||||
@@ -125,6 +126,16 @@ public sealed class TikuDbContext(DbContextOptions<TikuDbContext> options) : DbC
|
||||
public DbSet<TenantContentNotification> TenantContentNotifications => Set<TenantContentNotification>();
|
||||
public DbSet<TenantThemeTemplate> TenantThemeTemplates => Set<TenantThemeTemplate>();
|
||||
public DbSet<TenantThemeConfig> TenantThemeConfigs => Set<TenantThemeConfig>();
|
||||
public DbSet<PlatformSaasPlan> PlatformSaasPlans => Set<PlatformSaasPlan>();
|
||||
public DbSet<TenantBillingProfile> TenantBillingProfiles => Set<TenantBillingProfile>();
|
||||
public DbSet<TenantInvoice> TenantInvoices => Set<TenantInvoice>();
|
||||
public DbSet<TenantInvoiceItem> TenantInvoiceItems => Set<TenantInvoiceItem>();
|
||||
public DbSet<TenantInvoicePayment> TenantInvoicePayments => Set<TenantInvoicePayment>();
|
||||
public DbSet<TenantInvoiceReminder> TenantInvoiceReminders => Set<TenantInvoiceReminder>();
|
||||
public DbSet<PlatformAuditAlertRule> PlatformAuditAlertRules => Set<PlatformAuditAlertRule>();
|
||||
public DbSet<PlatformAuditAlert> PlatformAuditAlerts => Set<PlatformAuditAlert>();
|
||||
public DbSet<PlatformDunningNotificationChannel> PlatformDunningNotificationChannels => Set<PlatformDunningNotificationChannel>();
|
||||
public DbSet<PlatformDunningNotificationEvent> PlatformDunningNotificationEvents => Set<PlatformDunningNotificationEvent>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
|
||||
@@ -6,6 +6,7 @@ using Tiku.Domain.Content;
|
||||
using Tiku.Domain.Growth;
|
||||
using Tiku.Domain.Learning;
|
||||
using Tiku.Domain.Operations;
|
||||
using Tiku.Domain.Platform;
|
||||
using Tiku.Domain.QuestionBanks;
|
||||
using Tiku.Domain.Tenancy;
|
||||
using Tiku.Infrastructure.Persistence;
|
||||
@@ -28,7 +29,7 @@ public sealed class PersistenceModelTests
|
||||
.Select(entity => entity.GetTableName())
|
||||
.ToHashSet(StringComparer.Ordinal);
|
||||
|
||||
Assert.Equal(111, tableNames.Count);
|
||||
Assert.Equal(121, tableNames.Count);
|
||||
Assert.Contains("tenants", tableNames);
|
||||
Assert.Contains("tenant_settings", tableNames);
|
||||
Assert.Contains("content_entries", tableNames);
|
||||
@@ -118,6 +119,16 @@ public sealed class PersistenceModelTests
|
||||
Assert.Contains("tenant_content_notifications", tableNames);
|
||||
Assert.Contains("tenant_theme_templates", tableNames);
|
||||
Assert.Contains("tenant_theme_configs", tableNames);
|
||||
Assert.Contains("platform_saas_plans", tableNames);
|
||||
Assert.Contains("tenant_billing_profiles", tableNames);
|
||||
Assert.Contains("tenant_invoices", tableNames);
|
||||
Assert.Contains("tenant_invoice_items", tableNames);
|
||||
Assert.Contains("tenant_invoice_payments", tableNames);
|
||||
Assert.Contains("tenant_invoice_reminders", tableNames);
|
||||
Assert.Contains("platform_audit_alert_rules", tableNames);
|
||||
Assert.Contains("platform_audit_alerts", tableNames);
|
||||
Assert.Contains("platform_dunning_notification_channels", tableNames);
|
||||
Assert.Contains("platform_dunning_notification_events", tableNames);
|
||||
Assert.Contains("questions", tableNames);
|
||||
Assert.Contains("question_versions", tableNames);
|
||||
Assert.Contains("answer_records", tableNames);
|
||||
@@ -714,4 +725,86 @@ public sealed class PersistenceModelTests
|
||||
.SequenceEqual(propertyNames));
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(typeof(PlatformSaasPlan), nameof(PlatformSaasPlan.IncludedQuotas), "'{}'::jsonb")]
|
||||
[InlineData(typeof(PlatformSaasPlan), nameof(PlatformSaasPlan.OveragePrices), "'{}'::jsonb")]
|
||||
[InlineData(typeof(PlatformSaasPlan), nameof(PlatformSaasPlan.FeatureFlags), "'{}'::jsonb")]
|
||||
[InlineData(typeof(TenantBillingProfile), nameof(TenantBillingProfile.Metadata), "'{}'::jsonb")]
|
||||
[InlineData(typeof(TenantInvoice), nameof(TenantInvoice.Metadata), "'{}'::jsonb")]
|
||||
[InlineData(typeof(TenantInvoiceItem), nameof(TenantInvoiceItem.Metadata), "'{}'::jsonb")]
|
||||
[InlineData(typeof(TenantInvoicePayment), nameof(TenantInvoicePayment.RawPayload), "'{}'::jsonb")]
|
||||
[InlineData(typeof(TenantInvoiceReminder), nameof(TenantInvoiceReminder.Metadata), "'{}'::jsonb")]
|
||||
[InlineData(typeof(PlatformAuditAlertRule), nameof(PlatformAuditAlertRule.Conditions), "'{}'::jsonb")]
|
||||
[InlineData(typeof(PlatformAuditAlertRule), nameof(PlatformAuditAlertRule.Metadata), "'{}'::jsonb")]
|
||||
[InlineData(typeof(PlatformAuditAlert), nameof(PlatformAuditAlert.Details), "'{}'::jsonb")]
|
||||
[InlineData(typeof(PlatformDunningNotificationChannel), nameof(PlatformDunningNotificationChannel.Metadata), "'{}'::jsonb")]
|
||||
[InlineData(typeof(PlatformDunningNotificationEvent), nameof(PlatformDunningNotificationEvent.RequestPayload), "'{}'::jsonb")]
|
||||
[InlineData(typeof(PlatformDunningNotificationEvent), nameof(PlatformDunningNotificationEvent.Metadata), "'{}'::jsonb")]
|
||||
public void Platform_operations_json_properties_are_mapped_to_jsonb(
|
||||
Type entityType,
|
||||
string propertyName,
|
||||
string expectedDefaultValueSql)
|
||||
{
|
||||
using var context = new TikuDbContext(Options);
|
||||
|
||||
var property = context.Model.FindEntityType(entityType)!
|
||||
.FindProperty(propertyName)!;
|
||||
|
||||
Assert.Equal("jsonb", property.GetColumnType());
|
||||
Assert.Equal(expectedDefaultValueSql, property.GetDefaultValueSql());
|
||||
Assert.Equal(typeof(System.Text.Json.JsonElement), property.ClrType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Platform_operations_have_expected_unique_indexes_arrays_and_money_fields()
|
||||
{
|
||||
using var context = new TikuDbContext(Options);
|
||||
|
||||
AssertHasUniqueIndex<PlatformSaasPlan>(nameof(PlatformSaasPlan.Code));
|
||||
AssertHasUniqueIndex<TenantInvoice>(nameof(TenantInvoice.InvoiceNo));
|
||||
AssertHasUniqueIndex<TenantInvoice>(
|
||||
nameof(TenantInvoice.TenantId),
|
||||
nameof(TenantInvoice.BillingPeriodStart),
|
||||
nameof(TenantInvoice.BillingPeriodEnd));
|
||||
AssertHasUniqueIndex<TenantInvoiceReminder>(
|
||||
nameof(TenantInvoiceReminder.TenantId),
|
||||
nameof(TenantInvoiceReminder.InvoiceId),
|
||||
nameof(TenantInvoiceReminder.ReminderType),
|
||||
nameof(TenantInvoiceReminder.Channel),
|
||||
nameof(TenantInvoiceReminder.ReminderDate));
|
||||
AssertHasUniqueIndex<PlatformAuditAlertRule>(nameof(PlatformAuditAlertRule.Code));
|
||||
AssertHasUniqueIndex<PlatformAuditAlert>(nameof(PlatformAuditAlert.RuleId), nameof(PlatformAuditAlert.AuditLogId));
|
||||
AssertHasUniqueIndex<PlatformDunningNotificationChannel>(nameof(PlatformDunningNotificationChannel.ChannelCode));
|
||||
AssertHasUniqueIndex<PlatformDunningNotificationEvent>(
|
||||
nameof(PlatformDunningNotificationEvent.ChannelId),
|
||||
nameof(PlatformDunningNotificationEvent.ReminderId));
|
||||
|
||||
Assert.Equal(
|
||||
"text[]",
|
||||
context.Model.FindEntityType(typeof(PlatformAuditAlertRule))!
|
||||
.FindProperty(nameof(PlatformAuditAlertRule.ActionPatterns))!
|
||||
.GetColumnType());
|
||||
Assert.Equal(
|
||||
"uuid[]",
|
||||
context.Model.FindEntityType(typeof(PlatformDunningNotificationChannel))!
|
||||
.FindProperty(nameof(PlatformDunningNotificationChannel.TenantIds))!
|
||||
.GetColumnType());
|
||||
|
||||
var total = context.Model.FindEntityType(typeof(TenantInvoice))!
|
||||
.FindProperty(nameof(TenantInvoice.TotalCents))!;
|
||||
Assert.Equal(typeof(int), total.ClrType);
|
||||
|
||||
void AssertHasUniqueIndex<TEntity>(params string[] propertyNames)
|
||||
{
|
||||
var indexes = context.Model.FindEntityType(typeof(TEntity))!.GetIndexes();
|
||||
|
||||
Assert.Contains(
|
||||
indexes,
|
||||
index => index.IsUnique
|
||||
&& index.Properties
|
||||
.Select(property => property.Name)
|
||||
.SequenceEqual(propertyNames));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user